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
1 change: 1 addition & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fetched or linked at build time (not in this repository):
| Code | Use | License |
|---|---|---|
| [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross) at `aa217ae` | SPIR-V to Metal Shading Language | Apache-2.0 |
| [maxmetal](https://github.com/svich/maxmetal) at `919805d` | Maxwell shaders to Metal Shading Language, in the iPhone / iPad app | MIT |
| [hactool](https://github.com/SciresM/hactool) at `1d64a83` (with explicit private title-key-file hook), including its bundled mbedTLS | Base + update BKTR merge at build time only | ISC (hactool), Apache-2.0 (mbedTLS); see fetched source LICENSE files |
| libopus (Homebrew), linked statically | Opus audio | BSD-3-Clause |
| libvpx (Homebrew), linked statically | VP9 movies | BSD-3-Clause |
Expand Down
39 changes: 33 additions & 6 deletions build_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
3. shaders: SPIRV-Cross (fetched at a pinned commit, compiled with clang) and the shader translator, which ships
ready-made as a native program (games/totk/prebuilt/macos-arm64: vendor/ryujinx + patches + Service.cs, made with
games/totk/tools/totk_xlate/publish_native.sh); startup shaders are extracted from your selected RomFS and
translated into a bundled seed, ready for GPU warm-up even with an empty cache on the first launch
translated into a bundled seed, ready for GPU warm-up even with an empty cache on the first launch. The iPhone /
iPad app also carries maxmetal (Maxwell code straight to Metal Shading Language, fetched at a pinned commit) and
translates on the device the shaders its seed lacks instead of skipping them; the Mac keeps the Ryujinx translator
4. the game binary and either a macOS app bundle or an unsigned iPhone/iPad IPA (large game data is cloned when possible);
for the Mac the startup shaders are then compiled once into this Mac's Metal cache (about 12 minutes for 1.4.3 on an M4 Max), so the
first start does not compile them in the background (games/totk/tools/warm_metal_cache.m); with --mod
Expand Down Expand Up @@ -88,6 +90,8 @@
'spirv_cross_util.cpp', 'spirv_glsl.cpp', 'spirv_hlsl.cpp', 'spirv_msl.cpp', 'spirv_parser.cpp',
'spirv_reflect.cpp')
SPIRV_CROSS_FLAGS = ('-O3', '-DNDEBUG', '-std=c++11', '-arch', 'arm64', '-Wno-deprecated-declarations')
# maxmetal: the shader translator in the iPhone / iPad app (ios/project.yml, totk_maxmetal_rt.h)
MAXMETAL = ('https://github.com/svich/maxmetal.git', '919805d2567ddd7a53e96492d0abb7c42830d0e2')
# Homebrew first; then the folder of the running interpreter (the Python uv selected, or an activated virtual environment),
# last so that a system Python's /usr/bin never shadows the Homebrew tools for the build's subprocesses.
TOOL_PATHS = ['/opt/homebrew/bin', '/usr/local/bin', str(Path(sys.executable).parent)]
Expand Down Expand Up @@ -999,6 +1003,14 @@ def compile_one(name):
logs / 'spirv-cross' / 'link.log', quiet=False)


def fetch_maxmetal(tree, logs):
"""maxmetal's sources at the pinned commit (MAXMETAL) in work/src/maxmetal, linked as the tree's tooling/maxmetal."""
say('fetching the pinned maxmetal sources')
src = tree.parent / 'src/maxmetal'
git_fetch(*MAXMETAL, src, logs / 'fetch-maxmetal.log')
link(tree / 'tooling/maxmetal', src)


