Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion runtime/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions runtime/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion runtime/launch_flags_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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."},
Expand Down Expand Up @@ -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;
5 changes: 5 additions & 0 deletions runtime/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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=<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;
Expand Down
19 changes: 12 additions & 7 deletions runtime/nvn/vulkan/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/platform/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions runtime/rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions runtime/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions runtime/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tools/game_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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,
Expand Down
Loading