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:
authorRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 21:26:25 +0800
committerRain156 <77776581+Rain156@users.noreply.github.com>2026-11-24 21:26:25 +0800
commitaa8be9aef20291e6faf6ed986d2b6691aad96abf (patch)
tree80e0224b761256cd2375d069089f1eb6413dcd7a /runtime/src/main.cpp
parentbe767b6889cb373cc695b67367644ea4370f8e9c (diff)
parentdb19bbd3455a757c3fe986c6e6db36998f275150 (diff)
Integrate the offline first-run UI runtime with current main and the published renderer fixes
Use the tagged pipeline variants and indexed quad expansion from #722 and #682 with the shared shader cache. Retain title-backed index uploads, cross-queue scratch sharing, amd64 console return conventions and the PC/console caller discriminator. The local shader build indexes native containers and includes the executable's embedded stages.
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp70
1 files changed, 68 insertions, 2 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 098a8ee40..8ea202fa0 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -52,10 +52,50 @@ const char* binding_name(rt::Binding b) {
52 } 52 }
53} 53}
54 54
55// Module TLS index 0 for the game's executable (rt::reserve_tls_index0), from the CRT's initializer table: .CRT$XCT
56// runs after the C runtime is set up and before every C++ dynamic initializer of gta6rt (.CRT$XCU), so before any of
57// gta6rt's code can have started a thread. The frames above it (wmainCRTStartup, __scrt_common_main_seh, _initterm)
58// hold no TLS slot address. The result is read again where an image is run.
59void __cdecl reserve_tls_index0_at_start() { rt::reserve_tls_index0(); }
60#pragma section(".CRT$XCT", read)
61extern "C" __declspec(allocate(".CRT$XCT")) void(__cdecl* const gta6rt_reserve_tls_index0)() =
62 reserve_tls_index0_at_start;
63#pragma comment(linker, "/INCLUDE:gta6rt_reserve_tls_index0")
64
65// Select the settings used to display the offline first-run UI. Keep explicit caller settings.
66bool configure_first_run_ui(const std::filesystem::path& package, std::filesystem::path& overlay) {
67 const auto set_default = [](const wchar_t* name, const wchar_t* value) {
68 return GetEnvironmentVariableW(name, nullptr, 0) != 0 || _wputenv_s(name, value) == 0;
69 };
70 const struct { const wchar_t* name; const wchar_t* value; } settings[] = {
71 {L"LEONIDA_D3D12", L"vulkan"}, {L"LEONIDA_ARENA_MB", L"2048"},
72 {L"LEONIDA_GRAPHICS_STAGE_LAYOUT", L"1"}, {L"LEONIDA_PIPELINE_COMPILE", L"on-demand"},
73 {L"LEONIDA_GPU_LABELS", L"1"}, {L"LEONIDA_FRAME_PACING", L"1"},
74 {L"LEONIDA_LOCAL_SPEECH", L"1"}, {L"LEONIDA_KEYBOARD_PAD", L"always"},
75 };
76 for (const auto& setting : settings) {
77 if (!set_default(setting.name, setting.value)) return false;
78 }
79 std::wstring executable(32768, L'\0');
80 const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast<DWORD>(executable.size()));
81 if (!length || length >= executable.size()) return false;
82 executable.resize(length);
83 const auto state = std::filesystem::path(executable).parent_path() / L"first-run-ui";
84 if (overlay.empty()) overlay = state / L"overlay";
85 if (!GetEnvironmentVariableW(L"LEONIDA_XMEM_BACKING_DIR", nullptr, 0)) {
86 std::error_code error;
87 const auto backing = state / L"xmem";
88 std::filesystem::create_directories(backing, error);
89 if (error || !set_default(L"LEONIDA_XMEM_BACKING_DIR", backing.c_str())) return false;
90 }
91 return SetCurrentDirectoryW(package.c_str()) != FALSE;
92}
93
55int usage() { 94int usage() {
56 std::fprintf(stderr, 95 std::fprintf(stderr,
57 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 96 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
58 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" 97 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
98 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
59 " 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"); 99 " 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");
60 return 2; 100 return 2;
61} 101}
@@ -67,7 +107,7 @@ int wmain(int argc, wchar_t** argv) {
67 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 107 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
68 int archetype_lookup_arg = 0; 108 int archetype_lookup_arg = 0;
69 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context 109 uint32_t view_mode_getter_rva = 0; // --view-mode-getter RVA: the game's view mode of a camera context
70 bool dry_run = false, init = false, run = false, trace_d3d12 = false; 110 bool dry_run = false, init = false, run = false, trace_d3d12 = false, first_run_ui = false;
71 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) 111 std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84)
72 std::wstring trace_file; 112 std::wstring trace_file;
73 for (int i = 1; i < argc; ++i) { 113 for (int i = 1; i < argc; ++i) {
@@ -78,6 +118,7 @@ int wmain(int argc, wchar_t** argv) {
78 } 118 }
79 if (a == L"--dry-run") dry_run = true; 119 if (a == L"--dry-run") dry_run = true;
80 else if (a == L"--init") init = true; 120 else if (a == L"--init") init = true;
121 else if (a == L"--first-run-ui") first_run_ui = true;
81 else if (a == L"--run") run = true; 122 else if (a == L"--run") run = true;
82 else if (a == L"--trace-d3d12") trace_d3d12 = true; 123 else if (a == L"--trace-d3d12") trace_d3d12 = true;
83 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i]; 124 else if (a == L"--trace" && i + 1 < argc) trace_file = argv[++i];
@@ -104,7 +145,8 @@ int wmain(int argc, wchar_t** argv) {
104 } 145 }
105 if (image.empty()) return usage(); 146 if (image.empty()) return usage();
106 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 147 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point.
107 if (dry_run + init + run != 1) return usage(); 148 if (dry_run + init + run + first_run_ui != 1) return usage();
149 run = run || first_run_ui;
108 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). 150 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h).
109 if ((init || run) && !rt::host_cpu::check_skipped()) 151 if ((init || run) && !rt::host_cpu::check_skipped())
110 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { 152 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
@@ -119,6 +161,11 @@ int wmain(int argc, wchar_t** argv) {
119 std::filesystem::path a = std::filesystem::absolute(p, ec); 161 std::filesystem::path a = std::filesystem::absolute(p, ec);
120 return ec ? p : a; 162 return ec ? p : a;
121 }; 163 };
164 if (first_run_ui) {
165 image = absolute_or_same(image);
166 if (!mods_dir.empty()) mods_dir = absolute_or_same(mods_dir);
167 if (!trace_file.empty()) trace_file = absolute_or_same(trace_file).wstring();
168 }
122 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir); 169 game_dir = game_dir.empty() ? absolute_or_same(image).parent_path() : absolute_or_same(game_dir);
123 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir); 170 if (!overlay_dir.empty()) overlay_dir = absolute_or_same(overlay_dir);
124 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved. 171 // LEONIDA_SHADERS too: the D3D12 device reads it at the title's first pipeline state, after it may have moved.
@@ -135,6 +182,10 @@ int wmain(int argc, wchar_t** argv) {
135 if (!rt::base_reservation::inherited()) 182 if (!rt::base_reservation::inherited())
136 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range); 183 if (const auto range = rt::base_reservation::needed(image)) return rt::base_reservation::relaunch(*range);
137 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package. 184 // GAME: paths read under --game-dir; whatever a title writes lands in the overlay, never in the package.
185 if (first_run_ui && !configure_first_run_ui(game_dir, overlay_dir)) {
186 std::fprintf(stderr, "cannot initialize the first-run UI paths or graphics settings\n");
187 return 1;
188 }
138 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay"; 189 if (overlay_dir.empty()) overlay_dir = game_dir / "overlay";
139 rt::fs::configure(game_dir, overlay_dir); 190 rt::fs::configure(game_dir, overlay_dir);
140 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here; 191 // Mods (docs/modding.md): <folder of gta6rt.exe>\mods unless --mods names another. Loaded and reported here;
@@ -168,6 +219,7 @@ int wmain(int argc, wchar_t** argv) {
168 rt::d3d12_trace::set_enabled(trace_d3d12); 219 rt::d3d12_trace::set_enabled(trace_d3d12);
169 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. 220 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first.
170 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); 221 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
222 rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data);
171 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { 223 if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) {
172 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" 224 std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n"
173 : "error: cannot create the trace file\n"); 225 : "error: cannot create the trace file\n");
@@ -186,6 +238,20 @@ int wmain(int argc, wchar_t** argv) {
186 try { 238 try {
187 rt::Loader loader(game_dir); 239 rt::Loader loader(game_dir);
188 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code 240 rt::Module& main_module = loader.load(image); // maps, binds and installs TLS; runs no image code
241 // An executable with TLS that did not get index 0: its /GA code would read and write the TLS in slot 0, which
242 // is gta6rt's own (or another executable's), so no code of it may run.
243 const auto* main_nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(
244 main_module.base + reinterpret_cast<const IMAGE_DOS_HEADER*>(main_module.base)->e_lfanew);
245 const bool main_is_exe = (main_nt->FileHeader.Characteristics & IMAGE_FILE_DLL) == 0;
246 if ((init || run) && main_is_exe && main_module.tls_slot != rt::kNoTlsIndex && main_module.tls_index != 0) {
247 std::string why;
248 if (rt::reserve_tls_index0(&why)) why = "another mapped executable holds it";
249 std::fprintf(stderr,
250 "error: %s cannot have module TLS index 0 (%s); code built with /GA would read and write "
251 "another module's TLS through slot 0, so it is not run\n",
252 main_module.name.c_str(), why.c_str());
253 return 1;
254 }
189 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA 255 // The mods' archetype swaps (docs/modding.md): hooked here, before any image code runs, when the lookup's RVA
190 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup. 256 // for this executable is given. The count of redirected calls shows whether the RVA is the real lookup.
191 if (view_mode_getter_rva) { 257 if (view_mode_getter_rva) {