def check_translator(tree):
"""The shader translation service: a native program in this repository (no .NET needed to build or to play)."""
translator = tree / 'prebuilt/macos-arm64/totk_xlate'
Expand Down Expand Up @@ -2113,6 +2125,11 @@ def parse_args(argv):
'held is moved next to it as <version>.backup-<date>-<time>, not deleted. Without this flag '
'the build leaves the folder alone and prints the two commands that fill it by hand')
ap.add_argument('--shader-cache', type=Path, help=argparse.SUPPRESS) # removed: the seed comes from the game data and the shared list
ap.add_argument('--no-startup-shaders', action='store_true',
help='iPhone / iPad: prepare no startup shaders (the build skips translating and compiling about 30,000 '
'of them, about an hour of a first build here); maxmetal in the app translates each shader the '
'first time the game asks for it and Metal compiles it on the device, so the first play stutters '
'until the device has them (the device keeps them)')
ap.add_argument('--no-shader-reminder', action='store_true', help='iOS: do not install the Mac agent that asks, while an iPhone / iPad with the game is connected, to send the shaders the shared list lacks')
ap.add_argument('--native-4k', action=argparse.BooleanOptionalAction, default=None,
help='Mac, Tears of the Kingdom 1.4.3 / 1.2.1 and Breath of the Wild: the settings panel also offers native 4K (a 3840x2160 canvas, UI and scene); the game then '
Expand All @@ -2132,6 +2149,8 @@ def parse_args(argv):
def main(argv):
args = parse_args(argv)
device_app = args.platform in ('ios', 'visionos')
if args.no_startup_shaders and not device_app:
raise BuildError('--no-startup-shaders is for iPhone / iPad builds: the Mac app translates with Ryujinx and needs them')
game, keys = args.game.expanduser().resolve(), args.keys.expanduser().resolve()
if not game.is_file():
raise BuildError(f'no such file: {game}')
Expand Down Expand Up @@ -2241,16 +2260,20 @@ def existing_volume_path(path):
' (this Mac has ' + ('16 GB or more' if native_4k else 'less than 16 GB') + ' of memory; --native-4k / --no-native-4k decide)'))
step_code_botw(tree, botw, update or game, keys, env, logs, version=version)
step_data_botw(tree, botw, game, keys, logs, version=version, update=update, dlc=dlcs, firmware=firmware, shared_fonts=shared_fonts)
step_shaders_botw(tree, botw, logs, version=version)
if not args.no_startup_shaders:
step_shaders_botw(tree, botw, logs, version=version)
else:
say('3/4 shaders: none prepared (--no-startup-shaders); maxmetal in the app translates each one on the device')
if not device_app: # the Mac app's metallibs; build_ipa.py compiles the device's own (6 of a 21-minute iPhone build, 2026-10-28)
step_shader_libs(tree, logs, version=version)
# The work folder's copy of games/botw (prepare_work), relative: make splits a path with spaces into several targets
step_binary(tree, game, keys, {**env, 'SHADER_LIST': '../botw/assets/shader_list.tsv'}, logs, version=version, title='01007ef00011e000')
say(f'4/4 app: assembling {out}')
if device_app: # the same IPA tooling as Tears of the Kingdom (ios/tools/build_ipa.py --version botw160 / botw190)
fetch_maxmetal(tree, logs) # the shader translator in the app
run([py, 'ios/tools/build_ipa.py', '--tree', tree, '--out', out, '--version', version,
'--logs', logs, '--app-name', APPS['botw'][2], '--bundle-id', APPS['botw'][1],
*(['--image-outside'] if args.ios_game_data == 'documents' else []),
*(['--image-outside'] if args.ios_game_data == 'documents' else []), *(['--no-seed'] if args.no_startup_shaders else []),
*[x for g in args.device_gpu or [] for x in ('--device-gpu', g)]], logs / 'build-ipa.log', cwd=tree, env=env)
say_device_pipelines(logs)
say(f'done in {(time.time() - t0) / 60:.0f} min: {out}')
Expand Down Expand Up @@ -2326,18 +2349,22 @@ def existing_volume_path(path):
step_update_data(tree, game, update, keys, logs, version=version, mod=mod, native_4k=native_4k, native_5k=args.native_5k, vr=args.vr)
if mod:
check_mod_overlays(tree, mod, version)
step_shaders(tree, logs, version=version)
if not args.no_startup_shaders:
step_shaders(tree, logs, version=version)
else:
say('3/4 shaders: none prepared (--no-startup-shaders); maxmetal in the app translates each one on the device')
if not device_app: # the Mac app's metallibs; build_ipa.py compiles the device's own
step_shader_libs(tree, logs, version=version)
step_binary(tree, game, keys, env, logs, version=version)
say(f'4/4 app: assembling {out}')
if device_app:
fetch_maxmetal(tree, logs) # the shader translator in the app
run([py, 'ios/tools/build_ipa.py', '--tree', tree, '--out', out, '--version', version,
'--logs', logs, '--app-name', IOS_APP_NAME, '--bundle-id', BUNDLE_ID, '--platform', args.platform,
*(['--image-outside'] if args.ios_game_data == 'documents' else []),
*(['--image-outside'] if args.ios_game_data == 'documents' else []), *(['--no-seed'] if args.no_startup_shaders else []),
*[x for g in args.device_gpu or [] for x in ('--device-gpu', g)]], logs/'build-ipa.log', cwd=tree, env=env)
say_device_pipelines(logs)
if not args.no_shader_reminder:
if not args.no_shader_reminder and not args.no_startup_shaders: # without them nothing says what the shared list lacks
# The iOS app never asks during play; while the device is connected, this Mac asks to send the shaders the
# shared list lacks (ios/tools/shader_contrib_watch.py). Not essential: a failure only says so.
try:
Expand Down
2 changes: 1 addition & 1 deletion docs/first-launch-shaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## 构建与启动行为

