From 1a07a8d46f0184af4ac5e3e31946e523664f7540 Mon Sep 17 00:00:00 2001 From: Wellington Dias Date: Fri, 30 Oct 2026 01:01:33 -0300 Subject: [PATCH] Fix the sun shadow map at one size, so foliage shadows stop turning blocky and jumping. Each frame the engine sizes its shadow cascades to the area they cover (main+0xcebdf4 in 1.2.1): from 960 down to about 400 near the ground, so leaf shadows turn coarse, and every change of size moves the shadow texels, so they jump instead of sliding. The Switch does the same; the community mods force a fixed size. The shadow patch set loads the size from patch page +92 just before the engine rounds it (main+0xcec22c in 1.2.1, 0x683604 in 1.4.3), and the menu's Shadow map size (shadow_res in totk.ini) chooses it: game, 512, 768, 1024 (default), 1536 or 2048, with the texture heap grown above 960. A write watch on the size field found the one writer; with the setting, every shadow map in both versions is the fixed size. recomp/patches.py lists the patch page words in use. --- docs/maxwell.md | 1 + recomp/patches-v121.txt | 10 ++++++++++ recomp/patches-v143.txt | 6 ++++++ recomp/patches.py | 4 ++-- runtime/cfg.c | 4 +++- runtime/resolution.c | 41 ++++++++++++++++++++++++++++++++++++++--- runtime/ui/menu.cpp | 10 ++++++++++ runtime/ui_text.c | 10 ++++++++++ runtime/ui_text.h | 1 + totk.ini.example | 4 ++++ 10 files changed, 85 insertions(+), 6 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index dc71a041..472360f7 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -142,6 +142,7 @@ Source shorthands: | TotK | "detective mode" Link on AMD | AMD EDS3 colour-blend driver regression | YZ-2306 PR 10946 | | BotW | shadow artifacts at object edges (AMD) | gather rounding | yuzu commit 780240e (2.10) | | TotK | dashed dark lines along wall and floor edges, changing with the camera angle (AMD, RADV) | gather rounding | gather bias (2.10) | +| TotK | blocky foliage shadows that jump instead of sliding | not a GPU difference: the engine sizes its sun shadow map to the area it covers, down to about 400 near the ground, and each change of size moves the shadow texels | fixed size, `shadow_res` (the shadow set of `recomp/patches-v121.txt`; the community mods force 1024 the same way) | | BotW | black textures on GCN | HTILE (depth compression) | `AMD_DEBUG=nohyperz` wiki (1.9) | | BotW | 3D scene black | deferred texture handles | NXN | | BotW | albedo scattered squares | duplicate colour attachments | NXN | diff --git a/recomp/patches-v121.txt b/recomp/patches-v121.txt index 7dbb63ed..a68eb80a 100644 --- a/recomp/patches-v121.txt +++ b/recomp/patches-v121.txt @@ -9,6 +9,7 @@ set cloth TOTK_CLOTH_REALTIME 1 set bounce TOTK_HIGHFPS_FIX 1 set output TOTK_PATCH_OUTPUT 0 set scene TOTK_PATCH_SCENE 0 +set shadow TOTK_PATCH_SHADOW 0 # 60 fps: the adaptive present-interval controller (main+0x8f26c8, 1.0.0 0x8f6bec) gets the minimum interval 1. site fps60 main 8f273c 6b08015f 7100055f # clamp: cmp w10, #1 (was cmp w10, w8 = minimum) @@ -947,3 +948,12 @@ spin sdk 23a590 23a5d8 # 19 instructions; loads spin sdk 23a658 23a6a0 # 19 instructions; loads spin sdk 23a7fc 23a918 # 19 instructions; loads spin sdk 3d3250 3d3270 # 9 instructions; loads + +# Sun shadow map size (runtime/resolution.c shadow_init switches the set on). Each frame the engine sizes its shadow +# cascades (a D16 4-layer array) in main+0xcebdf4: a base size (960 in the field), interpolated down by how close the +# cover is, rounded up to 8 at main+0xcec22c and stored as the width (+0x3d8 of the cascade set) and, through the packed +# store at +0xe4, the height (+0x3f8). Near the ground it falls to about 400, which draws foliage shadows blocky, and +# every change of size moves the shadow texels, so leaf shadows jump instead of sliding. The page's fixed size (+92) +# goes in just before the rounding, as the community's shadow resolution mods do (1.1.0: mov w11, #1024 over the +# computation). Found by a write watch on the size field (the writer was this function, and no other). +site shadow main cec22c 11001d6b @page:x11 b9405d6b 11001d6b # add w11, w11, #7 -> adrp x11, page; ldr w11, [x11, #92]; add w11, w11, #7 diff --git a/recomp/patches-v143.txt b/recomp/patches-v143.txt index c1a839fe..696f94b1 100644 --- a/recomp/patches-v143.txt +++ b/recomp/patches-v143.txt @@ -10,6 +10,7 @@ set cloth TOTK_CLOTH_REALTIME 1 set bounce TOTK_HIGHFPS_FIX 1 set output TOTK_PATCH_OUTPUT 0 set scene TOTK_PATCH_SCENE 0 +set shadow TOTK_PATCH_SHADOW 0 # 60 fps: the adaptive present-interval controller (main+0x2db174, 1.2.1 0x8f26c8) gets the minimum interval 1. site fps60 main 2db204 6b08015f 7100055f # clamp: cmp w10, #1 (was cmp w10, w8 = minimum) @@ -976,3 +977,8 @@ spin subsdk0 1c4e54 1c4e60 # 4 instructions; loads spin sdk 2d3654 2d3668 # 6 instructions; loads spin sdk 443864 443884 # 9 instructions; loads spin sdk 541e54 541f98 # 32 instructions; loads + +# Sun shadow map size (patches-v121.txt explains the set and the page). 1.4.3 interpolates the size between a minimum +# and the base and rounds it up to 8 at main+0x683604; the rounded value is the width (+0x3d8) and, through the packed +# store at +0xf4, the height (+0x3f8). The page's size goes in just before the rounding. +site shadow main 683604 11001d6b @page:x11 b9405d6b 11001d6b # add w11, w11, #7 -> adrp x11, page; ldr w11, [x11, #92]; add w11, w11, #7 diff --git a/recomp/patches.py b/recomp/patches.py index ecc12e7e..72ef2f74 100644 --- a/recomp/patches.py +++ b/recomp/patches.py @@ -14,8 +14,8 @@ encoded for the site's address. The patch page is the first 4 KiB page after mai of the 2 MiB-aligned module slot free); the runtime commits it and owns its contents. Words in use, by byte offset: +0..15 the frame-rate step (runtime/frame_rate.c), +16..55 the output, swapchain and scene sizes (runtime/resolution.c res_init), +56..59 the camera's aspect ratio (runtime/resolution.c res_init, set aspect), +60..63 -the field-of-view factor (runtime/resolution.c res_fov_live, set fov). A new -set takes words outside these. +the field-of-view factor (runtime/resolution.c res_fov_live, set fov), +92..95 the sun shadow map size +(runtime/resolution.c shadow_init, set shadow). A new set takes words outside these. The replacement words are lifted at the site's address in place of the original instruction, and the generated C reads `if (rt_patch_on[]) { replacement } else { original }`: one byte load and a branch per executed site. Replacement diff --git a/runtime/cfg.c b/runtime/cfg.c index d9c4cd61..0a3117c5 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -47,7 +47,7 @@ static const char *const known[] = { "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", - "SHADER_MEMO", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "SWKBD_ANSWER", + "SHADER_MEMO", "SHADOW_RES", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", "WB_EXACT", "WB_FENCE_LIMIT", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", @@ -62,6 +62,7 @@ static const struct { const char *key, *section; } sections[] = { {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"COLOR_PRESET", "display"}, {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, {"COLOR_GAMMA", "display"}, + {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"SHADOW_RES", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, {"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"VOLUME", "audio"}, @@ -103,6 +104,7 @@ static const CfgMeta metas[] = { {"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE}, {"COLOR_GAMMA", "1", "0.8..1.2", LIVE}, {"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART}, + {"SHADOW_RES", "1024", "game|512|768|1024|1536|2048", RESTART}, // Game {"FPS_LIMIT", "60", "30|40|45|60|90|120|144|display", LIVE}, {"FPS60", "1", "0|1", LIVE}, {"REALTIME", "1", "0|1", RESTART}, {"CLOTH_REALTIME", "1", "0|1", RESTART}, diff --git a/runtime/resolution.c b/runtime/resolution.c index 1d8e2355..d042ef8b 100644 --- a/runtime/resolution.c +++ b/runtime/resolution.c @@ -43,6 +43,7 @@ #define SCENE_H 900 #define DTA_KIB 113664u // DynamicTextureAllocatorMem, shipped #define SCENE_MAX 4096 // per side, as MacNativeGames' TOTK_RENDER_RES +#define SHADOW_BASE 960 // the sun shadow map's base size (its cascades: a D16 array of 4 layers) static struct { int decided; @@ -50,6 +51,7 @@ static struct { int scene_set; // the scene differs from the game's 5/6 of the output int heapdef_ok; // romfs.c serves the grown HeapDef int on; // res_init switched the patch sets on + unsigned shadow; // fixed sun shadow map size (TOTK_SHADOW_RES), 0: the game's own } R; static void decide(void) { @@ -83,19 +85,31 @@ static void decide(void) { if (fit < 1.0) R.scene_set = 1; R.sw = (unsigned)(sw * fit + 0.5); R.sh = (unsigned)(sh * fit + 0.5); + const char *sr = cfg_value("TOTK_SHADOW_RES"); + if (strcmp(sr, "game")) { + unsigned n = (unsigned)atoi(sr); + if (n >= 256 && n <= 4096 && !(n & 7)) R.shadow = n; + else rt_log("resolution: TOTK_SHADOW_RES=%s is not game or a size 256..4096 (a multiple of 8); game", sr); + } +} + +// Texture heap for a sun shadow map above the base size: the four layers of 2 bytes, three maps in flight (the engine +// makes the array anew each frame). +static uint64_t shadow_kib(void) { + return R.shadow > SHADOW_BASE ? ((uint64_t)R.shadow * R.shadow - SHADOW_BASE * SHADOW_BASE) * 8 * 3 / 1024 : 0; } static int bigger(void) { decide(); - return R.ow != BASE_W || R.oh != BASE_H || R.scene_set; + return R.ow != BASE_W || R.oh != BASE_H || R.scene_set || shadow_kib(); } uint64_t res_heap_extra_kib(void) { if (!bigger()) return 0; double grow = fmax(0.0, (double)R.ow * R.oh / (BASE_W * BASE_H) - 1.0) + fmax(0.0, (double)R.sw * R.sh / (SCENE_W * SCENE_H) - 1.0); - if (grow <= 0) return 0; - return (uint64_t)ceil(DTA_KIB * (1.0 + grow) / 1024.0) * 1024 - DTA_KIB; + uint64_t kib = grow > 0 ? (uint64_t)ceil(DTA_KIB * (1.0 + grow) / 1024.0) * 1024 - DTA_KIB : 0; + return kib + ((shadow_kib() + 1023) & ~1023ull); // whole MiB } uint64_t res_heap_extra_bytes(void) { return ((res_heap_extra_kib() << 10) + 0x1fffff) & ~0x1fffffull; } @@ -190,12 +204,32 @@ int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n) { void res_heapdef_served(void) { R.heapdef_ok = 1; } +// ---- sun shadow map size: each frame the engine sizes its shadow cascades to the area they cover, down to about 400 near +// the ground, which draws foliage shadows blocky, and each change of size moves the shadow texels, so leaf shadows jump. +// A fixed size (the shadow set, recomp/patches-v121.txt) keeps them still and sharper. Page +92. +static void shadow_init(uint8_t *page) { + if (!R.shadow) { + rt_log("resolution: sun shadow map sized by the game (TOTK_SHADOW_RES=game)"); + return; + } + int set = rt_patch_set("shadow"); + if (set < 0) { + rt_log("resolution: the generated C has no shadow patch set: the game sizes its shadow map"); + return; + } + int32_t n = (int32_t)R.shadow; + memcpy(page + 92, &n, 4); + rt_patch_switch(set, 1); + rt_log("resolution: sun shadow map %ux%u (TOTK_SHADOW_RES)", R.shadow, R.shadow); +} + // ---- start: the patch page and the sets static uint64_t pack(unsigned w, unsigned h) { return (uint64_t)h << 32 | w; } void res_init(void) { if (!bigger()) { rt_log("resolution: the game's own (output 1920x1080, scene 1600x900)"); + shadow_init(rt_patch_page()); return; } int output = rt_patch_set("output"), scene = rt_patch_set("scene"); @@ -238,6 +272,7 @@ void res_init(void) { } else { rt_patch_switch(aspect, 0); // the page holds no ratio } + shadow_init(page); rt_log("resolution: output %ux%u, scene %ux%u%s; texture heap +%llu KiB", R.ow, R.oh, R.sw, R.sh, scene_on ? "" : " (the game's own 5/6 of the output)", (unsigned long long)res_heap_extra_kib()); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 0e02e4bf..dba1adf9 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -505,6 +505,15 @@ static void row_render_scale(void) { opt_choice_list("TOTK_RENDER_SCALE", UI_MENU_RENDER_SCALE, vals, names, N, UI_MENU_RENDER_SCALE_DESC); } +static void row_shadow_res(void) { + static const char *const vals[] = {"game", "512", "768", "1024", "1536", "2048"}; + enum { N = sizeof vals / sizeof *vals }; + const char *names[N]; + names[0] = menu_text(UI_MENU_SHADOW_RES_GAME); + for (int i = 1; i < N; i++) names[i] = vals[i]; + opt_choice_list("TOTK_SHADOW_RES", UI_MENU_SHADOW_RES, vals, names, N, UI_MENU_SHADOW_RES_DESC); +} + static void row_gpu(void) { const char *vals[MAX_GPUS + 1], *names[MAX_GPUS + 1]; char idx[MAX_GPUS][4]; @@ -623,6 +632,7 @@ static void tab_screen(float scale) { opt_group(UI_MENU_GRAPHICS); row_output_res(); row_render_scale(); + row_shadow_res(); static const UiTextId up[] = {UI_MENU_UPSCALER_FSR, UI_MENU_UPSCALER_BILINEAR}; opt_choice("TOTK_UPSCALER", UI_MENU_UPSCALER, up, 2, UI_MENU_UPSCALER_DESC); opt_slider("TOTK_SHARPNESS", UI_MENU_SHARPNESS, 0, 100, UI_MENU_SHARPNESS_DESC, 5, "%.0f %%"); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index f697e68c..1a70f92c 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -69,6 +69,11 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_ASPECT_FIT] = "Black bars", [UI_MENU_ASPECT_STRETCH] = "Stretch", [UI_MENU_FOV] = "Field of view", [UI_MENU_FOV_DESC] = "How much of the world the camera shows, in percent of the game's own. Game version 1.4.3 only.", + [UI_MENU_SHADOW_RES] = "Shadow map size", + [UI_MENU_SHADOW_RES_DESC] = "Size of the sun's shadow map. The game's own size changes with the view, down to about 400 " + "near the ground, so leaf shadows look blocky and jump; a fixed size keeps them still and " + "sharper. Above 1024 the shadows cost more GPU time and video memory.", + [UI_MENU_SHADOW_RES_GAME] = "Game (changes)", [UI_MENU_SHARPNESS] = "FSR sharpness", [UI_MENU_SHARPNESS_DESC] = "Sharpening after FSR scaling; 0 turns it off.", [UI_MENU_COLOR] = "Colour", @@ -329,6 +334,11 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_ASPECT_FIT] = "Чёрные полосы", [UI_MENU_ASPECT_STRETCH] = "Растянуть", [UI_MENU_FOV] = "Поле зрения", [UI_MENU_FOV_DESC] = "Сколько мира показывает камера, в процентах от игрового. Только для версии игры 1.4.3.", + [UI_MENU_SHADOW_RES] = "Размер карты теней", + [UI_MENU_SHADOW_RES_DESC] = "Размер карты теней от солнца. Собственный размер игры меняется вместе с видом, у земли до " + "примерно 400, поэтому тени листвы выглядят квадратными и дёргаются; постоянный размер делает " + "их ровными и чётче. Выше 1024 тени требуют больше времени GPU и видеопамяти.", + [UI_MENU_SHADOW_RES_GAME] = "Как в игре (меняется)", [UI_MENU_SHARPNESS] = "Резкость FSR", [UI_MENU_SHARPNESS_DESC] = "Повышение резкости после масштабирования FSR; 0 выключает его.", [UI_MENU_COLOR] = "Цвет", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index a2088960..d664776d 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -37,6 +37,7 @@ typedef enum { UI_MENU_UPSCALER, UI_MENU_UPSCALER_DESC, UI_MENU_UPSCALER_FSR, UI_MENU_UPSCALER_BILINEAR, UI_MENU_ASPECT, UI_MENU_ASPECT_DESC, UI_MENU_ASPECT_FIT, UI_MENU_ASPECT_STRETCH, UI_MENU_FOV, UI_MENU_FOV_DESC, + UI_MENU_SHADOW_RES, UI_MENU_SHADOW_RES_DESC, UI_MENU_SHADOW_RES_GAME, UI_MENU_SHARPNESS, UI_MENU_SHARPNESS_DESC, UI_MENU_COLOR, // group heading over the colour rows UI_MENU_COLOR_PRESET, UI_MENU_COLOR_PRESET_DESC, UI_MENU_COLOR_DEFAULT, UI_MENU_COLOR_VIVID, UI_MENU_COLOR_CLARITY, diff --git a/totk.ini.example b/totk.ini.example index 6d3d5f1e..c9101a95 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -153,6 +153,10 @@ # color_contrast = 100 # color_saturation = 100 # color_gamma = 1 +# Sun shadow map size: game (the game's own, which changes with the view and drops to about 400 near the ground, so +# leaf shadows look blocky and jump) or a fixed 512, 768, 1024, 1536 or 2048. Above 960 the texture heap grows with it. +# Read at start. +# shadow_res = 1024 [language] # Language: a Windows locale such as de-DE, or a Switch code (en-US, ja, zh-Hant, ...). Default: the Windows locale.