From 8c2f7b5716217785f61118c98ac3c10e74708d62 Mon Sep 17 00:00:00 2001 From: Zarigis Date: Wed, 25 Nov 2026 00:27:05 -0800 Subject: [PATCH] Add game_memory_mb: more game memory for HD texture packs The game's heap (3120 MB) and the limits HD texture packs run past grow with the setting, off by default: the root heap, the models' texture pool (500 MB), ForResourceL and StarterPackMgr (the resident packs), FixedHeap and TexArcWork. Each size comes from the runtime (runtime/kernel.c), the game's own without the setting. The guest window holds a heap of up to 6144 MB; past the device's storage buffer limit the shaders read it in two bindings or by address. SAVAGEWIND_GAME_MEMORY_MB overrides the setting for one run. --- runtime/kernel.c | 68 +++++++++++++++++++++++++++++++++++- runtime/kernel.h | 5 +++ runtime/launch_flags_table.c | 3 +- runtime/main.c | 5 +++ runtime/nvn/vulkan/exec.c | 19 ++++++---- runtime/platform/windows.c | 2 +- runtime/rt.h | 12 +++++-- runtime/settings.c | 2 ++ runtime/settings.h | 2 ++ tools/game_sites.py | 38 ++++++++++++++++++++ tools/patches.py | 67 +++++++++++++++++++++++++++++++++++ 11 files changed, 210 insertions(+), 13 deletions(-) diff --git a/runtime/kernel.c b/runtime/kernel.c index 9e78fb57a..78001fa2b 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -301,6 +301,71 @@ static int nregions; static uint64_t heap_size; static uint64_t tls_next = TLS_BASE; uint64_t kernel_heap_size(void) { return heap_size; } // bytes from HEAP_BASE set by SetHeapSize + +// The game's memory (game_memory_mb). The game asks SetMemoryHeapSize for 3120 MB at start (main 0x700000071c) and +// gives its root heap that less the 32 MB it keeps for malloc (0x7000c617c0); its resource heap (res::System) takes +// what the root heap has left, so a bigger heap is room for bigger resources (HD texture packs). Both sites read the +// size from here (tools/patches.py _game_heap_size). The renderer sizes its view of the heap by HEAP_MAX, so the size +// is decided once, at start, and kept across "Quit to Start Screen". +#define GAME_HEAP_CONSOLE 0xC3000000ull // 3120 MB +uint64_t g_heap_max = HEAP_MAX_CONSOLE; +static uint64_t game_heap = GAME_HEAP_CONSOLE; + +void kernel_memory_setup(int mb) { + static bool done; + if (done) return; + done = true; + if (mb <= 0) return; + uint64_t want = (uint64_t)mb << 20, room = HEAP_MAX_CONSOLE - GAME_HEAP_CONSOLE; + if (want < GAME_HEAP_CONSOLE) want = GAME_HEAP_CONSOLE; + if (want > HEAP_MAX_LARGEST - room) want = HEAP_MAX_LARGEST - room; + want &= ~0xFFFFFull; // (whole MB) + game_heap = want; + g_heap_max = (want + room + 0xFFFFFFull) & ~0xFFFFFFull; // (16 MB steps) + LOG("[kernel] game memory: %llu MB heap (the game's own: %llu MB), limit %llu MB\n", (unsigned long long)(want >> 20), + (unsigned long long)(GAME_HEAP_CONSOLE >> 20), (unsigned long long)(g_heap_max >> 20)); +} + +uint64_t rt_game_heap_size(void) { return game_heap; } + +// The resource system's FixedHeap and the TexArcWork heap inside it grow by the same amount: a quarter of what +// game_memory_mb adds, at most 256 MB (both come out of the root heap, so the rest is left to the resource heap). +// Not needed for an HD texture pack to load, but with one the texture archives' first try (TextureArchiveWork) fails +// less often and falls back less (Shrine of Resurrection load with the HD pack and its terrain: 24 failed tries +// instead of 41; the game's own files: 10). The game's own sizes without the setting. +uint64_t rt_game_heap_grow(uint64_t size) { + uint64_t add = (game_heap - GAME_HEAP_CONSOLE) / 4; + if (add > 0x10000000ull) add = 0x10000000ull; + return size + (add & ~0xFFFFFull); +} + +// The resource manager's ForResourceS (which 0) and ForResourceL (1) heaps, sized from its settings, and +// StarterPackMgr's heap inside ForResourceL (2, 225 MB), where the resident packs load: an HD Pack/TitleBG.pack (363 MB) +// does not fit. ForResourceL and StarterPackMgr grow by a sixth of what game_memory_mb adds, at most 512 MB (the +// pack's own emulator patch: ForResourceL 694 -> 1104 MB), so the rest of ForResourceL keeps its room; ForResourceS +// stays. The game's own sizes without the setting. +uint64_t rt_game_resource_heap(uint32_t size, int which) { + uint64_t add = which ? (game_heap - GAME_HEAP_CONSOLE) / 6 : 0; + if (add > 0x20000000ull) add = 0x20000000ull; + add &= ~0xFFFFFull; + static bool logged[3]; + if (add && !logged[which]) { + static const char *const names[3] = {"ForResourceS", "ForResourceL", "StarterPackMgr"}; + logged[which] = true; + LOG("[kernel] game memory: %s %u MB -> %llu MB\n", names[which], size >> 20, + (unsigned long long)((size + add) >> 20)); + } + return size + add > 0xFFFFFFFFull ? 0xFFFFFFFFull : size + add; +} + +// The GPU memory pool the models' textures load into (500 MB, made at boot): with an HD texture pack models whose +// textures don't fit are left out. It takes two thirds of what game_memory_mb adds, at most 2 GB (the pack's own +// emulator patch: about 2.5 GB at 6 GB). The game's own size without the setting. +uint64_t rt_game_texture_pool(uint64_t size) { + uint64_t add = (game_heap - GAME_HEAP_CONSOLE) / 3 * 2; + if (add > 0x80000000ull) add = 0x80000000ull; + return size + (add & ~0xFFFFFull); +} // Writable guest data regions (heap, mapped/physical memory, module .data): for debug/runtime memory scans. int kernel_data_regions(uint64_t *addr, uint64_t *size, int max) { int n = 0; @@ -1146,7 +1211,8 @@ static void svc_impl(Cpu *c, uint32_t imm, uint64_t pc) { case 3: v = ALIAS_SIZE; break; case 4: v = HEAP_BASE; break; case 5: v = HEAP_MAX; break; - case 6: case 21: v = 0xCD500000ull; break; // application memory (3.2 GB, as on hardware) + case 6: case 21: v = 0xCD500000ull + (HEAP_MAX - HEAP_MAX_CONSOLE); break; // application memory (3.2 GB, + // as on hardware; + game_memory_mb) case 7: case 22: v = heap_size + 0x4000000; break; case 8: v = 0; break; case 9: v = 0; break; diff --git a/runtime/kernel.h b/runtime/kernel.h index 7377c1838..786489b0f 100644 --- a/runtime/kernel.h +++ b/runtime/kernel.h @@ -87,3 +87,8 @@ typedef struct { volatile int v; char pad[60]; } __attribute__((aligned(64))) Co extern CoreBestWait g_core_best_wait[4]; void kernel_preempt(KThread *t); uint64_t kernel_heap_size(void); // bytes from HEAP_BASE set by SetHeapSize +void kernel_memory_setup(int mb); // game_memory_mb (0: the game's own 3120 MB); once, at start, before the renderer +uint64_t rt_game_heap_size(void); // the heap the game asks for (tools/patches.py _game_heap_size) +uint64_t rt_game_heap_grow(uint64_t size); // a resource heap's size with game_memory_mb (tools/patches.py _game_heap_grow) +uint64_t rt_game_texture_pool(uint64_t size); // the models' texture pool with game_memory_mb (the same) +uint64_t rt_game_resource_heap(uint32_t size, int which); // ForResourceS (0) / L (1) / StarterPackMgr (2), the same diff --git a/runtime/launch_flags_table.c b/runtime/launch_flags_table.c index 441ea4526..9b3fd9ae7 100644 --- a/runtime/launch_flags_table.c +++ b/runtime/launch_flags_table.c @@ -141,6 +141,7 @@ const LfInfo LF_TABLE[] = { {"SAVAGEWIND_FSR3_VIEW", LF_KIND_TEXT, LF_GROUP_DEV, "", ""}, {"SAVAGEWIND_FSR4_LINEAR", LF_KIND_TEXT, LF_GROUP_OTHER, "", ""}, {"SAVAGEWIND_GAME_LIB", LF_KIND_TEXT, LF_GROUP_OTHER, "", ""}, + {"SAVAGEWIND_GAME_MEMORY_MB", LF_KIND_TEXT, LF_GROUP_OTHER, "", ""}, {"SAVAGEWIND_GEN", LF_KIND_TEXT, LF_GROUP_BUILD, "", "The program that builds the game code."}, {"SAVAGEWIND_GLYPH_DUMP", LF_KIND_SWITCH, LF_GROUP_DEV, "", ""}, {"SAVAGEWIND_GPU", LF_KIND_TEXT, LF_GROUP_PLAY, "auto", "Which graphics card to use: a number, part of its name, or auto."}, @@ -556,4 +557,4 @@ const LfInfo LF_TABLE[] = { {"SAVAGEWIND_XLATE_VERIFY", LF_KIND_TEXT, LF_GROUP_DEV, "", ""}, {"SAVAGEWIND_YAZ0", LF_KIND_NUMBER, LF_GROUP_OTHER, "", ""}, }; -const int LF_TABLE_N = 552; +const int LF_TABLE_N = 553; diff --git a/runtime/main.c b/runtime/main.c index 373d4794c..5c0355cd0 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -457,6 +457,11 @@ int main(int argc, char **argv) { if (from_swmm) LOG("[launcher] started by SWMM+: straight into the game%s\n", config ? " (--config: Configuration)" : ""); // Savage Wind Configuration's Companion apps switch: the read-only companion socket (runtime/control.h) if (g_settings.companion_apps) control_companion_start(CONTROL_COMPANION_PORT); + { // (before the renderer: it sizes its view of the heap by HEAP_MAX). SAVAGEWIND_GAME_MEMORY_MB= overrides + // game_memory_mb for one run (a launcher starting the game with an HD texture pack) + const char *e = getenv("SAVAGEWIND_GAME_MEMORY_MB"); + kernel_memory_setup(e && *e ? atoi(e) : g_settings.game_memory_mb); + } static const char *const sparse_heap_modes[SPARSE_HEAP_COUNT] = {"auto", "on", "off"}; int sparse_heap = g_settings.sparse_heap; if (sparse_heap < 0 || sparse_heap >= SPARSE_HEAP_COUNT) sparse_heap = SPARSE_HEAP_AUTO; diff --git a/runtime/nvn/vulkan/exec.c b/runtime/nvn/vulkan/exec.c index c94e9c2a9..d412b979b 100644 --- a/runtime/nvn/vulkan/exec.c +++ b/runtime/nvn/vulkan/exec.c @@ -760,11 +760,16 @@ void vk_translator_setup(void) { VkPhysicalDeviceFeatures2 f = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &f12}; vkGetPhysicalDeviceFeatures2(g_vk.phys, &f); bool can_bda = f12.bufferDeviceAddress && f.features.shaderInt64; - maxwell_gmem_bda = can_bda && (bda ? atoi(bda) != 0 : drv.driverID == VK_DRIVER_ID_MESA_TURNIP && - (lim < GMEM_SPLIT || HEAP_MAX - GMEM_SPLIT > lim)); + // A heap bigger than the game's own (game_memory_mb) past the limit: two bindings, or its address where even the + // second binding would be past it (a 6 GB heap on NVIDIA's 4 GB limit) + bool big = HEAP_MAX > HEAP_MAX_CONSOLE && HEAP_MAX > lim; + bool past = lim < GMEM_SPLIT || HEAP_MAX - GMEM_SPLIT > lim; + maxwell_gmem_bda = can_bda && (bda ? atoi(bda) != 0 : (drv.driverID == VK_DRIVER_ID_MESA_TURNIP || big) && past); if (bda && atoi(bda) && !can_bda) LOG("[vk] SAVAGEWIND_VK_GMEM_BDA=1: the driver lacks bufferDeviceAddress or shaderInt64\n"); + if (big && past && !maxwell_gmem_bda) LOG("[vk] the game's memory (%llu MB) is past what the shaders can read here\n", + (unsigned long long)(HEAP_MAX >> 20)); maxwell_gmem_split = maxwell_gmem_bda ? false : split ? atoi(split) != 0 : (g_vk.is_mali && HEAP_MAX > g_vk.props.limits.maxStorageBufferRange) || - drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY; + drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY || big; maxwell_gmem_snap = gsnap_mode() != 0; // (the heap window too, from here on: translate_program and the precompile set it before translating, but the // pre-warm's loads of cached translations read the variant before any translation - with 0+0 there, the global @@ -5663,7 +5668,7 @@ static bool gmem_sparse; #define GMEM_SPARSE_PAGE_SIZE 0x10000ull #define GMEM_SPARSE_CHUNK_SIZE (1ull << 16) #define GMEM_SPARSE_CHUNK_PAGES (GMEM_SPARSE_CHUNK_SIZE / GMEM_SPARSE_PAGE_SIZE) -#define GMEM_SPARSE_PAGE_COUNT ((HEAP_MAX + GMEM_SPARSE_PAGE_SIZE - 1) / GMEM_SPARSE_PAGE_SIZE) +#define GMEM_SPARSE_PAGE_COUNT ((HEAP_MAX_LARGEST + GMEM_SPARSE_PAGE_SIZE - 1) / GMEM_SPARSE_PAGE_SIZE) // (room for the largest heap) #define GMEM_SPARSE_MEM_COUNT GMEM_SPARSE_PAGE_COUNT static uint8_t gmem_sparse_bound_pages[GMEM_SPARSE_PAGE_COUNT]; static uint64_t gmem_sparse_generation; @@ -7108,9 +7113,9 @@ static void hc_list_begin(void) { #define VDYN_HITS 3 #define VDYN_COPY_MAX (64u << 10) static int vdyn_mode = -1; -static _Atomic uint8_t vdyn[HEAP_MAX >> VDYN_SHIFT]; // (a region: 1 once marked, see above) -static uint8_t vdyn_hits[HEAP_MAX >> VDYN_SHIFT]; // (vdyn_lock: frames it changed in, in a row) -static uint64_t vdyn_last[HEAP_MAX >> VDYN_SHIFT]; // (vdyn_lock: the frame of the last change) +static _Atomic uint8_t vdyn[HEAP_MAX_LARGEST >> VDYN_SHIFT]; // (a region: 1 once marked, see above) +static uint8_t vdyn_hits[HEAP_MAX_LARGEST >> VDYN_SHIFT]; // (vdyn_lock: frames it changed in, in a row) +static uint64_t vdyn_last[HEAP_MAX_LARGEST >> VDYN_SHIFT]; // (vdyn_lock: the frame of the last change) static struct { uint64_t addr, size, hash, seq; } vdyn_chk[VDYN_N]; static _Atomic uint64_t vdyn_w, vdyn_r; // (written by the render thread / read by a waiting thread) static uint64_t vdyn_tick; diff --git a/runtime/platform/windows.c b/runtime/platform/windows.c index fbe15c14b..c9ef42228 100644 --- a/runtime/platform/windows.c +++ b/runtime/platform/windows.c @@ -31,7 +31,7 @@ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; // does not fault: such buffers must be inside committed regions. static uint64_t g_lo, g_hi; #define COMMIT_CHUNK 0x10000ull -#define SPARSE_HEAP_MAX_PAGES (4ull * 1024 * 1024 * 1024 / COMMIT_CHUNK) +#define SPARSE_HEAP_MAX_PAGES (0x0190000000ull / COMMIT_CHUNK) // (the heap at its largest: HEAP_MAX_LARGEST, rt.h) #define SPARSE_HEAP_BITMAP_WORDS ((SPARSE_HEAP_MAX_PAGES + 63) / 64) static atomic_uint_fast64_t guest_commit_gen; static uint64_t sparse_heap_base, sparse_heap_size; diff --git a/runtime/rt.h b/runtime/rt.h index 17b4abf76..75c0b62aa 100644 --- a/runtime/rt.h +++ b/runtime/rt.h @@ -31,9 +31,10 @@ static inline void rt_log(const char *fmt, ...) { // ---- address space layout (guest addresses == host addresses) // macOS reserves 0x2_0000_0000-0x70_0000_0000, and guest addresses must fit in 39 bits, so the -// guest lives in a 12.5 GB window at 0x70_0000_0000 claimed at startup (see main.c). +// guest lives in a 15.5 GB window at 0x70_0000_0000 claimed at startup (see main.c). It holds the heap at its largest +// (game_memory_mb, HEAP_MAX below); only what the game maps in it is committed. #define GUEST_LO 0x7000000000ull -#define GUEST_HI 0x7320000000ull +#define GUEST_HI 0x73E0000000ull // The exefs modules (main, subsdk0..9, sdk) sit back to back from MAIN_BASE, main first, as tools/layout.py // places them; the game code library says where each one is (runtime/gamelib.h). Both bounds: tools/layout.py too. #define MAIN_BASE 0x7000000000ull @@ -47,7 +48,12 @@ static inline void rt_log(const char *fmt, ...) { #define ALIAS_BASE 0x7100000000ull // alias region (svcMapMemory targets) #define ALIAS_SIZE 0x0100000000ull #define HEAP_BASE 0x7240000000ull -#define HEAP_MAX 0x00D0000000ull // 3.25 GB +// The heap's limit: 3.25 GB for the game's own 3120 MB heap, more with game_memory_mb (kernel_memory_setup, set once +// at start, before the renderer sizes its view of the heap). At most HEAP_BASE + HEAP_MAX_LARGEST, inside GUEST_HI. +#define HEAP_MAX_CONSOLE 0x00D0000000ull // 3.25 GB +#define HEAP_MAX_LARGEST 0x0190000000ull // 6.25 GB: a 6144 MB heap and the same room the game's own has +extern uint64_t g_heap_max; +#define HEAP_MAX g_heap_max // ---- loader.c typedef struct Module { diff --git a/runtime/settings.c b/runtime/settings.c index 4c9f045f9..0ca89f26b 100644 --- a/runtime/settings.c +++ b/runtime/settings.c @@ -709,6 +709,7 @@ void settings_load(void) { g_settings.color_lut_set = true; } else if (!strcmp(key, "colour_lut_strength")) g_settings.color_lut_strength = atoi(val); // (percent) + else if (!strcmp(key, "game_memory_mb")) g_settings.game_memory_mb = atoi(val); // (0: the game's own) else if (!strcmp(key, "sharper_menus")) // (off / 2x / 4x) g_settings.sharper_menus = !strcmp(val, "4x") ? 4 : !strcmp(val, "2x") ? 2 : 0; else if (!strcmp(key, "sharper_text")) // (off / 2x / 4x) @@ -925,6 +926,7 @@ bool settings_save_to(const char *path) { fprintf(f, "scene_copies = %s\n", k_scene_copies[g_settings.scene_copies >= 0 && g_settings.scene_copies < SCENE_COPIES_COUNT ? g_settings.scene_copies : SCENE_COPIES_AUTO]); if (g_settings.hdr_white) fprintf(f, "hdr_paper_white = %d\n", g_settings.hdr_white); + if (g_settings.game_memory_mb) fprintf(f, "game_memory_mb = %d\n", g_settings.game_memory_mb); if (g_settings.hdr_peak_nits) fprintf(f, "hdr_peak_nits = %d\n", g_settings.hdr_peak_nits); if (g_settings.hdr_game_nits) fprintf(f, "hdr_game_nits = %d\n", g_settings.hdr_game_nits); fprintf(f, "hdr_ui_nits = %d\n", g_settings.hdr_ui_nits); // (0: the scene's white, a state worth keeping) diff --git a/runtime/settings.h b/runtime/settings.h index 6889e0bc0..2cfef1891 100644 --- a/runtime/settings.h +++ b/runtime/settings.h @@ -178,6 +178,8 @@ typedef struct { bool check_updates; // Savage Wind Configuration asks SW Update if there is a new version bool companion_apps; // the read-only companion socket for map and tracker apps (control_companion_start); off bool swmm_mods; // Play loads the mods SWMM+ deployed (runtime/ui/launcher.mm); on + int game_memory_mb; // the game's heap in MB, for HD texture packs (0: the game's own 3120; up to 6144), read at + // start (runtime/kernel.c kernel_memory_setup) int shadow_res; // SHADOW_*: shadow maps at the console's size, bigger, or (Potato) smaller int sharper_menus; // the menus' one-channel textures enlarged this many times (2 or 4; 0: off), runtime/nvn/text_upscale.c int sharper_text; // the game's font sheets enlarged this many times (2 or 4; 0: off), runtime/nvn/text_upscale.c diff --git a/tools/game_sites.py b/tools/game_sites.py index 90e4ef8d2..a3a935c32 100644 --- a/tools/game_sites.py +++ b/tools/game_sites.py @@ -549,6 +549,31 @@ # Cheats > Map hooks the read (patches.py _place_icon) and clears the bit for places that were never visited, # so they keep the normal look while staying travel destinations. Site('place_icon', 'code', 'b9403e68 7217011f f9406121 a9414ff4 1e200c20 a8c27bfd d61f0020 a9414ff4', 0), + # The game's memory (tools/patches.py _game_heap_size; runtime/kernel.c kernel_memory_setup): the size nninitStartup + # asks SetMemoryHeapSize for (mov w0, #0xc3000000, then the 32 MB block malloc keeps), and the root heap's size + # main hands sead's heap manager (mov w8, #0xc1000000: the heap less those 32 MB) + Site('game_heap_size', 'code', '52b86000 94000000/fc000000 910023e0 52a04001 94000000/fc000000 f94007e0 52a04001 ' + '94000000/fc000000', 0), + Site('game_root_heap_size', 'code', '52b82008 52800020 a9027fe8 94000000/fc000000 52800020 94000000/fc000000 ' + '52800020 94000000/fc000000', 0), + # ... and the resource system's FixedHeap (mov w0, #0x17600000) and the TexArcWork heap made inside it + # (mov w0, #0xc000000), where texture archives are tried first + Site('game_fixed_heap_size', 'code', '52a2ec00 94000000/fc000000 f9001a60 b5000000/ff00001f a9434ff4 f94013f5 ' + 'a9417bfd 910103ff', 0), + Site('game_texarc_heap_size', 'code', '52a18000 52800103 52800024 52800025 91004108 a90027e8 94000000/fc000000 ' + 'f9009260', 0), + # ... and the GPU memory pool the models' textures load into: 500 MB (mov w9, #0x1f400000), or 400 MB in the + # other mode the csel after them picks (mov w11, #0x19000000) + Site('game_texture_pool', 'code', '52a3e809 f9000feb 52a3200b f9000914 b94003e8 3941427b b940d156 7100091f', 0), + Site('game_texture_pool_b', 'code', '52a3200b f9000914 b94003e8 3941427b b940d156 7100091f 39414668 9a89017a', 0), + # ... and the resource manager's ForResourceS / ForResourceL heaps, whose sizes it reads from its settings + # (ldr w8, [x20, #0xcc] / [x20, #0xc8]): StarterPackMgr, where the resident packs load (Pack/TitleBG.pack), takes + # what ForResourceL has left + Site('game_resource_s', 'code', 'b940ce88 a904e3fa a93f27ba 290523f9 94000000/fc000000 b980ae68 9102227c 52a8141b', 0), + Site('game_resource_l', 'code', 'b940ca88 90000009/9f00001f 91000129/ffc003ff d10043a1 aa1503e0 b90023f6 79004bf7 ' + '39009bf7', 0), + # ... and StarterPackMgr's heap inside ForResourceL: 225 MB (mov w8, #0xe100000; 20 MB in the mode the csel picks) + Site('game_starter_pack', 'code', '52a1c208 52a02809 9a881120 94000000/fc000000 aa0003e1 f9012e60 52802000 52800102', 0), Site('main_data', 'data', '', 0), Site('main_end', 'end', '', 0), ] @@ -624,6 +649,10 @@ ('actor_create', 'actor_creator', 'actor_create_heap', 'inst_params_vt', 'inst_params_init', 'inst_params_add', 'inst_params_add_string'), # (reward chests: without them a reward waits for room in the pouch) ('horse_cam_lr',), + ('game_heap_size', 'game_root_heap_size'), # (both or neither: the root heap must fit in the heap) + ('game_fixed_heap_size', 'game_texarc_heap_size'), # (both or neither: TexArcWork is made inside FixedHeap) + ('game_texture_pool', 'game_texture_pool_b'), + ('game_resource_s', 'game_resource_l', 'game_starter_pack'), # (StarterPackMgr grows inside ForResourceL) ] OPTIONAL = {n for g in OPTIONAL_GROUPS for n in g} @@ -844,6 +873,15 @@ 'grass_blade_lateral_near': 0x70013a8f0c, 'grass_blade_lateral_far': 0x70013a8f20, 'tree_ranges': 0x70013c94ac, + 'game_heap_size': 0x700000071c, + 'game_root_heap_size': 0x7000c617c0, + 'game_fixed_heap_size': 0x700131aef8, + 'game_texarc_heap_size': 0x700131b49c, + 'game_texture_pool': 0x700127b6e0, + 'game_texture_pool_b': 0x700127b6e8, + 'game_resource_s': 0x700155fda4, + 'game_resource_l': 0x700155fde0, + 'game_starter_pack': 0x7001209a8c, 'act_hold': 0x7000d16f0c, 'act_trig': 0x7000d16f74, 'act_masks': 0x70016d89b4, diff --git a/tools/patches.py b/tools/patches.py index 8ac8ec808..b39e43fe4 100644 --- a/tools/patches.py +++ b/tools/patches.py @@ -1065,6 +1065,64 @@ def _grass_blade_lateral(gen, a): f' /* {a:#x}: fcmp s0, s{n} (grass blades: a cell inside the region\'s width) */') +# The game's memory (game_memory_mb, runtime/kernel.c kernel_memory_setup). nninitStartup asks SetMemoryHeapSize for +# 3120 MB (1.9.0 0x700000071c) and main gives sead's root heap that less the 32 MB malloc keeps (0x7000c617c0); the +# resource heap takes what the root heap has left. Both sizes come from the runtime: the game's own unless the +# setting asks for more. + +def _game_heap_size(gen, a): + assert _word(gen, a) == 0x52b86000, hex(_word(gen, a)) # mov w0, #0xc3000000 + return ('{ extern uint64_t rt_game_heap_size(void); c->x[0] = rt_game_heap_size(); }' + f' /* {a:#x}: mov w0, #0xc3000000 (SetMemoryHeapSize) */') + + +def _game_root_heap_size(gen, a): + assert _word(gen, a) == 0x52b82008, hex(_word(gen, a)) # mov w8, #0xc1000000 + return ('{ extern uint64_t rt_game_heap_size(void); c->x[8] = rt_game_heap_size() - 0x2000000; }' + f' /* {a:#x}: mov w8, #0xc1000000 (the root heap: the heap less malloc\'s 32 MB) */') + + +# The resource system's FixedHeap (374 MB, 1.9.0 0x700131aef8) and the TexArcWork heap it makes inside itself (192 MB, +# 0x700131b49c), where texture archives are tried first: with an HD texture pack those tries fail more often and fall +# back. Both grow by the same amount with game_memory_mb (runtime/kernel.c rt_game_heap_grow; nothing without it). + +def _game_heap_grow(word, reg, size, fn='rt_game_heap_grow'): + def patch(gen, a): + assert _word(gen, a) == word, hex(_word(gen, a)) + return (f'{{ extern uint64_t {fn}(uint64_t); c->x[{reg}] = {fn}({size:#x}); }}' + f' /* {a:#x}: mov w{reg}, #{size:#x} */') + return patch + + +# The resource manager's ForResourceS / ForResourceL heaps (1.9.0 0x700155fda4 / 0x700155fde0), sized from its +# settings: StarterPackMgr, where the resident packs load, takes what ForResourceL (694 MB) has left, and an HD +# Pack/TitleBG.pack (363 MB) does not fit (crash at the title). The size read grows with game_memory_mb +# (runtime/kernel.c rt_game_resource_heap; nothing without it). + +def _game_resource_heap(word, off, which): + def patch(gen, a): + assert _word(gen, a) == word, hex(_word(gen, a)) + return ('{ extern uint64_t rt_game_resource_heap(uint32_t, int); ' + f'c->x[8] = rt_game_resource_heap(LD32(c->x[20] + {off:#x}), {which}); }}' + f' /* {a:#x}: ldr w8, [x20, #{off:#x}] */') + return patch + + +# ... and StarterPackMgr's own heap inside ForResourceL (225 MB, 1.9.0 0x7001209a8c; 20 MB in the mode the csel after +# it picks), which the resident packs load into: it grows by what ForResourceL grows by. + +def _game_starter_pack(gen, a): + assert _word(gen, a) == 0x52a1c208, hex(_word(gen, a)) # mov w8, #0xe100000 + return ('{ extern uint64_t rt_game_resource_heap(uint32_t, int); c->x[8] = rt_game_resource_heap(0xe100000, 2); }' + f' /* {a:#x}: mov w8, #0xe100000 (StarterPackMgr) */') + + +# The GPU memory pool the models' textures load into (1.9.0 0x700127b6e0: 500 MB, or 400 MB in the mode the csel +# after it picks). It is made at boot and never grows: with an HD texture pack it runs out and models whose textures +# do not fit are left out (the Shrine of Resurrection without walls). It grows with game_memory_mb +# (runtime/kernel.c rt_game_texture_pool; nothing without it). + + # The terrain's distant trees (tera::Core::Tree, 1.9.0 0x70013c94ac, 1.8.2 0x70012b55cc: its per-frame shader setup). # The trees past the actor trees are drawn by the terrain, from where it fades them in (+0x14cb0, 100 m; +0x14cb4 the # fade, 100 m) out to +0x14cb8 (4000 m, fading over the last +0x14cbc, 500 m; +0x14cc0, 4000 m, where its blocks @@ -1532,6 +1590,15 @@ def _camera_leave(gen, a): 'grass_blade_lateral_near': _grass_blade_lateral, 'grass_blade_lateral_far': _grass_blade_lateral, 'tree_ranges': _tree_ranges, + 'game_heap_size': _game_heap_size, + 'game_root_heap_size': _game_root_heap_size, + 'game_fixed_heap_size': _game_heap_grow(0x52a2ec00, 0, 0x17600000), # mov w0, #0x17600000 + 'game_texarc_heap_size': _game_heap_grow(0x52a18000, 0, 0xc000000), # mov w0, #0xc000000 + 'game_texture_pool': _game_heap_grow(0x52a3e809, 9, 0x1f400000, 'rt_game_texture_pool'), # mov w9, #0x1f400000 + 'game_texture_pool_b': _game_heap_grow(0x52a3200b, 11, 0x19000000, 'rt_game_texture_pool'), # mov w11, #0x19000000 + 'game_resource_s': _game_resource_heap(0xb940ce88, 0xcc, 0), # ldr w8, [x20, #0xcc] + 'game_resource_l': _game_resource_heap(0xb940ca88, 0xc8, 1), # ldr w8, [x20, #0xc8] + 'game_starter_pack': _game_starter_pack, 'menu_wrap': _menu_wrap, 'act_hold': _act_query(0), 'act_trig': _act_query(1),