1. `build_app.py` 在签名打包前,用所选版本、已经通过完整校验的本地 RomFS 准备着色器,不读取现有用户缓存。`prepare_shader_seed.py` 解压字典压缩包,扫描整个 RomFS(`Shader/` 包、SDK/Bootup 包,以及特效 `Effect/*.esetb` 和界面布局里嵌的着色器,2026-10-19 起)和游戏程序模块(main 里嵌着 BNSH),解析 BNSH/NVN 程序,也解析 agl 引擎自己的着色器包 `.sharcb`(文件头 `BAHS`:记录头里是代码在代码池的偏移、控制段大小和到控制段的有符号偏移,几个记录可以共用一个控制段)。标题画面的合成、后期处理等用的就是 agl 的着色器(`Lib/agl`、`Shader/ApplicationPackage`、`Lib/Terrain`、`Lib/gsys`),只扫 BNSH 时全部漏掉,全新安装的 iPhone 因此整屏品红。控制段有 0x880 和 0x88c 两种大小,运行时的键只算前 0x880 字节;不为旧式短 control 结构编造补齐字节。
2. 按运行时相同的 control + code SHA-256 去重,用原生翻译器和 SPIRV-Cross 为启动包里的着色器生成默认绑定状态的 MSL、反射及依赖索引;再按共享清单 `assets/shader_list.tsv`([shader-list.md](shader-list.md))为本游戏里找得到的着色器逐个生成清单记下的绑定状态,和已有变体依赖相同的只留一个。iPhone / iPad 不能边玩边翻译,靠的就是这些。成功准备少于 8,000 个时停止构建,不能只靠清单、重复条目或虚增计数交付。失败翻译留下的半成品不打包。每个 CPU 核心各起一个翻译服务进程,按着色器分配(同一着色器的默认与清单绑定状态在同一进程里按原顺序翻),结果按原顺序合并,输出与逐个翻译逐字节相同;Apple M4(10 核)上 1.4.3 的 24,032 个翻译任务由约 166 秒降到约 29 秒。
2. 按运行时相同的 control + code SHA-256 去重,用原生翻译器和 SPIRV-Cross 为启动包里的着色器生成默认绑定状态的 MSL、反射及依赖索引;再按共享清单 `assets/shader_list.tsv`([shader-list.md](shader-list.md))为本游戏里找得到的着色器逐个生成清单记下的绑定状态,和已有变体依赖相同的只留一个。iPhone / iPad 有了这些,就不必边玩边用 App 里的 maxmetal 翻译。成功准备少于 8,000 个时停止构建,不能只靠清单、重复条目或虚增计数交付。失败翻译留下的半成品不打包。每个 CPU 核心各起一个翻译服务进程,按着色器分配(同一着色器的默认与清单绑定状态在同一进程里按原顺序翻),结果按原顺序合并,输出与逐个翻译逐字节相同;Apple M4(10 核)上 1.4.3 的 24,032 个翻译任务由约 166 秒降到约 29 秒。
3. 生成 `build/shader-seed`。其中 `shader-keys.json` 列出这个版本游戏里找到的全部着色器键(包括没翻译或翻译失败的),带翻译链、版本和 RomFS 哈希,并计入 `seed-manifest.json` 的文件哈希;没有它的旧种子会重新准备。版本、翻译链、工具身份和所有文件哈希在复用/打包时检查;这是用户游戏衍生数据,保留在工作目录和 App,不提交源码仓库。Metal 库在目标 Mac 上生成,不打包其他 GPU 的二进制缓存。
4. 装有 Metal 工具链时,`step_shader_libs` 用与运行时相同的安全数学选项把种子里的每个 MSL 预先编译成 `build/shader-libs/<源码 SHA-256>.metallib` 并放进 App;重新构建只编译变化的着色器。运行时按本地 MSL 的 SHA-256 查找,只用完全一致的库,否则照旧现编。系统 Metal 缓存不跟着 App 走(App 挪到路径长度不同的目录就不命中,见下文“Metal 的缓存”),这些库随 App 移动。
5. 首次启动验证并导入到外部用户缓存,先发布 shader 文件,再合并 `.variants`;保留用户已有变体及其他文件。相同种子不重复导入,删除文件后可补齐;错误翻译链、篡改源文件和越界文件名被拒绝。并发调用等待同一次导入完成。
Expand Down
10 changes: 5 additions & 5 deletions docs/shader-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The game builds a shader *variant* from each shader for the bind state of a draw
slot, attribute types, output formats. A variant can only be translated once it is known.

- On the Mac, a missing variant is translated while you play. That costs a short stall the first time.
- An iPhone or iPad cannot translate. It skips any draw whose variant the app does not already carry. A new install
with only the default variants shows little or nothing of the picture.
- An iPhone or iPad translates it on the spot too, with maxmetal inside the app, at the same cost. It skips the draws of
a variant maxmetal cannot translate and records it for `fill_shaders.py`.

The list names the variants players have met, so each build carries them from the start.

Expand Down Expand Up @@ -74,8 +74,8 @@ app) the whole cache is warmed as before; `TOTK_WARM_ALL=1` does that for compar

## Sending what you played (players)

You do not need to do anything while playing. When the Mac has to translate a shader variant, or an iPhone or iPad
misses one, that variant is not in the list yet, so the game writes it to `shader-list-contribution.txt`: one `V` row
You do not need to do anything while playing. When the Mac or an iPhone or iPad has to translate a shader variant, or
a device misses one, that variant is not in the list yet, so the game writes it to `shader-list-contribution.txt`: one `V` row
per shader and bind state, nothing else (`third_party/d2r_gfx/gfx/nvn/totk_shader_contrib.h`). Since every build carries
each shader's default variant ([shader-variants.md](shader-variants.md); `all_default` in the seed's manifest), only
states those variants do not serve are written, never a default variant (`totk_shader_rt.m contrib_wanted`): the list
Expand Down Expand Up @@ -216,7 +216,7 @@ The tool drops rows that are not shader identifiers. Merging the same source twi

`games/totk/assets/shader_list.tsv` 是唯一的一份着色器清单。Mac、iPhone、iPad 都用它,所有人玩出来的新变体都加进这一份,欢迎提 PR。

- **为什么要有它**:iOS 不能当场翻译着色器,缺哪个变体就跳过用到它的绘制。清单里的变体在打包时就用玩家自己的游戏文件翻译好。
- **为什么要有它**:缺的变体要当场翻译(iPhone / iPad 用 App 里的 maxmetal),第一次会卡一下;maxmetal 翻译不了的,就跳过用到它的绘制。清单里的变体在打包时就用玩家自己的游戏文件翻译好。
- **里面是什么**:
- `V` 行:着色器键 + 绑定状态(deps)。
- `L` 行:打包程序自己找不到的着色器在玩家游戏文件中的位置:文件路径、控制段和代码的偏移、代码长度。打包时从玩家自己的文件读出,核对哈希等于键才用。BNSH 文件、agl 的 `.sharcb` 包和游戏程序里的着色器,打包程序会自己找到。
Expand Down
2 changes: 1 addition & 1 deletion docs/shader-variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A game shader is translated for the bind state of a draw (`deps`: texture kinds, image formats, vertex input and
fragment output types). Until 2026-10-22 almost every bound texture slot was a dependency, so one shader had up to nine
variants, and the ones a build did not carry had to be met in play and collected in the shared list
([shader-list.md](shader-list.md)). An iPhone or iPad, which cannot translate, skips draws whose variant it lacks.
([shader-list.md](shader-list.md)). An iPhone or iPad translates a variant it lacks with maxmetal in the app, and skips its draws only when that fails.

## What the variants differed in (measured 2026-10-22)

Expand Down
2 changes: 1 addition & 1 deletion docs/test-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Version 1.4.3 has been played on an **iPhone 14 (iOS 26.6.1)** and an **iPad wit

- **First launch:** the app checks the game data (about 15 seconds) and imports the shaders it carries, with a progress bar. On an iPhone 14 this takes about 3 minutes before the title appears. Later launches show the title in about 8 seconds.
- **Picture:** the game runs in handheld mode, as a Switch does without its dock: 1280×720.
- **Shaders:** an iPhone or iPad cannot translate shaders while playing. A draw whose shader variant the app does not carry is skipped, so objects can be missing in places no player has reported yet. The device records what it missed in `shader-list-contribution.txt` (Files app › On My iPhone › Tears of the Kingdom); the game never asks about it during play. While the device is connected to the Mac that built the app (cable or the same Wi-Fi), the Mac checks every 10 minutes and every 500 new shaders asks once to send the file in a [GitHub issue](https://github.com/ly05010419/MacNativeGames/issues/new?template=shader-list.yml) (`games/totk/ios/tools/shader_contrib_watch.py`, installed by the iOS build; `--no-shader-reminder` skips it, `shader_contrib_watch.py uninstall` removes it). Once merged into the [shared shader list](shader-list.md), the next build carries them for everyone. With a Mac, `games/totk/ios/tools/fill_shaders.py` also translates them and sends them to the device right away.
- **Shaders:** an iPhone or iPad translates a shader variant the app does not carry while playing, with maxmetal inside the app: a short stall the first time. A draw whose variant maxmetal cannot translate is skipped, so objects can be missing there. The device records the variants it translated or missed in `shader-list-contribution.txt` (Files app › On My iPhone › Tears of the Kingdom); the game never asks about it during play. While the device is connected to the Mac that built the app (cable or the same Wi-Fi), the Mac checks every 10 minutes and every 500 new shaders asks once to send the file in a [GitHub issue](https://github.com/ly05010419/MacNativeGames/issues/new?template=shader-list.yml) (`games/totk/ios/tools/shader_contrib_watch.py`, installed by the iOS build; `--no-shader-reminder` skips it, `shader_contrib_watch.py uninstall` removes it). Once merged into the [shared shader list](shader-list.md), the next build carries them for everyone. With a Mac, `games/totk/ios/tools/fill_shaders.py` also translates them and sends them to the device right away. An app built with `--no-startup-shaders` carries no startup shaders at all: the build skips about an hour of preparing them, maxmetal translates every shader on the device, and the first play stutters while Metal compiles them there (the device keeps them). Such an app records no contributions.
- **Memory:** the game uses close to 4 GB on an iPhone 14, near what the phone can give it. In busy areas iOS can close the game after several minutes of play when the phone runs out of memory (seen twice on an iPhone 14, about 8 minutes in). Reducing the game's memory on iOS is the next piece of work.

## Automated tests
Expand Down
Loading
Loading