diff --git a/.github/ISSUE_TEMPLATE/1-crash.yml b/.github/ISSUE_TEMPLATE/1-crash.yml index adb68726..14055cc4 100644 --- a/.github/ISSUE_TEMPLATE/1-crash.yml +++ b/.github/ISSUE_TEMPLATE/1-crash.yml @@ -23,10 +23,23 @@ body: validations: required: true - type: input + id: last-good + attributes: + label: Last version that worked + description: If an earlier version did not crash at this point, which one. + - type: textarea id: system attributes: - label: GPU, driver and Windows version - description: Copy system info in the menu's Advanced tab puts them on the clipboard. + label: System info + description: Paste what Copy system info (F1, Advanced tab) puts on the clipboard. On a Steam Deck, add LCD or OLED. + render: text + validations: + required: true + - type: textarea + id: mods + attributes: + label: Mods and cheats + description: The folders in mods/ with their download links, or "none". Say whether it also crashes without them. validations: required: true - type: dropdown @@ -56,10 +69,32 @@ body: label: What you did before the crash validations: required: true + - type: markdown + attributes: + value: | + ### The log + + - The log of the run that crashed is `logs\totk.log` in the folder of `totk.exe` (F1, Advanced tab, Open log folder). Copy it before you start the game again: the next start renames it to `totk.1.log`, and after five starts it is gone. + - Attach the whole file. Do not cut or edit it: the crash report is at the end, the GPU and driver lines are at the start. + - The log says `device lost` or `VK_ERROR_DEVICE_LOST` and the GPU is an NVIDIA one: set GPU pass marks to On (F1, Advanced tab, Diagnostics), restart the game, make it crash again and attach that log. The `vk: pass marks` lines then name what the GPU was drawing. - type: textarea id: log attributes: label: Log - description: Attach logs\totk.log from the folder of totk.exe. Copy it before you start the game again, because the next start overwrites it. + description: Drag totk.log into this field. validations: required: true + - type: markdown + attributes: + value: | + ### Save and input recording + + When the crash comes back at one place in the game (an area, a shrine, a cutscene, a quest step), the save that gets there lets us crash it on our machine. + + 1. Zip the `profile` folder next to `totk.exe` and attach it. Say which slot you load, or that you use the autosave. + 2. If it takes more than walking a few steps from the save: turn on Input recording (F1, Advanced tab, Diagnostics), restart the game, load the save, make it crash, and attach `input-record.txt` from the logs folder together with the `totk.log` of that run. + - type: textarea + id: save + attributes: + label: Save and input recording + description: Drag the zipped profile folder and input-record.txt into this field, when the crash comes back at one place. diff --git a/.github/ISSUE_TEMPLATE/2-graphics.yml b/.github/ISSUE_TEMPLATE/2-graphics.yml index bf297032..bce2b3f5 100644 --- a/.github/ISSUE_TEMPLATE/2-graphics.yml +++ b/.github/ISSUE_TEMPLATE/2-graphics.yml @@ -23,24 +23,37 @@ body: validations: required: true - type: input + id: last-good + attributes: + label: Last version that worked + description: If an earlier version drew this correctly, which one. + - type: textarea id: system attributes: - label: GPU, driver and Windows version - description: Copy system info in the menu's Advanced tab puts them on the clipboard. + label: System info + description: Paste what Copy system info (F1, Advanced tab) puts on the clipboard. It lists the settings you changed too. On a Steam Deck, add LCD or OLED. + render: text + validations: + required: true + - type: textarea + id: mods + attributes: + label: Mods and cheats + description: The folders in mods/ with their download links, or "none". Say whether the glitch also shows without them. Turn off ReShade and other overlays that draw into the game before you report. validations: required: true - type: textarea id: screenshot attributes: label: Screenshot or video - description: Drag the file into this field. Say where in the game it was taken. + description: Drag the file into this field. Say where in the game it was taken. Flicker needs a video. validations: required: true - type: textarea id: details attributes: label: What looks wrong and when - description: Does it show all the time, in one place, or after some time in play? Which settings did you change from the defaults? + description: Does it show all the time, in one place, or after some time in play? Does it go away after a restart, or when you change a setting? validations: required: true - type: dropdown @@ -56,4 +69,6 @@ body: id: log attributes: label: Log - description: Attach logs\totk.log from the folder of totk.exe. + description: Drag logs\totk.log from the folder of totk.exe into this field, from the run that showed the glitch. Copy it before you start the game again. + validations: + required: true diff --git a/.github/ISSUE_TEMPLATE/3-bug.yml b/.github/ISSUE_TEMPLATE/3-bug.yml deleted file mode 100644 index 20d7087f..00000000 --- a/.github/ISSUE_TEMPLATE/3-bug.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: Other bug -description: Sound, controls, performance, the settings menu, the installer, or game logic. -labels: [bug] -body: - - type: markdown - attributes: - value: | - Check the pinned issues at the top of the issue list first. - - Do not attach or link game files, keys, firmware or builds. Issues that do are closed. - - type: checkboxes - id: duplicates - attributes: - label: Existing issues - options: - - label: I read the pinned issues and [searched open and closed issues](https://github.com/valentderah/totk-pc-recomp/issues?q=is%3Aissue) for this bug. - required: true - - type: dropdown - id: area - attributes: - label: Area - options: - - Audio - - Controls and input - - Performance and stutter - - Settings menu and overlay - - Installer - - Game logic and physics - - Something else - validations: - required: true - - type: input - id: version - attributes: - label: Version - description: The title line of the settings menu (F1), for example "TotK Recomp 0.1.0, game 1.4.3". - validations: - required: true - - type: input - id: system - attributes: - label: GPU, driver and Windows version - description: Copy system info in the menu's Advanced tab puts them on the clipboard. - validations: - required: true - - type: dropdown - id: blocker - attributes: - label: Can you keep playing? - options: - - "Yes" - - "No, it stops the game or a quest" - validations: - required: true - - type: textarea - id: steps - attributes: - label: What happened and how to reproduce it - validations: - required: true - - type: textarea - id: log - attributes: - label: Log - description: Attach logs\totk.log from the folder of totk.exe. diff --git a/.github/ISSUE_TEMPLATE/3-performance.yml b/.github/ISSUE_TEMPLATE/3-performance.yml new file mode 100644 index 00000000..bb5d134b --- /dev/null +++ b/.github/ISSUE_TEMPLATE/3-performance.yml @@ -0,0 +1,108 @@ +name: Performance +description: Low frame rate, freezes, hitches, stutter or long loading. +labels: [performance] +body: + - type: markdown + attributes: + value: | + Check the pinned issues at the top of the issue list first. + + Shaders the setup did not prepare are built the first time they are needed, so a short hitch the first time an area or effect shows is expected. Report it when it happens again in the same place. + + Do not attach or link game files, keys, firmware or builds. Issues that do are closed. + - type: checkboxes + id: duplicates + attributes: + label: Existing issues + options: + - label: I read the pinned issues and [searched open and closed issues](https://github.com/valentderah/totk-pc-recomp/issues?q=is%3Aissue) for this problem. + required: true + - type: input + id: version + attributes: + label: Version + description: The title line of the settings menu (F1), for example "TotK Recomp 0.1.0, game 1.4.3". + validations: + required: true + - type: input + id: last-good + attributes: + label: Last version that ran better + description: If an earlier version was faster in the same place, which one. + - type: textarea + id: system + attributes: + label: System info + description: Paste what Copy system info (F1, Advanced tab) puts on the clipboard. It lists the settings you changed too. On a Steam Deck, add LCD or OLED. + render: text + validations: + required: true + - type: dropdown + id: machine + attributes: + label: Machine + options: + - Desktop PC + - Laptop, on mains power + - Laptop, on battery + - Steam Deck + - Other handheld PC + validations: + required: true + - type: textarea + id: mods + attributes: + label: Mods and cheats + description: The folders in mods/ with their download links, or "none". + validations: + required: true + - type: dropdown + id: kind + attributes: + label: What kind of problem? + options: + - The frame rate is low all the time + - Short freezes or hitches that come back in the same place + - The frame rate is fine, but motion is not smooth + - It gets slower the longer I play + - Loading takes long + validations: + required: true + - type: textarea + id: where + attributes: + label: Where and what you were doing + description: For example "Lookout Landing, walking to the tower", "the Depths below Hyrule Castle", "fighting a Talus in the Sky". How long into the session it started. + validations: + required: true + - type: textarea + id: overlay + attributes: + label: Performance overlay + description: Press F10 twice (or both stick clicks) for the full panel. Stay a minute in the place and write down the frame rate, the mean and the longest frame time, or attach a screenshot of the panel. + validations: + required: true + - type: markdown + attributes: + value: | + ### Recording the log + + 1. In the settings menu (F1), Advanced tab, Diagnostics: turn on Performance log. For freezes and hitches, also turn on Flight recorder. Restart the game: both take effect at the next start. + 2. Play at least three minutes in the place where it happens. The log gets a frame-time summary every 60 s, and a line for every frame over 50 ms that names what the frame waited for. + 3. With the flight recorder on, press F8 right after a hitch. It also saves on its own after every frame longer than the freeze threshold (100 ms), at most once per 10 s. + 4. Close the game with Quit game in the menu or the window's close button. Do not end it from Task Manager: the last summary is written when the game exits. + 5. Copy `logs\totk.log` from the folder of `totk.exe` before you start the game again. + 6. Each flight recorder save is a folder; the log line `flight: dump` names it. Zip the folders and attach them. + 7. Turn both off again afterwards: they cost a little frame time. + - type: textarea + id: log + attributes: + label: Log + description: Drag totk.log into this field. + validations: + required: true + - type: textarea + id: flight + attributes: + label: Flight recorder saves + description: Drag the zipped folders into this field, for freezes and hitches. diff --git a/.github/ISSUE_TEMPLATE/4-bug.yml b/.github/ISSUE_TEMPLATE/4-bug.yml new file mode 100644 index 00000000..fe63bd26 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/4-bug.yml @@ -0,0 +1,94 @@ +name: Other bug +description: Sound, controls, the settings menu, the installer, or game logic. +labels: [bug] +body: + - type: markdown + attributes: + value: | + Check the pinned issues at the top of the issue list first. Crashes, graphics glitches and performance have their own templates. + + Do not attach or link game files, keys, firmware or builds. Issues that do are closed. + - type: checkboxes + id: duplicates + attributes: + label: Existing issues + options: + - label: I read the pinned issues and [searched open and closed issues](https://github.com/valentderah/totk-pc-recomp/issues?q=is%3Aissue) for this bug. + required: true + - type: dropdown + id: area + attributes: + label: Area + options: + - Audio + - Controls and input + - Settings menu and overlay + - Installer + - Game logic and physics + - Something else + validations: + required: true + - type: input + id: version + attributes: + label: Version + description: The title line of the settings menu (F1), for example "TotK Recomp 0.1.0, game 1.4.3". For the installer, the version of TotK-Setup. + validations: + required: true + - type: input + id: last-good + attributes: + label: Last version that worked + description: If an earlier version did this correctly, which one. + - type: textarea + id: system + attributes: + label: System info + description: Paste what Copy system info (F1, Advanced tab) puts on the clipboard. On a Steam Deck, add LCD or OLED. For the installer, write your OS and the install disk. + render: text + validations: + required: true + - type: textarea + id: mods + attributes: + label: Mods and cheats + description: The folders in mods/ with their download links, or "none". Say whether it also happens without them. + validations: + required: true + - type: dropdown + id: blocker + attributes: + label: Can you keep playing? + options: + - "Yes" + - "No, it stops the game or a quest" + validations: + required: true + - type: textarea + id: steps + attributes: + label: What happened and how to reproduce it + description: What you expected, what happened instead, and the steps that lead there. + validations: + required: true + - type: markdown + attributes: + value: | + ### What to attach for your area + + Close the game with Quit game in the menu or the window's close button, then copy `logs\totk.log` from the folder of `totk.exe` before you start the game again. + + **Audio.** Open `totk.ini` in the folder of `totk.exe` and add the line `trace = audio`; when the file already has `trace = perf` (Performance log on), change it to `trace = perf,audio`. Start the game, play at least two minutes where the sound goes wrong and close the game. The log then has an `audio:` line every 5 s with the buffer underruns. Name the output device and how it is connected (speakers, headphones, USB, Bluetooth, HDMI), and attach a short video with sound if you can. Remove the line afterwards. + + **Controls and input.** Name the controller and how it is connected (USB, Bluetooth, Steam Input). Does it also happen with keyboard and mouse? + + **Controls, game logic, anything you can trigger again.** Turn on Input recording (F1, Advanced tab, Diagnostics), restart the game, load a save, make the bug happen and close the game. Attach `input-record.txt` from the logs folder and a zip of the save you loaded: the `profile` folder next to `totk.exe`. The recording replays what you did, so we can see the bug on our machine. + + **Installer.** Attach the newest `install-*.log` from `temp\logs` in the folder of `TotK-Setup.exe` (on Linux: `.setup-temp/logs` in the install folder), and the log file the error message names. + - type: textarea + id: log + attributes: + label: Log and other files + description: Drag totk.log and the files your area asks for into this field. + validations: + required: true diff --git a/.github/ISSUE_TEMPLATE/4-feature.yml b/.github/ISSUE_TEMPLATE/5-feature.yml similarity index 100% rename from .github/ISSUE_TEMPLATE/4-feature.yml rename to .github/ISSUE_TEMPLATE/5-feature.yml diff --git a/README.md b/README.md index 2d434c67..94c803bb 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,70 @@ # TotK Recomp -**TotK Recomp** is a static recompilation of *The Legend of Zelda: Tears of the Kingdom* (Nintendo Switch, 2023) into a native Windows x86-64 application ([docs/architecture.md](docs/architecture.md)). +**TotK Recomp** is a static recompilation of *The Legend of Zelda: Tears of the Kingdom* (Nintendo Switch, 2023) into a native x86-64 application for Windows and Linux ([docs/architecture.md](docs/architecture.md)). > [!IMPORTANT] -> **No game files are included.** You need your own legally obtained copy of *Tears of the Kingdom*, dumped from your own console: the base game and the 1.2.1 or 1.4.3 update. Instructions for obtaining those files will not be provided. +> **No game files are included.** You need your own legally obtained copy of *Tears of the Kingdom*, dumped from your own console: the base game and the 1.1.0, 1.2.1 or 1.4.3 update. Instructions for obtaining those files will not be provided. > > The game is built from your files on your machine, so the result contains the game. It is for your own use. Do not share it. ## Status -Early development. The game is playable on Windows; visual bugs and crashes are still expected. +Early development. The game is playable on Windows and Linux; visual bugs and crashes are still expected. Report problems as issues, following [CONTRIBUTING.md](CONTRIBUTING.md). ## Features - Native code, not emulation -- Game updates 1.2.1 and 1.4.3, with saves that carry over between them +- Game updates 1.1.0, 1.2.1 and 1.4.3; 1.2.1 and 1.4.3 saves carry over between them +- Takes NSP and XCI game files, and compressed NSZ and XCZ as they are: the setup unpacks them itself - 60 fps by default; up to 144 fps or the monitor's refresh rate (experimental) - Custom resolutions (1080p, 2k...), supersampling and AMD FSR 1 upscaling -- 16:10 (Steam Deck, handhelds), 21:9 and 32:9 (ultrawide) game resolutions, with the camera and the interface in that shape +- NVIDIA DLSS / DLAA on RTX GPUs: the setup downloads `nvngx_dlss.dll` from NVIDIA's DLSS SDK repository + (github.com/NVIDIA/DLSS, NVIDIA RTX SDK license) when it finds an RTX GPU; untick it in the setup to skip it +- HDR10 output with peak, game and HUD brightness settings +- 16:10 (Steam Deck, handhelds), 3:2 (Surface, Framework), 21:9 and 32:9 (ultrawide) game resolutions, with the camera and the interface in that shape - Anisotropic texture filtering up to 16x, for sharp ground into the distance - Optional full model detail at any distance (1.2.1) - In-game settings menu (F1) and performance overlay (F10) +- Discord Rich Presence ("Playing TotK Recomp" with the play time), off by default, own application id supported +- Setup and in-game menu in English, Russian, Brazilian Portuguese and Simplified Chinese - Keyboard, mouse and gamepad with remapping, rumble and gyro -- Xbox button prompts: a layout that keeps the Switch button positions and shows the Xbox labels +- Stereo or 5.1 surround sound output +- Xbox button prompts: LB, RB, LT, RT, Menu and View, and A, B, X and Y as labelled on the button to press, with either button layout - Shaders prepared at install instead of during play +- Mods made for emulators: unpack a mod into the `mods` folder next to `totk.exe`; switch and order them in the menu. RomFS + mods, TKMM's merged output and ExeFS code patches (`.ips`, `.pchtxt`) work; the game rebuilds itself for code patches + ([docs/mods.md](docs/mods.md)) +- Atmosphère cheats: put a cheat file into a mod and switch its cheats in the menu ([docs/cheats.md](docs/cheats.md)) ## Planned -- Linux and the Steam Deck -- Mods made for emulators: unpack a mod into a `mods` folder - VRR ## Installing -Requirements: Windows 10 or later, an x86-64 CPU with AVX2, a GPU with Vulkan 1.3 and `VK_EXT_external_memory_host`, about 36 GiB free during setup (20 GiB after). +### Windows + +Requirements: Windows 10 or later, an x86-64 CPU with SSE4.2 (AVX2/FMA machines get a faster build), a GPU with Vulkan 1.3 and `VK_EXT_external_memory_host`, about 36 GiB free during setup (20 GiB after). 1. Download `TotK-Setup--windows.zip` from [Releases](../../releases/latest) and unpack it. 2. Start `TotK-Setup.exe`. If SmartScreen warns about an unsigned app, choose *More info*, then *Run anyway*. -3. Pick your game files, the language and an install folder, and press *Start*. The setup builds the game once. +3. Pick your game files, the language and an install folder, and press *Start*. The setup builds the game once. With + an NVIDIA RTX GPU it also downloads NVIDIA DLSS, unless you untick *Download NVIDIA DLSS*; turn DLSS on in the + menu (F1). 4. Play `totk.exe` from the install folder. -To update, run a newer `TotK-Setup.exe` on the same folder; saves and settings are kept. Settings are in the in-game menu and in `totk.ini` ([totk.ini.example](totk.ini.example) lists every key). Controls: [docs/controls.md](docs/controls.md). +### Linux + +Requirements: as on Windows, plus Linux 6.7 or later and glibc 2.34 or later (Ubuntu 22.04, Debian 12, Fedora 35, SteamOS 3). + +1. Download `TotK-Setup-x86_64.AppImage` from [Releases](../../releases/latest), or build it ([docs/installer.md](docs/installer.md), "Linux release"). +2. Make it executable (`chmod +x TotK-Setup-x86_64.AppImage`) and start it. On a Steam Deck, do this in Desktop Mode. +3. Pick your game files, the language and an install folder, and press *Start*. +4. Play `totk` from the install folder. + +To update, run a newer setup on the same folder; saves and settings are kept. Settings are in the in-game menu and in `totk.ini` ([totk.ini.example](totk.ini.example) lists every key). Controls: [docs/controls.md](docs/controls.md). ## Credits diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 27c6106e..2d890f97 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -11,6 +11,8 @@ Code copied or adapted from another project is listed here with its source, the | MacNativeGames TotK frame-rate patches: 60 fps present-interval controller, real-time frame step and its present sampling, hair and cloth clock, trampoline slow-motion check (instruction words and 1.2.1 sites), player position chain; quick-menu capture background RomFS overlay | https://github.com/ly05010419/MacNativeGames @ `ebe621de99bfc48f3c42f2d2aa73e04fbcc6eae2` (`games/totk/runtime/game_patches.c`, `game_version.h`, `world_survey.c`, `games/totk/scripts/make_romfs_patches.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/frame_rate.c`, `recomp/patches-v121.txt`, `tools/make_romfs_patches.py` | | MacNativeGames TotK 1.4.3 fade fix: the four distance / LOD and pre-actor fade sites, their instruction words and the floor of the fade step at 1 | https://github.com/ly05010419/MacNativeGames @ `3e9e7f67fdfdd3404134dcfa0f24969a9425a33f` (`games/totk/runtime/game_patches_v143.c` patch_fade_increment), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set fade; the floor without flags, clz >> 5, as in pull request #78) | | MacNativeGames TotK aspect ratio: the camera and viewport sites that hand the perspective projection its aspect ratio, and the UI layout edits for a canvas wider than 16:9 | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/runtime/aspect_patch.h`, `aspect_math.h`, `game_patches_v143.c`, `games/totk/scripts/ultrawide_layout.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set aspect), `tools/ultrawide_layout.py`, `tools/make_romfs_patches.py` (--aspect) | +| MacNativeGames TotK button icons: the Xbox controller's shoulder buttons, triggers, Menu, View and d-pad drawn with letters from the game's own text fonts, the .bfotf key, the button guide's images and their block-linear layout, the resource size table raised for the archives that grew | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/scripts/make_button_icons.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/button_icons.py`, `tools/make_romfs_patches.py` (--prompts) | +| RenoDX TotK mod (game mod by souperman9, tonemapper by Musa, RenoDX framework by ShortFuse, RenoVK fork by Ritsu): the approach of the HDR tonemap (the game's 1 - e^-x curve kept below a pivot and continued along its tangent above it, the saturation grade applied to the colour scaled into 0..1) and the observation that the passes after the tonemap keep values above 1. No code copied; the shader is our own | https://github.com/clshortfuse/renodx @ `b6f87272ad1bfdc0e6444910a93606815944a085` (`src/games/totk/tonemap.slang`, `addon.cpp`), Copyright (c) 2025 Carlos Lopez Jr. | MIT | `runtime/shaders/hdr_tonemap.frag`, `runtime/vk_hdr.c` | | TOTK-Ultrawide, the community mod for emulators: the list of UI panes the ultrawide layout edits and the distances they move (data only, no code copied; the edit code is our own, and MacNativeGames' `ultrawide_layout.py` in the row above already carries the same values). The mod's repository URL and licence version are not recorded in this repository | TOTK-Ultrawide (community mod; URL not recorded here) | GPL (version not recorded here) | `tools/ultrawide_layout.py` (`KEEP_ROOT`, `edits`, `anim_edits`) | | MacNativeGames TotK virtual-core preemption: asynchronous preemption of an owner in top-level game code, priority inheritance through guest mutexes, the SDK's preemption-disable protocol (TLR+0x100/+0x102, SVC 0x36), the core-wait diagnostics | https://github.com/ly05010419/MacNativeGames @ `33e8fa83052ea1ae09a3583137008e9797e12950` (`games/totk/runtime/threads.c`, commits 8bf0893 and 19730cd), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/kernel.c` (SuspendThread and a register-saving stub replace SIGUSR2), `runtime/hostfn.c`, `runtime/svc.c` (host depth) | | MacNativeGames RomFS index builder (`TOTKRFS1` layout) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/scripts/prepare_romfs_backend.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/romfs_index.py` | @@ -37,6 +39,7 @@ Code copied or adapted from another project is listed here with its source, the | AMD FidelityFX Super Resolution 1.0 (`ffx_a.h`, `ffx_fsr1.h`, unmodified; licence text in `runtime/shaders/ffx/LICENSE.txt`) | https://github.com/GPUOpen-Effects/FidelityFX-FSR @ `a21ffb8f6c13233ba336352bdff293894c706575`, Copyright (c) 2021 Advanced Micro Devices, Inc. | MIT | `runtime/shaders/ffx/`, `runtime/shaders/fsr_easu.comp`, `runtime/shaders/fsr_rcas.comp` (compiled into `runtime/fsr_easu_spv.h`, `runtime/fsr_rcas_spv.h`) | | AMD Display Library (ADL) declarations: `AdapterInfo`, `ADLSingleSensorData`, `ADLPMLogDataOutput`, the `ADL_PMLOG_*` sensor numbers and the `ADL2_*` function signatures | https://github.com/GPUOpen-LibrariesAndSDKs/display-library @ `84c881cb2136f04287eb3af12de00a9e2867aa9e` (`include/adl_structures.h`, `include/adl_defines.h`, `include/adl_sdk.h`), Copyright (c) 2016 - 2022 Advanced Micro Devices, Inc. | MIT | `runtime/perfmon.c` | | Zstandard 1.5.7 (MSYS2 `mingw-w64-ucrt-x86_64-zstd`), static link | https://github.com/facebook/zstd, Copyright (c) Meta Platforms, Inc. and affiliates | BSD-3-Clause | `runtime/resolution.c` (the HeapDef table), `runtime/ov_zstd.c` (the game's zstd frames decoded on the host) | +| musl `fma.c`, `fmaf.c` and `scalbn.c`, adapted into one file for the x86-64-v2 build | https://github.com/kraj/musl @ `5a56ad13e1e0790c06122978aad2a14d6b9efc1a` (mirror of https://git.musl-libc.org/cgit/musl), Copyright (c) 2005-2020 Rich Felker and contributors | MIT | `runtime/fma.c` (the correctly rounded fma/fmaf the FP lowering needs on CPUs without FMA3) | ## Compiler runtime in a source build @@ -67,7 +70,7 @@ These tools run on the developer's or player's machine during the build. They ar | Tcl/Tk 9.0.4, rebuilt with Xft for the Linux installer | https://github.com/tcltk/tcl and https://github.com/tcltk/tk, tag `core-9-0-4`; archives pinned by SHA-256 in `tools/make_release_linux.py` | Tcl/Tk licence (BSD-style) | `python/lib/`; licence texts in `licenses/tcl-license.terms` and `licenses/tk-license.terms` | | libXft 2 and libXrender 1, the sniper SDK sysroot's shared libraries, bundled beside the Xft Tk of the Linux installer; their Debian `copyright` files go into `licenses/` | https://repo.steampowered.com/steamrt-images-sniper/snapshots/3.0.20261021.266025/ | HPND (MIT-style) | `python/lib/`; `licenses/libXft-copyright.txt`, `licenses/libXrender-copyright.txt` | | CPython 3.14.9 embeddable package, with `tkinter` and Tcl/Tk 9.0.4 taken from the python.org 3.14.9 installer's component MSI (unpacked, never run); includes `vcruntime140*.dll` (Microsoft, redistributable with Python), OpenSSL, libffi, SQLite, zlib, libtommath | https://www.python.org/downloads/release/python-3149/ | PSF-2.0 (Tcl/Tk: Tcl/Tk licence, BSD-style) | `python/` | -| numpy 2.5.4 (with its bundled OpenBLAS), lz4 4.4.5, cryptography 50.0.2 (with its bundled OpenSSL), cffi 2.1.1, pycparser 3.11 | PyPI binary wheels, pins in `tools/requirements-installer.txt` | BSD-3-Clause; lz4 BSD-3-Clause; cryptography Apache-2.0 OR BSD-3-Clause; cffi MIT; pycparser BSD-3-Clause | `python/Lib/site-packages/` | +| numpy 2.5.4 (with its bundled OpenBLAS), lz4 4.4.5, cryptography 50.0.2 (with its bundled OpenSSL), fontTools 4.68.0, cffi 2.1.1, pycparser 3.11 | PyPI binary wheels, pins in `tools/requirements-installer.txt` | BSD-3-Clause; lz4 BSD-3-Clause; cryptography Apache-2.0 OR BSD-3-Clause; fontTools MIT; cffi MIT; pycparser BSD-3-Clause | `python/Lib/site-packages/` | | llvm-mingw 20261020 (LLVM/Clang/LLD 23, mingw-w64, libc++, libunwind, compiler-rt), x86_64 subset | https://github.com/mstorsjo/llvm-mingw/releases/tag/20261020 | Apache-2.0 with LLVM exception; mingw-w64 ZPL-2.1/BSD/public domain; libc++ and libunwind Apache-2.0 with LLVM exception and MIT/UIUC; winpthreads MIT | `llvm/` | | SDL 3.4.18 upstream Windows release (`SDL3.dll`) and mingw development package (headers, `libSDL3.dll.a`) | https://github.com/libsdl-org/SDL/releases/tag/release-3.4.18 | zlib | `bin/SDL3.dll`, `runtime/deps/` | | libopus 1.6.1 (MSYS2 `mingw-w64-ucrt-x86_64-opus` 1.6.1-2; `libopus-0.dll` imports only KERNEL32 and the UCRT api-ms-win-crt DLLs), headers and import library | https://repo.msys2.org/mingw/ucrt64/ | BSD-3-Clause | `bin/libopus-0.dll`, `runtime/deps/` | diff --git a/VERSION b/VERSION index a45be462..0f826853 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.8 +0.3.7 diff --git a/config.example.toml b/config.example.toml index bd34eba8..0140e584 100644 --- a/config.example.toml +++ b/config.example.toml @@ -10,6 +10,7 @@ updates = ['C:\Games\TotK\update-1.4.3.nsp'] keys = 'C:\Switch\prod.keys' # Optional: a firmware NCA directory (an unpacked firmware, or an emulator's installed registered/ directory). # tools/extract_fonts.py takes the system shared fonts from it; the game needs them only with Chinese or Korean text. +# The installer asks for the same folder (--firmware) when the language needs those fonts. # firmware = 'C:\Switch\firmware' [work] diff --git a/docs/architecture.md b/docs/architecture.md index 12f75987..49a977aa 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -54,7 +54,9 @@ MacNativeGames cannot run on x86-64 as is: its CPU path executes ARM64 code nati libzstd on the host (`ov_zstd.c`, `TOTK_ZSTD_NATIVE`); the lifted decoder took almost three times as long. - Settings and menu: every key with its default is in one table (`cfg.c`), read from `totk.ini` and `TOTK_*` variables and written back when the in-game menu changes it. The menu and the performance overlay are Dear ImGui, - drawn into the swapchain image (`runtime/ui/`); their text is in `ui_text.c` (English, Russian). + drawn into the swapchain image (`runtime/ui/`); their text is in `ui_text.c` (English, Russian, Brazilian Portuguese, + Simplified Chinese). The Chinese glyphs come from a system font (Microsoft YaHei, `plat_font_path`) merged into the + font atlas. - OS calls the host makes go through `runtime/platform/` (`platform.h`, `win32.c`), so a Linux or macOS host adds one file instead of touching the rest. The split is not finished: `tools/check_platform.py` lists the Win32 uses left in the other files. #### Threads and cores @@ -74,15 +76,16 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app - Spin waits. Loops that only another thread can end (857 in 1.4.3, 864 in 1.2.1, found by `tools/spin_waits.py`, the nxnative search) send their back-edges through `rt_spin_gate`, which pauses the core once per pass and hands it to a waiter after 50 us of polling (`TOTK_SPIN`, `TOTK_SPIN_HANDOFF_US`). -- `TOTK_FREE_CORES=1` turns the gate off, for experiments. +- `free_cores = 1` (`TOTK_FREE_CORES=1`) turns the gate off, for experiments only: the game crashes or hangs, usually + at start, because its job workers all report core 0 and share per-core memory (#212). ### 4. Graphics - NVN front end from MacNativeGames (`nvn_api_*.c`, `nvn_obj.h`, texture layout and deswizzle). The game records command buffers through it; nothing reaches the GPU until the game submits. - Vulkan 1.3 back end in place of MacNativeGames' Metal one. At `QueueSubmitCommands` the recorded NVN commands are replayed into Vulkan command buffers (`vk_exec*.c`). - Memory pools. Apple's unified memory let MacNativeGames use the game's pools zero-copy. The back end imports the pools as host memory (`VK_EXT_external_memory_host`). On a discrete GPU, pools bound as buffers are mirrored in device-local memory and the pages the CPU wrote (write watch, above) are uploaded before a replay; idle pool regions are made read-only so the write-watch queries skip them (`vk_memory.c`). GPU-rendered textures are written back when the CPU reads them: on a pool invalidate, on a CPU copy, and for small targets once the CPU waits for a fence (`vk_texture.c`). -- Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default because no timing has shown a gain yet. -- Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. `TOTK_VK_RECORDER=0` makes every call on the submitting thread. +- Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is on by default: where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in the Depths, issue #88), elsewhere it is neutral; it adds up to a frame of latency, and `replay_async = 0` turns it off. +- Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. It is off by default on AMD GPUs, where the driver loses the device or hangs the GPU at startup with it (`TOTK_VK_RECORDER=1` forces it on, `=0` makes every call on the submitting thread). - Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame that reads them, while the replay still records it. At the start of a submission's replay every uniform range it binds from CPU-written pools is copied, and the draws bind the copy (`vk_usnap.c`, after MacNativeGames' @@ -96,12 +99,32 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app RCAS) on the present. An `output_res` that is not 16:9 (more than 1% off, `res_is_16x9`) also switches on the `aspect` patch set, which hands the output's ratio to the camera, and `romfs.c` `overlay_init` loads the `romfs-patches-x` overlay for it when it exists (`tools/make_romfs_patches.py --aspect`, laid out by - `tools/ultrawide_layout.py`; the installer builds ten sizes: 21:9 2560x1080, 3440x1440, 3840x1600, 5120x2160, 32:9 - 3840x1080, 5120x1440 and 16:10 1280x800, 1680x1050, 1920x1200, 2560x1600). A wider layout moves the HUD to the + `tools/ultrawide_layout.py`; the installer builds fifteen sizes: 21:9 2560x1080, 3440x1440, 3840x1600, 5120x2160, + 32:9 3840x1080, 5120x1440, 16:10 1280x800, 1680x1050, 1920x1200, 2560x1600 and 3:2 1920x1280, 2160x1440, 2256x1504, + 2880x1920, 3000x2000). A wider layout moves the HUD to the screen's corners, a narrower one to its top and bottom edges. Other sizes get the camera of their shape and the 16:9 HUD stretched over it. The `aspect` setting (`fit`, the default; `stretch`) decides what the present does when the window's shape differs from the output's: `fit` letterboxes, where a 16:9 game in a wider window used to be stretched. +- HDR (`hdr = 1`, `vk_hdr.c`). The game tonemaps its scene into an r11g11b10f target, upscales it and draws the HUD + into another float target, and a last draw copies that into the 8-bit sRGB window texture it presents. With HDR on, + the tonemap's fragment shader (recognised by its constants and resource names, not a hash) runs as + `shaders/hdr_tonemap.frag`, which keeps highlights up to the display's peak; the present reads the float target the + window texture was copied from (any other last draw into it, such as a menu or a movie, leaves the window texture, + shown at the HUD's white); a compute pass turns the picture into PQ code values in Rec. 2020 for an HDR10 + swapchain, which FSR 1 or the blit then scale; the in-game menu is drawn into an RGBA8 image and blended on at the + HUD's white. Without an HDR10 surface format (Windows with "Use HDR" off, a compositor without colour management) + the swapchain stays SDR. `TOTK_SHOTS` reads the window texture, so its pictures clip highlights in HDR. +- DLAA (`vk_dlaa.c`, `dlaa`, off by default). NVIDIA DLSS at the scene's own size, through the driver's NGX core loaded + at run time (no SDK code compiled in). It runs on the R11G11B10F light target right before the bloom chain's first + pass, so post effects and the HUD read the anti-aliased picture. The scene's draws get a sub-pixel Halton viewport + jitter; camera motion vectors are reprojected from the scene depth through this and last frame's view-projection of + the scene camera block (constant bank c4), in double precision on the CPU. Per-object motion (`dlaa_objmv`, + `vk_mvprev.c`, the per-object section of `vk_exec_draw.c`, translator patch 0015): solid scene draws whose position + reads an object's own uniform blocks (bones c5, place c7, c6) are drawn a second time with a vertex shader variant + that computes last frame's position from last frame's blocks (the game writes them into two copies that take turns; + the uniform snapshot keeps both) and writes the difference into the motion image. `TOTK_MV_CHECK` measures the + vectors against the picture, `TOTK_DLAA_TIMES` times the passes on the GPU. `fxaa` switches the game's own FXAA pass. - Occlusion queries never block the CPU; a result that is not ready counts as visible. - ASTC textures are uploaded as they are where the device reports `textureCompressionASTC_LDR`, and decoded by a compute shader everywhere else (`vk_astc.c`, `TOTK_ASTC`). The CPU decoder (`tex_astc*.c`) is the fallback and the test oracle. - Draw state. A draw whose program, bound state and textures repeat reuses the previous draw's shaders and skips rebuilding its descriptor sets; descriptor sets are written through update templates (`vk_exec_draw.c`, `vk_pipeline.c`). diff --git a/docs/building.md b/docs/building.md index f457b81a..75585d71 100644 --- a/docs/building.md +++ b/docs/building.md @@ -1,7 +1,9 @@ # Building from source -Windows 10 or later, an x86-64 CPU with AVX2, and a GPU with Vulkan 1.3 and `VK_EXT_external_memory_host`. Allow about -60 GB for the work folder. Extraction wants about 55 GB free while it runs and keeps about 35 GB afterwards. +Windows 10 or later, an x86-64 CPU with SSE4.2, and a GPU with Vulkan 1.3 and `VK_EXT_external_memory_host`. The +default build targets x86-64-v3 (AVX2/FMA); a CPU without them builds with `--cpu v2` instead and runs slower (see +"Build"). Allow about 60 GB for the work folder. Extraction wants about 55 GB free while it runs and keeps about 35 GB +afterwards. Players who only want to play can use the [installer](installer.md) instead: it builds the game on their machine without this toolchain. @@ -52,7 +54,7 @@ hactool reads `-k `; MSYS2 shells set `HOME`, which hides its default ## Build From the repository root, in PowerShell. Every step that depends on the game version takes it as ``: `v143` for -1.4.3, `v121` for 1.2.1 or `v111` for 1.1.1. The tools default to `v121` when `--version` is left out. Saves load on any +1.4.3, `v121` for 1.2.1 or `v110` for 1.1.0. The tools default to `v121` when `--version` is left out. Saves load on any of them. 1. Python environments (once). `.venv` runs the lifter and the build; `.venv-py314` runs the extraction scripts, which @@ -69,13 +71,15 @@ of them. keys file. `config.local.toml` is ignored by git. The tools read the keys file themselves. 3. Extract and verify. This writes `build\extracted\v0` (1.0.0) and `build\extracted\` for each update in - `updates`, identified by its hash; a hash mismatch stops the run: + `updates`, identified by its hash; a hash mismatch stops the run. With another `[work] dir` in `config.local.toml`, + `extracted` goes there instead, and `build_game.py` reads the ExeFS and RomFS from there: ```powershell .venv-py314\Scripts\python tools\extract.py ``` -4. Shader translator (once; no game files). This publishes `build\xlate\totk_xlate.exe`: +4. Shader translator (no game files; again after a change to its sources, and `tools\make_release.py` stops on an older + one). This publishes `build\xlate\totk_xlate.exe`: ```powershell .venv\Scripts\python tools\build_xlate.py @@ -90,6 +94,14 @@ of them. .venv\Scripts\python tools\build_game.py --version ``` + `--cpu v2` targets x86-64-v2 (SSE4.2, no AVX2/FMA) for older CPUs; the default `v3` needs AVX2. The v2 build links + `runtime/fma.c`, a correctly rounded software fma the FP lowering needs, and runs slower. Inline assembly, + target attributes and intrinsics are not bound by `-march`, so a v2 build runs `tools/check_isa.py` over the runtime + objects before it links: any AVX, BMI or other instruction above x86-64-v2 outside a function listed there as + dispatched by a runtime CPU check stops the build. + + `gen.py` sizes its emit pool from the CPU count and the free memory; `--jobs N` sets it, and so does the environment variable `TOTK_LIFT_JOBS=N`, which the installer's lift step honours too (it has no `--jobs`). When a worker runs out of memory, the pool restarts with half the workers (`emit: out of memory with N workers, retrying with M`), down to one; only an out-of-memory error with one worker fails the step. + Lifting takes about 15 s, a clean compile about 200 s on 24 threads; later builds recompile only what changed. A compiler cache in front of clang shortens it further: with `SCCACHE` set to an sccache executable, `build_game.py` writes it into the ninja file ahead of `cc`, and every lift rewrites the generated chunks with the same contents, so @@ -101,9 +113,9 @@ of them. ``` `--patches` builds in the frame-rate, resolution and other patch sets, switchable at run time. The link copies `SDL3.dll`, `libiconv-2.dll` and `libopus-0.dll` next to the exe. - For Xbox button prompts, run `make_romfs_patches.py` once more with `--xbox-prompts`: it writes - `build\\romfs-patches-xbox` ([controls.md](controls.md), "Xbox button prompts"); `build_game.py` copies it next - to `totk.exe` like `romfs-patches`. + For Xbox button prompts, run `make_romfs_patches.py` twice more, with `--prompts xbox` and `--prompts xbox-label`: + they write `build\\romfs-patches-xbox` and `romfs-patches-xbox-label` ([controls.md](controls.md), "Xbox + button prompts"), which `build_game.py` copies next to `totk.exe` like `romfs-patches`. 6. Prepare the shaders (once per build folder and translator). It needs `refs\MacNativeGames` (`tools\fetch_refs.sh`) for the shared shader list: @@ -115,6 +127,11 @@ of them. Without this step the first launch compiles shaders and pipelines during play and stutters until the caches fill. The AMD driver keeps its pipeline cache per exe path and holds a limited amount: preparing another build folder can evict this one's. `totk.exe --warm-shaders` rebuilds it from the shader cache. + When the driver's shader compiler crashes on a variant (NVIDIA 615.78.08 on an RTX 5080 does, issue #30), + `prepare_shaders.py` runs the driver step again with `--warm-shaders`, up to three times, without the variants the + driver was building at the crash. `shader-prepare-skip.txt` next to the exe lists them with the GPU and driver + version; they build at their first draw instead, another driver version builds them again, and so does deleting the + file. 7. Run. The game keeps running when its window loses the focus (`assume_focus = 1` in `totk.ini`, the default; the Game tab switches it). With 0 it pauses, as it does on the Switch when Home is pressed. @@ -183,6 +200,10 @@ Before a commit: `git config core.hooksPath .githooks` runs it before each local commit. - `build\\totk.exe --selftest`: layout, dispatch table and host-function registry, without game files. +A change to the button prompts (`tools/make_romfs_patches.py --prompts`, `tools/button_icons.py`) passes +`.venv-py314\Scripts\python tests\romfs_prompts_test.py`, which runs the tool on a RomFS it makes itself (no game +files); a change to the settings passes `totk.exe --probe-cfg`. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: @@ -196,6 +217,9 @@ change passes these, all run with `.venv`: | `tests\excl_mt.py` | the exclusive monitor helpers under contention and ABA, on several threads | no | | `tests\jumptables_check.py` | `recomp/jumptables.py` against a Unicorn oracle; must report 0 disagreements, on 1.4.3 too | yes | +A `--cpu v2` build passes the same differential tests with `--cflags=-march=x86-64-v2`; they compile `runtime/fma.c` +too, so they check the software fma the v2 build links rather than the C runtime's. + Runtime probes, each a mode of `totk.exe` (the list is at the top of `runtime/main.c`): `--probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex` and `--probe-shader` cover most runtime changes. `--probe-shader` and the draw part of `--probe-vkexec` translate shaders of the census below: they look for `build//shaders/jobs.json` of the diff --git a/docs/cheats.md b/docs/cheats.md new file mode 100644 index 00000000..93fd61ef --- /dev/null +++ b/docs/cheats.md @@ -0,0 +1,74 @@ +# Cheats + +The port reads Atmosphère cheat files, the format Switch homebrew and the emulators use. It ships no cheat files: a cheat file goes into a mod in the `mods` folder next to `totk.exe`, like any other mod ([mods.md](mods.md)). Open the menu (F1) and switch the cheats on the Cheats tab. The switches are saved under `[cheats]` in `totk.ini`. + +## Adding a cheat file + +1. Find the cheat file for your update. Cheat sites and collections name it after the first 8 bytes of the game's build id: + + | Update | File | + |---|---| + | 1.2.1 | `9B4E43650501A4D4.txt` | + | 1.4.3 | `277178B7DBA1B6D4.txt` | + + The Cheats tab names the file the installed game reads when no mod holds one, and `logs\totk.log` says `cheats: no enabled mod holds cheats/.txt`. +2. Put it in the `cheats` folder of a mod, a folder of its own under `mods`: + + ``` + mods\ + My cheats\ + cheats\ + 277178B7DBA1B6D4.txt + ``` + + A download laid out for the Switch's SD card or an emulator works as it is: unpack it into a folder of its own under `mods`, and the port finds `atmosphere\contents\0100F2C0115B6000\cheats`, `contents\0100F2C0115B6000\cheats` or `0100F2C0115B6000\cheats` inside it. The file name's case does not matter. +3. Start the game. Cheats that write the game's memory work now. +4. Cheats that change the game's code need the game rebuilt with them. When the game starts and finds such cheats it did not build, it opens the setup's rebuild window, which builds them in (a few minutes; the shaders stay prepared) and starts the game again. Until then they are greyed out with "after the rebuild". When the rebuild fails or you cancel it, the game starts without them and does not try again for the same cheats; "Rebuild the game now" on the Cheats tab tries again. `mods_rebuild = off` under `[mods]` in `totk.ini` turns the automatic rebuild off; running `TotK-Setup.exe` on the same folder does the same job by hand. Saves and settings are kept. +5. Switch the cheats on the Cheats tab of the menu. + +The Cheats tab lists the cheats grouped by mod, in the order of the Mods tab. A mod switched off on the Mods tab, or one the port cannot use, contributes no cheats; the change applies at the next start. The Mods tab says how many cheats each mod brought. + +## The file format + +Standard Atmosphère: a `[name]` line starts a cheat, `{name}` a master cheat that is always on, and the cheat's opcodes follow as 8-digit hex words. A name with no opcodes is shown as a heading. The file's title line (the one with `TID=`) is not shown. + +``` +[The Legend of Zelda: Tears of the Kingdom v1.4.3 TID=0100F2C0115B6000 BID=277178B7DBA1B6D4] + +[Player] +[A code cheat] +040A0000 XXXXXXXX YYYYYYYY + +[A data cheat (Hold ZL+X)] +80000104 +580F0000 XXXXXXXX +640F0000 00000000 YYYYYYYY +20000000 +``` + +The words above are placeholders: `XXXXXXXX` an offset, `YYYYYYYY` a value. + +Each cheat's switch is `cheat_ = 1`, where the hash is FNV-1a 32 of `/`. A name repeated in one file gets `#2`, `#3` and so on. The build names each code cheat the same way, so renaming a mod's folder resets its cheats' switches to off and makes the next start rebuild its code cheats. Rename the folder before you switch its cheats on. + +## How cheats run + +The game's code is compiled ahead of time, so a cheat cannot write new instructions into it while the game runs. The port sorts each cheat by what it writes (`tools/cheats.py` at setup, `runtime/cheats.c` in game): + +| Kind | What the cheat does | How the port runs it | +|---|---|---| +| code | Opcode 0 writes into main's code | Built into the game at setup as a patch set (`recomp/patches.py`); switches at once. | +| key | Code writes inside a button condition (`8` mask, writes, `21` else, the original words, `20`) | A patch set that is on while the cheat is on and the buttons are held. | +| data | Writes data only: heap, main's data, pointer chains, button conditions | Runs in a cheat VM after dmnt's, opcodes 0 to A, every presented frame while on. | +| master | `{name}` with code writes | A patch set that is always on. | +| restore | Writes back only the game's original instructions | Not needed: switching a cheat off restores them. Greyed out. | + +A cheat whose code writes call or jump into a code cave the cheat writes itself (into main's zero padding) gets the cave inlined at the call site. The setup builds the code cheats of every mod in the `mods` folder, switched on or off, so switching a mod on later needs only a restart. A code cheat added or edited after the last build waits for the rebuild; a data cheat runs at the next start. + +Not supported, and shown greyed out with the reason: + +- the 0xC and 0xF opcode extensions; +- opcode 5 with a register offset; +- code writes mixed with other opcodes, except the button pattern above; +- code writes that change the CPU flags differently from the game's instruction, or code caves with branches or pc-relative instructions. + +`build-cache\cheats.status` next to `totk.exe`, written by the setup, records which cheats were built and how; the log line `cheats: ...` sums them up at start. When the code cheats cannot be built, the setup warns and installs the game without them. diff --git a/docs/controls.md b/docs/controls.md index 3cd4082c..12c4d7c0 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -7,9 +7,8 @@ Input comes from the first SDL3 gamepad, the keyboard and the mouse, merged into - `nintendo`: by position, as on a Switch (the table below). - `xbox`: south A, east B, west X, north Y, as an Xbox pad labels them; on a PlayStation pad cross is A, circle B, square X, triangle Y. - `auto` (default): `nintendo` for a pad whose east button SDL reports labelled A (Switch Pro Controller, Nintendo-layout pads), `xbox` for the others. -- `xbox_prompts`: the buttons as `nintendo`, and the game's prompts name the Xbox labels (see Xbox button prompts below). -The log names the layout in effect (`input: gamepad layout ...`); the menu shows it under *Automatic*. With `nintendo`, `xbox` and `auto` the game's button prompts stay Switch letters; `xbox_prompts` changes them. +The log names the layout in effect (`input: gamepad layout ...`); the menu shows it under *Automatic*. Which controller's buttons the game's prompts show is `TOTK_PROMPTS` (see Xbox button prompts below). 0.1.9's `pad_layout = xbox_prompts` is read as `pad_layout = nintendo` with `prompts = xbox`, and the menu writes it that way. | Switch | Gamepad (by position, Nintendo layout) | Keyboard and mouse | Action in the world | |---|---|---|---| @@ -33,16 +32,29 @@ Esc is unbound in the game; it opens the settings menu and releases the mouse cu ## Xbox button prompts -`pad_layout = xbox_prompts` (*Xbox prompts (A right)* on the Controls tab) is for an Xbox pad played by position. The buttons work as with `nintendo`: the right button, labelled B, does Switch A (interact, confirm), and the bottom one, labelled A, does Switch B (run, cancel). The letters the game shows are swapped to match the labels, A with B and X with Y, so each prompt names the button to press. The other layouts leave the prompts alone: with `xbox` the Switch letters already match the labels. +`TOTK_PROMPTS` (`prompts`, the *Button prompts* row on the Controls tab, read at start) picks the controller whose buttons the game's prompts show: `nintendo` (default) keeps the game's own, `xbox` shows an Xbox controller's. -The swapped letters are part of the game's textures, fonts and messages, so they follow the layout and not the bindings. A button remapped with a `bind_` line or on the Controls tab keeps the letter the game draws for it, and the prompt then names the wrong button. Use `xbox_prompts` with the default bindings. +- L, R, ZL, ZR, Plus and Minus become LB, RB, LT, RT, Menu and View, and the d-pad prompts a cross with an arrow for each direction meant. The letters on the keys are cut out in the game's own text font. +- A, B, X and Y name the label of the button the pad layout presses, so each prompt names the button to press. `pad_layout = nintendo` plays an Xbox pad by position: the right button, labelled B, does Switch A (interact, confirm), and the bottom one, labelled A, does Switch B (run, cancel), so the prompts swap A with B and X with Y. `xbox` and `auto` put Switch A on the button labelled A, and the letters stay. -The buttons change at once, but the game reads its UI archive and fonts once at boot, so the prompts change at the next start (the menu says *prompts change after restart*). `xbox_prompts` loads `romfs-patches-xbox` next to `romfs-index.bin` in place of `romfs-patches` (`runtime/romfs.c`, `overlay_init`): the same 60 fps patches, with the swap made from the game's own files by `tools/make_romfs_patches.py --xbox-prompts`, which the setup runs next to the plain one: +| Switch | `nintendo` layout | `xbox` or `auto` layout | +|---|---|---| +| A, B, X, Y | B, A, Y, X | A, B, X, Y | +| L, R | LB, RB | LB, RB | +| ZL, ZR | LT, RT | LT, RT | +| Plus, Minus | Menu, View | Menu, View | +| D-pad | cross | cross | -- the action prompt (`DoCommand_00`) draws the letter images `Nt_KeyTexA/B/X/Y_00`, whose image data is swapped, in a diamond that names the other face buttons. The diamond's letters are text the game takes from `LayoutMsg/DoCommand_00.msbt` of each language's `Mals` archive (and the layout's own text panes behind it); both are swapped. The diamond's shape shows the button's position and stays. -- every other prompt is text in the button font (`scft/External.bfotf` in each `Font/Font*.Nin_NX_NVN.bfarc.zs`, glyphs U+E040 to U+E043); the A and B, X and Y glyphs are swapped. +The action prompt (`DoCommand_00`) shows its button in a diamond whose shape gives the face buttons' positions as on a Switch, with the other three letters around it. With `nintendo` its letters are swapped with the rest and every one names the button in that place. With `xbox` and `auto` the letters are right but the diamond keeps the Switch positions: A shows on its right, while the button labelled A is at the bottom. The sticks keep the Switch icons, and so do prompts drawn as fixed images (some tutorial animations). -Without the folder (an install made by an older setup, or one where the setup could not prepare it and showed a warning) the menu says the files are missing, the log has `RomFS overlay: pad_layout = xbox_prompts, but ...`, and the Switch prompts stay; running the setup again on the install folder writes it. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version --xbox-prompts` writes `build\\romfs-patches-xbox`. +The prompts are part of the game's fonts, textures and messages, so they follow the layout and not the bindings. A button remapped with a `bind_` line or on the Controls tab keeps the prompt the game draws for it, which then names the wrong button. Use the Xbox prompts with the default bindings. + +The game reads its UI archive and fonts once at boot, so the prompts change at the next start, also after `pad_layout` changes between `nintendo` and the others (the menu says *prompts change after restart*); the buttons change at once. Under that note the menu offers *Restart now*: after a confirmation it writes `totk.ini`, closes the game as *Quit game* does and starts the game again with the same command line, which waits until the old process is gone before it opens the log and the saves (`runtime/gfx_present.c`, `gfx_window_closed`; `plat_restart` and `plat_restart_wait` in `runtime/platform/win32.c` and `linux.c`). Progress since the last save is lost, as with *Quit game*; while the game writes a save, the confirmation waits. With `prompts = xbox` the game loads `romfs-patches-xbox` (`pad_layout = nintendo`) or `romfs-patches-xbox-label` (`xbox`, `auto`) next to `romfs-index.bin` in place of `romfs-patches` (`runtime/romfs.c`, `overlay_init`; `runtime/input_map.c`, `input_prompts_folder`): the same 60 fps patches, with the prompts drawn from the game's own files by `tools/make_romfs_patches.py --prompts xbox` or `--prompts xbox-label` (`tools/button_icons.py`, after MacNativeGames' `make_button_icons.py`), which the setup runs next to the plain one: + +- every prompt but the button guide in play is text in the button font, `scft/External.bfotf` in each `Font/Font*.Nin_NX_NVN.bfarc.zs`: glyphs U+E040 to U+E043 (A B X Y), U+E044 to U+E047 (L R ZL ZR), U+E04B and U+E04C (Plus, Minus), U+E049 and U+E060 to U+E065 (the d-pad). fontTools writes the font again, so the archives grow and the folder also carries the resource size table (`System/Resource/ResourceSizeTable.Product....rsizetable.zs`) with their entries raised by as much; without that the game stops at start. +- the button guide in play draws L and ZL from the images `Nt_KeyTexL_00` and `Nt_KeyTexZL_00` of the Common archive's `timg/__Combined.bntx`, painted with the same icons. The action prompt draws its button from `Nt_KeyTexA/B/X/Y_00`, whose image data is swapped for `nintendo`, and the diamond's letters are text the game takes from `LayoutMsg/DoCommand_00.msbt` of each language's `Mals` archive (and the layout's own text panes behind it), swapped the same way. + +The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay: ...` line in the log, when the folder is missing (an install made by an older setup, or one where the setup could not prepare it and showed a warning; running the setup again on the install folder writes it), when it fails its check, with a game resolution of another shape than 16:9 (its own UI layout wins; 16:10 and 21:9), and when the `romfs_mods` folder replaces one of its files: `romfs-patches` then loads in its place, where two overlays replacing one file would stop the game. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version --prompts xbox` (and `--prompts xbox-label`) writes `build\\romfs-patches-xbox` (`-label`); it needs fontTools from `tools/requirements-py314.txt`. `--xbox-prompts` is the older name of `--prompts xbox`. ## Window keys @@ -82,7 +94,7 @@ Mouse motion (raw input) becomes right-stick deflection. Each presented frame ta `TOTK_MOUSE_SENS` (0 to 10, default 1; higher turns faster, 0 turns the mouse camera and the cursor capture off). While the window is the foreground window the cursor is hidden and confined to the client area. It is released on focus loss, minimise, while F11 changes the window, during a size or move loop and while a text entry is open. Esc releases it by hand: the cursor shows, the mouse camera and mouse buttons stop, and the window can be moved, minimised or closed. A mouse click inside the client area captures it again; that click does not reach the game. Activating the window by a click on its title bar or border does not capture, so it can be dragged or closed right away; Alt+Tab or a click in the client area captures as before. `TOTK_INPUT=none` disables the capture too. -`TOTK_STICK_DEADZONE` (0 to 50, default 8) is the gamepad stick dead zone in percent of full deflection, the same for both sticks. It is round: a stick inside the circle reads centred, and the rest of its travel is scaled to start at 0 at the edge, so full deflection still reaches full. Raise it for a stick that drifts. The game's own dead zone applies after it. +`TOTK_STICK_DEADZONE` (0 to 50, default 8) is the gamepad stick dead zone in percent of full deflection, the same for both sticks. It is round: a stick inside the circle reads centred, and the rest of its travel is scaled to start at 0 at the edge, so full deflection still reaches full. Raise it for a stick that drifts. The game's own dead zone applies after it. `TOTK_LSTICK_DEADZONE` and `TOTK_RSTICK_DEADZONE` (the *Left/Right stick dead zone* rows under *Analog sticks* on the Controls tab) set one stick each and fall back to `TOTK_STICK_DEADZONE` while unset, so a worn stick can get a larger zone than the other. With a gamepad connected each row shows its stick's deflection now, before the dead zone (*stick now*): let go of the stick and set the row a few percent above the value it rests at. The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sticks at 50% deflection. A gamepad plugged in later is picked up within one second; with none connected the log says `input: no gamepad`. diff --git a/docs/installer.md b/docs/installer.md index 304e239c..942cb282 100644 --- a/docs/installer.md +++ b/docs/installer.md @@ -9,20 +9,26 @@ holds only the game; the installer and its tools stay outside it. 1. Downloads `TotK-Setup--windows.zip` from GitHub Releases (none is published yet), unpacks it anywhere and starts `TotK-Setup.exe`. 2. Picks: - - the base game (XCI or NSP) and the 1.2.1 or 1.4.3 update (NSP); + - the base game (XCI or NSP) and the 1.1.0, 1.2.1 or 1.4.3 update (NSP); an NSZ or XCZ (compressed by the nsz + tool) works as it is, the setup unpacks it itself; - their `prod.keys`; + - optionally the firmware folder (`system/Contents/registered` of an unpacked firmware or an emulator's install), + needed for Chinese, Korean and Traditional Chinese text; - the game language and the install folder. 3. Waits for the setup, one time. The window shows each step and its progress. -4. Plays `totk.exe` from the install folder. The installer and the source NSP or XCI can be deleted. +4. Plays `totk.exe` from the install folder. The installer and the source NSP or XCI (or NSZ, XCZ) can be deleted. The install folder (`main.c` `portable_paths()` reads this layout; it stays portable): ``` / totk.exe, SDL3.dll, libopus-0.dll, totk_xlate.exe + nvngx_dlss.dll NVIDIA DLSS, downloaded by the setup's DLSS step when the player keeps it ticked totk.ini language; the other keys as commented defaults (totk.ini.example) exefs/, romfs/ extracted from the player's files + shared-fonts/ the Switch system fonts, with Chinese, Korean or Traditional Chinese text profile/ saves + mods/ the player's mods, one folder per mod (created by the menu's "Open mods folder") logs/ totk.log of the last run, totk.1.log to totk.4.log of the runs before it shader and pipeline caches prepared by the setup; shader-cache/shader-cache.pack holds the translations totk-install.json what built this folder: release version, input hashes, finished steps @@ -31,43 +37,80 @@ The install folder (`main.c` `portable_paths()` reads this layout; it stays port ## Updating the port The player runs a newer installer on the same folder. It reads `totk-install.json`, keeps `romfs/`, `exefs/`, -`profile/` and `totk.ini` after checking their hashes, rebuilds `totk.exe` and re-prepares the shaders. A newer -release that changes nothing game-derived skips the rebuild. +`shared-fonts/`, `profile/` and `totk.ini` after checking their hashes, rebuilds `totk.exe` and re-prepares the +shaders. A newer release that changes nothing game-derived skips the rebuild. -Given an update file other than the recorded one (1.2.1, then 1.4.3), the installer extracts and builds again from it and -keeps `profile/`. Saves carry over both ways: 1.4.3 converts a 1.2.1 save on load, and 1.2.1 loads the saves 1.4.3 wrote. +Given an update file other than the recorded one (1.1.0, then 1.2.1, then 1.4.3), the installer extracts and builds again from it +and keeps `profile/`. Saves carry over both ways between 1.2.1 and 1.4.3: 1.4.3 converts a 1.2.1 save on load, and 1.2.1 +loads the saves 1.4.3 wrote. ## Setup steps -1. **Check** before writing anything: the files open with the keys, the update is 1.2.1 or 1.4.3 (by hash, as +1. **Check** before writing anything: an NSZ or XCZ input is unpacked first (below), the files open with the keys, the update is 1.1.0, 1.2.1 or 1.4.3 (by hash, as `tools/extract.py`), the keys are new enough for the update, enough space is free on the target disk (about 36 GiB at the peak of a - first install, 20 GiB after it), the CPU has - AVX2 (`-march=x86-64-v3`), a Vulkan 1.3 GPU exists. Each failure is one clear message. -2. **Extract** ExeFS and RomFS into the install folder and verify the hashes. -3. **Lift** the ExeFS to C (`recomp/gen.py`, `recomp/patches-v121.txt` or `patches-v143.txt`) into a temp folder next - to the installer. -4. **Compile** the generated C, `modules.c`, `romfs.c`, `patches.c` and `ov_zstd.c` (they bake the per-install layout, RomFS + first install, 20 GiB after it), the CPU is at least x86-64-v2 (SSE4.2; with AVX2/FMA the faster `v3` build is used, + otherwise the `v2` one), a Vulkan 1.3 GPU exists. Each failure is one clear message. +2. **Fonts** with Chinese, Korean or Traditional Chinese text and a firmware folder: extract the Switch system fonts + from the firmware into `shared-fonts/` (MacNativeGames' `extract_shared_fonts.py`, which verifies every NCA layer + and BFTTF header). Without a firmware folder the setup warns and goes on; the game then lacks those glyphs. +3. **Extract** ExeFS and RomFS into the install folder and verify the hashes. +4. **Lift** the ExeFS to C (`recomp/gen.py`, `recomp/patches-v121.txt` or `patches-v143.txt`) into a temp folder next + to the installer. The code cheats of the mods' cheat files and the mods' ExeFS patch files become patch sets first + (`tools/cheats.py`, `tools/exefs_patches.py`, status in `build-cache/cheats.status` and `exefs.status`); a file the + tools refuse costs those cheats or patches, not the install. +5. **Compile** the generated C, `modules.c`, `romfs.c`, `patches.c` and `ov_zstd.c` (they bake the per-install layout, RomFS hashes and the game version's patch page) and link - them with the prebuilt runtime library into `totk.exe` (`build_game.py --runtime-lib`; about 3 min on 24 cores). -5. **Prepare** the RomFS patches and the shaders (`tools/make_romfs_patches.py`, `tools/prepare_shaders.py`, + them with the prebuilt runtime library for the host's level (`libtotkrt.a` or `libtotkrt-v2.a`) into `totk.exe` + (`build_game.py --runtime-lib`; about 3 min on 24 cores). +6. **Prepare** (only when the runtime, the translator or the game version changed, not for a new lift) the RomFS + patches and the shaders (`tools/make_romfs_patches.py`, `tools/prepare_shaders.py`, `totk.exe --prepare-shaders`) in the install folder, then fold the shader cache's loose files (four per shader, about 117,000) into `shader-cache/shader-cache.pack` (`tools/pack_shader_cache.py`). totk.exe reads the pack first and writes the translations it makes in play as loose files; running the tool again with the game closed folds those in. - The patches are written twice: `romfs-patches` and `romfs-patches-xbox` (`--xbox-prompts`, the A/B and X/Y prompts - swapped for `pad_layout = xbox_prompts`). -6. **Clean up** the temp folder (generated C and objects, about 1.3 GB). + The patches are written three times: `romfs-patches`, and `romfs-patches-xbox` and `romfs-patches-xbox-label` + (`--prompts`: Xbox button prompts for `prompts = xbox`, the first for `pad_layout = nintendo`, the second for `xbox` + and `auto`). The two Xbox folders are optional: when one cannot be made, the setup warns and finishes. + When the graphics driver crashes while building the shaders, `prepare_shaders.py` runs that part again without the + variants it was building ([building.md](building.md), step 6), and the setup finishes with a warning that names how + many were left out. +7. **DLSS** (the window's tick box, on by default, or `--dlss`; Windows with an NVIDIA RTX GPU): download + `nvngx_dlss.dll` from NVIDIA's own DLSS SDK repository (github.com/NVIDIA/DLSS, a pinned tag, NVIDIA RTX SDK + license) into the install folder and check its size and SHA-256 (`steps.py`, `Dlss`). The release does not ship + the library; NGX loads it from the folder of totk.exe for `dlaa` and the DLSS modes. A DLSS library already in the + folder (a newer one the player put there) is kept. Without an RTX GPU the step only logs and does nothing; a failed + download warns, the game runs without DLSS, and the next run of the setup tries again. +8. **Clean up** the temp folder, except the generated C and the objects (about 1.3 GB), and write + `build-cache/rebuild.txt` in the install folder. A cancelled or failed setup resumes from the last finished step (`totk-install.json`). +## Rebuilding for the mods + +The generated C and the objects stay in the temp folder, so the next lift rewrites only the chunks whose C changed and +Ninja recompiles only those: a new or edited code cheat costs the lift (under a minute on 24 cores, a few minutes on 4) +and a few dozen chunks, not a full compile, and the shaders stay prepared. + +`build-cache/rebuild.txt` holds the command that opens the setup window in its rebuild mode on the install folder +(`installer/gui.py --rebuild `, started through `python -c` so the release folder is on `sys.path` +whatever the working folder), one argument per line. When an enabled mod has code cheats or ExeFS patch files the build +lacks, `totk.exe` runs it at start and exits (`runtime/cheats.c` `rebuild_if_needed`); the +window runs the setup on the folder at once and starts the game again when it succeeds. A failed or cancelled rebuild +offers to start the game with the code it has; `build-cache/rebuild.skip` then keeps the same files from starting it +again, until "Rebuild the game now" on the Cheats or Mods tab. `[mods] mods_rebuild = off` turns it off. The rebuild +needs the release folder the setup ran from: a moved or deleted release folder means running a setup again. + ## Command line `installer/` (stdlib only; `installer/layout.py` maps the release folder and, in a repository, the dev toolchain): ``` -python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--root DIR] [--temp DIR] [--verify] +python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--firmware DIR] [--root DIR] [--temp DIR] [--verify] ``` - On a folder with `totk-install.json` only `--dest` is needed; the game files only when the extraction must run again. +- `--firmware DIR` is the folder holding the firmware NCAs: an unpacked firmware, or the emulators' installed + `Contents/registered` (the parent `Contents` folder works too). It is read only with Chinese, Korean or Traditional + Chinese text, and only when `shared-fonts/` is not already in the install folder. - stdout: one JSON event per line, types `start`, `plan`, `step` (started, done, skipped), `progress` (step, phase, done/total/unit), `space` (bytes needed and free on the install disk and the temp disk), `warning`, `error`, `end` (`installer/events.py`). Messages are ids with parameters; `installer/text/en.json` holds their English text. The human-readable log goes to `/logs/`. @@ -79,9 +122,13 @@ python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR the merged RomFS (15.9 GiB) before it deletes them. The check computes the exact figure. - Window (`python -m installer.gui`, `installer/gui.py` draws, `installer/guilogic.py` decides): form, step list with progress, space line, collapsible log, Cancel (Ctrl+Break to the CLI's process group, then it waits for the exit), - finish screen with Open folder. English or Russian, chosen from the Windows display language, switchable in the - window; every sentence is an id in `installer/text/.json`. A folder with `totk-install.json` needs only the - folder. `--selftest [--shots DIR]` builds every screen in both languages and closes itself. + finish screen with Open folder. English, Russian, Brazilian Portuguese or Simplified Chinese, chosen from the Windows + display language (zh-CN and zh-SG give Chinese; the Traditional Chinese regions give English), switchable in the + window; every sentence is an id in `installer/text/.json`. A Chinese window still starts with + English as the game language, because Chinese game text needs the Switch shared fonts, which the setup extracts only + from a firmware folder. A folder with + `totk-install.json` needs only the folder. `--selftest [--shots DIR]` builds every screen in every language and + closes itself. - `tests/installer_gui_test.py`: event model, form, catalogs, cancel, the self-closing window and one end-to-end run of the CLI in `--verify` mode (needs `build/install-test` and `build/extracted/v121`; see its docstring). - `tests/installer_test.py`: bookkeeping with fake steps. `tests/installer_e2e.py`: a dev-mode run with the files of @@ -93,11 +140,12 @@ The unpacked release; `tools/make_release.py` assembles it and `--zip` packs it: ``` TotK-Setup.exe starts python/pythonw.exe -m installer.gui (installer/launcher/launcher.c, no console) -installer/ the installer: steps, CLI, window, en/ru text catalog +installer/ the installer: steps, CLI, window, en/ru/zh-Hans text catalog python/ Python 3.14.9 embeddable + numpy, lz4, cryptography, cffi, pycparser (+ tkinter for the window) recomp/, tools/ the lifter and the build, extraction and shader scripts the steps run (copied from the repo) llvm/ llvm-mingw, x86_64-only subset (Apache-2.0 with LLVM exception; mingw-w64 parts permissive) -runtime/ libtotkrt.a and the headers the generated code and modules.c/romfs.c/patches.c include +runtime/ libtotkrt.a, libtotkrt-v2.a and the headers the generated code and modules.c/romfs.c/patches.c + include bin/ totk_xlate.exe, hactool.exe, SDL3.dll, libopus-0.dll, ninja.exe data/ shader_list.tsv (MacNativeGames, MIT) THIRD_PARTY_NOTICES.md @@ -109,11 +157,23 @@ totk.ini.example, VERSION the default ini; the release string (the repository's - The release contains nothing game-derived; `make_release.py` runs `tools/check_content.py` over the folder before it finishes. +## Switch system fonts + +Simplified Chinese, Traditional Chinese and Korean text needs the Switch system fonts (`0-standard.bfttf` and the other +nn::pl shared fonts). The game does not ship them. Without them it exits before its first frame in those languages +(`rt_fatal` in `runtime/ov_system.c`). Supply them from your own console, in either way: + +- Give the setup the firmware folder (`--firmware DIR`, or the window's firmware field); the Fonts step extracts them to + `shared-fonts/` in the install folder. +- Run `python tools/extract_fonts.py --firmware ` with `.venv-py314` and the keys from `config.local.toml`, or + point `TOTK_SHARED_FONTS` at a folder that holds the extracted fonts. + ## Linux release `tools/make_release_linux.py` builds the same setup for Linux and packs it as `TotK-Setup-x86_64.AppImage` (the static type-2 runtime: no libfuse2 needed on the host). The player makes it executable and starts it; on a Steam Deck, from -Desktop Mode. Building the release also needs `make` and `pkg-config`. The bundled Tcl/Tk 9.0.4 is rebuilt with +Desktop Mode. The command-line setup needs the desktop session's display (`DISPLAY` or `WAYLAND_DISPLAY`) because preparing the +shaders opens a window; the check step fails at once without it. Building the release also needs `make` and `pkg-config`. The bundled Tcl/Tk 9.0.4 is rebuilt with Xft against the sniper sysroot so Russian text uses system TrueType fonts. The setup builds the game into the folder they pick, as on Windows, and the game is `totk` there with `libSDL3.so.0` beside it. The folder inside the AppImage: @@ -125,8 +185,8 @@ python/ python-build-standalone 3.14 (with tkinter) + the installe tools/ and recomp/ on sys.path llvm/ LLVM 23 (clang, lld, llvm-ar) and llvm/sysroot: the files of the Steam Runtime 3 "sniper" SDK the game's compile and link read (about 130 files) -runtime/ libtotkrt.a, headers, the install-side sources, totk.png; deps/ with the Vulkan headers, VMA and a - static zstd +runtime/ libtotkrt.a, libtotkrt-v2.a, headers, the install-side sources, totk.png; deps/ with the Vulkan + headers, VMA and a static zstd bin/ totk_xlate, hactool, ninja, libSDL3.so.0 ``` @@ -161,8 +221,15 @@ bin/ totk_xlate, hactool, ninja, libSDL3.so.0 the file and asks to copy it again (`extract.DamagedFile`). A failed merge checks the base Program NCA alone to tell which file is damaged. - Unsigned exe: SmartScreen warns on first start. +- NSZ and XCZ input (game files compressed by the nsz tool: NCAs stored as NCZ, their sections decrypted and + compressed with zstd): the check step tells them by their content, not their name (an NSP or XCI holding NCZ files), + so the file dialog has one filter for all four. It unpacks each into `/unpacked/.nsp` + (`tools/ncz.py`), an NSP of the NSZ's files or of the XCZ's secure partition, and the extraction reads that. + Unpacking encrypts the sections again, so every NCA comes back byte for byte, and checks each against the hash its + name carries; a mismatch is `check.input_damaged`. It needs the unpacked size free in the temp folder (checked + first), runs at about 500 MB/s, and an interrupted setup reuses a finished NSP of the same file. The extract step + deletes the folder when it is done. An XCZ update is refused like an XCI one. ## Later - Re-warm the driver cache on start after a move, GPU or driver change. -- NSZ and XCZ input. diff --git a/docs/maxwell.md b/docs/maxwell.md index dc71a041..a02c4787 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -68,6 +68,7 @@ Source shorthands: | 2.9 | **LOD**: TSC min/max LOD clamp, LOD bias, `TWO_D_NO_MIPMAP` texture type, a min filter without mips stays on the base level; TIC carries its own max anisotropy and aniso quality fields. | `minLod`/`maxLod`, `mipLodBias` limited by `maxSamplerLodBias`. | yuzu: anisotropy heuristics for mods forcing trilinear (YZ-2306, PR 10744). Ours: non-mip filter → `maxLod = minLod + 0.25`, bias clamped (`vk_texture.c` `make_sampler`). | G80TEX | | 2.10 | **Texture gather rounding**: Maxwell sub-texel precision differs from AMD/Intel; edge texels picked by TLD4 differ by one. | Implementation-defined `subTexelPrecisionBits`. | yuzu adds a sub-pixel offset to gathers on non-NVIDIA hosts: fixed BotW shadow artifacts at object edges ("shader_recompiler: Add subpixel offset for correct rounding at ImageGather", , title only). Ours: the translator biases gather coordinates by the host's `subTexelPrecisionBits` on every non-NVIDIA GPU (`vk_caps.c` → job `gather_bias` → Ryujinx `TexturePass.InsertCoordGatherBias`); TotK on RADV drew dashed dark lines along wall and floor edges in the prologue caves without it. `TOTK_GATHER_BIAS=0` turns it off. | — | | 2.11 | **Gather offsets**: `TLD4.PTP` takes four per-texel offsets, possibly from registers. | `ConstOffsets` must be constant. | Ours: `QueryHostSupportsNonConstantTextureOffset() => false` makes Ryujinx emulate register offsets (`tools/xlate/Program.cs`). | RYU-SH `Translation/Optimizations` (TexturePass) | +| 2.13 | **TXD gradient register order**: TotK's terrain shaders fill the gradient registers of a `TXD` per coordinate (du/dx, du/dy, dv/dx, dv/dy). | Vulkan's `Grad` takes dPdx (du/dx, dv/dx) and dPdy (du/dy, dv/dy). | Ryujinx's `EmitTxd` takes the first two registers as dPdx and the last two as dPdy, so the mip level came from the wrong derivatives and was too sharp on terrain faces turned across the screen: patches that followed the faces crawled when the camera moved (1.4.3, the Depths; found by the user from their F9 frame, a gradient sample's coordinate and gradients traced back to `DPdx` and `DPdy` of the interpolants). Ours: `gfx_shader.c` `patch_txd_transpose` swaps the two inner components of every two-component gradient pair in the loaded SPIR-V (the shader cache is untouched). `TOTK_TXD_TRANSPOSE=0` turns it off. Only 2D gradients are handled; 3D and cube ones are left as translated. | — | | 2.12 | **Depth compare** (`TSC` depth compare + `TIC_4_DEPTH_TEXTURE`), min/max reduction filtering. | `compareEnable`; reduction mode needs `samplerFilterMinmax` and is not allowed with compare. | Ours: reduction dropped when compare is on (`vk_texture.c` `make_sampler`). | G80TEX | | 2.13 | **ASTC / BC**: native ASTC; zero ASTC block = error colour on hosts. | AMD and NVIDIA desktop GPUs lack ASTC: decode to RGBA8. yuzu offered ASTC recompression to BC1/BC3 for low VRAM (YZ-2305, PR 10398). | Ours: native upload where the device samples ASTC, else a compute-shader decode (`vk_astc.c`, `TOTK_ASTC`), CPU decode (`tex_astc.c`) as the fallback. | `runtime/vk_astc.c` | | 2.14 | **Linear CPU-written textures** (`FLAGS_LINEAR`, small ramps, glyph caches): CPU writes visible to the GPU directly. | Must re-upload or use linear images over host memory. | NXN Mario: sky black without its 64×1 RGBA16F ramps. | NXN | @@ -85,6 +86,9 @@ Source shorthands: | 3.7 | **Integer division** does not exist (compiled to `XMAD`/`IMAD` sequences); MUFU.RCP(0) = +Inf, LG2(0) = -Inf. | Same IEEE edge cases through `1/x`, `Log2`. | Low risk. | — | | 3.8 | **FP64** in some TotK cutscene shaders. | No FP64 on some hosts. | yuzu FP64 → FP32 (YZ-2306, PR 10693); ours: `softfloat64` job flag (`tools/xlate/Program.cs` `QueryHostSupportsShaderFloat64`). | — | | 3.9 | **Integer vertex inputs read as raw bits** (bone indices `R8UI`). | Float-declared inputs convert integers. | NXN Mario: stretched characters; re-translated with `uint` inputs. Ours: attribute types passed to `QueryGraphicsState` (`Program.cs`). | NXN | +| 3.10 | **BFE / BFI fields past bit 31** (as PTX `bfe` / `bfi`): a field that runs past bit 31 is cut there; one that starts past it extracts 0 (signed: bit 31 repeated) and inserts nothing. TotK 1.4.3's Depths terrain vertex shaders (42 variants) take the position from a register, `((2y + x) * 8) & 0xff` with 5-bit x and y, so up to 248. | `OpBitField*Extract` / `OpBitFieldInsert` undefined when offset or offset + count exceeds 32; AMD's bitfield instructions take offset and width modulo 32. | Ryujinx passes the fields through. UltraTears saw the Depths walls and ground missing on the Steam Deck and a Linux RX 570 (RADV). Ours: clamped where the field is not a constant inside the word (translator patch 0012). RX 9070 on Windows drew the Depths the same before and after. | PTX ISA `bfe`, `bfi` | +| 3.11 | **SHF by 0** gives the unshifted word (C for `SHF.L`, A for `SHF.R`). | Ryujinx's funnel shifts the other word by 32: undefined in SPIR-V; AMD shifts take the low 5 bits and give A \| C. | yuzu fixed Depths terrain gaps in its funnel shift (PR 10459). Ours: a select on a zero amount (translator patch 0013). In 1.4.3 every register SHF pairs its word with 0, so the result was right already. | — | +| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: every function-local variable starts at 0 (`OpConstantNull`, translator patch 0014). | — | ## 4. Memory model @@ -142,6 +146,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/docs/mods.md b/docs/mods.md new file mode 100644 index 00000000..72f9eca6 --- /dev/null +++ b/docs/mods.md @@ -0,0 +1,107 @@ +# Mods + +TotK Recomp loads the mods players make for the Switch emulators (Ryujinx, yuzu and its forks): texture and model +packs, gameplay and text changes from GameBanana and similar sites, TKMM's merged output, code patches (`.ips`, +`.pchtxt`) and Atmosphère cheat files. No mod or cheat file ships with the port. + +## Install a mod + +1. Open the settings menu (F1, Esc, or Minus and Plus together on a controller), go to the Mods tab and choose + **Open mods folder**. That is the `mods` folder next to `totk.exe`; the menu creates it when it is missing. +2. Make one folder per mod in it and unpack the mod's archive into that folder. The mod's own folders (`romfs`, + `exefs`, `cheats`) must sit directly inside the mod's folder: + + ``` + mods\ + Better Grass\ + romfs\ + ... + Fast Travel Anywhere\ + exefs\ + patch.pchtxt + My cheats\ + cheats\ + 277178B7DBA1B6D4.txt + ``` + + An archive laid out for a Switch SD card or an emulator works as it is. Inside the mod's folder the port also finds + `atmosphere\contents\0100F2C0115B6000\romfs`, `contents\0100F2C0115B6000\romfs` and `0100F2C0115B6000\romfs`, the + same places for `exefs` and `cheats`, and `atmosphere\exefs_patches\\` for code patches. +3. Start the game. A mod that patches the game's code makes the game rebuild itself first (see Code patches below). +4. Open the Mods tab to check the mod. Each row says how many game files the mod replaces and adds, how many cheats it + brings, and why a part of it is not used. + +## Order and switches + +The Mods tab lists every folder in `mods`. Select a mod and press A or Enter to switch it on or off. The top face button +or Ctrl+Up moves it up, the left face button or Ctrl+Down moves it down. When two mods change the same file, the +higher one wins; the lower mod's row says how many of its files the higher one covers. Changes apply after a restart. + +The menu saves the order and the switches in `totk.ini`, under `[mods]`, by folder name: `mods_order` and +`mods_disabled`. A folder the list does not name yet, such as a new or renamed one, goes below the named ones. + +### Mods that change the same files + +Two mods that edit the same game file do not combine: the higher one wins the whole file. Merge such mods with +[TKMM](https://github.com/TKMM-Team/Tkmm) (TotK Mod Manager) and set its export location to an empty folder inside +`mods`, for example `mods\TKMM`. The exported folder then counts as one mod here. Mods packaged for TKMM only +(`.tkcl` files) go the same way. + +The port merges the resource size table on its own: at every start with mods on, it combines the tables the mods ship +and raises the entries for files a mod made larger or added. A separate RESTBL mod is not needed. + +## Code patches (ExeFS) + +A mod with `.ips` or `.pchtxt` files patches the game's code. A patch is for one game update: an `.ips` file is named +after the build id of the update's code, a `.pchtxt` file names it on its `@nsobid` line. The game's code is compiled +ahead of time, so these patches are built into `totk.exe`: + +1. The first start after such a mod is added closes the game and opens the setup's rebuild window. The rebuild takes + a few minutes (the shaders stay prepared), then the game starts again. Saves and settings are kept. +2. Until the rebuild succeeds, the whole mod stays off, its other files included. +3. After the rebuild the mod switches on and off from the Mods tab like any other, without another rebuild. + +The rebuild needs the setup folder the game was installed from. When that folder was moved or deleted, run +`TotK-Setup.exe` again on the same install folder: it does the same job. `mods_rebuild = off` under `[mods]` in +`totk.ini` turns the automatic rebuild off. + +A code patch over one of the port's own patches (the 60 FPS patch, the resolution patch and the other entries under +**Built into the port**) turns that built-in patch off while the mod is on. The mod's row names it. + +## Cheats + +A cheat file is part of a mod: `mods\\cheats\.txt`, switched cheat by cheat on the Cheats tab. Cheats +that change the game's code need the same rebuild as code patches. [cheats.md](cheats.md) explains the file names for +each game update, the rebuild and which cheats cannot run. + +## Renaming a mod folder + +The port keys a mod by its folder name. A renamed folder counts as a new mod: + +- it moves below the mods the list names, and comes back switched on; +- its cheats come back switched off; +- its code patches and code cheats are built again, so the next start runs the rebuild. + +Rename mod folders before you set them up in the menu, not after. + +## What does not work + +| On the Mods tab | Why | What to do | +|---|---|---| +| This folder has no romfs, exefs or cheats files. | The archive was unpacked one level too deep (`mods\Mod\Mod\romfs`), or it holds no game files. | Move the folders up so `romfs` sits directly in the mod's folder. | +| ... files are made for another game version and are left out. | The mod's files carry another update's version in their names (`.Product..`). | Download the mod's release for your game update. | +| Its code patches (exefs) are made for another game version. | The patch files name another update's build id. | Same: get the release for your update. | +| This mod brings its own game code (a subsdk plugin or a replaced main). | Mods such as UltraCam ship compiled Switch code. The port cannot load it. | No workaround; the whole mod stays off. | +| This mod patches the game code (exefs) in a way the port cannot apply. | The patch writes the game's data, not its code, or changes code in a form the port cannot build. | No workaround; the reason follows on the row. | +| The folder name contains \|. | The mod list uses `\|` between names. | Rename the folder. | + +A mod never stops the game from starting. Whatever the port cannot use is left out and shown on the Mods tab. + +## When something looks wrong + +1. Switch the newest mod off on the Mods tab and restart. If the problem goes away, check that the mod is made for your + game update and does not fight another mod over the same files. +2. `logs\totk.log` next to `totk.exe` names every mod at the start (lines starting with `mods:`), the cheats + (`cheats:`) and the resource size table (`RomFS: resource size table:`). Open log folder on the Advanced tab of + the menu shows the logs. +3. When reporting a problem, attach `totk.log` and list your mods with the links you downloaded them from. diff --git a/docs/tester-guide.md b/docs/tester-guide.md index 67a9fc70..2ddd4ac9 100644 --- a/docs/tester-guide.md +++ b/docs/tester-guide.md @@ -6,9 +6,10 @@ from it may be shared. ## What you need -- Windows 10 or later, an x86-64 CPU with AVX2, about 60 GB free. +- Windows 10 or later, an x86-64 CPU with SSE4.2 (AVX2/FMA recommended: they build the default, faster version), about + 60 GB free. - A GPU with Vulkan 1.3 and `VK_EXT_external_memory_host`, with a current driver. -- *Tears of the Kingdom* base game (1.0.0) and the 1.2.1 or 1.4.3 update, dumped from your own Switch as NSP or XCI. +- *Tears of the Kingdom* base game (1.0.0) and the 1.1.0, 1.2.1 or 1.4.3 update, dumped from your own Switch as NSP or XCI. - `prod.keys` dumped from your own Switch. Never send it to anyone, and never paste it into a log or an issue. ## Build @@ -38,6 +39,10 @@ build\\totk.exe `TOTK_VK_VALIDATION=1` turns on the Vulkan validation layer (it comes with the Vulkan SDK). It makes the game slower; do one run with it and one without (`set TOTK_VK_VALIDATION=`). +If the game stops with a lost device (the log says `device lost` or `VK_ERROR_DEVICE_LOST`), the lines starting with +`vk: pass marks` name the render pass or dispatch the GPU was running. AMD GPUs write them by default. On an NVIDIA GPU, +`set TOTK_PASS_CRUMBS=1` (or GPU pass marks: On in the Advanced tab) before the run that reproduces the crash. + The game keeps running behind other windows (`assume_focus = 0` in `totk.ini` pauses it instead). Shaders the preparation did not cover compile on first use, so the first minutes of play can stutter. Play into the open field (load a save, walk around for a minute, fight something if you can), then close the window. @@ -51,6 +56,13 @@ A laptop with an iGPU and a discrete GPU, or two cards, lists every GPU at the t The line `vk: using gpu N: ` says which one ran. +## Mods + +Mods and cheats made for the emulators go into `mods/` next to `totk.exe`, one folder per mod. [mods.md](mods.md) covers +installing them, their order, code patches, what does not work and what to check when something looks wrong; +[cheats.md](cheats.md) covers cheat files. The log names every mod at the start (`mods: : ...`); when you +report a problem with mods, list them with their download links. + ## What to send back 1. `logs\totk.log` from each run. It contains no keys and no game files. The lines that matter most start with diff --git a/installer/__init__.py b/installer/__init__.py index 7c523f73..df2ec242 100644 --- a/installer/__init__.py +++ b/installer/__init__.py @@ -1,9 +1,10 @@ """The TotK installer: builds a playable folder from the player's own game files (docs/installer.md). - python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--root DIR] [--temp DIR] + python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--firmware DIR] [--root DIR] + [--temp DIR] Modules: layout (where the bundled tools are, release or repository), events (JSON progress on stdout and the log -file), state (totk-install.json), runner (which steps run: resume and update), steps (the six steps), ini (totk.ini), +file), state (totk-install.json), runner (which steps run: resume and update), steps (the eight steps), ini (totk.ini), cli (the command line). text/en.json holds the English text of every message id the events carry. Standard library only, apart from what the extraction tools import (cryptography). Runs on Python 3.14. diff --git a/installer/cli.py b/installer/cli.py index 538f49d1..14fa57ed 100644 --- a/installer/cli.py +++ b/installer/cli.py @@ -1,10 +1,15 @@ -"""Command line: python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--root DIR] - [--temp DIR] [--verify] +"""Command line: python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--firmware DIR] + [--root DIR] [--temp DIR] [--verify] [--dlss] -A first install needs every option. On a folder with totk-install.json (resume after a cancel or failure, or update -with a newer release) only --dest is needed: --lang defaults to the recorded language, and the game files are needed -only when the extraction has to run again. Prints JSON events on stdout (events.py). Exit code: 0 done, 1 failed, -2 bad command line, 3 cancelled (Ctrl+C or Ctrl+Break). +A first install needs every option. --firmware names the Switch firmware folder (an unpacked firmware or an emulator's +Contents/registered); the setup extracts the system shared fonts from it when the language is Chinese, Korean or +Traditional Chinese. On a folder with totk-install.json (resume after a cancel or failure, or update with a newer +release) only --dest is needed: --lang defaults to the recorded language, and the game files are needed only when the +extraction has to run again. Prints JSON events on stdout (events.py). Exit code: 0 done, 1 failed, 2 bad command +line, 3 cancelled (Ctrl+C or Ctrl+Break). + +--dlss downloads NVIDIA's DLSS library (nvngx_dlss.dll) from NVIDIA's DLSS SDK repository into the install folder on +Windows with an NVIDIA RTX GPU (steps.py, Dlss). """ import argparse import hashlib @@ -17,7 +22,7 @@ from . import layout as layouts from . import state as statefile from .events import Reporter from .runner import Context, run_steps -from .steps import all_steps +from .steps import all_steps, migrate_state EXIT = {'ok': 0, 'failed': 1, 'cancelled': 3} @@ -45,15 +50,19 @@ def cancel(signum, frame): def main(argv=None): ap = Parser(prog='python -m installer', description=__doc__.splitlines()[0]) - ap.add_argument('--base', type=Path, help='base game, XCI or NSP') - ap.add_argument('--update', type=Path, help='the update NSP: 1.1.1, 1.2.1 or 1.4.3') + ap.add_argument('--base', type=Path, help='base game, XCI or NSP (or NSZ, XCZ: unpacked first)') + ap.add_argument('--update', type=Path, help='the update NSP or NSZ: 1.1.0, 1.2.1 or 1.4.3') ap.add_argument('--keys', type=Path, help='prod.keys') + ap.add_argument('--firmware', type=Path, help='Switch firmware folder (Contents/registered) for the shared fonts ' + 'of Chinese, Korean and Traditional Chinese text') ap.add_argument('--lang', help='game language, a Switch language code (en-US, en-GB, ja, de, ru, ...)') ap.add_argument('--dest', type=Path, required=True, help='install folder') ap.add_argument('--root', type=Path, help='release folder or repository (default: the folder above installer/)') ap.add_argument('--temp', type=Path, help='temp folder (default: temp/ of the release on Windows, .setup-temp in ' 'the install folder on Linux, build/install-temp in a repository)') ap.add_argument('--verify', action='store_true', help='hash every kept file, also those this release made') + ap.add_argument('--dlss', action='store_true', help="download NVIDIA's nvngx_dlss.dll (NVIDIA RTX SDK license) next " + 'to the game, for dlaa and the DLSS modes') args = ap.parse_args(argv) # The window cancels with Ctrl+Break to the installer's process group (CREATE_NEW_PROCESS_GROUP), which also reaches @@ -75,6 +84,7 @@ def main(argv=None): rep.emit('end', result='failed', seconds=0) return 1 ctx = Context(layout, dest, temp, rep, args, state=state, verify=args.verify) + migrate_state(ctx) steps = all_steps() rep.emit('start', release=layout.release, mode=ctx.mode, dev=layout.dev, dest=str(dest), log=str(log), steps=[s.id for s in steps]) diff --git a/installer/gui.py b/installer/gui.py index cd94fcde..a0709cc6 100644 --- a/installer/gui.py +++ b/installer/gui.py @@ -1,11 +1,14 @@ """The installer window: python -m installer.gui -A front end over the CLI (docs/installer.md): it collects the game files, the keys file path, the game language and the -install folder, runs `python -m installer ...` as a subprocess and shows its JSON events. Decisions live in guilogic.py; -this file only draws. The window text is in installer/text/.json under "ui.*" and "launcher.*" ids. +A front end over the CLI (docs/installer.md): it collects the game files, the keys file path, the optional firmware +folder, the game language and the install folder, runs `python -m installer ...` as a subprocess and shows its JSON +events. Decisions live in guilogic.py; this file only draws. The window text is in +installer/text/.json under "ui.*" and "launcher.*" ids. - --selftest [--shots DIR] build every screen in both languages and close by itself (PNG per screen with --shots) - --lang en|ru window language (default: the Windows display language) + --selftest [--shots DIR] build every screen in every language and close by itself (PNG per screen with --shots) + --lang en|ru|pt-BR|zh-Hans window language (default: the Windows display language) + --rebuild DIR rebuild the install folder DIR for its mods (totk.exe starts this, build-cache/rebuild.txt): + the run starts at once with that folder, and the game starts again when it succeeds """ import argparse import json @@ -23,8 +26,8 @@ except ImportError: from . import ini from . import layout as layouts -from .guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, UI_LANGUAGES, Catalog, Form, Model, Process, - amount, default_game_language, describe_event, parse_line, windows_ui_language) +from .guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, UI_LANGUAGE_LABELS, UI_LANGUAGES, Catalog, Form, Model, + Process, amount, default_game_language, describe_event, parse_line, windows_ui_language) POLL_MS = 100 GLYPH = {PENDING: '\u25cb', RUNNING: '\u25b6', DONE: '\u2714', SKIPPED: '\u2013', FAILED: '\u2716'} @@ -42,8 +45,9 @@ def fix_tcl_paths(): class App: - def __init__(self, root, ui_lang, layout, run_args=None): + def __init__(self, root, ui_lang, layout, run_args=None, rebuild=None): self.root, self.layout = root, layout + self.rebuild = rebuild # the install folder of --rebuild, or None self.cat = Catalog(ui_lang) self.model = Model() self.process = None @@ -51,7 +55,7 @@ class App: self.run_args = run_args or {} # extra CLI options for tests: temp, verify self.log_entries = [] # events and raw lines of the run, rendered by render_log self.bound = [] # (widget, option, id, params) retranslated on a language switch - self.vars = {n: tk.StringVar() for n in ('base', 'update', 'keys', 'dest')} + self.vars = {n: tk.StringVar() for n in ('base', 'update', 'keys', 'firmware', 'dest')} self.vars['lang'] = tk.StringVar() self.lang_code = default_game_language(ui_lang) self.screen = None @@ -83,16 +87,22 @@ class App: version.pack(side='left', padx=(10, 0), pady=(6, 0)) sw = ttk.Frame(top) sw.pack(side='right') - self.ui_var = tk.StringVar(value=self.cat.lang) - for code in UI_LANGUAGES: - ttk.Radiobutton(sw, text=code.upper(), value=code, variable=self.ui_var, style='Toolbutton', - command=self.switch_language).pack(side='left', padx=1) + self.ui_var = tk.StringVar(value=self.cat.lang) # the window language code; the box shows its name + labels = [UI_LANGUAGE_LABELS[c] for c in UI_LANGUAGES] + self.ui_box = ttk.Combobox(sw, values=labels, state='readonly', width=max(map(len, labels)) + 1) + self.ui_box.set(UI_LANGUAGE_LABELS[self.cat.lang]) + self.ui_box.bind('<>', self.pick_language) + self.ui_box.pack(side='left') self.body = ttk.Frame(root, padding=PAD) self.body.pack(fill='both', expand=True) self.frames = {'form': self.build_form(), 'run': self.build_run(), 'finish': self.build_finish()} self.vars['lang'].set(self.lang_label(self.lang_code)) self.vars['dest'].trace_add('write', lambda *_: self.refresh_form()) self.show('form') + if rebuild: + self.bind_text(self.title, 'text', 'ui.rebuild_title') + self.vars['dest'].set(str(rebuild)) + root.after(POLL_MS, self.start) # ---- text ----------------------------------------------------------------------------------------------------- @@ -120,8 +130,13 @@ class App: self.refresh_finish() self.render_log() + def pick_language(self, _event=None): + self.ui_var.set(UI_LANGUAGES[self.ui_box.current()]) + self.switch_language() + def switch_language(self): self.cat.set_language(self.ui_var.get()) + self.ui_box.set(UI_LANGUAGE_LABELS[self.ui_var.get()]) code = self.selected_lang() self.retext() self.vars['lang'].set(self.lang_label(code)) @@ -197,6 +212,10 @@ class App: b.grid(row=i, column=2, padx=(8, 0)) self.form_rows[name] = lab i = len(rows) + self.label(grid, 'ui.firmware').grid(row=i, column=0, sticky='w', pady=4) + ttk.Entry(grid, textvariable=self.vars['firmware']).grid(row=i, column=1, sticky='ew', pady=4) + self.button(grid, 'ui.browse', self.pick_firmware).grid(row=i, column=2, padx=(8, 0)) + i += 1 self.label(grid, 'ui.game_language').grid(row=i, column=0, sticky='w', pady=4) self.lang_box = ttk.Combobox(grid, textvariable=self.vars['lang'], state='readonly', width=30) self.lang_box.grid(row=i, column=1, sticky='w', pady=4) @@ -206,6 +225,14 @@ class App: self.label(grid, 'ui.dest').grid(row=i + 1, column=0, sticky='w', pady=4) ttk.Entry(grid, textvariable=self.vars['dest']).grid(row=i + 1, column=1, sticky='ew', pady=4) self.button(grid, 'ui.browse', self.pick_dest).grid(row=i + 1, column=2, padx=(8, 0)) + self.label(f, 'ui.compressed_help', wraplength=620, justify='left').pack(anchor='w', pady=(8, 0)) + self.label(f, 'ui.firmware_help', wraplength=620, justify='left').pack(anchor='w', pady=(8, 0)) + # On by default and named with its source and licence: the player sees what is downloaded and can untick it + self.dlss_var = tk.BooleanVar(value=True) + dlss = ttk.Checkbutton(f, variable=self.dlss_var) + self.bind_text(dlss, 'text', 'ui.dlss') + dlss.pack(anchor='w', pady=(8, 0)) + self.label(f, 'ui.dlss_help', wraplength=620, justify='left').pack(anchor='w', padx=(22, 0)) self.update_hint = ttk.Label(f, wraplength=620, justify='left') self.update_hint.pack(anchor='w', pady=(8, 0)) self.form_error = ttk.Label(f, style='Err.TLabel', wraplength=620, justify='left') @@ -252,6 +279,8 @@ class App: self.open_button.pack(side='right', padx=(0, 8)) self.back_button = self.button(bar, 'ui.back', lambda: self.show('form')) self.back_button.pack(side='left') + if self.rebuild: # a failed or cancelled rebuild: the game starts with the code it has + self.button(bar, 'ui.start_game', self.start_game).pack(side='right', padx=(0, 8)) self.finish_title = ttk.Label(f, style='Title.TLabel', wraplength=620, justify='left') self.finish_title.pack(anchor='w') self.finish_text = ttk.Label(f, wraplength=620, justify='left') @@ -266,7 +295,8 @@ class App: def form(self): return Form(base=self.vars['base'].get(), update=self.vars['update'].get(), keys=self.vars['keys'].get(), - lang=self.selected_lang(), dest=self.vars['dest'].get()) + firmware=self.vars['firmware'].get(), lang=self.selected_lang(), dest=self.vars['dest'].get(), + dlss=self.dlss_var.get()) def refresh_form(self): form = self.form() @@ -281,15 +311,19 @@ class App: self.fit() def pick_file(self, name, pick_id): - types = [(self.cat('ui.type_game'), '*.nsp *.xci'), (self.cat('ui.type_all'), '*.*')] - if name == 'update': - types = [(self.cat('ui.type_nsp'), '*.nsp'), (self.cat('ui.type_all'), '*.*')] + # One filter for every game file: the setup tells an NSZ or XCZ from a plain file by its content (Check.unpack) + types = [(self.cat('ui.type_game'), '*.nsp *.xci *.nsz *.xcz'), (self.cat('ui.type_all'), '*.*')] if name == 'keys': types = [(self.cat('ui.type_keys'), '*.keys'), (self.cat('ui.type_all'), '*.*')] path = filedialog.askopenfilename(title=self.cat(pick_id), filetypes=types) if path: self.vars[name].set(os.path.normpath(path)) + def pick_firmware(self): + path = filedialog.askdirectory(title=self.cat('ui.pick_firmware'), mustexist=True) + if path: + self.vars['firmware'].set(os.path.normpath(path)) + def pick_dest(self): path = filedialog.askdirectory(title=self.cat('ui.pick_dest'), mustexist=False) if path: @@ -305,8 +339,11 @@ class App: self.fit() return self.form_error.configure(text='') - command = form.command(self.layout.python, root=self.layout.root if self.layout.dev else None, - temp=self.run_args.get('temp'), verify=self.run_args.get('verify', False)) + # in a repository the Python running this window: the tools' embeddable Python there has no ._pth entry for + # the repository, so `-m installer` would not find the package + python = Path(sys.executable) if self.layout.dev else self.layout.python + command = form.command(python, root=self.layout.root if self.layout.dev else None, + temp=self.run_args.get('temp'), verify_hashes=self.run_args.get('verify', False)) self.begin(command, self.layout.root) def begin(self, command, cwd): @@ -352,6 +389,9 @@ class App: return self.model.finish(code) self.process = None + if self.rebuild and self.model.result == 'ok' and not self.closing: + self.start_game() + return self.refresh_run() self.refresh_finish() self.show('finish') @@ -482,6 +522,15 @@ class App: else: self.open_button.state(['disabled' if not Path(m.dest or '.').is_dir() else '!disabled']) + def start_game(self): + """Starts the game of the install folder and closes the window.""" + dest = Path(self.vars['dest'].get().strip()) + exe = dest / ('totk.exe' if os.name == 'nt' else 'totk') + flags = subprocess.DETACHED_PROCESS if os.name == 'nt' else 0 + if exe.is_file(): + subprocess.Popen([str(exe)], cwd=str(dest), creationflags=flags, close_fds=True) + self.root.destroy() + def open_folder(self): dest = self.model.dest if dest and Path(dest).is_dir(): @@ -495,10 +544,10 @@ class App: # ---- self test --------------------------------------------------------------------------------------------------- SAMPLE_START = {'type': 'start', 'release': 'selftest', 'mode': 'install', 'dest': 'Z:/selftest/TotK', 'log': 'D:\\log.txt', - 'steps': ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']} + 'steps': ['check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'dlss', 'cleanup']} SAMPLE_RUN = [ SAMPLE_START, - {'type': 'plan', 'run': ['check', 'extract'], 'skip': {}}, + {'type': 'plan', 'run': ['check', 'fonts', 'extract'], 'skip': {}}, {'type': 'step', 'step': 'check', 'state': 'started'}, {'type': 'progress', 'step': 'check', 'phase': 'inputs'}, {'type': 'space', 'dest': 'Z:/selftest/TotK', 'need': 36_400_000_000, 'free': 210_000_000_000, 'temp': 'C:\\t', @@ -511,12 +560,14 @@ SAMPLE_RUN = [ ] -SAMPLE_OK = [SAMPLE_START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'lift': 'current', 'compile': 'current', - 'prepare': 'current', 'cleanup': 'not_needed'}}, +SAMPLE_OK = [SAMPLE_START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'fonts': 'current', 'lift': 'current', + 'compile': 'current', 'prepare': 'current', + 'cleanup': 'not_needed'}}, {'type': 'step', 'step': 'check', 'state': 'started'}, {'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.0}, {'type': 'step', 'step': 'extract', 'state': 'started'}, {'type': 'step', 'step': 'extract', 'state': 'done', 'seconds': 1.0}, + {'type': 'step', 'step': 'fonts', 'state': 'skipped', 'reason': 'current'}, {'type': 'step', 'step': 'lift', 'state': 'skipped', 'reason': 'current'}, {'type': 'step', 'step': 'compile', 'state': 'skipped', 'reason': 'current'}, {'type': 'step', 'step': 'prepare', 'state': 'skipped', 'reason': 'current'}, @@ -534,7 +585,7 @@ def grab(root, path): def selftest(layout, shots=None): - """Every screen in both languages; the window closes itself. Returns 0 when nothing raised.""" + """Every screen in every language; the window closes itself. Returns 0 when nothing raised.""" fix_tcl_paths() root = tk.Tk() root.geometry('+60+40') @@ -612,13 +663,14 @@ def main(argv=None): ap.add_argument('--shots', type=Path) ap.add_argument('--lang', choices=UI_LANGUAGES) ap.add_argument('--root', type=Path) + ap.add_argument('--rebuild', type=Path) args = ap.parse_args(argv) layout = layouts.find(args.root) if args.selftest: return selftest(layout, args.shots) fix_tcl_paths() root = tk.Tk() - App(root, args.lang or windows_ui_language(), layout) + App(root, args.lang or windows_ui_language(), layout, rebuild=args.rebuild) root.mainloop() return 0 diff --git a/installer/guilogic.py b/installer/guilogic.py index 8d6c4e91..f17a2dc4 100644 --- a/installer/guilogic.py +++ b/installer/guilogic.py @@ -24,11 +24,17 @@ from . import state as statefile from .events import render TEXT = Path(__file__).resolve().parent / 'text' -UI_LANGUAGES = ('en', 'ru') -STEP_ORDER = ('check', 'extract', 'lift', 'compile', 'prepare', 'cleanup') +UI_LANGUAGES = ('en', 'ru', 'pt-BR', 'zh-Hans') +# what the language switch in the window's corner shows for each +UI_LANGUAGE_LABELS = {'en': 'English', 'ru': 'Русский', 'pt-BR': 'Português (Brasil)', 'zh-Hans': '简体中文'} +STEP_ORDER = ('check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'dlss', 'cleanup') # message parameter -> catalog id prefix of the names that replace its raw values PARAM_NAMES = {'lang': 'lang.', 'known': 'lang.', 'step': 'step.', 'input': 'param.input.'} LANG_RU = 0x19 # primary language id of Russian in a Windows LANGID +LANG_PT_BR = 0x0416 # Portuguese (Brazil), the whole LANGID: Portugal's Portuguese stays English +# Windows LANGIDs that write Simplified Chinese: zh-CN, zh-SG. zh-TW, zh-HK and zh-MO write Traditional Chinese, which +# the window does not have. +LANGIDS_ZH_HANS = (0x0804, 0x1004) def load_texts(lang): @@ -76,18 +82,35 @@ def flat(value): def windows_ui_language(): - """'ru' when the display language (Windows; elsewhere LC_ALL, LC_MESSAGES or LANG) is Russian, else 'en'.""" + """The window language for the display language: the Windows one (ui_language_for_langid); elsewhere LC_ALL, + LC_MESSAGES or LANG ('ru', 'pt_BR', 'zh_CN' or 'zh_SG', else English).""" if os.name != 'nt': - loc = os.environ.get('LC_ALL') or os.environ.get('LC_MESSAGES') or os.environ.get('LANG') or '' - return 'ru' if loc.lower().startswith('ru') else 'en' + loc = (os.environ.get('LC_ALL') or os.environ.get('LC_MESSAGES') or os.environ.get('LANG') or '').lower() + return ('ru' if loc.startswith('ru') else 'pt-BR' if loc.startswith(('pt_br', 'pt-br')) + else 'zh-Hans' if loc.startswith(('zh_cn', 'zh-cn', 'zh_sg', 'zh-sg', 'zh_hans', 'zh-hans')) else 'en') try: langid = ctypes.windll.kernel32.GetUserDefaultUILanguage() except (AttributeError, OSError): return 'en' - return 'ru' if langid & 0x3ff == LANG_RU else 'en' + return ui_language_for_langid(langid) + + +def ui_language_for_langid(langid): + """'ru' for a Russian Windows LANGID, 'pt-BR' for Brazilian Portuguese, 'zh-Hans' for a Simplified Chinese one, + else 'en'.""" + if langid & 0x3ff == LANG_RU: + return 'ru' + if langid == LANG_PT_BR: + return 'pt-BR' + if langid in LANGIDS_ZH_HANS: + return 'zh-Hans' + return 'en' def default_game_language(ui): + """The game language the form starts with. A Chinese window still starts with English: Chinese game text needs + the Switch shared fonts (ini.NEED_SHARED_FONTS), which the setup extracts only from a firmware folder, and the game + stops when they are missing; the player can still pick Chinese.""" return 'ru' if ui == 'ru' else 'en-US' @@ -111,8 +134,10 @@ class Form: base: str = '' update: str = '' keys: str = '' + firmware: str = '' lang: str = '' dest: str = '' + dlss: bool = False def installed_state(self): """The recorded state of the install folder (totk-install.json), or None. Never raises.""" @@ -147,20 +172,24 @@ class Form: value = getattr(self, name).strip() if value and not Path(value).is_file(): found.append(f'ui.missing_{name}') + if self.firmware.strip() and not Path(self.firmware.strip()).is_dir(): + found.append('ui.missing_firmware') return found - def command(self, python, root=None, temp=None, verify=False): + def command(self, python, root=None, temp=None, verify_hashes=False): cmd = [str(python), '-m', 'installer'] for flag, value in (('--base', self.base), ('--update', self.update), ('--keys', self.keys), - ('--lang', self.lang), ('--dest', self.dest)): + ('--firmware', self.firmware), ('--lang', self.lang), ('--dest', self.dest)): if value.strip(): cmd += [flag, value.strip()] if root: cmd += ['--root', str(root)] if temp: cmd += ['--temp', str(temp)] - if verify: + if verify_hashes: cmd.append('--verify') + if self.dlss: + cmd.append('--dlss') return cmd diff --git a/installer/hostcheck.c b/installer/hostcheck.c index 6cd08332..d3099271 100644 --- a/installer/hostcheck.c +++ b/installer/hostcheck.c @@ -1,5 +1,5 @@ // Host check of the installer's first step, compiled with the bundled clang at install time: does the CPU run -// -march=x86-64-v3 code, and does a Vulkan 1.3 device exist. Prints one JSON object on stdout. +// -march=x86-64-v3 code (or only x86-64-v2), and does a Vulkan 1.3 device exist. Prints one JSON object on stdout. // The Vulkan loader is opened at run time, so a machine without one gets "loader": false instead of a failed start. #include #include @@ -25,10 +25,24 @@ static void json_string(const char *s) { putchar('"'); } +// The x86-64-v2 level from CPUID: SSE3, SSSE3, SSE4.1, SSE4.2, POPCNT, CMPXCHG16B and LAHF/SAHF. Every CPU with +// SSE4.2 has the others, but the level is what the v2 build is compiled for, so check them all. +static int x86_64_v2(void) { + unsigned a, b, c, d; + if (!__get_cpuid(1, &a, &b, &c, &d)) return 0; + const unsigned sse3 = 1u << 0, ssse3 = 1u << 9, cx16 = 1u << 13, sse41 = 1u << 19, sse42 = 1u << 20, + popcnt = 1u << 23; + if ((c & (sse3 | ssse3 | cx16 | sse41 | sse42 | popcnt)) != (sse3 | ssse3 | cx16 | sse41 | sse42 | popcnt)) + return 0; + if (!__get_cpuid(0x80000001, &a, &b, &c, &d)) return 0; + return (c & (1u << 0)) != 0; // LAHF/SAHF +} + // The x86-64-v3 level from CPUID: AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT and MOVBE, with the OS saving the YMM state. // Not __builtin_cpu_supports("x86-64-v3"): clang reads that from the C runtime's CPU model, and libgcc before GCC 12 // (the Linux release's sysroot has GCC 10) has no level bits, so it answers false on every CPU. static int x86_64_v3(void) { + if (!x86_64_v2()) return 0; unsigned a, b, c, d; if (!__get_cpuid(1, &a, &b, &c, &d)) return 0; const unsigned fma = 1u << 12, movbe = 1u << 22, osxsave = 1u << 27, avx = 1u << 28, f16c = 1u << 29; @@ -44,7 +58,8 @@ static int x86_64_v3(void) { } int main(void) { - printf("{\"x86_64_v3\": %s, ", x86_64_v3() ? "true" : "false"); + printf("{\"x86_64_v2\": %s, \"x86_64_v3\": %s, ", x86_64_v2() ? "true" : "false", + x86_64_v3() ? "true" : "false"); void *lib = open_lib(); PFN_vkGetInstanceProcAddr gipa = lib ? (PFN_vkGetInstanceProcAddr)lib_sym(lib, "vkGetInstanceProcAddr") : NULL; if (!gipa) { diff --git a/installer/ini.py b/installer/ini.py index 13147862..5cad6f00 100644 --- a/installer/ini.py +++ b/installer/ini.py @@ -14,7 +14,8 @@ LANGUAGES = ('ja', 'en-US', 'en-GB', 'fr', 'fr-CA', 'de', 'it', 'es', 'es-419', 'zh-Hans', 'zh-Hant') # Older installer builds wrote these two codes; they are read as the canonical ones and rewritten on the next install. ALIASES = {'zh-CN': 'zh-Hans', 'zh-TW': 'zh-Hant'} -# Their text falls back to the Switch shared fonts (runtime/ov_system.c), which the installer does not extract. +# Their text falls back to the Switch shared fonts (runtime/ov_system.c), which the fonts step extracts from the +# firmware folder when the player gives one. NEED_SHARED_FONTS = ('ko', 'zh-Hans', 'zh-Hant') ACTIVE = re.compile(r'^(\s*)(?:totk_)?lang(\s*=\s*)(.*?)(\s*)$', re.IGNORECASE) diff --git a/installer/launcher/launcher.c b/installer/launcher/launcher.c index 11994cd9..7ddec021 100644 --- a/installer/launcher/launcher.c +++ b/installer/launcher/launcher.c @@ -9,14 +9,26 @@ #include "launcher_text.h" -#define LANGID_RUSSIAN 0x19 +#define LANGID_RUSSIAN 0x19 /* primary language id */ +#define LANGID_PT_BR 0x0416 /* the whole LANGID: Portugal's Portuguese stays English */ #define PATH_CHARS (MAX_PATH * 2) +/* The language of the window (installer/guilogic.py ui_language_for_langid): Russian, Simplified Chinese (zh-CN and + zh-SG; the Traditional Chinese regions get English, as the window has no Traditional Chinese), else English. */ static void fail(void) { - int russian = (GetUserDefaultUILanguage() & 0x3ff) == LANGID_RUSSIAN; - MessageBoxW(NULL, russian ? LAUNCHER_PYTHON_MISSING_RU : LAUNCHER_PYTHON_MISSING_EN, - russian ? LAUNCHER_TITLE_RU : LAUNCHER_TITLE_EN, MB_OK | MB_ICONERROR); + LANGID ui = GetUserDefaultUILanguage(); + int russian = (ui & 0x3ff) == LANGID_RUSSIAN, brazilian = ui == LANGID_PT_BR; + int chinese = ui == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED) || + ui == MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE); + MessageBoxW(NULL, + russian ? LAUNCHER_PYTHON_MISSING_RU + : brazilian ? LAUNCHER_PYTHON_MISSING_PT_BR + : chinese ? LAUNCHER_PYTHON_MISSING_ZH_HANS + : LAUNCHER_PYTHON_MISSING_EN, + russian ? LAUNCHER_TITLE_RU : brazilian ? LAUNCHER_TITLE_PT_BR : chinese ? LAUNCHER_TITLE_ZH_HANS + : LAUNCHER_TITLE_EN, + MB_OK | MB_ICONERROR); } int WINAPI WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR command_line, int show) diff --git a/installer/layout.py b/installer/layout.py index 7f31443e..0c6995d8 100644 --- a/installer/layout.py +++ b/installer/layout.py @@ -7,7 +7,8 @@ Release (docs/installer.md "Release layout"), root = the unpacked folder: recomp/, tools/ the lifter and the scripts; recomp/patches-.txt, tools/data/expected-v.json for each supported update (SUPPORTED) llvm/ llvm-mingw (bin/clang.exe) - runtime/ libtotkrt.a, the headers, install-side sources (romfs.c, patches.c, ov_zstd.c) and totk.ico; runtime/deps/ holds + runtime/ libtotkrt.a and libtotkrt-v2.a (x86-64-v3 / v2), the headers, install-side sources (romfs.c, + patches.c, ov_zstd.c) and totk.ico; runtime/deps/ holds include/ + lib/ of Vulkan, SDL3, Opus, libvpx and VMA (build_game.py --deps) bin/ totk_xlate.exe (+ totk_xlate.id), hactool.exe, ninja.exe, SDL3.dll, libopus-0.dll data/shader_list.tsv the MacNativeGames shader list @@ -21,8 +22,8 @@ against), runtime/deps/ with include/ and the static Opus, libvpx and zstd, bin/ Dev mode, root = a repository checkout (no python/python.exe): build/py314-embed/py/python.exe (the embeddable Python the release ships, with the same packages), else this Python; toolchain/llvm-mingw-*/ and toolchain/deps-mingw; -runtime/ as the sources with build/install-rtlib/libtotkrt.a (tools/build_game.py --make-runtime-lib --out -build/install-rtlib); MSYS2 UCRT64 ninja.exe; toolchain/hactool; build/xlate/totk_xlate.exe; the shader list from +runtime/ as the sources with build/install-rtlib/libtotkrt.a and libtotkrt-v2.a (tools/build_game.py --make-runtime-lib +--out build/install-rtlib, once per --cpu); MSYS2 UCRT64 ninja.exe; toolchain/hactool; build/xlate/totk_xlate.exe; the shader list from refs/MacNativeGames; build/install-temp. The version is VERSION with -dev and the commit: 0.1.0-dev+973c626. On Linux, dev mode takes clang, lld and ninja from PATH and the system's libraries (no deps folder, no SDL3 copy). """ @@ -36,7 +37,7 @@ from pathlib import Path WIN = os.name == 'nt' EXE = '.exe' if WIN else '' UCRT = Path(r'C:\msys64\ucrt64\bin') -SUPPORTED = ('1.2.1', '1.4.3', '1.1.1') # updates the installer builds; the check identifies the player's by hash +SUPPORTED = ('1.2.1', '1.4.3', '1.1.0') # updates the installer builds; the check identifies the player's by hash def version_tag(version): @@ -64,11 +65,20 @@ class Layout: ini_example: Path | None = None temp: Path | None = None sysroot: Path | None = None # Linux release: what the game links against, instead of the host's + runtime_lib_v2: Path | None = None # libtotkrt-v2.a: the runtime for CPUs without AVX2 (build_game.py --cpu v2) @property def clang(self): return self.llvm / 'bin' / ('clang' + EXE) + def runtime_lib_for(self, cpu): + """The prebuilt runtime library for cpu (v2 or v3). None in dev mode when it was not built: the runtime is + then compiled from the sources in runtime/ instead.""" + lib = self.runtime_lib_v2 if cpu == 'v2' else self.runtime_lib + if lib and lib.is_file(): + return lib + return None if self.dev else lib + def patches(self, version): """The code patch table for recomp/gen.py.""" return self.recomp / f'patches-{version_tag(version)}.txt' @@ -77,13 +87,18 @@ class Layout: """The expected hashes of the update.""" return self.tools / 'data' / f'expected-v{version}.json' - def missing(self): - """Paths the steps need that do not exist.""" - need = [self.python, self.tools / 'extract.py', self.tools / 'build_game.py', self.tools / 'prepare_shaders.py', + def missing(self, cpu='v3'): + """Paths the steps need that do not exist. cpu picks the runtime library the host will use.""" + lib = self.runtime_lib_for(cpu) + need = [self.python, self.tools / 'extract.py', self.tools / 'ncz.py', self.tools / 'build_game.py', + self.tools / 'prepare_shaders.py', self.tools / 'pack_shader_cache.py', self.tools / 'make_romfs_patches.py', self.tools / 'ultrawide_layout.py', + self.tools / 'button_icons.py', + self.tools / 'vendor' / 'macnativegames' / 'extract_shared_fonts.py', self.recomp / 'gen.py', self.clang, - *((self.deps / 'include', self.deps / 'lib') if self.deps else ()), self.runtime, self.runtime_lib, self.ninja, self.hactool, + *((self.deps / 'include', self.deps / 'lib') if self.deps else ()), self.runtime, *([lib] if lib else ()), + self.ninja, self.hactool, self.xlate, *self.dlls, *(f(v) for v in SUPPORTED for f in (self.patches, self.expected))] if self.shader_list: need.append(self.shader_list) @@ -109,6 +124,7 @@ def release_layout(root): tools=root / 'tools', recomp=root / 'recomp', llvm=root / 'llvm', deps=root / 'runtime' / 'deps', runtime=root / 'runtime', runtime_lib=root / 'runtime' / 'libtotkrt.a', + runtime_lib_v2=root / 'runtime' / 'libtotkrt-v2.a', ninja=root / 'bin' / ('ninja' + EXE), hactool=root / 'bin' / ('hactool' + EXE), xlate=root / 'bin' / ('totk_xlate' + EXE), dlls=(root / 'bin' / 'SDL3.dll', root / 'bin' / 'libopus-0.dll') if WIN else (root / 'bin' / 'libSDL3.so.0',), @@ -134,6 +150,7 @@ def dev_layout(root): tools=root / 'tools', recomp=root / 'recomp', llvm=trees[-1] if trees else root / 'toolchain' / 'llvm-mingw-missing', deps=deps, runtime=root / 'runtime', runtime_lib=root / 'build' / 'install-rtlib' / 'libtotkrt.a', + runtime_lib_v2=root / 'build' / 'install-rtlib' / 'libtotkrt-v2.a', ninja=UCRT / ('ninja' + EXE), hactool=root / 'toolchain' / 'hactool' / ('hactool' + EXE), xlate=root / 'build' / 'xlate' / ('totk_xlate' + EXE), dlls=tuple(sorted((deps / 'bin').glob('*.dll'))), @@ -153,6 +170,7 @@ def dev_layout_posix(root): python=Path(sys.executable), tools=root / 'tools', recomp=root / 'recomp', llvm=Path(clang).parent.parent if clang else root / 'toolchain' / 'clang-missing', deps=None, runtime=root / 'runtime', runtime_lib=root / 'build' / 'install-rtlib' / 'libtotkrt.a', + runtime_lib_v2=root / 'build' / 'install-rtlib' / 'libtotkrt-v2.a', ninja=Path(ninja) if ninja else root / 'toolchain' / 'ninja-missing', hactool=root / 'toolchain' / 'hactool' / 'hactool', xlate=root / 'build' / 'xlate' / 'totk_xlate', shader_list=None, ini_example=root / 'totk.ini.example', temp=root / 'build' / 'install-temp') diff --git a/installer/steps.py b/installer/steps.py index 2a165641..2a7c089e 100644 --- a/installer/steps.py +++ b/installer/steps.py @@ -1,17 +1,26 @@ -"""The six setup steps of docs/installer.md: check, extract, lift, compile, prepare, cleanup. +"""The eight setup steps of docs/installer.md: check, fonts, extract, lift, compile, prepare, dlss, cleanup. -Extraction runs in this process through tools/extract.py (the package's sibling tools/, which ships with it); lift, -compile and prepare run the bundled Python on recomp/gen.py, tools/build_game.py, tools/make_romfs_patches.py and -tools/prepare_shaders.py, whose output goes to /logs/-.log and is read for progress. No Win32 call -here: the host check is a small C program built with the bundled clang (hostcheck.c). +Extraction runs in this process through tools/extract.py (the package's sibling tools/, which ships with it); fonts +runs the vendored extract_shared_fonts.py on the firmware folder; lift, compile and prepare run the bundled Python on +recomp/gen.py, tools/build_game.py, tools/make_romfs_patches.py and tools/prepare_shaders.py, whose output goes to +/logs/-.log and is read for progress. No Win32 call here: the host check is a small C program built +with the bundled clang (hostcheck.c). -Install folder: exefs/, romfs/ (romfs.bin, index.json), profile/, totk.exe and its DLLs, romfs-index.bin, -romfs-patches/, romfs-patches-xbox/, totk_xlate.exe, the shader state of totk.exe --prepare-shaders, totk.ini, -totk-install.json. -Temp folder (/-): check/ (ExeFS of both inputs, the host check), gen/ (C), build/ (objects), -logs/ (kept by the cleanup). +Install folder: exefs/, romfs/ (romfs.bin, index.json), profile/, shared-fonts/ (Chinese and Korean text), totk.exe +and its DLLs, nvngx_dlss.dll (with --dlss), romfs-index.bin, romfs-patches/, romfs-patches-xbox/, romfs-patches-xbox-label/, totk_xlate.exe, the +shader state of totk.exe --prepare-shaders, totk.ini, totk-install.json. +Temp folder (/-): check/ (ExeFS of both inputs, the host check), unpacked/ (the NSP of an NSZ +or XCZ input until the extraction is done), gen/ (C), build/ (objects), logs/. The cleanup keeps gen/, build/ and +logs/: the next run lifts into the same gen/, where recomp/gen.py rewrites only the chunks whose C changed, and Ninja +in build/ recompiles only those. A change of the mods' code cheats then costs the lift (about a minute) and the chunks +it touches, not a full compile. + +Install folder build-cache/: cheats.status (tools/cheats.py), exefs.status (tools/exefs_patches.py) and rebuild.txt, the command that starts this setup in +its rebuild mode on the install folder (installer/gui.py --rebuild), one argument per line; totk.exe runs it at start +when an enabled mod has code cheats the build lacks (runtime/cheats.c). """ import hashlib +import http.client import json import os import re @@ -21,6 +30,7 @@ import subprocess import sys import threading import time +import urllib.request from pathlib import Path from . import ini @@ -29,6 +39,7 @@ from .runner import InstallError, Step sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'tools')) import extract # noqa: E402 +import ncz # noqa: E402 import shader_list # noqa: E402 from extract_exefs import (common_tickets, container_entries, load_keys, nca_ctr_key, nca_inventory, # noqa: E402 program_nca) @@ -167,6 +178,12 @@ def raise_if_damaged(error, name, path): raise InstallError('check.input_damaged', input=name, path=str(path), detail=str(error)) from None +def game_file(ctx, name): + """The file the extraction reads for the base or the update: the NSP that Check.unpack made from an NSZ or XCZ, + else the chosen file itself.""" + return getattr(ctx, 'unpacked', {}).get(name) or Path(getattr(ctx.options, name)) + + def game_version(ctx): """The update this install is built from: what the check identified, else the recorded one. An install made before 1.4.3 support records no version, and that install is 1.2.1: name it here rather than leaning on SUPPORTED's order.""" @@ -177,6 +194,25 @@ def expected_update(ctx): return extract.load_expected()[game_version(ctx)] +# Releases before 0.3.1 recorded the 0x20000 update as 1.1.1 (pull request #220); it is 1.1.0 (#371), with the same +# files, hashes and patch sites under new names. +RENAMED_VERSIONS = {'1.1.1': '1.1.0'} + + +def migrate_state(ctx): + """Read a recorded update by its current name. The extraction it recorded stays: its record gets the key of the + renamed expected file, and Extract.current and the full check of output another release kept + (runner.Context.verify_kept) still test the files against the expected hashes.""" + old = ctx.state.get('version') + if old not in RENAMED_VERSIONS: + return + ctx.state['version'] = RENAMED_VERSIONS[old] + record = ctx.state['steps'].get('extract') + if record: + record['key'] = Extract().key(ctx) + ctx.reporter.log(f'state: the update recorded as {old} is {ctx.state["version"]}') + + def shader_list_text(layout): if layout.shader_list: return layout.shader_list.read_text(encoding='utf-8') @@ -217,15 +253,100 @@ def size_of(paths): return sum(p.stat().st_size for p in paths if p.is_file()) +# The Switch system shared fonts (runtime/ov_system.c): TotK falls back to them for Chinese, Korean and Traditional +# Chinese text. tools/vendor/macnativegames/extract_shared_fonts.py writes these names. +SHARED_FONTS = 'shared-fonts' +SHARED_FONT_FILES = ('0-standard.bfttf', '1-chinese-simplified.bfttf', '2-chinese-simplified-extension.bfttf', + '3-chinese-traditional.bfttf', '4-korean.bfttf', '5-nintendo-extension.bfttf') +FONTS_NEED = 32 << 20 # the six BFTTF files and their manifest + + +def requested_lang(ctx): + """The language this run uses: --lang, else the one recorded for the install (Check canonicalizes it).""" + return ini.canonical_language(getattr(ctx.options, 'lang', None) or ctx.state.get('lang')) + + +def firmware_dir(value): + """The firmware NCA folder behind a --firmware path: the folder itself, or its registered/ child (the emulators' + layout, Contents/registered).""" + if not value: + return None + path = Path(value) + return path / 'registered' if (path / 'registered').is_dir() else path + + +def firmware_has_ncas(folder): + """Whether folder holds firmware NCAs: .nca files, or the hashed directories with a 00 file (registered/).""" + try: + entries = list(Path(folder).iterdir()) + except OSError: + return False + return any((e.is_file() and e.suffix.lower() == '.nca') or (e.is_dir() and (e / '00').is_file()) for e in entries) + + +def firmware_stamp(folder): + """A cheap identity of the firmware NCAs: names, sizes and mtimes, not their contents (the folder is ~326 MB).""" + try: + entries = sorted(Path(folder).iterdir()) + except OSError: + return 'missing' + rows = [] + for entry in entries: + if entry.is_dir(): + file = entry / '00' + if not file.is_file(): + continue + elif not entry.is_file() or entry.suffix.lower() != '.nca': + continue + else: + file = entry + st = file.stat() + rows.append((entry.name, st.st_size, st.st_mtime_ns)) + return rows + + +def shared_fonts_ready(dest): + """Whether dest/shared-fonts holds a complete set that the game can load.""" + folder = Path(dest) / SHARED_FONTS + return (folder / 'manifest.json').is_file() and all((folder / name).is_file() for name in SHARED_FONT_FILES) + + # ---- 1. check +UPDATE_TITLE_ID = '0100F2C0115B6800' + + +def base_misuse(records): + """Name why a file chosen as the base game holds an update or a combined package, or return None. + + The Meta NCA (content type 1) carries the package's own title id: the update's id (...800) for an update NSP, and + several Meta NCAs mean base, update and DLC repacked into one file. + """ + metadata = [r['title_id'] for r, _ in records if r['content_type'] == 1] + if len(metadata) > 1: + return 'check.base_combined' + if UPDATE_TITLE_ID in metadata: + return 'check.base_is_update' + return None + + class Check(Step): id = 'check' always = True + def key(self, ctx): + # The compile step fingerprints the CPU level, and every key is computed before a step runs: probe the host + # here, once, so both steps see the same answer (the check step itself is always run). A failed probe is + # reported by run(), where the error names this step. + try: + self.host(ctx) + except InstallError: + pass + return '' + def run(self, ctx): layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest - missing = layout.missing() + missing = layout.missing(getattr(ctx, 'cpu', 'v3')) if missing: raise InstallError('check.release_incomplete', paths=missing[:5]) if dest.exists() and not dest.is_dir(): @@ -233,13 +354,35 @@ class Check(Step): own = (ctx.temp, *ctx.temp.parents) # a temp folder inside the install folder (installer/cli.py) is ours if ctx.mode == 'install' and dest.is_dir() and any(p not in own for p in dest.iterdir()): raise InstallError('check.dest_not_empty', path=str(dest)) - lang = ini.canonical_language(ctx.options.lang or ctx.state.get('lang')) + lang = requested_lang(ctx) if not lang: raise InstallError('check.lang_missing') if lang not in ini.LANGUAGES: raise InstallError('check.lang_unknown', lang=lang, known=', '.join(ini.LANGUAGES)) + firmware = firmware_dir(getattr(ctx.options, 'firmware', None)) + if firmware is not None: + if not firmware.is_dir(): + raise InstallError('check.firmware_missing', path=str(firmware)) + if not firmware_has_ncas(firmware): + raise InstallError('check.firmware_empty', path=str(firmware)) if lang in ini.NEED_SHARED_FONTS: - rep.warning('warn.lang_needs_fonts', lang=lang) + if firmware is None: + rep.warning('warn.lang_needs_fonts', lang=lang) + elif not shared_fonts_ready(dest): + keys = getattr(ctx.options, 'keys', None) + if not keys or not Path(keys).is_file(): + raise InstallError('check.fonts_keys_needed') + try: + key_values = load_keys(Path(keys)) + except (ValueError, OSError, UnicodeDecodeError): + raise InstallError('check.keys_unreadable') from None + if 'header_key' not in key_values: + raise InstallError('check.keys_no_header_key') + if (sys.platform == 'linux' and ctx.will_run('prepare') + and not (os.environ.get('DISPLAY') or os.environ.get('WAYLAND_DISPLAY'))): + # totk --prepare-shaders opens a window and needs a present-capable queue family; without a display it + # fails after lift and compile (~15 min), so stop here + raise InstallError('check.no_display') ctx.lang = lang ctx.temp.mkdir(parents=True, exist_ok=True) @@ -258,6 +401,8 @@ class Check(Step): return {} def host(self, ctx): + if getattr(ctx, 'host', None) is not None: + return layout, work = ctx.layout, ctx.temp / 'check' glibc = linux_glibc() if glibc and glibc < GLIBC_NEEDED and not layout.dev: @@ -282,8 +427,10 @@ class Check(Step): except ValueError: raise InstallError('check.host_probe_failed', code=r.returncode) from None ctx.reporter.log(f'host: {r.stdout.strip()}') - if not host['x86_64_v3']: - raise InstallError('check.cpu_no_avx2') + if not host['x86_64_v2']: + raise InstallError('check.cpu_too_old') + # v3 is the faster build; v2 runs on CPUs without AVX2/FMA (runtime/fma.c supplies the fused multiply-add). + ctx.cpu = 'v3' if host['x86_64_v3'] else 'v2' if not host['loader']: raise InstallError('check.vulkan_missing') devices = host['devices'] @@ -314,14 +461,73 @@ class Check(Step): raise InstallError('check.keys_unreadable') from None if 'header_key' not in keys: raise InstallError('check.keys_no_header_key') + self.unpack(ctx) total = 0 for name, is_update in (('base', False), ('update', True)): - path = Path(getattr(o, name)) + path = game_file(ctx, name) with path.open('rb') as f: total += self.open_input(f, path, keys, name, is_update) del keys return total + def unpack(self, ctx): + """Unpack an NSZ or XCZ (a game file compressed by the nsz tool) into /unpacked/.nsp with + tools/ncz.py, which checks every NCA against its hash; the extraction then reads that NSP. An earlier run's NSP + of the same file is reused. The extract step removes the folder once it is done.""" + o, rep, folder = ctx.options, ctx.reporter, ctx.temp / 'unpacked' + ctx.unpacked = {} + jobs = [] + for name in ('base', 'update'): + path = Path(getattr(o, name)) + try: + found = ncz.compressed(path) + if found is None: + continue + if name == 'update' and found[0] != 'nsz': + raise InstallError('check.update_not_nsp') + size = ncz.unpacked_size(path) + except ValueError as e: + raise_if_damaged(e, name, path) + raise InstallError('check.not_game_file', input=name) from None + st = path.stat() + jobs.append((name, path, size, f'{path.resolve()}\n{st.st_size}\n{st.st_mtime_ns}\n{size}\n')) + if not jobs: + remove(folder) + return + done = [] + for name, path, size, tag in jobs: + target, stamp = folder / f'{name}.nsp', folder / f'{name}.source' + try: + if target.stat().st_size == size and stamp.read_text(encoding='utf-8') == tag: + done.append(name) + ctx.unpacked[name] = target + except OSError: + pass + need = sum(size for name, _, size, _ in jobs if name not in done) + free, _ = free_bytes(folder) + rep.log(f'unpack: {", ".join(name for name, *_ in jobs)}: {need / GiB:.1f} GiB to write, ' + f'{free / GiB:.1f} free') + if need > free: + raise InstallError('check.disk_space', path=str(folder), need_gb=round(need / 1e9, 1), + free_gb=round(free / 1e9, 1)) + for name, path, size, tag in jobs: + if name in done: + continue + target, stamp = folder / f'{name}.nsp', folder / f'{name}.source' + remove(stamp) + try: + folder.mkdir(parents=True, exist_ok=True) + ncz.unpack(path, target, lambda d, t, n=name: rep.progress('check', 'unpack_' + n, d, t, 'bytes')) + stamp.write_text(tag, encoding='utf-8') + except ncz.DamagedFile as e: + raise InstallError('check.input_damaged', input=name, path=str(e.path), detail=e.detail) from None + except ValueError as e: + raise_if_damaged(e, name, path) + raise InstallError('check.unpack_failed', input=name, detail=str(e)) from None + except OSError as e: + raise InstallError('check.unpack_failed', input=name, detail=str(e)) from None + ctx.unpacked[name] = target + @staticmethod def open_input(f, path, keys, name, is_update): try: @@ -350,6 +556,10 @@ class Check(Step): except ValueError as e: if is_update and 'BKTR' in str(e): raise InstallError('check.update_is_base') from None + if not is_update: + reason = base_misuse(records) + if reason: + raise InstallError(reason) from None raise InstallError('check.update_not_totk' if is_update else 'check.base_not_totk', detail=str(e)) from None try: nca_ctr_key(header, keys, tickets) @@ -368,6 +578,8 @@ class Check(Step): romfs = ctx.dest / 'romfs' / 'romfs.bin' if romfs.is_file(): # a resumed extraction replaces it dest_need -= romfs.stat().st_size + if ctx.will_run('fonts'): + dest_need += FONTS_NEED if ctx.will_run('prepare') or ctx.will_run('extract'): dest_need += DEST_EXTRA if ctx.will_run('lift') or ctx.will_run('compile'): @@ -396,9 +608,10 @@ class Check(Step): out = work / name remove(out) try: - extract.extract_exefs(Path(getattr(o, name)), Path(o.keys), out, logs, update=is_update) + extract.extract_exefs(game_file(ctx, name), Path(o.keys), out, logs, update=is_update) except extract.DamagedFile as e: - raise InstallError('check.input_damaged', input=name, path=str(e.path), detail=e.detail) from None + raise InstallError('check.input_damaged', input=name, path=str(getattr(o, name)), + detail=e.detail) from None except ValueError: raise InstallError('check.exefs_failed', input=name, log=str(logs / f'extract_exefs-{name}.log')) \ from None @@ -419,7 +632,72 @@ class Check(Step): ctx.state['version'] = found -# ---- 2. extract +# ---- 2. fonts + +class Fonts(Step): + """The Switch system fonts for Chinese, Korean and Traditional Chinese text (tools/extract_fonts.py, which runs the + vendored extract_shared_fonts.py). The firmware folder is optional: without it the setup warns and the game runs + with those glyphs missing. The extraction verifies the firmware NCAs and writes the six BFTTF files the console + puts in shared memory.""" + + id = 'fonts' + + def key(self, ctx): + lang = requested_lang(ctx) + if lang not in ini.NEED_SHARED_FONTS: + return fingerprint('fonts-1', 'not-needed') + firmware = firmware_dir(getattr(ctx.options, 'firmware', None)) + if firmware is None: + return fingerprint('fonts-1', 'no-firmware') + return fingerprint('fonts-1', lang, str(firmware), firmware_stamp(firmware)) + + def current(self, ctx, record): + if requested_lang(ctx) not in ini.NEED_SHARED_FONTS or not getattr(ctx.options, 'firmware', None): + return True + return shared_fonts_ready(ctx.dest) + + def verify(self, ctx, record): + # An update run may omit --firmware; the installed set still has to be there for these languages. + if requested_lang(ctx) in ini.NEED_SHARED_FONTS: + return shared_fonts_ready(ctx.dest) + return True + + def run(self, ctx): + lang = getattr(ctx, 'lang', None) or requested_lang(ctx) + if lang not in ini.NEED_SHARED_FONTS: + return {} + firmware = firmware_dir(getattr(ctx.options, 'firmware', None)) + if firmware is None: + return {} # Check warned: the game runs, some text may not show + keys = getattr(ctx.options, 'keys', None) + if not keys: + raise InstallError('check.fonts_keys_needed') + out = ctx.temp / 'shared-fonts' + remove(out) + ctx.reporter.progress('fonts', 'extract', force=True) + tool = ctx.layout.tools / 'vendor' / 'macnativegames' / 'extract_shared_fonts.py' + code, log = run_tool(ctx, 'fonts', tool, ['--keys', keys, '--firmware', firmware, '--output', out]) + if code: + detail = '' + try: + detail = log.read_text(encoding='utf-8', errors='replace').strip().splitlines()[-1][:300] + except (OSError, IndexError): + pass + raise InstallError('fonts.failed', code=code, detail=detail, log=str(log)) + missing = [name for name in SHARED_FONT_FILES if not (out / name).is_file()] + if missing or not (out / 'manifest.json').is_file(): + raise InstallError('fonts.incomplete', files=', '.join(missing) or 'manifest.json') + ctx.reporter.progress('fonts', 'copy', force=True) + try: + ctx.dest.mkdir(parents=True, exist_ok=True) + remove(ctx.dest / SHARED_FONTS) + shutil.move(str(out), str(ctx.dest / SHARED_FONTS)) + except OSError as e: + raise InstallError('fonts.copy_failed', path=str(ctx.dest / SHARED_FONTS), detail=str(e)) from None + return {'files': len(SHARED_FONT_FILES)} + + +# ---- 3. extract class Extract(Step): id = 'extract' @@ -452,9 +730,9 @@ class Extract(Step): def run(self, ctx): o, dest, rep = ctx.options, ctx.dest, ctx.reporter expected = expected_update(ctx) - base, update, keys = Path(o.base), Path(o.update), Path(o.keys) + keys = Path(o.keys) inputs = {} - for name, path in (('base', base), ('update', update)): + for name, path in (('base', Path(o.base)), ('update', Path(o.update))): known = ctx.state['inputs'].get(name, {}) st = path.stat() if known.get('size') == st.st_size and known.get('mtime_ns') == st.st_mtime_ns and known.get('sha256'): @@ -464,6 +742,7 @@ class Extract(Step): inputs[name] = {'name': path.name, 'size': st.st_size, 'mtime_ns': st.st_mtime_ns, 'sha256': digest} ctx.state['inputs'] = inputs ctx.save() + base, update = game_file(ctx, 'base'), game_file(ctx, 'update') dest.mkdir(parents=True, exist_ok=True) for stale in dest.glob('update-merge-*'): # a killed merge leaves its NCA copies (and key copies) behind @@ -475,7 +754,7 @@ class Extract(Step): try: extract.extract_exefs(update, keys, ctx.temp / 'check' / 'update', ctx.temp / 'logs', update=True) except extract.DamagedFile as e: - raise InstallError('check.input_damaged', input='update', path=str(e.path), detail=e.detail) from None + raise InstallError('check.input_damaged', input='update', path=str(o.update), detail=e.detail) from None except ValueError: raise InstallError('check.exefs_failed', input='update', log=str(ctx.temp / 'logs' / 'extract_exefs-update.log')) from None @@ -497,6 +776,7 @@ class Extract(Step): raise InstallError('extract.romfs_mismatch', version=game_version(ctx), size=report['romfs_size'], sha256=report['romfs_sha256'][:16]) (dest / 'profile').mkdir(exist_ok=True) + remove(ctx.temp / 'unpacked') # the NSPs unpacked from an NSZ or XCZ (Check.unpack) st = (romfs / 'romfs.bin').stat() return {'romfs_size': st.st_size, 'romfs_mtime_ns': st.st_mtime_ns} @@ -518,7 +798,8 @@ class Extract(Step): try: extract.merge_update_romfs(base, update, keys, ctx.layout.hactool, romfs, logs, phase=phase) except extract.DamagedFile as e: - raise InstallError('check.input_damaged', input='base' if e.path == base else 'update', path=str(e.path), + name = 'base' if e.path == base else 'update' + raise InstallError('check.input_damaged', input=name, path=str(getattr(ctx.options, name)), detail=e.detail) from None except ValueError as e: raise InstallError('extract.romfs_merge_failed', detail=str(e), log=str(logs / 'merge_romfs-' @@ -527,18 +808,69 @@ class Extract(Step): watch.stop() -# ---- 3. lift +# ---- 4. lift EMITTED = re.compile(r'^emitted (\d+)/(\d+) chunks') DISCOVERED = re.compile(r'^(\w+): \d+ functions \(.*discovery') +CHEAT_PLACES = ('cheats', 'atmosphere/contents/0100F2C0115B6000/cheats', 'contents/0100F2C0115B6000/cheats', + '0100F2C0115B6000/cheats') + + +def cheat_files(ctx): + """The Atmosphere cheat files of the mods folder (/mods//cheats/*.txt and the Atmosphere places, as + tools/cheats.py finds them), for the lift key. All of them: the ones the build reads go by the build id of the + installed main, and the keys are computed before Extract runs (runner plan), when a fresh install has no main and an + update may still have another version's.""" + mods = ctx.dest / 'mods' + if not mods.is_dir(): + return [] + return sorted(f for mod in mods.iterdir() if mod.is_dir() for where in CHEAT_PLACES + for f in ((mod / where).glob('*.txt') if (mod / where).is_dir() else ())) + + +EXEFS_PLACES = ('exefs', 'atmosphere/contents/0100F2C0115B6000/exefs', 'contents/0100F2C0115B6000/exefs', + '0100F2C0115B6000/exefs') + + +def exefs_files(ctx): + """The ExeFS patch files of the mods folder (.ips and .pchtxt in each mod's exefs places and its + atmosphere/exefs_patches//, as tools/exefs_patches.py finds them), for the lift key.""" + mods = ctx.dest / 'mods' + if not mods.is_dir(): + return [] + out = [] + for mod in (m for m in mods.iterdir() if m.is_dir()): + folders = [mod / w for w in EXEFS_PLACES if (mod / w).is_dir()] + patches = mod / 'atmosphere' / 'exefs_patches' + folders += [d for d in patches.iterdir() if d.is_dir()] if patches.is_dir() else [] + out += [f for d in folders for f in d.iterdir() if f.is_file() and f.suffix.lower() in ('.ips', '.pchtxt')] + return sorted(out) + + +def exefs_status(ctx): + """The build's ExeFS patch status file (tools/exefs_patches.py --status), which runtime/mods.c reads.""" + return ctx.dest / 'build-cache' / 'exefs.status' + + +def cheats_status(ctx): + """The build's cheat status file (tools/cheats.py --status), which runtime/cheats.c reads: in build-cache, not in a + mod's own cheats folder, which the player (or TKMM's merge) may empty.""" + return ctx.dest / 'build-cache' / 'cheats.status' + + class Lift(Step): id = 'lift' transient = True def key(self, ctx): - return fingerprint('lift-1', ctx.keys['extract'], ctx.layout.recomp, ctx.layout.patches(game_version(ctx))) + # fingerprint takes a file's name and content only; the paths under mods/ go in too, because tools/cheats.py + # and tools/exefs_patches.py name each patch set after its mod folder, so a renamed folder needs a lift + files = cheat_files(ctx) + exefs_files(ctx) + return fingerprint('lift-4', ctx.keys['extract'], ctx.layout.recomp, ctx.layout.patches(game_version(ctx)), + ctx.layout.tools / 'cheats.py', ctx.layout.tools / 'exefs_patches.py', *files, + *(f.relative_to(ctx.dest / 'mods').as_posix() for f in files)) def current(self, ctx, record): gen = ctx.temp / 'gen' @@ -546,8 +878,10 @@ class Lift(Step): return (gen / 'layout.json').is_file() and stamp.is_file() and stamp.read_text().strip() == ctx.keys['lift'] def run(self, ctx): + # gen/ of the last run stays: gen.py rewrites only the chunks whose C changed (and removes the ones a smaller + # partition no longer has), so Ninja recompiles only those layout, rep, gen = ctx.layout, ctx.reporter, ctx.temp / 'gen' - remove(gen) + (gen / STAMP).unlink(missing_ok=True) rep.progress('lift', 'discover', force=True) def parse(line): @@ -555,28 +889,62 @@ class Lift(Step): if m: rep.progress('lift', 'emit', int(m.group(1)), int(m.group(2)), 'chunks') - code, log = run_tool(ctx, 'lift', layout.recomp / 'gen.py', - ['--exefs', ctx.dest / 'exefs', '--out', gen, '--patches', layout.patches(game_version(ctx))], parse) + # The code cheats of the mods' cheat files and the mods' ExeFS patch files become patch sets (tools/cheats.py, + # tools/exefs_patches.py); their status files tell the runtime what the build has. Both are the player's + # downloads, so files the tools refuse cost those cheats or patches, not the install: the game is built + # without them and the setup warns. + plain = layout.patches(game_version(ctx)) + stages = [('cheats.py', 'patches-cheats.txt', cheats_status(ctx), cheat_files(ctx), 'warn.cheats_skipped'), + ('exefs_patches.py', 'patches-exefs.txt', exefs_status(ctx), exefs_files(ctx), 'warn.exefs_skipped')] + patches, skipped = plain, {} + for tool, out, status, inputs, warn in stages: + status.unlink(missing_ok=True) # no status: the runtime treats every such file as not built + if not inputs: + continue + code, log = run_tool(ctx, 'lift', layout.tools / tool, + ['--exefs', ctx.dest / 'exefs', '--mods', ctx.dest / 'mods', '--patches', patches, + '--out', ctx.temp / out, '--status', status]) + if code: + status.unlink(missing_ok=True) + skipped[warn] = log + else: + patches = ctx.temp / out + args = ['--exefs', ctx.dest / 'exefs', '--out', gen] + code, log = run_tool(ctx, 'lift', layout.recomp / 'gen.py', args + ['--patches', patches], parse) + if code and patches != plain: + for tool, out, status, inputs, warn in stages: + if inputs and warn not in skipped: + status.unlink(missing_ok=True) + skipped[warn] = log + code, log = run_tool(ctx, 'lift', layout.recomp / 'gen.py', args + ['--patches', plain], parse) if code: raise InstallError('lift.failed', code=code, log=str(log)) + for warn, log in skipped.items(): + rep.warning(warn, log=str(log)) (gen / STAMP).write_text(ctx.keys['lift'] + '\n') return {} -# ---- 4. compile +# ---- 5. compile NINJA_LINE = re.compile(r'^\[(\d+)/(\d+)\] ') +def runtime_key(ctx): + """Fingerprint of what totk.exe is built from besides the lifted code: the runtime, the toolchain and the build + tools. Shader preparation depends on these, not on the code cheats a lift adds.""" + layout, cpu = ctx.layout, getattr(ctx, 'cpu', 'v3') + clang = subprocess.run([str(layout.clang), '--version'], capture_output=True, text=True).stdout + tools = [layout.tools / n for n in ('build_game.py', 'romfs_index.py', 'check_content.py', 'check_isa.py')] + return fingerprint('runtime-1', cpu, layout.runtime_lib_for(cpu), layout.runtime, layout.deps or 'system', + layout.sysroot or 'host', *tools, clang, *[d.name for d in layout.dlls]) + + class Compile(Step): id = 'compile' def key(self, ctx): - layout = ctx.layout - clang = subprocess.run([str(layout.clang), '--version'], capture_output=True, text=True).stdout - tools = [layout.tools / n for n in ('build_game.py', 'romfs_index.py', 'check_content.py')] - return fingerprint('compile-1', ctx.keys['lift'], layout.runtime, layout.runtime_lib, layout.deps or 'system', - layout.sysroot or 'host', *tools, clang, *[d.name for d in layout.dlls]) + return fingerprint('compile-3', ctx.keys['lift'], runtime_key(ctx)) def current(self, ctx, record): exe = ctx.dest / ('totk' + EXE) @@ -600,10 +968,12 @@ class Compile(Step): rep.progress('compile', 'link', force=True) rep.progress('compile', 'compile', force=True) + cpu = getattr(ctx, 'cpu', 'v3') + lib = layout.runtime_lib_for(cpu) code, log = run_tool(ctx, 'compile', layout.tools / 'build_game.py', [ - '--gen', ctx.temp / 'gen', '--out', build, '--toolchain', layout.llvm, + '--gen', ctx.temp / 'gen', '--out', build, '--toolchain', layout.llvm, '--cpu', cpu, *(['--deps', layout.deps] if layout.deps else []), *(['--sysroot', layout.sysroot] if layout.sysroot else []), - '--runtime', layout.runtime, '--runtime-lib', layout.runtime_lib, '--ninja', layout.ninja, + '--runtime', layout.runtime, *(['--runtime-lib', lib] if lib else ()), '--ninja', layout.ninja, '--version', version_tag(game_version(ctx)), '--expected', layout.expected(game_version(ctx)), '--exefs', dest / 'exefs', '--romfs', dest / 'romfs', '--profile', dest / 'profile'], parse) @@ -617,18 +987,23 @@ class Compile(Step): replace_file(build / 'romfs-index.bin', dest / 'romfs-index.bin') for dll in layout.dlls: replace_file(dll, dest / dll.name) - return {'exe_sha256': file_sha256(dest / exe.name)} + return {'exe_sha256': file_sha256(dest / exe.name), 'cpu': cpu} -# ---- 5. prepare +# ---- 6. prepare TRANSLATED = re.compile(r'gfx_shader: prepare: (\d+) of (\d+) pairs') +# runtime/vk_pipeline.c lb_warm_prepared: variants the driver crashed on in an earlier run (tools/prepare_shaders.py +# runs the driver step again after such a crash) +SKIPPED = re.compile(r'prepare-shaders: (\d+) variants skipped: the graphics driver crashed') # The game resolutions of another shape than 16:9 that runtime/ui/menu.cpp row_output_res lists: 21:9 and 32:9 monitors, -# and 16:10 screens (the Steam Deck's 1280x800, handhelds and laptops) +# 16:10 screens (the Steam Deck's 1280x800, handhelds and laptops) and 3:2 screens (Surface and Framework laptops and +# tablets) ASPECT_SIZES = ('2560x1080', '3440x1440', '3840x1600', '5120x2160', '3840x1080', '5120x1440', - '1280x800', '1680x1050', '1920x1200', '2560x1600') + '1280x800', '1680x1050', '1920x1200', '2560x1600', + '1920x1280', '2160x1440', '2256x1504', '2880x1920', '3000x2000') class Prepare(Step): @@ -638,8 +1013,10 @@ class Prepare(Step): layout = ctx.layout xlate_id = layout.xlate.with_suffix('.id') tools = [layout.tools / n for n in ('prepare_shaders.py', 'shader_census.py', 'make_romfs_patches.py', - 'pack_shader_cache.py', 'ultrawide_layout.py')] - return fingerprint('prepare-1', ctx.keys['compile'], layout.xlate, xlate_id, shader_list_text(layout), *tools) + 'pack_shader_cache.py', 'ultrawide_layout.py', 'button_icons.py')] + # not the compile key: a rebuild for the mods' code cheats keeps the prepared shaders + return fingerprint('prepare-2', ctx.keys['extract'], runtime_key(ctx), layout.xlate, xlate_id, + shader_list_text(layout), *tools) def current(self, ctx, record): dest = ctx.dest @@ -654,10 +1031,18 @@ class Prepare(Step): # Another translator: its cache keys differ, so the old translations would only take space. for name in ('shader-cache', 'shader-prepared.bin'): remove(dest / name) - replace_file(layout.xlate, xlate) + # totk.exe starts totk_xlate.exe as a child while it prepares shaders; a preparation that was stopped from + # outside leaves that child holding this file, and Windows then refuses to replace it (issue #174). + try: + replace_file(layout.xlate, xlate) + except PermissionError: + raise InstallError('prepare.xlate_in_use', path=str(xlate)) from None xlate_id = layout.xlate.with_suffix('.id') if xlate_id.is_file(): - replace_file(xlate_id, xlate.with_suffix('.id')) + try: + replace_file(xlate_id, xlate.with_suffix('.id')) + except PermissionError: + raise InstallError('prepare.xlate_in_use', path=str(xlate.with_suffix('.id'))) from None else: remove(xlate.with_suffix('.id')) @@ -678,26 +1063,37 @@ class Prepare(Step): dest / f'romfs-patches-{size}', '--aspect', size]) if code: raise InstallError('prepare.romfs_patches_failed', code=code, log=str(log)) - # romfs-patches-xbox: the same with the A/B and X/Y prompts swapped, for pad_layout = xbox_prompts (written on - # every install, so the menu choice works without another run of the setup). Optional: when it fails, the game - # keeps the Switch prompts and the menu says the files are missing, so the setup warns and goes on. - remove(dest / 'romfs-patches-xbox') - code, log = run_tool(ctx, 'prepare-xbox', layout.tools / 'make_romfs_patches.py', - ['--version', version_tag(game_version(ctx)), '--romfs', dest / 'romfs', '--out', - dest / 'romfs-patches-xbox', '--xbox-prompts']) - if code: - remove(dest / 'romfs-patches-xbox') - rep.warning('warn.xbox_prompts_failed', code=code, log=str(log)) + # The same with Xbox button icons in the prompts, for prompts = xbox (make_romfs_patches.py --prompts): + # romfs-patches-xbox for the Nintendo pad layout, romfs-patches-xbox-label for xbox and auto (runtime/input_map.c + # input_prompts_folder). Both are written on every install, so the menu choices work without another run of + # the setup. Optional: when one fails, the game keeps the Switch prompts with that layout and the menu says the + # files are missing, so the setup warns once and goes on. + failed = None + for style in ('xbox', 'xbox-label'): + out = dest / f'romfs-patches-{style}' + remove(out) + code, log = run_tool(ctx, f'prepare-{style}', layout.tools / 'make_romfs_patches.py', + ['--version', version_tag(game_version(ctx)), '--romfs', dest / 'romfs', '--out', out, + '--prompts', style]) + if code: + remove(out) + failed = failed or (code, log) + if failed: + rep.warning('warn.xbox_prompts_failed', code=failed[0], log=str(failed[1])) shader_list = layout.shader_list if not shader_list: shader_list = ctx.temp / 'shader_list.tsv' shader_list.write_text(shader_list_text(layout), encoding='utf-8', newline='\n') + skipped = [0] + def parse(line): m = TRANSLATED.search(line) if m: rep.progress('prepare', 'translate', int(m.group(1)), int(m.group(2)), 'shaders') + elif SKIPPED.search(line): + skipped[0] = int(SKIPPED.search(line).group(1)) elif 'listed variants' in line: rep.progress('prepare', 'translate', force=True) elif 'gfx_shader: prepare:' in line and 'translated' in line and 'programs' in line: @@ -709,6 +1105,8 @@ class Prepare(Step): dest / 'exefs', '--list', shader_list], parse) if code or not (dest / 'shader-prepared.bin').is_file(): raise InstallError('prepare.shaders_failed', code=code, log=str(log)) + if skipped[0]: + rep.warning('warn.shaders_skipped', count=skipped[0], log=str(log)) # One shader-cache.pack instead of four files per shader (~117,000): totk.exe reads the pack first. rep.progress('prepare', 'pack', force=True) @@ -718,7 +1116,82 @@ class Prepare(Step): return {'xlate_sha256': file_sha256(xlate)} -# ---- 6. clean up +# ---- 7. NVIDIA DLSS + +# NVIDIA's DLSS library, which NGX loads from the folder of totk.exe (runtime/vk_dlaa.c). The setup does not ship it: +# with --dlss it downloads NVIDIA's own copy from the DLSS SDK repository (NVIDIA RTX SDK license) at a pinned tag and +# checks its hash. The game runs without it; dlaa and the DLSS modes then do nothing. +DLSS_DLL = 'nvngx_dlss.dll' +DLSS_VERSION = '310.9.1' +DLSS_URL = f'https://raw.githubusercontent.com/NVIDIA/DLSS/v{DLSS_VERSION}/lib/Windows_x86_64/rel/{DLSS_DLL}' +DLSS_SIZE = 58956912 +DLSS_SHA256 = '3975567b8943c53acce397f2b72380092f84f162d00b0d2c7d08a1025c563983' +DLSS_LICENSE = 'https://github.com/NVIDIA/DLSS/blob/main/LICENSE.txt' + + +def nvidia_rtx(host): + """An NVIDIA RTX GPU among the host check's Vulkan devices: the GPUs DLSS runs on.""" + return any('NVIDIA' in d['name'].upper() and 'RTX' in d['name'].upper() for d in (host or {}).get('devices', [])) + + +class Dlss(Step): + """With --dlss on Windows and an NVIDIA RTX GPU: nvngx_dlss.dll next to totk.exe. A DLSS library the player put + there (another version) is kept. A failed download warns and is tried again on the next run; the game runs + without it.""" + + id = 'dlss' + + def key(self, ctx): + if not getattr(ctx.options, 'dlss', False): + return fingerprint('dlss-1', 'off') + return fingerprint('dlss-1', DLSS_VERSION, DLSS_SHA256) + + def current(self, ctx, record): + if record.get('failed'): + return False + return not record.get('installed') or (ctx.dest / DLSS_DLL).is_file() + + def run(self, ctx): + if not getattr(ctx.options, 'dlss', False): + return {} + if os.name != 'nt': + return {'skipped': 'not_windows'} + if not nvidia_rtx(getattr(ctx, 'host', None)): + # The box is ticked by default, so most players without an RTX GPU never chose it: a log line, no warning + ctx.reporter.log('dlss: no NVIDIA RTX GPU among the Vulkan devices, nothing downloaded') + return {'skipped': 'no_rtx'} + rep, target = ctx.reporter, ctx.dest / DLSS_DLL + if target.is_file(): + if file_sha256(target) == DLSS_SHA256: + return {'installed': DLSS_VERSION} + rep.warning('warn.dlss_kept', path=str(target)) + return {'kept': True} + part = ctx.temp / (DLSS_DLL + '.part') + rep.progress('dlss', 'download', 0, DLSS_SIZE, 'bytes', force=True) + rep.log(f'dlss: {DLSS_URL} (license {DLSS_LICENSE})') + try: + part.parent.mkdir(parents=True, exist_ok=True) + h, done = hashlib.sha256(), 0 + with urllib.request.urlopen(DLSS_URL, timeout=60) as r, open(part, 'wb') as f: + while block := r.read(1 << 20): + f.write(block) + h.update(block) + done += len(block) + rep.progress('dlss', 'download', min(done, DLSS_SIZE), DLSS_SIZE, 'bytes') + if done != DLSS_SIZE or h.hexdigest() != DLSS_SHA256: + raise OSError(f'{done} bytes, SHA-256 {h.hexdigest()[:16]}, not the expected file') + replace_file(part, target) + remove(part) + # urllib's errors, timeouts and a bad file; a connection cut mid-file raises http.client.IncompleteRead, + # which is not an OSError + except (OSError, http.client.HTTPException) as e: + remove(part) + rep.warning('warn.dlss_download_failed', url=DLSS_URL, detail=str(e)[:300]) + return {'failed': True} + return {'installed': DLSS_VERSION} + + +# ---- 8. clean up class Cleanup(Step): id = 'cleanup' @@ -736,12 +1209,40 @@ class Cleanup(Step): rep.progress('cleanup', 'temp', force=True) for stale in dest.glob('update-merge-*'): remove(stale) - for name in ('gen', 'build', 'check', 'shader_list.tsv'): + for name in ('check', 'unpacked', 'shader_list.tsv', 'shared-fonts'): # gen/ and build/ stay for the next rebuild remove(ctx.temp / name) romfs_problem(dest / 'romfs', expected_update(ctx)) + write_rebuild_command(ctx) return {} +# Puts the release folder (the argument after -c) on sys.path and runs installer.gui with the rest. `-m installer.gui` +# finds the package only through the release Python's ._pth: the Linux release's Python and the dev-mode embeddable one +# lack that entry, and totk.exe starts the command in the install folder. +REBUILD_BOOT = ('import runpy, sys; sys.path.insert(0, sys.argv.pop(1)); ' + "runpy.run_module('installer.gui', run_name='__main__', alter_sys=True)") + + +def rebuild_command(ctx): + """The command that starts the setup window in its rebuild mode on this install folder (installer/gui.py + --rebuild): pythonw beside the setup's Python on Windows, so no console opens. In a repository that is the Python + running this setup: the tools' embeddable Python there has no tkinter.""" + layout = ctx.layout + python = Path(sys.executable) if layout.dev else layout.python + if os.name == 'nt' and python.with_name('pythonw.exe').is_file(): + python = python.with_name('pythonw.exe') + return [str(python), '-c', REBUILD_BOOT, str(layout.root), '--root', str(layout.root), '--rebuild', str(ctx.dest)] + + +def write_rebuild_command(ctx): + """build-cache/rebuild.txt in the install folder: rebuild_command, one argument per line, for totk.exe.""" + cache = ctx.dest / 'build-cache' + cache.mkdir(exist_ok=True) + tmp = cache / 'rebuild.txt.tmp' + tmp.write_text('\n'.join(rebuild_command(ctx)) + '\n', encoding='utf-8', newline='\n') + os.replace(tmp, cache / 'rebuild.txt') + + def romfs_problem(romfs, expected): """Raise cleanup.romfs_unreadable unless romfs/ still holds the verified image. The game opens romfs.bin at start; an install whose romfs/ went empty and unreadable after the extraction verified it (a folder Windows was still @@ -758,4 +1259,4 @@ def romfs_problem(romfs, expected): def all_steps(): - return [Check(), Extract(), Lift(), Compile(), Prepare(), Cleanup()] + return [Check(), Fonts(), Extract(), Lift(), Compile(), Prepare(), Dlss(), Cleanup()] diff --git a/installer/text/en.json b/installer/text/en.json index c792391e..418ce82c 100644 --- a/installer/text/en.json +++ b/installer/text/en.json @@ -1,16 +1,22 @@ { "step.check": "Checking the computer and the game files", + "step.fonts": "Extracting the system fonts", "step.extract": "Extracting the game", "step.lift": "Translating the game code to C", "step.compile": "Compiling {game_exe}", "step.prepare": "Preparing the shaders", + "step.dlss": "Downloading NVIDIA DLSS", "step.cleanup": "Cleaning up", "phase.check.host": "Checking the processor and the graphics card", "phase.check.verify": "Checking the files already installed", "phase.check.verify_romfs": "Checking the installed game data", "phase.check.inputs": "Opening the game files with the keys", + "phase.check.unpack_base": "Unpacking the compressed base game", + "phase.check.unpack_update": "Unpacking the compressed update", "phase.check.versions": "Checking the game versions", + "phase.fonts.extract": "Extracting the shared fonts from the firmware", + "phase.fonts.copy": "Copying the shared fonts into the install folder", "phase.extract.hash_base": "Computing the checksum of the base game", "phase.extract.hash_update": "Computing the checksum of the update", "phase.extract.exefs": "Extracting the game code", @@ -27,6 +33,7 @@ "phase.prepare.translate": "Translating the shaders", "phase.prepare.libraries": "Building the shaders for the graphics driver", "phase.prepare.pack": "Packing the shader cache into one file", + "phase.dlss.download": "Downloading nvngx_dlss.dll from NVIDIA's DLSS SDK", "phase.cleanup.ini": "Writing totk.ini", "phase.cleanup.temp": "Removing temporary files", @@ -44,36 +51,47 @@ "check.toolchain_broken": "The bundled compiler does not work: {detail}", "check.glibc_old": "This system's C library is glibc {version}; the setup's compiler needs {need} or later (Ubuntu 22.04, Debian 12, Fedora 35, SteamOS 3 and newer have it). Updating the system fixes this.", "check.host_probe_failed": "The computer check did not run (exit code {code}).", - "check.cpu_no_avx2": "This processor lacks AVX2 (x86-64-v3), which the game needs. Intel Haswell (2013) or AMD Excavator / Zen and newer have it.", + "check.cpu_too_old": "This processor lacks x86-64-v2 (SSE4.2, POPCNT and CMPXCHG16B), the oldest level the game is built for. Intel Nehalem (2008) or AMD Bulldozer (2011) and newer have it.", "check.vulkan_missing": "No Vulkan driver is installed. Install the latest driver for your graphics card.", "check.vulkan_no_device": "Vulkan finds no graphics card. Install the latest driver for your graphics card.", "check.vulkan_too_old": "The game needs a graphics card with Vulkan 1.3; found: {devices}. Update the graphics driver.", - "check.inputs_needed": "The game files are needed: choose the base game, the 1.1.1 or 1.2.1 or 1.4.3 update and prod.keys.", + "check.no_display": "Preparing the shaders needs a display. Run the setup from the desktop session, or export DISPLAY=:0 (on a Steam Deck in Game Mode also XDG_RUNTIME_DIR=/run/user/1000).", + "check.inputs_needed": "The game files are needed: choose the base game, the 1.1.0 or 1.2.1 or 1.4.3 update and prod.keys.", "check.input_missing": "File not found ({input}): {path}", "check.keys_unreadable": "The keys file cannot be read. Choose a prod.keys file.", "check.keys_no_header_key": "The keys file has no header_key. Dump prod.keys from your console again.", "check.keys_wrong": "The keys cannot open the {input} file. Use the prod.keys of your console.", "check.keys_too_old": "The keys file is too old for the {input} file (needs key generation {generation}). Dump prod.keys from a console with newer firmware.", - "check.not_game_file": "The {input} file is not an XCI or NSP game file.", + "check.not_game_file": "The {input} file is not an XCI, NSP, XCZ or NSZ game file.", "check.file_unreadable": "The {input} file cannot be read: {detail}", + "check.unpack_failed": "The compressed {input} file could not be unpacked: {detail}", "check.ticket_personalized": "The {input} file carries a personalized ticket; a common ticket is needed.", "check.ticket_missing": "The {input} file lacks the ticket its content needs.", "check.base_not_totk": "The base game file is not The Legend of Zelda: Tears of the Kingdom ({detail}).", - "check.update_not_nsp": "The update must be an NSP file.", - "check.update_is_base": "The update file holds a base game. Choose the 1.1.1 or 1.2.1 or 1.4.3 update NSP.", + "check.base_is_update": "The base game file is an update. Choose the base game NSP or XCI (version 1.0.0) here and this file as the update.", + "check.base_combined": "The base game file holds several packages (base game, update or DLC in one file). Choose a base game dump that contains only the game.", + "check.update_not_nsp": "The update must be an NSP or NSZ file.", + "check.update_is_base": "The update file holds a base game. Choose the 1.1.0 or 1.2.1 or 1.4.3 update NSP.", "check.update_not_totk": "The update file is not a Tears of the Kingdom update ({detail}).", "check.exefs_failed": "The game code of the {input} file failed its checks; see {log}.", "check.input_damaged": "The {input} file is damaged or incomplete: {path} ({detail}). Copy that file again from its source, or dump it again, then run the setup again.", "check.base_wrong_version": "The base game is not version 1.0.0 ({modules} differ).", "check.update_wrong_version": "The update is version {found}; this release takes {wanted}.", "check.disk_space": "Not enough free space for {path}: {need_gb} GB needed, {free_gb} GB free.", + "check.firmware_missing": "The firmware folder {path} does not exist. Choose the firmware's Contents/registered folder.", + "check.firmware_empty": "The firmware folder {path} holds no NCA files. Choose the firmware's Contents/registered folder.", + "check.fonts_keys_needed": "The shared fonts for this language are not installed yet; choose prod.keys so the setup can extract them from the firmware.", "extract.exefs_mismatch": "The extracted game code does not match version {version} ({modules}).", "extract.romfs_merge_failed": "Applying the update to the game data failed: {detail}. Details: {log}", "extract.romfs_mismatch": "The extracted game data does not match version {version} (size {size}, checksum {sha256}...).", + "fonts.failed": "Extracting the shared fonts failed (exit code {code}): {detail}. Details: {log}", + "fonts.incomplete": "The shared font extraction did not write every file ({files}).", + "fonts.copy_failed": "The shared fonts cannot be copied into {path}: {detail}", "lift.failed": "Translating the game code failed (exit code {code}). Details: {log}", "compile.failed": "Compiling {game_exe} failed (exit code {code}). Details: {log}", "compile.exe_in_use": "{path} is in use. Close the game and run the installer again.", + "prepare.xlate_in_use": "{path} is in use. Close the game, end any totk_xlate process left from an earlier run, then run the installer again.", "prepare.shader_list_missing": "The shader list is missing.", "prepare.romfs_patches_failed": "Writing the game data patches failed (exit code {code}). Details: {log}", "prepare.shaders_failed": "Preparing the shaders failed (exit code {code}). Details: {log}", @@ -81,28 +99,40 @@ "prepare.pack_failed": "Packing the shader cache failed (exit code {code}). Details: {log}", "cleanup.ini_failed": "totk.ini cannot be written: {detail}", - "warn.lang_needs_fonts": "Text in {lang} needs the Switch system fonts, which the installer does not extract; some text may not show.", + "warn.lang_needs_fonts": "Text in {lang} needs the Switch system fonts, and none were given. The game will not start in this language until they are added. Choose the firmware folder (Contents/registered), or see \"Switch system fonts\" in docs/installer.md.", "warn.npdm_differs": "main.npdm differs from the reference {version} dump; only the code modules are used.", + "warn.cheats_skipped": "The cheats of the mods folder that change the game's code could not be built; the game installs without them. Details: {log}", + "warn.exefs_skipped": "The mods' ExeFS patches (.ips, .pchtxt) could not be built; the game installs without them. Details: {log}", "warn.xbox_prompts_failed": "The Xbox button prompts could not be prepared (exit code {code}); the game keeps the Switch prompts. Details: {log}", + "warn.shaders_skipped": "The graphics driver crashed while building some shaders, so {count} were left out of the preparation. The game builds them when it first needs them, and the driver may crash there too; a driver update may fix it. Details: {log}", "warn.kernel_old": "Linux {version} lacks the write tracking the game relies on (6.7 or later); the game may run very slowly. Updating the system fixes this.", + "warn.dlss_kept": "A DLSS library is already in the install folder ({path}); the setup keeps it.", + "warn.dlss_download_failed": "NVIDIA DLSS could not be downloaded from {url}: {detail}. The game runs without it; the next setup run tries again.", "ui.title": "TotK Setup", + "ui.rebuild_title": "Rebuilding the game for your mods", + "ui.start_game": "Start the game", "ui.version": "version {version}", "ui.intro": "Builds a playable Tears of the Kingdom folder from your own game files. This takes a while and runs once.", "ui.notice": "The folder this setup builds contains the game. It is for your own use only; do not share or upload it.", "ui.keys_private": "The keys file is read in place. Its contents are never shown, copied or stored.", - "ui.base": "Base game (NSP or XCI)", - "ui.update": "Update 1.1.1 or 1.2.1 or 1.4.3 (NSP)", + "ui.base": "Base game (NSP, XCI, NSZ or XCZ)", + "ui.update": "Update 1.1.0 or 1.2.1 or 1.4.3 (NSP or NSZ)", "ui.keys": "prod.keys", + "ui.firmware": "Firmware folder (optional)", "ui.game_language": "Game language", "ui.dest": "Install folder", "ui.browse": "Browse...", "ui.pick_game": "Choose the base game file", - "ui.pick_update": "Choose the 1.1.1 or 1.2.1 or 1.4.3 update file", + "ui.pick_update": "Choose the 1.1.0 or 1.2.1 or 1.4.3 update file", "ui.pick_keys": "Choose prod.keys", + "ui.pick_firmware": "Choose the Switch firmware folder (Contents/registered)", "ui.pick_dest": "Choose the install folder", - "ui.type_game": "Game files (NSP, XCI)", - "ui.type_nsp": "NSP files", + "ui.firmware_help": "For Chinese, Korean and Traditional Chinese text: the firmware's Contents/registered folder, for example from Ryujinx or Yuzu. The setup copies the Switch system fonts from it into the install folder.", + "ui.dlss": "Download NVIDIA DLSS (nvngx_dlss.dll) for NVIDIA RTX graphics cards", + "ui.dlss_help": "From NVIDIA's DLSS SDK on GitHub, under NVIDIA's RTX SDK license (github.com/NVIDIA/DLSS). It makes the dlaa and DLSS settings work; without it they do nothing.", + "ui.compressed_help": "Compressed NSZ and XCZ files work as they are: the setup unpacks them by itself (until the game data is extracted, that needs as much free space as the unpacked files take).", + "ui.type_game": "Game files (NSP, XCI, NSZ, XCZ)", "ui.type_keys": "Key files (*.keys)", "ui.type_all": "All files", "ui.start": "Start", @@ -116,12 +146,13 @@ "ui.update_mode": "This folder holds an earlier installation. The setup continues or updates it; the game files are asked for only if they are needed again.", "ui.need_dest": "Choose the install folder.", "ui.need_base": "Choose the base game file.", - "ui.need_update": "Choose the 1.1.1 or 1.2.1 or 1.4.3 update file.", + "ui.need_update": "Choose the 1.1.0 or 1.2.1 or 1.4.3 update file.", "ui.need_keys": "Choose the prod.keys file.", "ui.need_lang": "Choose the game language.", "ui.missing_base": "The base game file does not exist.", "ui.missing_update": "The update file does not exist.", "ui.missing_keys": "The keys file does not exist.", + "ui.missing_firmware": "The firmware folder does not exist.", "ui.running_install": "Installing", "ui.running_resume": "Continuing the earlier setup", "ui.running_update": "Updating the installation", diff --git a/installer/text/pt-BR.json b/installer/text/pt-BR.json new file mode 100644 index 00000000..8839ac2b --- /dev/null +++ b/installer/text/pt-BR.json @@ -0,0 +1,208 @@ +{ + "step.check": "Verificando o computador e os arquivos do jogo", + "step.fonts": "Extraindo as fontes do sistema", + "step.extract": "Extraindo o jogo", + "step.lift": "Traduzindo o código do jogo para C", + "step.compile": "Compilando o {game_exe}", + "step.prepare": "Preparando os shaders", + "step.dlss": "Baixando o NVIDIA DLSS", + "step.cleanup": "Finalizando", + + "phase.check.host": "Verificando o processador e a placa de vídeo", + "phase.check.verify": "Verificando os arquivos já instalados", + "phase.check.verify_romfs": "Verificando os dados do jogo instalados", + "phase.check.inputs": "Abrindo os arquivos do jogo com as chaves", + "phase.check.unpack_base": "Descompactando o jogo base compactado", + "phase.check.unpack_update": "Descompactando a atualização compactada", + "phase.check.versions": "Verificando as versões do jogo", + "phase.fonts.extract": "Extraindo as fontes compartilhadas do firmware", + "phase.fonts.copy": "Copiando as fontes compartilhadas para a pasta de instalação", + "phase.extract.hash_base": "Calculando o checksum do jogo base", + "phase.extract.hash_update": "Calculando o checksum da atualização", + "phase.extract.exefs": "Extraindo o código do jogo", + "phase.extract.copy": "Copiando os dados do jogo", + "phase.extract.merge": "Aplicando a atualização aos dados do jogo", + "phase.extract.diagnose": "Procurando o arquivo do jogo danificado", + "phase.extract.index": "Verificando os dados do jogo", + "phase.lift.discover": "Encontrando as funções", + "phase.lift.emit": "Gerando o código C", + "phase.compile.compile": "Compilando", + "phase.compile.link": "Ligando o {game_exe}", + "phase.prepare.romfs_patches": "Gravando os patches de 60 fps nos dados do jogo", + "phase.prepare.seed": "Lendo os shaders dos dados do jogo", + "phase.prepare.translate": "Traduzindo os shaders", + "phase.prepare.libraries": "Montando os shaders para o driver de vídeo", + "phase.prepare.pack": "Juntando o cache de shaders num único arquivo", + "phase.dlss.download": "Baixando o nvngx_dlss.dll do DLSS SDK da NVIDIA", + "phase.cleanup.ini": "Gravando o totk.ini", + "phase.cleanup.temp": "Removendo os arquivos temporários", + + "cli.usage": "Linha de comando inválida: {detail}", + "internal": "Erro inesperado ({exception}): {detail}", + "state.unreadable": "Não foi possível ler {path}. Escolha outra pasta ou apague esse arquivo.", + "state.newer_format": "{path} foi gravado por um instalador mais novo. Use esse instalador ou outra pasta.", + "state.output_changed": "Os arquivos instalados da etapa '{step}' mudaram desde que foram criados; essa etapa será executada de novo.", + + "check.release_incomplete": "A pasta do instalador está incompleta; faltam: {paths}. Extraia o pacote de novo.", + "check.dest_not_folder": "O local de instalação {path} é um arquivo, não uma pasta.", + "check.dest_not_empty": "A pasta de instalação {path} não está vazia e não tem uma instalação anterior. Escolha uma pasta vazia.", + "check.lang_missing": "Escolha o idioma do jogo.", + "check.lang_unknown": "Idioma desconhecido '{lang}'. Conhecidos: {known}.", + "check.toolchain_broken": "O compilador incluído não funciona: {detail}", + "check.glibc_old": "A biblioteca C deste sistema é a glibc {version}; o compilador da instalação precisa da {need} ou mais nova (Ubuntu 22.04, Debian 12, Fedora 35, SteamOS 3 e mais novos têm). Atualizar o sistema resolve.", + "check.host_probe_failed": "A verificação do computador não rodou (código de saída {code}).", + "check.cpu_too_old": "Este processador não tem x86-64-v2 (SSE4.2, POPCNT e CMPXCHG16B), o nível mais antigo para o qual o jogo é compilado. Intel Nehalem (2008) ou AMD Bulldozer (2011) e mais novos têm.", + "check.vulkan_missing": "Nenhum driver Vulkan está instalado. Instale o driver mais recente da sua placa de vídeo.", + "check.vulkan_no_device": "O Vulkan não encontra nenhuma placa de vídeo. Instale o driver mais recente da sua placa de vídeo.", + "check.vulkan_too_old": "O jogo precisa de uma placa de vídeo com Vulkan 1.3; encontrado: {devices}. Atualize o driver de vídeo.", + "check.no_display": "Preparar os shaders exige uma tela. Execute a instalação na sessão da área de trabalho ou use export DISPLAY=:0 (no Steam Deck em Game Mode, também XDG_RUNTIME_DIR=/run/user/1000).", + "check.inputs_needed": "Os arquivos do jogo são necessários: escolha o jogo base, a atualização 1.1.0, 1.2.1 ou 1.4.3 e o prod.keys.", + "check.input_missing": "Arquivo não encontrado ({input}): {path}", + "check.keys_unreadable": "Não foi possível ler o arquivo de chaves. Escolha um arquivo prod.keys.", + "check.keys_no_header_key": "O arquivo de chaves não tem header_key. Extraia o prod.keys do seu console de novo.", + "check.keys_wrong": "As chaves não abrem o arquivo de {input}. Use o prod.keys do seu console.", + "check.keys_too_old": "O arquivo de chaves é antigo demais para o arquivo de {input} (precisa da geração de chaves {generation}). Extraia o prod.keys de um console com firmware mais novo.", + "check.not_game_file": "O arquivo de {input} não é um arquivo de jogo XCI, NSP, XCZ ou NSZ.", + "check.file_unreadable": "Não foi possível ler o arquivo de {input}: {detail}", + "check.unpack_failed": "Não foi possível descompactar o arquivo compactado de {input}: {detail}", + "check.ticket_personalized": "O arquivo de {input} tem um ticket personalizado; é preciso um ticket comum.", + "check.ticket_missing": "O arquivo de {input} não tem o ticket de que o conteúdo precisa.", + "check.base_not_totk": "O arquivo do jogo base não é The Legend of Zelda: Tears of the Kingdom ({detail}).", + "check.base_is_update": "O arquivo do jogo base é uma atualização. Escolha aqui o NSP ou XCI do jogo base (versão 1.0.0) e este arquivo como a atualização.", + "check.base_combined": "O arquivo do jogo base contém vários pacotes (jogo base, atualização ou DLC em um só arquivo). Escolha um dump do jogo base que contenha só o jogo.", + "check.update_not_nsp": "A atualização precisa ser um arquivo NSP ou NSZ.", + "check.update_is_base": "O arquivo de atualização contém um jogo base. Escolha o NSP da atualização 1.1.0, 1.2.1 ou 1.4.3.", + "check.update_not_totk": "O arquivo de atualização não é uma atualização de Tears of the Kingdom ({detail}).", + "check.exefs_failed": "O código do jogo no arquivo de {input} falhou nas verificações; veja {log}.", + "check.input_damaged": "O arquivo de {input} está danificado ou incompleto: {path} ({detail}). Copie esse arquivo de novo da origem, ou extraia-o de novo, e então execute a instalação outra vez.", + "check.base_wrong_version": "O jogo base não é a versão 1.0.0 ({modules} diferem).", + "check.update_wrong_version": "A atualização é a versão {found}; esta versão do instalador aceita {wanted}.", + "check.disk_space": "Espaço livre insuficiente em {path}: são necessários {need_gb} GB, há {free_gb} GB livres.", + "check.firmware_missing": "A pasta de firmware {path} não existe. Escolha a pasta Contents/registered do firmware.", + "check.firmware_empty": "A pasta de firmware {path} não contém arquivos NCA. Escolha a pasta Contents/registered do firmware.", + "check.fonts_keys_needed": "As fontes compartilhadas deste idioma ainda não estão instaladas; escolha o prod.keys para que a instalação possa extraí-las do firmware.", + + "extract.exefs_mismatch": "O código do jogo extraído não corresponde à versão {version} ({modules}).", + "extract.romfs_merge_failed": "Falha ao aplicar a atualização aos dados do jogo: {detail}. Detalhes: {log}", + "extract.romfs_mismatch": "Os dados do jogo extraídos não correspondem à versão {version} (tamanho {size}, checksum {sha256}...).", + "fonts.failed": "A extração das fontes compartilhadas falhou (código de saída {code}): {detail}. Detalhes: {log}", + "fonts.incomplete": "A extração das fontes compartilhadas não gravou todos os arquivos ({files}).", + "fonts.copy_failed": "Não foi possível copiar as fontes compartilhadas para {path}: {detail}", + "lift.failed": "Falha ao traduzir o código do jogo (código de saída {code}). Detalhes: {log}", + "compile.failed": "Falha ao compilar o {game_exe} (código de saída {code}). Detalhes: {log}", + "compile.exe_in_use": "{path} está em uso. Feche o jogo e execute o instalador de novo.", + "prepare.xlate_in_use": "{path} está em uso. Feche o jogo, encerre qualquer processo totk_xlate que tenha sobrado de uma execução anterior e execute o instalador de novo.", + "prepare.shader_list_missing": "A lista de shaders está faltando.", + "prepare.romfs_patches_failed": "Falha ao gravar os patches nos dados do jogo (código de saída {code}). Detalhes: {log}", + "prepare.shaders_failed": "Falha ao preparar os shaders (código de saída {code}). Detalhes: {log}", + "cleanup.romfs_unreadable": "Os dados do jogo em {path} estão faltando ou não podem ser lidos depois da instalação. Reinicie o Windows e execute o instalador de novo na mesma pasta: ele extrai os dados do jogo outra vez e mantém o resto.", + "prepare.pack_failed": "Falha ao juntar o cache de shaders (código de saída {code}). Detalhes: {log}", + "cleanup.ini_failed": "Não foi possível gravar o totk.ini: {detail}", + + "warn.lang_needs_fonts": "Textos em {lang} precisam das fontes de sistema do Switch, e nenhuma foi informada. O jogo não inicia neste idioma até que sejam adicionadas. Escolha a pasta do firmware (Contents/registered) ou veja \"Switch system fonts\" em docs/installer.md.", + "warn.cheats_skipped": "Não foi possível compilar os cheats da pasta de mods que alteram o código do jogo; o jogo é instalado sem eles. Detalhes: {log}", + "warn.exefs_skipped": "Não foi possível compilar os patches ExeFS dos mods (.ips, .pchtxt); o jogo é instalado sem eles. Detalhes: {log}", + "warn.npdm_differs": "O main.npdm difere do dump de referência da versão {version}; só os módulos de código são usados.", + "warn.xbox_prompts_failed": "Não foi possível preparar os ícones de botão do Xbox (código de saída {code}); o jogo mantém os ícones do Switch. Detalhes: {log}", + "warn.shaders_skipped": "O driver gráfico travou ao compilar alguns shaders, então {count} ficaram fora da preparação. O jogo os compila quando precisar deles pela primeira vez, e o driver pode travar ali também; uma atualização do driver pode resolver. Detalhes: {log}", + "warn.kernel_old": "O Linux {version} não tem o rastreamento de escrita de que o jogo depende (6.7 ou mais novo); o jogo pode rodar muito devagar. Atualizar o sistema resolve.", + "warn.dlss_kept": "Já existe uma biblioteca DLSS na pasta de instalação ({path}); a instalação a mantém.", + "warn.dlss_download_failed": "Não foi possível baixar o NVIDIA DLSS de {url}: {detail}. O jogo funciona sem ele; a próxima execução da instalação tenta de novo.", + + "ui.title": "Instalação do TotK", + "ui.rebuild_title": "Recompilando o jogo para os seus mods", + "ui.start_game": "Iniciar o jogo", + "ui.version": "versão {version}", + "ui.intro": "Monta uma pasta jogável de Tears of the Kingdom a partir dos seus próprios arquivos do jogo. Isso demora um pouco e só precisa ser feito uma vez.", + "ui.notice": "A pasta que esta instalação monta contém o jogo. Ela é só para seu uso pessoal; não compartilhe nem envie para a internet.", + "ui.keys_private": "O arquivo de chaves é lido no lugar. O conteúdo dele nunca é mostrado, copiado nem guardado.", + "ui.base": "Jogo base (NSP, XCI, NSZ ou XCZ)", + "ui.update": "Atualização 1.1.0, 1.2.1 ou 1.4.3 (NSP ou NSZ)", + "ui.keys": "prod.keys", + "ui.firmware": "Pasta de firmware (opcional)", + "ui.game_language": "Idioma do jogo", + "ui.dest": "Pasta de instalação", + "ui.browse": "Procurar...", + "ui.pick_game": "Escolha o arquivo do jogo base", + "ui.pick_update": "Escolha o arquivo da atualização 1.1.0, 1.2.1 ou 1.4.3", + "ui.pick_keys": "Escolha o prod.keys", + "ui.pick_firmware": "Escolha a pasta de firmware do Switch (Contents/registered)", + "ui.pick_dest": "Escolha a pasta de instalação", + "ui.firmware_help": "Para textos em chinês, coreano e chinês tradicional: a pasta Contents/registered do firmware, por exemplo do Ryujinx ou do Yuzu. A instalação copia dela as fontes do sistema do Switch para a pasta de instalação.", + "ui.dlss": "Baixar o NVIDIA DLSS (nvngx_dlss.dll) para placas de vídeo NVIDIA RTX", + "ui.dlss_help": "Do DLSS SDK da NVIDIA no GitHub, sob a licença NVIDIA RTX SDK (github.com/NVIDIA/DLSS). Sem ele as opções dlaa e DLSS não fazem nada.", + "ui.compressed_help": "Arquivos compactados NSZ e XCZ funcionam do jeito que estão: a instalação os descompacta sozinha (para isso, até os dados do jogo serem extraídos, ela precisa de tanto espaço livre quanto ocupam os arquivos descompactados).", + "ui.type_game": "Arquivos de jogo (NSP, XCI, NSZ, XCZ)", + "ui.type_keys": "Arquivos de chaves (*.keys)", + "ui.type_all": "Todos os arquivos", + "ui.start": "Iniciar", + "ui.cancel": "Cancelar", + "ui.cancelling": "Cancelando...", + "ui.show_log": "Mostrar log", + "ui.hide_log": "Ocultar log", + "ui.close": "Fechar", + "ui.back": "Voltar", + "ui.open_folder": "Abrir pasta", + "ui.update_mode": "Esta pasta tem uma instalação anterior. A instalação vai continuar ou atualizá-la; os arquivos do jogo só são pedidos se forem necessários de novo.", + "ui.need_dest": "Escolha a pasta de instalação.", + "ui.need_base": "Escolha o arquivo do jogo base.", + "ui.need_update": "Escolha o arquivo da atualização 1.1.0, 1.2.1 ou 1.4.3.", + "ui.need_keys": "Escolha o arquivo prod.keys.", + "ui.need_lang": "Escolha o idioma do jogo.", + "ui.missing_base": "O arquivo do jogo base não existe.", + "ui.missing_update": "O arquivo de atualização não existe.", + "ui.missing_keys": "O arquivo de chaves não existe.", + "ui.missing_firmware": "A pasta de firmware não existe.", + "ui.running_install": "Instalando", + "ui.running_resume": "Continuando a instalação anterior", + "ui.running_update": "Atualizando a instalação", + "ui.state_started": "iniciada", + "ui.state_done": "concluída", + "ui.state_skipped": "pulada", + "ui.state_pending": "aguardando", + "ui.state_running": "em andamento", + "ui.state_failed": "falhou", + "ui.log_warning": "Aviso", + "ui.log_error": "Erro", + "ui.log_space": "Espaço necessário: {need} GB, livre: {free} GB", + "ui.log_end": "Concluído", + "ui.result_ok": "O jogo está pronto", + "ui.result_failed": "A instalação falhou", + "ui.result_cancelled": "A instalação foi cancelada", + "ui.done_text": "Abra o {game_exe} em {dest}. O programa de instalação e os arquivos do jogo que você escolheu não são mais necessários.", + "ui.cancelled_text": "As etapas concluídas são mantidas. Inicie a instalação de novo com a mesma pasta para continuar.", + "ui.log_file": "Arquivo de log: {path}", + "ui.process_died": "A instalação parou de forma inesperada (código de saída {code}).", + "ui.confirm_cancel": "A instalação está em andamento. Cancelar e fechar a janela?", + "ui.space_one": "Espaço livre necessário em {path}: {need} GB (livre: {free} GB).", + "ui.space_two": "Espaço livre necessário em {path}: {need} GB (livre: {free} GB); em {temp}: {temp_need} GB (livre: {temp_free} GB).", + "ui.amount_bytes": "{done} / {total} GB", + "ui.amount_chunks": "{done} / {total} partes", + "ui.amount_objects": "{done} / {total} arquivos", + "ui.amount_shaders": "{done} / {total} shaders", + "ui.amount_other": "{done} / {total} {unit}", + "ui.seconds": "{n} s", + "param.input.base": "jogo base", + "param.input.update": "atualização", + "param.input.keys": "chaves", + + "launcher.title": "Instalação do TotK", + "launcher.python_missing": "Os arquivos de instalação estão incompletos: a pasta python ao lado do TotK-Setup.exe não existe. Extraia o arquivo compactado inteiro de novo e abra o TotK-Setup.exe a partir da pasta extraída.", + + "lang.ja": "Japonês", + "lang.en-US": "Inglês (EUA)", + "lang.en-GB": "Inglês (Reino Unido)", + "lang.fr": "Francês", + "lang.fr-CA": "Francês (Canadá)", + "lang.de": "Alemão", + "lang.it": "Italiano", + "lang.es": "Espanhol", + "lang.es-419": "Espanhol (América Latina)", + "lang.nl": "Holandês", + "lang.pt": "Português", + "lang.pt-BR": "Português (Brasil)", + "lang.ru": "Russo", + "lang.ko": "Coreano", + "lang.zh-Hans": "Chinês (Simplificado)", + "lang.zh-Hant": "Chinês (Tradicional)" +} diff --git a/installer/text/ru.json b/installer/text/ru.json index e3a668ba..58d12e65 100644 --- a/installer/text/ru.json +++ b/installer/text/ru.json @@ -1,16 +1,22 @@ { "step.check": "Проверка компьютера и игровых файлов", + "step.fonts": "Извлечение системных шрифтов", "step.extract": "Распаковка игры", "step.lift": "Перевод кода игры в C", "step.compile": "Сборка {game_exe}", "step.prepare": "Подготовка шейдеров", + "step.dlss": "Загрузка NVIDIA DLSS", "step.cleanup": "Уборка", "phase.check.host": "Проверка процессора и видеокарты", "phase.check.verify": "Проверка уже установленных файлов", "phase.check.verify_romfs": "Проверка установленных данных игры", "phase.check.inputs": "Открытие игровых файлов с помощью ключей", + "phase.check.unpack_base": "Распаковка сжатой базовой игры", + "phase.check.unpack_update": "Распаковка сжатого обновления", "phase.check.versions": "Проверка версий игры", + "phase.fonts.extract": "Извлечение общих шрифтов из прошивки", + "phase.fonts.copy": "Копирование общих шрифтов в папку установки", "phase.extract.hash_base": "Подсчёт контрольной суммы базовой игры", "phase.extract.hash_update": "Подсчёт контрольной суммы обновления", "phase.extract.exefs": "Распаковка кода игры", @@ -27,6 +33,7 @@ "phase.prepare.translate": "Трансляция шейдеров", "phase.prepare.libraries": "Сборка шейдеров для драйвера видеокарты", "phase.prepare.pack": "Упаковка кэша шейдеров в один файл", + "phase.dlss.download": "Загрузка nvngx_dlss.dll из DLSS SDK от NVIDIA", "phase.cleanup.ini": "Запись totk.ini", "phase.cleanup.temp": "Удаление временных файлов", @@ -44,36 +51,47 @@ "check.toolchain_broken": "Встроенный компилятор не работает: {detail}", "check.glibc_old": "Библиотека C этой системы — glibc {version}; компилятору установщика нужна {need} или новее (она есть в Ubuntu 22.04, Debian 12, Fedora 35, SteamOS 3 и новее). Обновление системы это исправит.", "check.host_probe_failed": "Проверка компьютера не выполнилась (код выхода {code}).", - "check.cpu_no_avx2": "Этому процессору не хватает AVX2 (x86-64-v3), который нужен игре. AVX2 есть у Intel Haswell (2013) и новее, у AMD Excavator и Zen и новее.", + "check.cpu_too_old": "Этому процессору не хватает x86-64-v2 (SSE4.2, POPCNT и CMPXCHG16B) — минимального уровня, под который собрана игра. Он есть у Intel Nehalem (2008) и AMD Bulldozer (2011) и новее.", "check.vulkan_missing": "Драйвер Vulkan не установлен. Установите свежий драйвер видеокарты.", "check.vulkan_no_device": "Vulkan не находит видеокарту. Установите свежий драйвер видеокарты.", "check.vulkan_too_old": "Игре нужна видеокарта с Vulkan 1.3; найдено: {devices}. Обновите драйвер видеокарты.", - "check.inputs_needed": "Нужны игровые файлы: выберите базовую игру, обновление 1.1.1 или 1.2.1 или 1.4.3 и prod.keys.", + "check.no_display": "Для подготовки шейдеров нужен дисплей. Запустите установку из сеанса рабочего стола или выполните export DISPLAY=:0 (на Steam Deck в игровом режиме ещё XDG_RUNTIME_DIR=/run/user/1000).", + "check.inputs_needed": "Нужны игровые файлы: выберите базовую игру, обновление 1.1.0 или 1.2.1 или 1.4.3 и prod.keys.", "check.input_missing": "Файл не найден ({input}): {path}", "check.keys_unreadable": "Не удаётся прочитать файл ключей. Выберите файл prod.keys.", "check.keys_no_header_key": "В файле ключей нет header_key. Снова получите prod.keys со своей консоли.", "check.keys_wrong": "Эти ключи не открывают файл «{input}». Используйте prod.keys со своей консоли.", "check.keys_too_old": "Файл ключей слишком старый для файла «{input}» (нужно поколение ключей {generation}). Получите prod.keys с консоли с более новой прошивкой.", - "check.not_game_file": "Файл «{input}» не является игровым файлом XCI или NSP.", + "check.not_game_file": "Файл «{input}» не является игровым файлом XCI, NSP, XCZ или NSZ.", "check.file_unreadable": "Не удаётся прочитать файл «{input}»: {detail}", + "check.unpack_failed": "Не удалось распаковать сжатый файл «{input}»: {detail}", "check.ticket_personalized": "В файле «{input}» личный тикет; нужен общий тикет.", "check.ticket_missing": "В файле «{input}» нет тикета, который нужен его содержимому.", "check.base_not_totk": "Файл базовой игры — не The Legend of Zelda: Tears of the Kingdom ({detail}).", - "check.update_not_nsp": "Обновление должно быть файлом NSP.", - "check.update_is_base": "Файл обновления содержит базовую игру. Выберите NSP с обновлением 1.1.1 или 1.2.1 или 1.4.3.", + "check.base_is_update": "Файл базовой игры — это обновление. Выберите здесь NSP или XCI базовой игры (версия 1.0.0), а этот файл укажите как обновление.", + "check.base_combined": "Файл базовой игры содержит несколько пакетов (игра, обновление или DLC в одном файле). Выберите дамп, в котором только базовая игра.", + "check.update_not_nsp": "Обновление должно быть файлом NSP или NSZ.", + "check.update_is_base": "Файл обновления содержит базовую игру. Выберите NSP с обновлением 1.1.0 или 1.2.1 или 1.4.3.", "check.update_not_totk": "Файл обновления — не обновление Tears of the Kingdom ({detail}).", "check.exefs_failed": "Код игры из файла «{input}» не прошёл проверку; подробности в {log}.", "check.input_damaged": "Файл «{input}» повреждён или скопирован не полностью: {path} ({detail}). Скопируйте этот файл заново из источника или сделайте дамп ещё раз, затем снова запустите установку.", "check.base_wrong_version": "Базовая игра не версии 1.0.0 (отличаются модули: {modules}).", "check.update_wrong_version": "Версия обновления {found}; этот выпуск принимает {wanted}.", "check.disk_space": "Не хватает свободного места для {path}: нужно {need_gb} ГБ, свободно {free_gb} ГБ.", + "check.firmware_missing": "Папки прошивки {path} нет. Выберите папку Contents/registered прошивки.", + "check.firmware_empty": "В папке прошивки {path} нет файлов NCA. Выберите папку Contents/registered прошивки.", + "check.fonts_keys_needed": "Общие шрифты для этого языка ещё не установлены; выберите prod.keys, чтобы установка извлекла их из прошивки.", "extract.exefs_mismatch": "Распакованный код игры не соответствует версии {version} ({modules}).", "extract.romfs_merge_failed": "Не удалось применить обновление к данным игры: {detail}. Подробности: {log}", "extract.romfs_mismatch": "Распакованные данные игры не соответствуют версии {version} (размер {size}, контрольная сумма {sha256}...).", + "fonts.failed": "Не удалось извлечь общие шрифты (код выхода {code}): {detail}. Подробности: {log}", + "fonts.incomplete": "При извлечении общих шрифтов записаны не все файлы ({files}).", + "fonts.copy_failed": "Не удаётся скопировать общие шрифты в {path}: {detail}", "lift.failed": "Не удалось перевести код игры (код выхода {code}). Подробности: {log}", "compile.failed": "Не удалось собрать {game_exe} (код выхода {code}). Подробности: {log}", "compile.exe_in_use": "Файл {path} занят. Закройте игру и запустите установку снова.", + "prepare.xlate_in_use": "Файл {path} занят. Закройте игру, завершите процесс totk_xlate, оставшийся от прошлого запуска, и запустите установку снова.", "prepare.shader_list_missing": "Нет списка шейдеров.", "prepare.romfs_patches_failed": "Не удалось записать патчи данных игры (код выхода {code}). Подробности: {log}", "prepare.shaders_failed": "Не удалось подготовить шейдеры (код выхода {code}). Подробности: {log}", @@ -81,28 +99,40 @@ "prepare.pack_failed": "Не удалось упаковать кэш шейдеров (код выхода {code}). Подробности: {log}", "cleanup.ini_failed": "Не удаётся записать totk.ini: {detail}", - "warn.lang_needs_fonts": "Для языка «{lang}» нужны системные шрифты Switch, которые установка не извлекает; часть текста может не отображаться.", + "warn.lang_needs_fonts": "Для языка «{lang}» нужны системные шрифты Switch, а они не указаны. Игра не запустится на этом языке, пока их не добавят. Выберите папку прошивки (Contents/registered) или см. раздел «Switch system fonts» в docs/installer.md.", "warn.npdm_differs": "main.npdm отличается от эталонного дампа {version}; используются только модули с кодом.", + "warn.cheats_skipped": "Не удалось собрать читы из папки модов, меняющие код игры; игра установится без них. Подробности: {log}", + "warn.exefs_skipped": "Не удалось собрать ExeFS-патчи модов (.ips, .pchtxt); игра установится без них. Подробности: {log}", "warn.xbox_prompts_failed": "Не удалось подготовить подсказки кнопок Xbox (код выхода {code}); игра оставит подсказки Switch. Подробности: {log}", + "warn.shaders_skipped": "Графический драйвер аварийно завершился при сборке части шейдеров, поэтому {count} из них пропущены при подготовке. Игра соберёт их, когда они впервые понадобятся, и драйвер может упасть и там; обновление драйвера может это исправить. Подробности: {log}", "warn.kernel_old": "В Linux {version} нет отслеживания записи, на которое опирается игра (нужна версия 6.7 или новее); игра может работать очень медленно. Обновление системы это исправит.", + "warn.dlss_kept": "В папке установки уже есть библиотека DLSS ({path}); установка её оставляет.", + "warn.dlss_download_failed": "Не удалось загрузить NVIDIA DLSS с {url}: {detail}. Игра работает и без него; при следующем запуске установка попробует снова.", "ui.title": "Установка TotK", + "ui.rebuild_title": "Пересборка игры под ваши моды", + "ui.start_game": "Запустить игру", "ui.version": "версия {version}", "ui.intro": "Собирает готовую к запуску папку с Tears of the Kingdom из ваших игровых файлов. Занимает много времени и выполняется один раз.", "ui.notice": "Папка, которую создаёт установка, содержит игру. Она предназначена только для личного использования: не передавайте и не выкладывайте её.", "ui.keys_private": "Файл ключей читается на месте. Его содержимое не показывается, не копируется и не сохраняется.", - "ui.base": "Базовая игра (NSP или XCI)", - "ui.update": "Обновление 1.1.1 или 1.2.1 или 1.4.3 (NSP)", + "ui.base": "Базовая игра (NSP, XCI, NSZ или XCZ)", + "ui.update": "Обновление 1.1.0 или 1.2.1 или 1.4.3 (NSP или NSZ)", "ui.keys": "prod.keys", + "ui.firmware": "Папка прошивки (необязательно)", "ui.game_language": "Язык игры", "ui.dest": "Папка установки", "ui.browse": "Обзор...", "ui.pick_game": "Выберите файл базовой игры", - "ui.pick_update": "Выберите файл обновления 1.1.1 или 1.2.1 или 1.4.3", + "ui.pick_update": "Выберите файл обновления 1.1.0 или 1.2.1 или 1.4.3", "ui.pick_keys": "Выберите prod.keys", + "ui.pick_firmware": "Выберите папку прошивки Switch (Contents/registered)", "ui.pick_dest": "Выберите папку установки", - "ui.type_game": "Файлы игры (NSP, XCI)", - "ui.type_nsp": "Файлы NSP", + "ui.firmware_help": "Для текста на китайском и корейском: папка Contents/registered прошивки, например из Ryujinx или Yuzu. Установка скопирует из неё системные шрифты Switch в папку установки.", + "ui.dlss": "Загрузить NVIDIA DLSS (nvngx_dlss.dll) для видеокарт NVIDIA RTX", + "ui.dlss_help": "Из DLSS SDK от NVIDIA на GitHub, по лицензии NVIDIA RTX SDK (github.com/NVIDIA/DLSS). Без него настройки dlaa и DLSS ничего не делают.", + "ui.compressed_help": "Сжатые файлы NSZ и XCZ подходят как есть: установка сама их распакует (для этого, пока извлекаются данные игры, нужно столько свободного места, сколько занимают распакованные файлы).", + "ui.type_game": "Файлы игры (NSP, XCI, NSZ, XCZ)", "ui.type_keys": "Файлы ключей (*.keys)", "ui.type_all": "Все файлы", "ui.start": "Начать", @@ -116,12 +146,13 @@ "ui.update_mode": "В этой папке уже есть установка. Программа продолжит или обновит её; файлы игры понадобятся, только если их снова придётся читать.", "ui.need_dest": "Выберите папку установки.", "ui.need_base": "Выберите файл базовой игры.", - "ui.need_update": "Выберите файл обновления 1.1.1 или 1.2.1 или 1.4.3.", + "ui.need_update": "Выберите файл обновления 1.1.0 или 1.2.1 или 1.4.3.", "ui.need_keys": "Выберите файл prod.keys.", "ui.need_lang": "Выберите язык игры.", "ui.missing_base": "Файл базовой игры не найден.", "ui.missing_update": "Файл обновления не найден.", "ui.missing_keys": "Файл ключей не найден.", + "ui.missing_firmware": "Папки прошивки нет.", "ui.running_install": "Установка", "ui.running_resume": "Продолжение прерванной установки", "ui.running_update": "Обновление установки", diff --git a/installer/text/zh-Hans.json b/installer/text/zh-Hans.json new file mode 100644 index 00000000..168c5f96 --- /dev/null +++ b/installer/text/zh-Hans.json @@ -0,0 +1,208 @@ +{ + "step.check": "检查电脑和游戏文件", + "step.fonts": "正在提取系统字体", + "step.extract": "解包游戏", + "step.lift": "将游戏代码转换为 C 代码", + "step.compile": "编译 {game_exe}", + "step.prepare": "准备着色器", + "step.dlss": "下载 NVIDIA DLSS", + "step.cleanup": "清理", + + "phase.check.host": "检查处理器和显卡", + "phase.check.verify": "检查已安装的文件", + "phase.check.verify_romfs": "检查已安装的游戏数据", + "phase.check.inputs": "用密钥打开游戏文件", + "phase.check.unpack_base": "正在解压压缩的游戏本体", + "phase.check.unpack_update": "正在解压压缩的更新", + "phase.check.versions": "检查游戏版本", + "phase.fonts.extract": "正在从固件中提取共享字体", + "phase.fonts.copy": "正在将共享字体复制到安装文件夹", + "phase.extract.hash_base": "计算游戏本体的校验和", + "phase.extract.hash_update": "计算更新的校验和", + "phase.extract.exefs": "解包游戏代码", + "phase.extract.copy": "复制游戏数据", + "phase.extract.merge": "将更新应用到游戏数据", + "phase.extract.diagnose": "查找损坏的游戏文件", + "phase.extract.index": "校验游戏数据", + "phase.lift.discover": "查找函数", + "phase.lift.emit": "生成 C 代码", + "phase.compile.compile": "编译", + "phase.compile.link": "链接 {game_exe}", + "phase.prepare.romfs_patches": "写入 60 帧游戏数据补丁", + "phase.prepare.seed": "从游戏数据中读取着色器", + "phase.prepare.translate": "转译着色器", + "phase.prepare.libraries": "为显卡驱动构建着色器", + "phase.prepare.pack": "将着色器缓存打包成一个文件", + "phase.dlss.download": "从 NVIDIA 的 DLSS SDK 下载 nvngx_dlss.dll", + "phase.cleanup.ini": "写入 totk.ini", + "phase.cleanup.temp": "删除临时文件", + + "cli.usage": "命令行无效:{detail}", + "internal": "意外错误({exception}):{detail}", + "state.unreadable": "无法读取 {path}。请选择其他文件夹,或删除该文件。", + "state.newer_format": "{path} 是由更新版本的安装程序写入的。请使用那个安装程序,或选择其他文件夹。", + "state.output_changed": "步骤“{step}”生成的已安装文件在生成后被改动过;该步骤将重新运行。", + + "check.release_incomplete": "安装程序文件夹不完整,缺少:{paths}。请重新解压发布包。", + "check.dest_not_folder": "安装位置 {path} 是一个文件,而不是文件夹。", + "check.dest_not_empty": "安装文件夹 {path} 不是空的,其中也没有之前的安装。请选择一个空文件夹。", + "check.lang_missing": "请选择游戏语言。", + "check.lang_unknown": "未知语言“{lang}”。可用语言:{known}。", + "check.toolchain_broken": "自带的编译器无法工作:{detail}", + "check.glibc_old": "此系统的 C 库为 glibc {version};安装程序的编译器需要 {need} 或更新版本(Ubuntu 22.04、Debian 12、Fedora 35、SteamOS 3 及更新版本都满足)。更新系统即可解决。", + "check.host_probe_failed": "电脑检查未能运行(退出代码 {code})。", + "check.cpu_too_old": "此处理器不支持 x86-64-v2(SSE4.2、POPCNT 和 CMPXCHG16B),这是游戏所需的最低指令集。Intel Nehalem(2008 年)或 AMD Bulldozer(2011 年)及更新的处理器都支持。", + "check.vulkan_missing": "未安装 Vulkan 驱动。请为你的显卡安装最新的驱动程序。", + "check.vulkan_no_device": "Vulkan 找不到显卡。请为你的显卡安装最新的驱动程序。", + "check.vulkan_too_old": "游戏需要支持 Vulkan 1.3 的显卡;检测到:{devices}。请更新显卡驱动程序。", + "check.no_display": "准备着色器需要显示器。请在桌面会话中运行安装程序,或执行 export DISPLAY=:0(Steam Deck 游戏模式下还需 XDG_RUNTIME_DIR=/run/user/1000)。", + "check.inputs_needed": "需要游戏文件:请选择游戏本体、1.1.0、1.2.1 或 1.4.3 更新以及 prod.keys。", + "check.input_missing": "找不到文件({input}):{path}", + "check.keys_unreadable": "无法读取密钥文件。请选择一个 prod.keys 文件。", + "check.keys_no_header_key": "密钥文件中没有 header_key。请从你的主机重新导出 prod.keys。", + "check.keys_wrong": "这些密钥无法打开{input}文件。请使用你自己主机的 prod.keys。", + "check.keys_too_old": "密钥文件对{input}文件来说太旧(需要密钥代数 {generation})。请从固件更新的主机导出 prod.keys。", + "check.not_game_file": "{input}文件不是 XCI、NSP、XCZ 或 NSZ 游戏文件。", + "check.file_unreadable": "无法读取{input}文件:{detail}", + "check.unpack_failed": "无法解压压缩的{input}文件:{detail}", + "check.ticket_personalized": "{input}文件带有个人化票据(personalized ticket);需要通用票据(common ticket)。", + "check.ticket_missing": "{input}文件缺少其内容所需的票据(ticket)。", + "check.base_not_totk": "游戏本体文件不是《塞尔达传说 王国之泪》({detail})。", + "check.base_is_update": "基础游戏文件是一个更新。请在这里选择基础游戏的 NSP 或 XCI(版本 1.0.0),并把此文件选为更新。", + "check.base_combined": "基础游戏文件包含多个内容包(基础游戏、更新或 DLC 合在一个文件中)。请选择只包含游戏本体的基础游戏转储。", + "check.update_not_nsp": "更新必须是 NSP 或 NSZ 文件。", + "check.update_is_base": "更新文件里是游戏本体。请选择 1.1.0、1.2.1 或 1.4.3 的更新 NSP。", + "check.update_not_totk": "更新文件不是《王国之泪》的更新({detail})。", + "check.exefs_failed": "{input}文件的游戏代码未通过检查;详见 {log}。", + "check.input_damaged": "{input}文件已损坏或不完整:{path}({detail})。请从来源重新复制该文件或重新导出,然后再次运行安装程序。", + "check.base_wrong_version": "游戏本体不是 1.0.0 版本({modules} 不一致)。", + "check.update_wrong_version": "更新的版本是 {found};此发布版需要 {wanted}。", + "check.disk_space": "{path} 的可用空间不足:需要 {need_gb} GB,可用 {free_gb} GB。", + "check.firmware_missing": "固件文件夹 {path} 不存在。请选择固件的 Contents/registered 文件夹。", + "check.firmware_empty": "固件文件夹 {path} 中没有 NCA 文件。请选择固件的 Contents/registered 文件夹。", + "check.fonts_keys_needed": "此语言的共享字体尚未安装;请选择 prod.keys,以便安装程序从固件中提取它们。", + + "extract.exefs_mismatch": "解包出的游戏代码与 {version} 版本不符({modules})。", + "extract.romfs_merge_failed": "将更新应用到游戏数据失败:{detail}。详情:{log}", + "extract.romfs_mismatch": "解包出的游戏数据与 {version} 版本不符(大小 {size},校验和 {sha256}...)。", + "fonts.failed": "提取共享字体失败(退出代码 {code}):{detail}。详情:{log}", + "fonts.incomplete": "共享字体提取没有写出全部文件({files})。", + "fonts.copy_failed": "无法将共享字体复制到 {path}:{detail}", + "lift.failed": "游戏代码转换失败(退出代码 {code})。详情:{log}", + "compile.failed": "编译 {game_exe} 失败(退出代码 {code})。详情:{log}", + "compile.exe_in_use": "{path} 正在使用中。请关闭游戏后重新运行安装程序。", + "prepare.xlate_in_use": "{path} 正在被占用。请关闭游戏,结束上次残留的 totk_xlate 进程,再重新运行安装程序。", + "prepare.shader_list_missing": "缺少着色器列表。", + "prepare.romfs_patches_failed": "写入游戏数据补丁失败(退出代码 {code})。详情:{log}", + "prepare.shaders_failed": "准备着色器失败(退出代码 {code})。详情:{log}", + "cleanup.romfs_unreadable": "安装完成后,{path} 中的游戏数据缺失或无法读取。请重启 Windows,然后对同一文件夹再次运行安装程序:它会重新提取游戏数据,其余内容保留。", + "prepare.pack_failed": "打包着色器缓存失败(退出代码 {code})。详情:{log}", + "cleanup.ini_failed": "无法写入 totk.ini:{detail}", + + "warn.lang_needs_fonts": "{lang}文本需要 Switch 系统字体,但未提供。在添加这些字体之前,游戏无法以该语言启动。请选择固件文件夹(Contents/registered),或参阅 docs/installer.md 中的“Switch system fonts”一节。", + "warn.cheats_skipped": "无法构建 mods 文件夹中修改游戏代码的金手指;游戏将不含它们进行安装。详情:{log}", + "warn.exefs_skipped": "无法构建模组的 ExeFS 补丁(.ips、.pchtxt);游戏将不含它们进行安装。详情:{log}", + "warn.npdm_differs": "main.npdm 与参考的 {version} 导出不同;只使用其中的代码模块。", + "warn.xbox_prompts_failed": "无法准备 Xbox 按键提示(退出代码 {code});游戏将继续使用 Switch 按键提示。详情:{log}", + "warn.shaders_skipped": "显卡驱动在编译部分着色器时崩溃,因此有 {count} 个着色器未被预先准备。游戏会在首次需要时编译它们,驱动在那时也可能崩溃;更新驱动可能会解决。详情:{log}", + "warn.kernel_old": "Linux {version} 缺少游戏依赖的写入跟踪功能(需要 6.7 或更新版本);游戏可能运行得很慢。更新系统即可解决。", + "warn.dlss_kept": "安装文件夹中已有 DLSS 库({path});安装程序将保留它。", + "warn.dlss_download_failed": "无法从 {url} 下载 NVIDIA DLSS:{detail}。游戏无需它也能运行;下次运行安装程序时会重试。", + + "ui.title": "TotK 安装程序", + "ui.rebuild_title": "正在为你的模组重新构建游戏", + "ui.start_game": "启动游戏", + "ui.version": "版本 {version}", + "ui.intro": "用你自己的游戏文件构建一个可以游玩的《王国之泪》文件夹。耗时较长,只需运行一次。", + "ui.notice": "此安装程序构建的文件夹包含游戏本身,仅供你个人使用;请勿分享或上传。", + "ui.keys_private": "密钥文件只在原位置读取,其内容绝不会被显示、复制或保存。", + "ui.base": "游戏本体(NSP、XCI、NSZ 或 XCZ)", + "ui.update": "1.1.0、1.2.1 或 1.4.3 更新(NSP 或 NSZ)", + "ui.keys": "prod.keys", + "ui.firmware": "固件文件夹(可选)", + "ui.game_language": "游戏语言", + "ui.dest": "安装文件夹", + "ui.browse": "浏览...", + "ui.pick_game": "选择游戏本体文件", + "ui.pick_update": "选择 1.1.0、1.2.1 或 1.4.3 更新文件", + "ui.pick_keys": "选择 prod.keys", + "ui.pick_firmware": "选择 Switch 固件文件夹(Contents/registered)", + "ui.pick_dest": "选择安装文件夹", + "ui.firmware_help": "用于中文、韩文和繁体中文文本:固件的 Contents/registered 文件夹,例如来自 Ryujinx 或 Yuzu。安装程序会从中把 Switch 系统字体复制到安装文件夹。", + "ui.dlss": "为 NVIDIA RTX 显卡下载 NVIDIA DLSS(nvngx_dlss.dll)", + "ui.dlss_help": "来自 GitHub 上 NVIDIA 的 DLSS SDK,遵循 NVIDIA RTX SDK 许可(github.com/NVIDIA/DLSS)。没有它,dlaa 和 DLSS 设置不起作用。", + "ui.compressed_help": "压缩的 NSZ 和 XCZ 文件可以直接使用:安装程序会自行解压(在游戏数据提取完成之前,需要与解压后文件大小相同的可用空间)。", + "ui.type_game": "游戏文件(NSP、XCI、NSZ、XCZ)", + "ui.type_keys": "密钥文件(*.keys)", + "ui.type_all": "所有文件", + "ui.start": "开始", + "ui.cancel": "取消", + "ui.cancelling": "正在取消...", + "ui.show_log": "显示日志", + "ui.hide_log": "隐藏日志", + "ui.close": "关闭", + "ui.back": "返回", + "ui.open_folder": "打开文件夹", + "ui.update_mode": "此文件夹中已有之前的安装。安装程序会继续或更新它;只有在需要时才会再次要求提供游戏文件。", + "ui.need_dest": "请选择安装文件夹。", + "ui.need_base": "请选择游戏本体文件。", + "ui.need_update": "请选择 1.1.0、1.2.1 或 1.4.3 更新文件。", + "ui.need_keys": "请选择 prod.keys 文件。", + "ui.need_lang": "请选择游戏语言。", + "ui.missing_base": "游戏本体文件不存在。", + "ui.missing_update": "更新文件不存在。", + "ui.missing_keys": "密钥文件不存在。", + "ui.missing_firmware": "固件文件夹不存在。", + "ui.running_install": "正在安装", + "ui.running_resume": "正在继续之前的安装", + "ui.running_update": "正在更新安装", + "ui.state_started": "已开始", + "ui.state_done": "完成", + "ui.state_skipped": "已跳过", + "ui.state_pending": "等待中", + "ui.state_running": "进行中", + "ui.state_failed": "失败", + "ui.log_warning": "警告", + "ui.log_error": "错误", + "ui.log_space": "所需空间:{need} GB,可用:{free} GB", + "ui.log_end": "已结束", + "ui.result_ok": "游戏已准备就绪", + "ui.result_failed": "安装失败", + "ui.result_cancelled": "安装已取消", + "ui.done_text": "从 {dest} 启动 {game_exe}。安装程序和你选择的游戏文件都不再需要了。", + "ui.cancelled_text": "已完成的步骤会保留。用同一个文件夹再次运行安装程序即可继续。", + "ui.log_file": "日志文件:{path}", + "ui.process_died": "安装程序意外停止(退出代码 {code})。", + "ui.confirm_cancel": "安装正在进行。要取消安装并关闭窗口吗?", + "ui.space_one": "{path} 需要的可用空间:{need} GB(可用:{free} GB)。", + "ui.space_two": "{path} 需要的可用空间:{need} GB(可用:{free} GB);{temp} 需要:{temp_need} GB(可用:{temp_free} GB)。", + "ui.amount_bytes": "{done} / {total} GB", + "ui.amount_chunks": "{done} / {total} 部分", + "ui.amount_objects": "{done} / {total} 个文件", + "ui.amount_shaders": "{done} / {total} 个着色器", + "ui.amount_other": "{done} / {total} {unit}", + "ui.seconds": "{n} 秒", + "param.input.base": "游戏本体", + "param.input.update": "更新", + "param.input.keys": "密钥", + + "launcher.title": "TotK 安装程序", + "launcher.python_missing": "安装文件不完整:TotK-Setup.exe 旁边缺少 python 文件夹。请重新解压整个压缩包,然后从解压出的文件夹中启动 TotK-Setup.exe。", + + "lang.ja": "日语", + "lang.en-US": "英语(美国)", + "lang.en-GB": "英语(英国)", + "lang.fr": "法语", + "lang.fr-CA": "法语(加拿大)", + "lang.de": "德语", + "lang.it": "意大利语", + "lang.es": "西班牙语", + "lang.es-419": "西班牙语(拉丁美洲)", + "lang.nl": "荷兰语", + "lang.pt": "葡萄牙语", + "lang.pt-BR": "葡萄牙语(巴西)", + "lang.ru": "俄语", + "lang.ko": "韩语", + "lang.zh-Hans": "中文(简体)", + "lang.zh-Hant": "中文(繁体)" +} diff --git a/recomp/gen.py b/recomp/gen.py index 1eaa00f2..bd582b68 100644 --- a/recomp/gen.py +++ b/recomp/gen.py @@ -211,7 +211,7 @@ class Emitter: def __init__(self, mod, starts, patched=None, reg_locals=regs.LOCALS, callee_saved=True, spins=None): self.mod = mod self.starts = starts - self.patched = patched or {} # {address: (set index, [replacement words])} (recomp/patches.py) + self.patched = patched or {} # {address: [(set index, [replacement words])]} (recomp/patches.py) self.spins = spins or {} # {spin wait back-edge: its head} (recomp/patches.py `spin`) self.reg_locals = reg_locals # recomp/regs.py: registers per function in C locals self.kept = regs.CALLEE_SAVED if callee_saved else frozenset() # not reloaded after a call @@ -250,33 +250,54 @@ class Emitter: self.unsupported[re.sub(r'\b[0-9a-f]{8}\b', '', msg).strip()] += 1 elif k == 'asm': raise ValueError(f'{entry + 4 * i:#x}: asm instruction from the portable lifter') + for a in self.patched: # a replacement's branch into the function needs its label + if entry <= a < end: + for words in self.replacements(a): + r = words[-1] + if r.kind in ('b', 'bcond') and entry <= r.target < end: + labels.add(r.target) return labels, tables def replacements(self, a): - """Lifted replacement words of the patched site a.""" - return [self.mod.lifter.lift(w, a) for w in self.patched[a][1]] + """The lifted words of each alternative of the patched site a, in set order. Only the last word of an + alternative may branch (bl, b, b.cond / cbz / tbz, ret).""" + out = [] + for _, words in self.patched[a]: + lifted = [self.mod.lifter.lift(w, a) for w in words] + for j, r in enumerate(lifted): + last = j == len(lifted) - 1 + if r.kind != 'op' and not (last and r.kind in ('bl', 'b', 'bcond', 'ret')): + raise patches.PatchError(f'{a:#x}: replacement word {words[j]:08x} is a {r.kind}') + out.append(lifted) + return out - @staticmethod - def flow(insns, entry, end, tables): + def flow(self, insns, entry, end, tables): """Successors of each instruction inside the function [entry, end). Index len(insns) stands for falling - through into the next function and has none.""" + through into the next function and has none. A patched site has the successors of all its alternatives.""" def idx(t): return [(t - entry) // 4] if entry <= t < end else [] - succ = [] - for i, ins in enumerate(insns): + def of(ins, i): k = ins.kind if k in ('op', 'bl', 'blr'): - succ.append([i + 1]) - elif k == 'b': - succ.append(idx(ins.target)) - elif k == 'bcond': - succ.append(idx(ins.target) + [i + 1]) - elif k == 'br': + return [i + 1] + if k == 'b': + return idx(ins.target) + if k == 'bcond': + return idx(ins.target) + [i + 1] + if k == 'br': jt = tables.get(entry + 4 * i) - succ.append([t for x in jt for t in idx(x)] if jt and jt != 'all' else []) - else: # ret, trap - succ.append([]) + return [t for x in jt for t in idx(x)] if jt and jt != 'all' else [] + return [] # ret, trap + + succ = [] + for i, ins in enumerate(insns): + s = of(ins, i) + a = entry + 4 * i + if a in self.patched: + for words in self.replacements(a): + s += [t for t in of(words[-1], i) if t not in s] + succ.append(s) succ.append([]) return succ @@ -294,27 +315,34 @@ class Emitter: exits.add(i) # through rt_dispatch, also a table's default if a in self.patched: effects.append(self.patched_effect(ins, a)) - if any(r.kind == 'bl' for r in self.replacements(a)): - calls.add(i) + for words in self.replacements(a): + r = words[-1] + if r.kind == 'bl': + calls.add(i) + elif r.kind in ('b', 'bcond') and not entry <= r.target < end: + exits.add(i) else: effects.append(flags.effect(ins.cond if k == 'bcond' else ins.code or '')) effects.append((False, None)) return flags.plan(effects, succ, calls, exits) def patched_effect(self, ins, a): - """Flag effect of a patched site: both alternatives must have the same one, in at most one word each.""" + """Flag effect of a patched site: every alternative (the original and each replacement) must write the same + flags, in at most one word each; the site reads flags when any alternative does (a b.cond reads them).""" alts = [] - for words in ([ins], self.replacements(a)): - fx = [flags.effect(w.code or '') for w in words if w.kind == 'op'] + for words in [[ins]] + self.replacements(a): + fx = [flags.effect(w.code or '') if w.kind == 'op' else flags.effect(w.cond) for w in words + if w.kind in ('op', 'bcond')] fx = [e for e in fx if e[0] or e[1]] if len(fx) > 1: raise patches.PatchError(f'{a:#x}: several flag-using words in one alternative') alts.append(fx[0] if fx else (False, None)) - if alts[0][1] != alts[1][1]: - raise patches.PatchError(f'{a:#x}: the alternatives write different flags ({alts[0][1]}, {alts[1][1]})') - if (alts[0][1] or alts[0][0] or alts[1][0]) and ins.kind == 'bl': + if any(e[1] != alts[0][1] for e in alts): + raise patches.PatchError(f'{a:#x}: the alternatives write different flags ({[e[1] for e in alts]})') + reads = any(e[0] for e in alts) + if (alts[0][1] or reads) and (ins.kind == 'bl' or any(w[-1].kind == 'bl' for w in self.replacements(a))): raise patches.PatchError(f'{a:#x}: a patched call that uses flags') - return alts[0][0] or alts[1][0], alts[0][1] + return reads, alts[0][1] def function(self, entry, end): """(lines of C, referenced function starts) for the guest function [entry, end).""" @@ -385,23 +413,30 @@ class Emitter: code = fx(i, code) return f'{spill}{code}{reload}' if regs.ESCAPE.search(code) else code + def one(i, r, a): + """C of one lifted word of a patched site.""" + if r.kind == 'op': + return op(i, r.code) + if r.kind == 'bl': + return call(r.target, a) + if r.kind == 'b': + return jump(r.target, i) + if r.kind == 'bcond': + return f'if ({fx(i, r.cond)}) {{ {jump(r.target, i)} }}' + if r.kind == 'ret': + return f'{spill}return;' + raise patches.PatchError(f'{a:#x}: patched instruction is a {r.kind}') + def patch(i, ins, a): - """if (switch) { replacement } else { original } for a patched site (recomp/patches.py).""" - k, words = self.patched[a] - if ins.kind not in ('op', 'bl'): + """if (switch) { replacement } else if ... else { original } for a patched site (recomp/patches.py).""" + if ins.kind not in ('op', 'bl', 'b', 'bcond'): raise patches.PatchError(f'{a:#x}: patched instruction is a {ins.kind}') - repl = [] - for j, (w, r) in enumerate(zip(words, self.replacements(a))): - if r.kind == 'op': - repl.append(op(i, r.code)) - elif r.kind == 'bl' and j == len(words) - 1: - repl.append(call(r.target, a)) - else: - raise patches.PatchError(f'{a:#x}: replacement word {w:08x} is a {r.kind}') - orig = op(i, ins.code) if ins.kind == 'op' else call(ins.target, a) + line = ' ' + for (k, _), words in zip(self.patched[a], self.replacements(a)): + line += f' if (rt_patch_on[{k}]) {{ {" ".join(one(i, r, a) for r in words)} }} else' self.uses_patches = True self.stats['patched_sites'] += 1 - return f' if (rt_patch_on[{k}]) {{ {" ".join(repl)} }} else {{ {orig} }}' + return f'{line} {{ {one(i, ins, a)} }}' body = [] # (instruction index or None for a label, line); a line belongs to the instruction it lifts @@ -445,7 +480,7 @@ class Emitter: else: raise ValueError(f'{a:#x}: unknown instruction kind {k!r}') n = len(insns) - if k not in TERMINATORS: # falls through into the next function + if k not in TERMINATORS or end - 4 in self.patched: # falls through into the next function if end < mod.hi: emit(n, ' ' + jump(end, n)) else: @@ -523,7 +558,8 @@ def _retry(fn, *args): _W = {} # worker state: 'mods' {name: (path, base)}, 'starts' {name: sorted array}, 'emitters' {name: Emitter} -def _worker_init(mods, starts, table, opts): +def _worker_init(mods, starts, table, opts, jobs): + _W['jobs'] = jobs _W['mods'] = mods _W['starts'] = starts _W['patches'] = table @@ -546,6 +582,10 @@ def _emit_chunk(task): """Worker: write one chunk. Returns its statistics.""" name, number, path, fns = task t0 = time.perf_counter() + # Test hook: a pool wider than this raises the MemoryError a busy machine does, so the retry can be exercised. + limit = os.environ.get('TOTK_LIFT_TEST_OOM_ABOVE') + if limit and _W['jobs'] > int(limit) and number % 2 == 1: + raise MemoryError e = _emitter(name) e.stats.clear() e.unsupported.clear() @@ -597,6 +637,14 @@ def pick_jobs(requested): """Worker count and the reason for it: --jobs if given, else the CPU count capped by the free memory.""" if requested: return requested, '--jobs' + env = os.environ.get('TOTK_LIFT_JOBS', '').strip() + if env: + try: + if int(env) >= 1: + return int(env), 'TOTK_LIFT_JOBS' + except ValueError: + pass + print(f'ignoring TOTK_LIFT_JOBS={env!r}: not a positive integer', file=sys.stderr, flush=True) cpus = os.cpu_count() or 4 free = hostmem.available_ram() if free is None: @@ -670,16 +718,31 @@ def main(): t_emit = time.perf_counter() ctx = mp.get_context('spawn') jobs, why = pick_jobs(args.jobs) - print(f'emitting with {jobs} workers ({why})', file=sys.stderr, flush=True) - with ctx.Pool(jobs, initializer=_worker_init, initargs=(paths, starts_abs, table, opts)) as pool: - for i, r in enumerate(pool.imap_unordered(_emit_chunk, [t for _, t in tasks], chunksize=1)): - results[r['module']].append(r) - if r['error']: - errors.append(r['error']) - done_at[r['module']] = time.perf_counter() - t_emit - if (i + 1) % 200 == 0 or i + 1 == len(tasks): - print(f'emitted {i + 1}/{len(tasks)} chunks, {time.perf_counter() - t_emit:.0f}s', file=sys.stderr, - flush=True) + pending = [t for _, t in tasks] + emitted = 0 + while True: + print(f'emitting with {jobs} workers ({why})', file=sys.stderr, flush=True) + try: + # Chunk writes are atomic and idempotent, so a chunk lost with a failed pool is simply emitted again. + with ctx.Pool(jobs, initializer=_worker_init, initargs=(paths, starts_abs, table, opts, jobs)) as pool: + for r in pool.imap_unordered(_emit_chunk, pending, chunksize=1): + results[r['module']].append(r) + if r['error']: + errors.append(r['error']) + done_at[r['module']] = time.perf_counter() - t_emit + emitted += 1 + if emitted % 200 == 0 or emitted == len(tasks): + print(f'emitted {emitted}/{len(tasks)} chunks, {time.perf_counter() - t_emit:.0f}s', + file=sys.stderr, flush=True) + break + except MemoryError: + if jobs == 1: + raise + smaller = jobs // 2 + print(f'emit: out of memory with {jobs} workers, retrying with {smaller}', file=sys.stderr, flush=True) + jobs, why = smaller, 'after out of memory' + done = {(r['module'], r['number']) for rs in results.values() for r in rs} + pending = [t for t in pending if (t[0], t[1]) not in done] for name, d in info.items(): rs = sorted(results[name], key=lambda r: r['number']) diff --git a/recomp/patches-v111.txt b/recomp/patches-v110.txt similarity index 99% rename from recomp/patches-v111.txt rename to recomp/patches-v110.txt index 95aa0e9d..5382f952 100644 --- a/recomp/patches-v111.txt +++ b/recomp/patches-v110.txt @@ -1,6 +1,6 @@ -# Code patches for TotK 1.1.1 main (recomp/patches.py). The sites come from MacNativeGames +# Code patches for TotK 1.1.0 main (recomp/patches.py). The sites come from MacNativeGames # games/totk/runtime/game_patches.c (MIT, Copyright (c) 2026 MacNativeGames contributors) at upstream ebe621d, through -# its 1.2.1 addresses in game_version.h (2db612b); each one was located again in 1.1.1 by instruction-window matching +# its 1.2.1 addresses in game_version.h (2db612b); each one was located again in 1.1.0 by instruction-window matching # and its original words checked against this build's main. The patch page holds two floats the runtime writes at every # present (frame_rate.c): +0 the 30 Hz ratio (seconds * 30), +4 the frame step in seconds. @@ -38,7 +38,7 @@ site realtime main 90e5e8 1e210800 d503201f # fmul s0, s0, s1 -> nop # Hair and cloth (upstream 290ec9f, 8f8022f): the actor animation update builds a second clock {seconds = factor / 30, # ratio = factor} per rendered frame. Three inlined places: at the fmul, seconds = measured seconds * factor; at the -# store of the factor into the ratio slot, measured ratio * factor. The call of the shared update (1.1.1 +# store of the factor into the ratio slot, measured ratio * factor. The call of the shared update (1.1.0 # main+0x8ecb40, 1.2.1 main+0x89822c) scales s2 (seconds) and s3 (ratio) by the measured ratio. Upstream's # veneer bodies, inlined at the site. site cloth main 9efc20 1e200900 f81f0fe9 @page:x9 bd400520 1e200900 f84107e9 # fmul s0, s8, s0 @@ -69,7 +69,7 @@ site scene main cea5e0 5e21d900 @page:x9 # scvtf s0, s8 -> adrp x9, page site scene main cea5e8 bd0cc660 2d420520 # str s0, [x19, #0xcc4] -> ldp s0, s1, [x9, #16] site scene main cea5ec 5e21d921 bd0cc660 # scvtf s1, s9 -> str s0, [x19, #0xcc4] -# Spin waits, from tools/spin_waits.py --exefs build/extracted/v111/exefs --patches recomp/patches-v111.txt: loops that only +# Spin waits, from tools/spin_waits.py --exefs build/extracted/v110/exefs --patches recomp/patches-v110.txt: loops that only # another thread can end; their back-edges pass rt_spin_gate (runtime/kernel.c), TOTK_SPIN=0 keeps the core. # spin waits found by tools/spin_waits.py on the 1.1.x modules diff --git a/recomp/patches-v121.txt b/recomp/patches-v121.txt index 3f64f7a3..d73d83f8 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) @@ -81,6 +82,17 @@ site aspect main 12521cc bd414260 f81f0fea @page:x10 bd403940 f84107ea # ldr s0 site aspect main 19b36f0 1e211800 f81f0fea @page:x10 bd403940 f84107ea # fdiv s0, s0, s1 (viewport) site aspect main 1ecdf68 1e211800 f81f0fea @page:x10 bd403940 f84107ea # fdiv s0, s0, s1 (viewport) +# Texture pool offsets past 2 GiB (MacNativeGames ca311e2d). The texture loader keeps a texture's place in its pool as a +# 32-bit offset (main+0xad4294: sub x25, x9, x10, stored as the word w25) and widens it as a signed number for +# nvnTextureBuilderSetStorage's offset and for SetPackagedTextureData's source (the pool's CPU address + offset). The +# shipped heap table keeps every pool far below 2 GiB; a texture pack's heap table with TOTK_APP_POOL raised can make +# one larger (MacNativeGames saw a 3.58 GiB pool), and every texture past 2 GiB then got storage 4 GiB below its pool. +# The offset is widened unsigned instead, the same value below 2 GiB, right up to 4 GiB. 1.4.3 needs no patch: its +# loaders (main+0x583c34, main+0x27ed214) keep the offset in 64 bits. +set pooloff TOTK_POOL_OFFSET_PATCH 1 +site pooloff main ad43f0 93407f22 2a1903e2 # sxtw x2, w25 -> mov w2, w25 (SetStorage offset) +site pooloff main ad440c 8b39c101 8b394101 # add x1, x8, w25, sxtw -> add x1, x8, w25, uxtw (packaged data source) + # Distant detail, after the emulator mods "LOD Improvement v2" (theboy181, sweetmini) and "Disable LOD Quality # Reduction" (main-1.2.1.pchtxt). Those turn a conditional branch into an unconditional one; the generator patches # only plain instructions (recomp/gen.py, the emitter's patch), so these sites feed the branch instead. @@ -966,3 +978,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 2b392773..be6cee6e 100644 --- a/recomp/patches-v143.txt +++ b/recomp/patches-v143.txt @@ -10,6 +10,8 @@ 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 +set cascade TOTK_PATCH_CASCADE 0 # Distant detail (patches-v121.txt explains both): no sites found in this version yet, so the switches do nothing. set lod TOTK_LOD_DETAIL 0 set steady TOTK_LOD_STEADY 0 @@ -979,3 +981,16 @@ 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 + +# Sun shadow cascade distances (runtime/resolution.c res_shadow_range switches the set on). The depth shadow parameter +# object (main+0x6fa3c0) registers depth_shadow_near_0_1..3 = 20, 40, 80 m and depth_shadow_far_0 = 160 m as +# immediates, and the field's .bgsdw files do not override them; with the set on they come from page +64..+76. +site cascade main 6fabb8 52a83408 @page:x8 b9404108 # mov w8, #20.0 -> ldr w8, [page+64] +site cascade main 6fac40 52a84408 @page:x8 b9404508 # mov w8, #40.0 -> ldr w8, [page+68] +site cascade main 6facc8 52a85408 @page:x8 b9404908 # mov w8, #80.0 -> ldr w8, [page+72] +site cascade main 6fad4c 52a86408 @page:x8 b9404d08 # mov w8, #160.0 -> ldr w8, [page+76] diff --git a/recomp/patches.py b/recomp/patches.py index ecc12e7e..544e5086 100644 --- a/recomp/patches.py +++ b/recomp/patches.py @@ -14,14 +14,16 @@ 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 -words must not branch except a final `bl` (lifted with the site's return address); the original must not be a branch -other than `bl`. So the function's control flow, labels and jump tables are the original's. The generator writes -patches.h (set names, switches, the page address) next to layout.h for the runtime. +`if (rt_patch_on[]) { replacement } else { original }`: one byte load and a branch per executed site. Several +sets may patch one address (cheats that write different values there): `if (a) {..} else if (b) {..} else {..}`, the +first set in file order wins. The original may be any instruction but br, blr and ret; a replacement's last word may be +a bl (lifted with the site's return address), a b or b.cond / cbz / tbz (its target joins the function's labels when +inside it) or a ret; earlier words must not branch. Every alternative of a site writes the same flags. The generator +writes patches.h (set names, switches, the page address) next to layout.h for the runtime. A `spin` line names a spin wait found by tools/spin_waits.py: a loop with head that only another thread can end, and the direct branches back to it (module offsets). Each back-edge (checked: a b, b.cond, cbz / cbnz or tbz / tbnz to @@ -46,7 +48,8 @@ def adrp(pc, target, rd): class Table: - """sets: [(name, env, default)]; sites: {module: {offset: (set index, [word | ('page', reg)], original)}}; page.""" + """sets: [(name, env, default)]; sites: {module: {offset: [(set index, [word | ('page', reg)], original)]}} (the + alternatives of a site in file order); page.""" def __init__(self, sets=(), sites=None, page=0, spins=None): self.sets = list(sets) @@ -59,11 +62,12 @@ class Table: return {base + b: base + h for b, h in self.spins.get(name, {}).items()} def for_module(self, name, base): - """{absolute address: (set index, [words])} with @page resolved.""" + """{absolute address: [(set index, [words])]} with @page resolved.""" out = {} - for off, (k, repl, _) in self.sites.get(name, {}).items(): + for off, alts in self.sites.get(name, {}).items(): a = base + off - out[a] = (k, [adrp(a, self.page, w[1]) if isinstance(w, tuple) else w for w in repl]) + out[a] = [(k, [adrp(a, self.page, w[1]) if isinstance(w, tuple) else w for w in repl]) + for k, repl, _ in alts] return out @@ -93,10 +97,12 @@ def load(path, mods): repl.append(('page', int(w[7:]))) else: repl.append(int(w, 16)) - mod = sites.setdefault(f[2], {}) - if off in mod: - raise PatchError(f'{where}: {f[2]}+{off:#x} patched twice') - mod[off] = (index[f[1]], repl, orig) + alts = sites.setdefault(f[2], {}).setdefault(off, []) + if any(k == index[f[1]] for k, _, _ in alts): + raise PatchError(f'{where}: {f[2]}+{off:#x} patched twice by set {f[1]}') + if alts and alts[0][2] != orig: + raise PatchError(f'{where}: {f[2]}+{off:#x} expects {orig:08x}, an earlier line {alts[0][2]:08x}') + alts.append((index[f[1]], repl, orig)) elif f[0] == 'spin' and len(f) >= 4: if f[1] not in bases: raise PatchError(f'{where}: module {f[1]} not in the layout') @@ -118,7 +124,7 @@ def load(path, mods): def check(table, mod, name): """Stops on a site whose original word differs from the module's.""" - for off, (_, _, want) in table.sites.get(name, {}).items(): + for off, [(_, _, want), *_] in table.sites.get(name, {}).items(): a = mod.base + off if not mod.in_text(a): raise PatchError(f'{name}+{off:#x}: patch site outside .text') diff --git a/runtime/amiibo.c b/runtime/amiibo.c new file mode 100644 index 00000000..d93a215b --- /dev/null +++ b/runtime/amiibo.c @@ -0,0 +1,400 @@ +// Amiibo dump loader and Horizon OS NFP state emulation. +#include +#include +#include + +#include "amiibo.h" +#include "host.h" +#include "platform/platform.h" + +static PlatMutex g_amiibo_lock = PLAT_MUTEX_INIT; + +// 540-byte raw NTAG215 binary dump +static uint8_t g_dump[AMIIBO_DUMP_SIZE]; +static bool g_has_dump = false; +static AmiiboInfo g_info; + +static bool g_device_present = true; +static bool g_detecting = false; +static bool g_tag_present = false; +static bool g_mounted = false; +static bool g_auto_remove = true; +static float g_auto_remove_delay_s = 2.5f; + +static uint64_t g_tap_timestamp_ns = 0; +static bool g_timer_active = false; + +static bool amiibo_validate_dump(const uint8_t *data, uint64_t size) { + if (size != AMIIBO_DUMP_SIZE) { + return false; + } + + // NTAG215 Capability Container (CC) at page 3 (offset 0x0C). An amiibo's is F1 10 FF EE, not the E1 of a plain + // NDEF tag (3dbrew, "Amiibo"); the CC is not encrypted, so encrypted dumps carry it too. + static const uint8_t amiibo_cc[4] = {0xF1, 0x10, 0xFF, 0xEE}; + if (memcmp(data + 0x0C, amiibo_cc, sizeof amiibo_cc)) { + return false; + } + + // UID sanity: UID (bytes 0..2 and 4..7) cannot be all zeroes or all 0xFF. + if ((data[0] == 0 && data[1] == 0 && data[2] == 0 && data[4] == 0 && + data[5] == 0 && data[6] == 0 && data[7] == 0) || + (data[0] == 0xFF && data[1] == 0xFF && data[2] == 0xFF && data[4] == 0xFF && + data[5] == 0xFF && data[6] == 0xFF && data[7] == 0xFF)) { + return false; + } + + // ISO 14443-3 / NTAG215 Bit Check Character (BCC) verification. + // Page 0 byte 3 (BCC0) = 0x88 ^ UID[0] ^ UID[1] ^ UID[2] + // Page 2 byte 0 (BCC1) = UID[3] ^ UID[4] ^ UID[5] ^ UID[6] + uint8_t bcc0 = (uint8_t)(0x88 ^ data[0] ^ data[1] ^ data[2]); + uint8_t bcc1 = (uint8_t)(data[4] ^ data[5] ^ data[6] ^ data[7]); + if (data[3] != bcc0 || data[8] != bcc1) { + return false; + } + + return true; +} + +// The 7-byte UID of an NTAG215 dump: bytes 0..2, then 4..7. Byte 3 is the check byte BCC0. +static void copy_uid(uint8_t out[7], const uint8_t *dump) { + memcpy(out, dump, 3); + memcpy(out + 3, dump + 4, 4); +} + +static void parse_dump_info(const char *path) { + memset(&g_info, 0, sizeof(g_info)); + if (path) { + snprintf(g_info.path, sizeof(g_info.path), "%s", path); + const char *slash = strrchr(path, '\\'); + const char *fslash = strrchr(path, '/'); + const char *name = slash ? slash + 1 : (fslash ? fslash + 1 : path); + if (slash && fslash) { + name = (slash > fslash ? slash : fslash) + 1; + } + snprintf(g_info.filename, sizeof(g_info.filename), "%s", name); + } + + copy_uid(g_info.uuid, g_dump); + g_info.uuid_len = 7; + + // Amiibo ID at offset 0x54 (8 bytes, 3dbrew "Amiibo"): character ID and variant, figure type, model number + // (big-endian), series, format version 02 + memcpy(g_info.character_id, g_dump + 0x54, 3); + g_info.nfp_type = g_dump[0x57]; + g_info.numbering_id = ((uint16_t)g_dump[0x58] << 8) | g_dump[0x59]; + g_info.series_id = g_dump[0x5A]; + g_info.version = g_dump[0x5B]; +} + +void amiibo_init(void) { + plat_mutex_lock(&g_amiibo_lock); + g_device_present = true; + g_detecting = false; + g_tag_present = false; + g_mounted = false; + g_has_dump = false; + g_timer_active = false; + memset(g_dump, 0, sizeof(g_dump)); + memset(&g_info, 0, sizeof(g_info)); + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_set_device_present(bool present) { + plat_mutex_lock(&g_amiibo_lock); + g_device_present = present; + plat_mutex_unlock(&g_amiibo_lock); +} + +bool amiibo_is_device_present(void) { + plat_mutex_lock(&g_amiibo_lock); + bool res = g_device_present; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +// While the game waits for a tag it stops and starts detection again several times a second (tens with the menu open), +// so the log names the first few and then a count every 200th start. +static uint64_t g_detect_starts; +static uint64_t g_detect_stop_ns; // the last stop: the menu holds "searching" across the game's restarts + +void amiibo_start_detection(void) { + plat_mutex_lock(&g_amiibo_lock); + g_detecting = true; + uint64_t n = ++g_detect_starts; + if (n <= 3) rt_log("amiibo: start detection"); + else if (n % 200 == 0) rt_log("amiibo: detection started %llu times so far", (unsigned long long)n); + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_stop_detection(void) { + plat_mutex_lock(&g_amiibo_lock); + if (g_detecting) g_detect_stop_ns = plat_now_ns(); + g_detecting = false; + if (g_detect_starts <= 3) rt_log("amiibo: stop detection"); + plat_mutex_unlock(&g_amiibo_lock); +} + +bool amiibo_is_detecting(void) { + plat_mutex_lock(&g_amiibo_lock); + bool res = g_detecting; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +bool amiibo_mount(void) { + plat_mutex_lock(&g_amiibo_lock); + if (!g_device_present || !g_tag_present) { + plat_mutex_unlock(&g_amiibo_lock); + return false; + } + g_mounted = true; + rt_log("amiibo: mounted tag '%s'", g_info.filename[0] ? g_info.filename : "unknown"); + plat_mutex_unlock(&g_amiibo_lock); + return true; +} + +void amiibo_unmount(void) { + plat_mutex_lock(&g_amiibo_lock); + g_mounted = false; + rt_log("amiibo: unmounted tag"); + plat_mutex_unlock(&g_amiibo_lock); +} + +bool amiibo_is_mounted(void) { + plat_mutex_lock(&g_amiibo_lock); + bool res = g_mounted; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +AmiiboDeviceState amiibo_get_device_state(void) { + plat_mutex_lock(&g_amiibo_lock); + if (!g_device_present) { + plat_mutex_unlock(&g_amiibo_lock); + return AMIIBO_DEV_STATE_UNAVAILABLE; + } + if (g_mounted) { + plat_mutex_unlock(&g_amiibo_lock); + return AMIIBO_DEV_STATE_TAG_MOUNTED; + } + if (g_tag_present) { + plat_mutex_unlock(&g_amiibo_lock); + return AMIIBO_DEV_STATE_TAG_FOUND; + } + if (g_detecting) { + plat_mutex_unlock(&g_amiibo_lock); + return AMIIBO_DEV_STATE_SEARCHING; + } + plat_mutex_unlock(&g_amiibo_lock); + return AMIIBO_DEV_STATE_INITIALIZED; +} + +AmiiboDeviceState amiibo_get_reader_state(void) { + AmiiboDeviceState state = amiibo_get_device_state(); + if (state != AMIIBO_DEV_STATE_INITIALIZED) return state; + plat_mutex_lock(&g_amiibo_lock); + bool recent = g_detect_stop_ns && plat_now_ns() - g_detect_stop_ns < 1000000000ull; + plat_mutex_unlock(&g_amiibo_lock); + return recent ? AMIIBO_DEV_STATE_SEARCHING : state; +} + +bool amiibo_load_file(const char *path) { + if (!path || !*path) return false; + uint64_t size = 0; + uint8_t *data = plat_file_read(path, &size); + if (!data) { + rt_log("amiibo: failed to read file: %s", path); + return false; + } + if (size != AMIIBO_DUMP_SIZE) { + rt_log("amiibo: invalid dump size (%llu bytes, expected exactly %d): %s", + (unsigned long long)size, AMIIBO_DUMP_SIZE, path); + free(data); + return false; + } + if (!amiibo_validate_dump(data, size)) { + rt_log("amiibo: dump failed NTAG215 structure sanity checks: %s", path); + free(data); + return false; + } + + plat_mutex_lock(&g_amiibo_lock); + memcpy(g_dump, data, AMIIBO_DUMP_SIZE); + g_has_dump = true; + parse_dump_info(path); + plat_mutex_unlock(&g_amiibo_lock); + + free(data); + rt_log("amiibo: loaded dump '%s' (char_id: %02x%02x%02x, series: %02x, num: %04x)", + g_info.filename, g_info.character_id[0], g_info.character_id[1], g_info.character_id[2], + g_info.series_id, g_info.numbering_id); + return true; +} + +void amiibo_tap_tag(void) { + plat_mutex_lock(&g_amiibo_lock); + if (!g_has_dump) { + plat_mutex_unlock(&g_amiibo_lock); + return; + } + g_tag_present = true; + g_tap_timestamp_ns = plat_now_ns(); + if (g_auto_remove) { + g_timer_active = true; + } + rt_log("amiibo: tag tapped on reader ('%s')", g_info.filename); + plat_mutex_unlock(&g_amiibo_lock); +} + +bool amiibo_scan_and_tap(const char *path) { + if (!amiibo_load_file(path)) return false; + amiibo_tap_tag(); + return true; +} + +void amiibo_remove_tag(void) { + plat_mutex_lock(&g_amiibo_lock); + if (g_tag_present || g_mounted) { + g_tag_present = false; + g_mounted = false; + g_timer_active = false; + rt_log("amiibo: tag removed from reader"); + } + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_tick(void) { + plat_mutex_lock(&g_amiibo_lock); + if (g_timer_active && g_tag_present) { + uint64_t now = plat_now_ns(); + uint64_t diff = now > g_tap_timestamp_ns ? now - g_tap_timestamp_ns : 0; + uint64_t limit_ns = (uint64_t)(g_auto_remove_delay_s * 1000000000.0f); + if (diff >= limit_ns) { + g_tag_present = false; + g_mounted = false; + g_timer_active = false; + rt_log("amiibo: auto-removed tag after %.1fs timeout", (double)g_auto_remove_delay_s); + } + } + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_set_auto_remove(bool enable) { + plat_mutex_lock(&g_amiibo_lock); + g_auto_remove = enable; + if (!enable) g_timer_active = false; + plat_mutex_unlock(&g_amiibo_lock); +} + +bool amiibo_get_auto_remove(void) { + plat_mutex_lock(&g_amiibo_lock); + bool res = g_auto_remove; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +void amiibo_set_auto_remove_delay_s(float seconds) { + plat_mutex_lock(&g_amiibo_lock); + if (seconds >= 0.5f && seconds <= 10.0f) g_auto_remove_delay_s = seconds; + plat_mutex_unlock(&g_amiibo_lock); +} + +float amiibo_get_auto_remove_delay_s(void) { + plat_mutex_lock(&g_amiibo_lock); + float res = g_auto_remove_delay_s; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +bool amiibo_has_tag(void) { + plat_mutex_lock(&g_amiibo_lock); + bool res = g_has_dump && g_tag_present; + plat_mutex_unlock(&g_amiibo_lock); + return res; +} + +const AmiiboInfo *amiibo_get_current_info(void) { + return &g_info; +} + +const uint8_t *amiibo_get_raw_data(void) { + return g_dump; +} + +void amiibo_get_tag_info(void *out_tag_info) { + plat_mutex_lock(&g_amiibo_lock); + uint8_t *out = (uint8_t *)out_tag_info; + memset(out, 0, 0x58); + // UUID: offset 0x00, 10 bytes max + copy_uid(out, g_dump); + out[0x0A] = 7; // UUID length + // Protocol at offset 0x20: TypeA = 1 + *(uint32_t *)(out + 0x20) = 1; + // TagType at offset 0x24: Type2 / NTAG215 = 2 + *(uint32_t *)(out + 0x24) = 2; + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_get_model_info(void *out_model_info) { + plat_mutex_lock(&g_amiibo_lock); + uint8_t *out = (uint8_t *)out_model_info; + memset(out, 0, 0x40); + // nn::nfp::ModelInfo from the amiibo ID at 0x54: character ID and variant in the tag's byte order, series, model + // number little-endian (big-endian on the tag), figure type. The game tells figures of one character apart by the + // model number. + memcpy(out + 0x00, g_dump + 0x54, 3); // character ID, variant + out[0x03] = g_dump[0x5A]; // series + out[0x04] = g_dump[0x59]; // model number + out[0x05] = g_dump[0x58]; + out[0x06] = g_dump[0x57]; // figure type + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_get_common_info(void *out_common_info) { + plat_mutex_lock(&g_amiibo_lock); + uint8_t *out = (uint8_t *)out_common_info; + memset(out, 0, 0x40); + // Last write date: 0x00..0x03 + out[0x00] = 0xE7; out[0x01] = 0x07; // 2023, u16 little-endian + out[0x02] = 0x05; out[0x03] = 0x0C; // May 12 + // Write counter: 0x04..0x05 + out[0x04] = 0x00; out[0x05] = 0x01; + // Version: 0x06 + out[0x06] = 0x02; + // Application area size: 0x08..0x0B = 0xD8 (216 bytes) + *(uint32_t *)(out + 0x08) = 0xD8; + plat_mutex_unlock(&g_amiibo_lock); +} + +void amiibo_get_register_info(void *out_register_info) { + plat_mutex_lock(&g_amiibo_lock); + uint8_t *out = (uint8_t *)out_register_info; + memset(out, 0, 0x100); + // Mii CharInfo at 0x00..0x57 (dummy valid mii) + out[0x00] = 0x03; // version 3 + // First write date at 0x58..0x5B + out[0x58] = 0xE7; out[0x59] = 0x07; // 2023, u16 little-endian + out[0x5A] = 0x05; out[0x5B] = 0x0C; + // Amiibo Name at 0x5C: copy up to 40 bytes NUL-terminated + const char *name = g_info.filename[0] ? g_info.filename : "Amiibo"; + snprintf((char *)(out + 0x5C), 0x29, "%s", name); + plat_mutex_unlock(&g_amiibo_lock); +} + +int amiibo_get_application_area(void *out_buf, uint32_t max_size) { + plat_mutex_lock(&g_amiibo_lock); + if (!g_has_dump || !out_buf) { + plat_mutex_unlock(&g_amiibo_lock); + return 0; + } + uint32_t to_copy = max_size < 0xD8 ? max_size : 0xD8; + // Decrypted/payload application area starts at offset 0xDC (or 0x0A0..0x178 in raw dump) + if (0xDC + to_copy <= AMIIBO_DUMP_SIZE) { + memcpy(out_buf, g_dump + 0xDC, to_copy); + } else { + memset(out_buf, 0, to_copy); + } + plat_mutex_unlock(&g_amiibo_lock); + return (int)to_copy; +} diff --git a/runtime/amiibo.h b/runtime/amiibo.h new file mode 100644 index 00000000..5a3e96d4 --- /dev/null +++ b/runtime/amiibo.h @@ -0,0 +1,87 @@ +// Amiibo loader and state management subsystem (540-byte NTAG215 binary dumps). +#pragma once +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define AMIIBO_DUMP_SIZE 540 + +// Device state corresponding to nn::nfp / nn::nfc DeviceState +typedef enum { + AMIIBO_DEV_STATE_INITIALIZED = 0, + AMIIBO_DEV_STATE_SEARCHING = 1, + AMIIBO_DEV_STATE_TAG_FOUND = 2, + AMIIBO_DEV_STATE_TAG_REMOVED = 3, + AMIIBO_DEV_STATE_TAG_MOUNTED = 4, + AMIIBO_DEV_STATE_UNAVAILABLE = 5 +} AmiiboDeviceState; + +// Amiibo identification info +typedef struct { + uint8_t uuid[10]; + uint8_t uuid_len; + uint8_t character_id[3]; + uint8_t series_id; + uint16_t numbering_id; + uint8_t nfp_type; + uint8_t version; + char path[512]; + char filename[128]; +} AmiiboInfo; + +// Subsystem lifecycle +void amiibo_init(void); + +// Hardware / device emulation status +void amiibo_set_device_present(bool present); +bool amiibo_is_device_present(void); + +// Tag detection & lifecycle +void amiibo_start_detection(void); +void amiibo_stop_detection(void); +bool amiibo_is_detecting(void); + +// Tag mounting +bool amiibo_mount(void); +void amiibo_unmount(void); +bool amiibo_is_mounted(void); + +// State query +AmiiboDeviceState amiibo_get_device_state(void); +// The state the menu shows: searching also within a second after a stop, since the game restarts detection many times +// a second while it waits for a tag. The game reads amiibo_get_device_state. +AmiiboDeviceState amiibo_get_reader_state(void); + +// File loading & triggering actions +bool amiibo_load_file(const char *path); +bool amiibo_scan_and_tap(const char *path); +void amiibo_tap_tag(void); +void amiibo_remove_tag(void); + +// Timer tick called during menu / frame updates +void amiibo_tick(void); + +// Options +void amiibo_set_auto_remove(bool enable); +bool amiibo_get_auto_remove(void); +void amiibo_set_auto_remove_delay_s(float seconds); +float amiibo_get_auto_remove_delay_s(void); + +// Tag inspection +bool amiibo_has_tag(void); +const AmiiboInfo *amiibo_get_current_info(void); +const uint8_t *amiibo_get_raw_data(void); + +// Horizon OS buffers extraction (called by NFP IPC/override services) +void amiibo_get_tag_info(void *out_tag_info); +void amiibo_get_model_info(void *out_model_info); +void amiibo_get_common_info(void *out_common_info); +void amiibo_get_register_info(void *out_register_info); +int amiibo_get_application_area(void *out_buf, uint32_t max_size); + +#ifdef __cplusplus +} +#endif diff --git a/runtime/audio_effects.c b/runtime/audio_effects.c index 2e030a4a..d3768ef6 100644 --- a/runtime/audio_effects.c +++ b/runtime/audio_effects.c @@ -22,8 +22,11 @@ struct AudioEffectDsp { }; static int channel_ok(unsigned n) { return n==1 || n==2 || n==4 || n==6; } static unsigned sample_count(unsigned rate, float ms) { return (unsigned)nearbyintf(rate*ms); } -/* The reference DelayLine wraps at CurrentSampleCount-1, including when its - backing allocation is larger. Preserve this timing for sample comparisons. */ +/* The ring has `count` slots. The reference DelayLine wraps its cursor at + CurrentSampleCount-1 while its taps index modulo CurrentSampleCount, so once + per turn every tap reads a slot that is never written (a one-sample dropout + per tap, audible as grain). The cursor and the taps share one ring length here; + each delay is one sample longer than the reference. */ static void line_set(DelayLine *d, unsigned n) { d->count=nmax?n:d->max; d->at=0; } static void line_init(DelayLine *d, unsigned rate, float ms) { d->max=sample_count(rate,ms); d->data=calloc((size_t)d->max+1,sizeof(float)); @@ -32,12 +35,12 @@ static void line_init(DelayLine *d, unsigned rate, float ms) { static float line_read(const DelayLine *d) { return d->data[d->at]; } static float line_update(DelayLine *d,float v) { float old=line_read(d);d->data[d->at++]=v; - if(d->at >= (d->count?d->count-1:0))d->at=0; + if(d->at >= d->count)d->at=0; return old; } static float line_tap(const DelayLine *d,unsigned sample,int offset) { int at=(int)d->at-(int)sample-offset; - if(at<0)at+=(int)d->count; + if(d->count){at%=(int)d->count;if(at<0)at+=(int)d->count;} if(at<0 || (unsigned)at>d->max)rt_fatal("audio effect delay tap bounds"); return d->data[at]; } diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index 6dc625b8..e0192db4 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -5,10 +5,11 @@ // // Per 5 ms frame the render thread (audren.c, renderer lock held) decodes every started voice (PCM8/16/24/32, float, // DSP-ADPCM) along the sample path the voice clock consumes, resamples it linearly, runs the voice biquads, scales it -// into its destination mix, folds the submixes, runs the mix effects and writes the device sink's stereo mix into a -// single-producer single-consumer ring. The SDL audio thread drains that ring without taking any lock: an empty ring +// into its destination mix, folds the submixes, runs the mix effects and writes the device sink's mix (stereo, or 5.1 +// with TOTK_AUDIO_CHANNELS, output_channels) into a single-producer single-consumer ring. The SDL audio thread drains +// that ring without taking any lock: an empty ring // plays silence and never blocks the render thread or the guest. TOTK_AUDIO=none (or TOTK_AUDIO_OFF) mixes without a -// device; TOTK_AUDIO_PREFILL_MS (0..500, default 20) is the queued audio the output waits for after start or underrun. +// device; TOTK_AUDIO_PREFILL_MS (0..500, default 20, 60 on Linux) is the queued audio the output waits for after start or underrun. // The device's clock paces the render thread, as the audio hardware paces the Switch's DSP (audio_mixer_pace_ns): a // frame is rendered when the ring holds less than the output target, max(prefill, two device periods), so the queue and // the delay from the game to the speakers stay there and the host and device clocks cannot drift apart. LASRecomp and @@ -32,7 +33,8 @@ #include "ipc.h" #define MAX_FRAME 1024 -#define RING_FRAMES 16384 // stereo frames, about 340 ms at 48 kHz +#define RING_FRAMES 16384 // output frames, about 340 ms at 48 kHz +#define OUT_MAX 6 // output channels at most: 5.1 #define QUEUE_SLACK_MS 40 // queued audio above the target plus the device's request that ring_pull skips #define STALL_MS 50 // the device took nothing this long (at least 4 periods): the timer paces #define SPLITTER_UNUSED 0xffffffffu @@ -51,7 +53,8 @@ typedef struct Mixer { int *offset; // first mix buffer of each mix id SplitInfo *split; int nsplit; SplitDest *dest; int ndest; SDL_AudioStream *stream; int device_ok, rate; - float ring[RING_FRAMES * 2]; + int channels; // output channels, 2 or 6 (output_channels) + float ring[RING_FRAMES * OUT_MAX]; _Atomic uint64_t wr, rd, underruns, overruns, missing_frames, callback_frames, callback_nonzero, skipped_frames; _Atomic int playing; int ready; // ready: the callback's own state int target, period; // output target and device period, frames (output_open) @@ -61,7 +64,7 @@ typedef struct Mixer { EffectState *effect; int *effect_order; unsigned effect_reported; uint64_t *sink_address; uint32_t *sink_size, *sink_offset, *sink_old, *sink_written; unsigned reported; // one-time reports - float voice_scratch[6][MAX_FRAME], filter_scratch[MAX_FRAME], output_scratch[MAX_FRAME * 2]; + float voice_scratch[6][MAX_FRAME], filter_scratch[MAX_FRAME], output_scratch[MAX_FRAME * OUT_MAX]; double level_sum, level_peak; uint64_t level_count, level_frames; int first_sound_logged; int prefill; // render blocks } Mixer; @@ -90,16 +93,47 @@ static void report_once(Mixer *m, unsigned bit, const char *msg, int value) { // ---- output: SDL3 audio stream fed from the ring // How much audio the output waits for before it starts, and again after an underrun: TOTK_AUDIO_PREFILL_MS rounded up // to whole 5 ms render blocks. Returns -1 for a value outside 0..500 ms. +// The Linux default is higher: at 20 ms PipeWire's queue (max(prefill, two 1024-sample periods)) underran +// thousands of times within minutes in busy scenes, and 60 ms removed the underruns. +#ifdef __linux__ +#define PREFILL_DEFAULT_MS 60 +#else +#define PREFILL_DEFAULT_MS 20 +#endif static int prefill_blocks_for(const char *e) { - if (!e || !*e) return 4; + if (!e || !*e) return PREFILL_DEFAULT_MS / 5; char *end; double ms = strtod(e, &end); if (end == e || *end || !(ms >= 0) || ms > 500) return -1; return (int)ceil(ms / 5.0); } -// Fills `frames` stereo frames from the ring (zeros for what is missing). Lock-free; runs on SDL's audio thread. +// TOTK_AUDIO_CHANNELS: the output channels of this run, decided once. stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR, +// the device sink's order and SDL's); auto gives 6 when the default playback device has at least six channels (a 5.1 +// or 7.1 speaker setup in Windows; SDL places 5.1 on 7.1) and 2 otherwise. IAudioDevice reports the same count to the +// game (GetActiveChannelCount), which picks its sink layout from it. TOTK_AUDIO=none: 2, without touching SDL. +static int output_channels(void) { + static _Atomic int decided; + int n = atomic_load(&decided); + if (n) return n; + const char *want = cfg_value("TOTK_AUDIO_CHANNELS"), *mode = cfg_get("TOTK_AUDIO"); + if ((mode && !strcmp(mode, "none")) || cfg_get("TOTK_AUDIO_OFF")) n = 2; + else if (!strcmp(want, "5.1")) n = 6; + else if (!strcmp(want, "stereo")) n = 2; + else { + SDL_AudioSpec dev = {0}; + n = SDL_InitSubSystem(SDL_INIT_AUDIO) && SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &dev, NULL) && + dev.channels >= 6 ? 6 : 2; + rt_log("audio: TOTK_AUDIO_CHANNELS=auto: the default device has %d channels, output %s", dev.channels, + n == 6 ? "5.1" : "stereo"); + } + int expected = 0; + return atomic_compare_exchange_strong(&decided, &expected, n) ? n : expected; +} + +// Fills `frames` output frames from the ring (zeros for what is missing). Lock-free; runs on SDL's audio thread. static void ring_pull(Mixer *m, float *out, uint32_t frames) { + const int oc = m->channels; atomic_fetch_add(&m->callback_frames, frames); unsigned epoch = atomic_load_explicit(&m->stop_epoch, memory_order_acquire); uint64_t rd = atomic_load(&m->rd), wr = atomic_load_explicit(&m->wr, memory_order_acquire), have = wr - rd; @@ -114,7 +148,7 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { m->ready = 0; } if (!atomic_load_explicit(&m->playing, memory_order_acquire)) { - memset(out, 0, (size_t)frames * 2 * sizeof(float)); + memset(out, 0, (size_t)frames * oc * sizeof(float)); m->ready = 0; atomic_store_explicit(&m->rd, wr, memory_order_release); return; @@ -124,7 +158,7 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { if (prefill > RING_FRAMES) prefill = RING_FRAMES; if (!m->ready) { if (have < prefill) { - memset(out, 0, (size_t)frames * 2 * sizeof(float)); + memset(out, 0, (size_t)frames * oc * sizeof(float)); return; } m->ready = 1; @@ -140,13 +174,16 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { uint64_t nonzero = 0; for (uint32_t i = 0; i < n; i++) { uint64_t k = (rd + i) % RING_FRAMES; - out[2 * i] = m->ring[2 * k]; - out[2 * i + 1] = m->ring[2 * k + 1]; - if (out[2 * i] != 0 || out[2 * i + 1] != 0) nonzero++; + int sound = 0; + for (int c = 0; c < oc; c++) { + out[oc * i + c] = m->ring[oc * k + c]; + sound |= out[oc * i + c] != 0; + } + nonzero += sound; } atomic_fetch_add(&m->callback_nonzero, nonzero); if (n < frames) { - memset(out + 2 * n, 0, (size_t)(frames - n) * 2 * sizeof(float)); + memset(out + (size_t)oc * n, 0, (size_t)(frames - n) * oc * sizeof(float)); atomic_fetch_add(&m->underruns, 1); atomic_fetch_add(&m->missing_frames, frames - n); m->ready = 0; @@ -157,15 +194,16 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { static void SDLCALL sdl_pull(void *user, SDL_AudioStream *stream, int additional, int total) { (void)total; Mixer *m = user; - float tmp[2 * 1024]; + float tmp[OUT_MAX * 1024]; + const uint32_t frame_bytes = (uint32_t)m->channels * sizeof(float); atomic_store_explicit(&m->pulled_ns, kern_now_ns(), memory_order_release); while (additional > 0) { - uint32_t frames = (uint32_t)additional / (2 * sizeof(float)); + uint32_t frames = (uint32_t)additional / frame_bytes; if (!frames) break; if (frames > 1024) frames = 1024; ring_pull(m, tmp, frames); - if (!SDL_PutAudioStreamData(stream, tmp, (int)(frames * 2 * sizeof(float)))) break; - additional -= (int)(frames * 2 * sizeof(float)); + if (!SDL_PutAudioStreamData(stream, tmp, (int)(frames * frame_bytes))) break; + additional -= (int)(frames * frame_bytes); } } @@ -179,7 +217,7 @@ static void output_open(Mixer *m, int rate) { rt_log("audio: SDL audio init failed (%s): mixing without sound", SDL_GetError()); return; } - SDL_AudioSpec spec = {SDL_AUDIO_F32, 2, rate}; + SDL_AudioSpec spec = {SDL_AUDIO_F32, m->channels, rate}; m->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, sdl_pull, m); if (!m->stream) { rt_log("audio: no output device (%s): mixing without sound", SDL_GetError()); @@ -192,7 +230,7 @@ static void output_open(Mixer *m, int rate) { return; } m->device_ok = 1; - SDL_AudioSpec dev; + SDL_AudioSpec dev = {0}; int period = 0; if (!SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(m->stream), &dev, &period) || period <= 0) period = m->frame; m->period = period; @@ -201,8 +239,9 @@ static void output_open(Mixer *m, int rate) { uint64_t period_ns = (uint64_t)period * 1000000000ull / (uint64_t)rate; m->stall_ns = 4 * period_ns > STALL_MS * 1000000ull ? 4 * period_ns : STALL_MS * 1000000ull; const char *name = SDL_GetAudioDeviceName(SDL_GetAudioStreamDevice(m->stream)); - rt_log("audio: SDL device '%s' opened, %d Hz stereo float, prefill %d ms, device period %d frames; the device paces " - "rendering at %.1f ms queued", name ? name : "?", rate, m->prefill * 5, period, m->target * 1000.0 / rate); + rt_log("audio: SDL device '%s' opened, %d Hz %s float (device %d channels), prefill %d ms, device period %d frames; " + "the device paces rendering at %.1f ms queued", name ? name : "?", rate, m->channels == 6 ? "5.1" : "stereo", + dev.channels, m->prefill * 5, period, m->target * 1000.0 / rate); } int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now) { @@ -217,7 +256,8 @@ int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now) { } // Render thread only. -static void output_push(Mixer *m, const float *lr, int frames) { +static void output_push(Mixer *m, const float *out, int frames) { + const int oc = m->channels; uint64_t wr = atomic_load(&m->wr), rd = atomic_load_explicit(&m->rd, memory_order_acquire); if (m->device_ok && wr + (uint64_t)frames - rd > RING_FRAMES) { // device stalled: drop this frame atomic_fetch_add(&m->overruns, 1); @@ -225,24 +265,51 @@ static void output_push(Mixer *m, const float *lr, int frames) { } for (int i = 0; i < frames; i++) { uint64_t k = (wr + (uint64_t)i) % RING_FRAMES; - m->ring[2 * k] = lr[2 * i]; - m->ring[2 * k + 1] = lr[2 * i + 1]; + memcpy(&m->ring[oc * k], &out[oc * i], (size_t)oc * sizeof(float)); } atomic_store_explicit(&m->wr, wr + (uint64_t)frames, memory_order_release); if (!m->device_ok) atomic_store(&m->rd, wr + (uint64_t)frames); // no device: the ring only keeps the latest frames } +// TOTK_AUDIO_DUMP=: the sink mix before the clamp as raw interleaved float32 stereo, and every 5 s one log line with +// how many frames were rendered less than 1 ms after the one before (a burst refill). File writes on the render thread: +// for diagnosis only. +static void sink_dump(const float *raw, int frames) { + static FILE *f; + static int opened; + static uint64_t last, window_start, total, bursts; + if (!opened) { + opened = 1; + const char *path = cfg_get("TOTK_AUDIO_DUMP"); + if (path && *path) f = fopen(path, "wb"); + } + if (!f) return; + fwrite(raw, sizeof(float), (size_t)frames * 2, f); + uint64_t now = kern_now_ns(); + total++; + if (last && now - last < 1000000ull) bursts++; + last = now; + if (!window_start) window_start = now; + if (now - window_start >= 5000000000ull) { + rt_log("audio: dump: %llu frames in %.1f s, %llu rendered under 1 ms after the previous", (unsigned long long)total, + (now - window_start) / 1e9, (unsigned long long)bursts); + total = bursts = 0; + window_start = now; + fflush(f); + } +} + // Level meter (TOTK_TRACE=audio): one line per 5 s of output, and the first nonzero frame. -static void level_note(AudioRenderer *r, Mixer *m, const float *lr, int frames) { +static void level_note(AudioRenderer *r, Mixer *m, const float *out, int frames) { if (!(rt_trace & RT_TRACE_AUDIO)) return; int sound = 0; - for (int k = 0; k < 2 * frames; k++) { - double x = lr[k]; + for (int k = 0; k < m->channels * frames; k++) { + double x = out[k]; m->level_sum += x * x; if (fabs(x) > m->level_peak) m->level_peak = fabs(x); if (x != 0) sound = 1; } - m->level_count += 2 * (uint64_t)frames; + m->level_count += (uint64_t)m->channels * (uint64_t)frames; m->level_frames += (uint64_t)frames; if (sound && !m->first_sound_logged) { m->first_sound_logged = 1; @@ -290,9 +357,10 @@ void audio_mixer_open(AudioRenderer *r) { const char *pe = cfg_get("TOTK_AUDIO_PREFILL_MS"); m->prefill = prefill_blocks_for(pe); if (m->prefill < 0) { - rt_log("audio: TOTK_AUDIO_PREFILL_MS=%s is not 0..500 ms: using 20", pe); - m->prefill = 4; + rt_log("audio: TOTK_AUDIO_PREFILL_MS=%s is not 0..500 ms: using %d", pe, PREFILL_DEFAULT_MS); + m->prefill = PREFILL_DEFAULT_MS / 5; } + m->channels = output_channels(); r->mixer = m; atomic_store(&m->playing, r->state == 0); output_open(m, r->param.sample_rate); @@ -505,8 +573,25 @@ static void walk_step(Walk *w) { /* the same transitions as advance_voice in a walk_skip_empty(w); } /* Decodes `frames` output samples per channel into out[c][k]; the renderer advances the voice afterwards by the same amount. */ +static double voice_step(const AudioRenderer *r, const ServerVoice *v) { + return v->in.sample_rate * (v->in.pitch > 0 ? v->in.pitch : 1.0) / r->param.sample_rate; +} +/* Source samples the voice consumes over `frames` output samples, counted with the exact accumulation voice_render steps by + (float rounding makes `fraction + seconds * rate * pitch` differ from it by one sample on some frames, a seam click); + leaves the fractional position in v->fraction. */ +uint64_t audio_voice_consumed(const AudioRenderer *r, ServerVoice *v, int frames) { + const double step = voice_step(r, v); + double frac = v->fraction; + uint64_t consumed = 0; + for (int k = 0; k < frames; k++) { + frac += step; + while (frac >= 1.0) { frac -= 1.0; consumed++; } + } + v->fraction = frac; + return consumed; +} static void voice_render(AudioRenderer *r, ServerVoice *v, float out[6][MAX_FRAME], int frames) { - const double step = v->in.sample_rate * (v->in.pitch > 0 ? v->in.pitch : 1.0) / r->param.sample_rate; + const double step = voice_step(r, v); Walk w = {v, v->index, v->offset, v->loops_done, {0}, 0, v->adpcm.history[0], v->adpcm.history[1], v->adpcm.buffer, v->adpcm.at}; memcpy(w.valid, v->valid, sizeof w.valid); walk_skip_empty(&w); @@ -545,6 +630,7 @@ static void mix_aux(AudioRenderer *r, int mix, const AudrenEffectIn *e) { uint8_t *send=ipc_guest(p.send_info,128+(uint64_t)p.capacity*4), *ret=ipc_guest(p.return_info,128+(uint64_t)p.capacity*4); uint32_t *sh=(uint32_t *)(send+64),*rh=(uint32_t *)(ret+64); int32_t *sd=(int32_t *)(send+128),*rd=(int32_t *)(ret+128); uint32_t wr=sh[1],read=rh[0],total=p.channels*(uint32_t)m->frame; + if(e->enabled) report_once(m,6,"auxiliary effect in use, channels",(int)p.channels); for(unsigned c=0;c=r->mixes[mix].buffer_count || p.output[c]>=r->mixes[mix].buffer_count)rt_fatal("auxiliary effect mix buffer"); float *input=mixbuf(m,m->offset[mix]+p.input[c]),*output=mixbuf(m,m->offset[mix]+p.output[c]); @@ -793,30 +879,42 @@ void audio_mixer_render(AudioRenderer *r, double seconds) { if (r->sinks[i].type == SINK_DEVICE && !sink) sink = &r->sinks[i]; else if (r->sinks[i].type == SINK_CIRCULAR){mix_circular_sink(r,i);circular=1;} } else {m->sink_address[i]=0;m->sink_size[i]=m->sink_offset[i]=m->sink_old[i]=m->sink_written[i]=0;} - float *lr=m->output_scratch; - memset(lr, 0, sizeof(float) * 2 * (size_t)frames); + const int oc = m->channels; + float *out=m->output_scratch; + memset(out, 0, sizeof(float) * (size_t)oc * (size_t)frames); if (sink && sink->input_count >= 1 && sink->input_count <= 6) { + if (!(m->reported & (1u << 6))) { + m->reported |= 1u << 6; + rt_log("audio: device sink has %u inputs, output %s", sink->input_count, oc == 6 ? "5.1" : "stereo"); + } const float gain = host_volume() * atomic_load(&device_volume) * (r->mixes[0].in_use ? r->mixes[0].volume : 1.0f); const float *ch[6] = {0}; + float raw[2 * MAX_FRAME]; for (uint32_t c = 0; c < sink->input_count; c++) if (sink->inputs[c] >= 0 && sink->inputs[c] < m->nbuf) ch[c] = mixbuf(m, sink->inputs[c]); for (int k = 0; k < frames; k++) { - float l, rr; - if (sink->input_count >= 6) { /* FL FR C LFE RL RR -> stereo */ + float s[OUT_MAX] = {0}; /* FL FR C LFE RL RR */ + if (sink->input_count >= 6 && oc == 6) { + for (int c = 0; c < 6; c++) s[c] = ch[c] ? ch[c][k] : 0; + } else if (sink->input_count >= 6) { /* 5.1 -> stereo; the LFE is left out, as the Switch's downmix does */ float ff = sink->downmix_enabled ? sink->downmix[0] : 1.0f, cf = sink->downmix_enabled ? sink->downmix[1] : 0.7071f, sf = sink->downmix_enabled ? sink->downmix[2] : 0.7071f; float C = ch[2] ? ch[2][k] : 0; - l = ff * (ch[0] ? ch[0][k] : 0) + cf * C + sf * (ch[4] ? ch[4][k] : 0); - rr = ff * (ch[1] ? ch[1][k] : 0) + cf * C + sf * (ch[5] ? ch[5][k] : 0); - } else if (sink->input_count >= 2) { l = ch[0] ? ch[0][k] : 0; rr = ch[1] ? ch[1][k] : 0; } - else l = rr = ch[0] ? ch[0][k] : 0; - l *= gain; rr *= gain; - lr[2 * k] = l > 1 ? 1 : l < -1 ? -1 : l; lr[2 * k + 1] = rr > 1 ? 1 : rr < -1 ? -1 : rr; + s[0] = ff * (ch[0] ? ch[0][k] : 0) + cf * C + sf * (ch[4] ? ch[4][k] : 0); + s[1] = ff * (ch[1] ? ch[1][k] : 0) + cf * C + sf * (ch[5] ? ch[5][k] : 0); + } else if (sink->input_count >= 2) { s[0] = ch[0] ? ch[0][k] : 0; s[1] = ch[1] ? ch[1][k] : 0; } + else s[0] = s[1] = ch[0] ? ch[0][k] : 0; + raw[2 * k] = s[0] * gain; raw[2 * k + 1] = s[1] * gain; /* TOTK_AUDIO_DUMP: the front pair before clipping */ + for (int c = 0; c < oc; c++) { + float x = s[c] * gain; + out[oc * k + c] = x > 1 ? 1 : x < -1 ? -1 : x; + } } + sink_dump(raw, frames); } else if (!sink && !circular) report_once(m, 5, "no device sink in use", r->param.sink_count); - level_note(r, m, lr, frames); - output_push(m, lr, frames); + level_note(r, m, out, frames); + output_push(m, out, frames); } -// ---- IAudioDevice: one stereo output device +// ---- IAudioDevice: one output device, stereo or 5.1 (output_channels) static const char device_name[] = "AudioTvOutput"; static void put_name(const IpcBuffer *b, IpcResponse *rs, int pointer) { @@ -855,7 +953,7 @@ void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) put_name(&b, rs, pointer); return; } - case 5: { int32_t n = 2; IPC_OUT(rs, n); return; } // GetActiveChannelCount + case 5: { int32_t n = output_channels(); IPC_OUT(rs, n); return; } // GetActiveChannelCount case 4: case 11: case 12: { // QueryAudioDeviceSystemEvent, ...InputEvent, ...OutputEvent uint32_t ev = __atomic_load_n(&change_event, __ATOMIC_ACQUIRE); if (!ev) { diff --git a/runtime/audio_reverb.c b/runtime/audio_reverb.c index aef08cfd..d477e54c 100644 --- a/runtime/audio_reverb.c +++ b/runtime/audio_reverb.c @@ -24,8 +24,8 @@ static void delay_set(Delay *d, unsigned n) { if (n > d->cap) rt_fatal("reverb d static void delay_init(Delay *d, unsigned khz, float ms) { d->cap = samples(khz, ms); d->data = calloc(d->cap + 1, sizeof(float)); if (!d->data) rt_fatal("reverb delay allocation"); delay_set(d, d->cap); } static float delay_update(Delay *d, float v) { d->data[d->write++] = v; float out = d->data[d->read++]; - if (d->read >= d->cap) d->read = 0; - if (d->write >= d->cap) d->write = 0; + if (d->read > d->cap) d->read = 0; + if (d->write > d->cap) d->write = 0; return out; } static float delay_tap(const Delay *d, unsigned at) { int i = (int)d->write - (int)at - 1; if (i < 0) i += (int)d->cap + 1; return d->data[i]; } diff --git a/runtime/audren.c b/runtime/audren.c index 61d3c609..5ede6072 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -318,11 +318,9 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s // ---- render tick: one frame each time the output device has taken the ring below its target (audio_mixer_pace_ns), // or every sample_count / sample_rate seconds of the host clock without a device, while stopped or while it stalls -static void advance_voice(AudioRenderer *r, ServerVoice *v, double out_seconds) { +static void advance_voice(AudioRenderer *r, ServerVoice *v) { if (!v->in.in_use || v->state != SV_STARTED || !v->in.sample_rate) return; - double want = v->fraction + out_seconds * v->in.sample_rate * (v->in.pitch > 0 ? v->in.pitch : 1.0); - uint64_t samples = (uint64_t)want; - v->fraction = want - (double)samples; + uint64_t samples = audio_voice_consumed(r, v, (int)r->param.sample_count); while (samples && v->valid[v->index]) { AudrenWaveBuffer *b = &v->buffers[v->index]; uint64_t length = (uint64_t)(b->end - b->start), left = length - v->offset; @@ -354,7 +352,19 @@ static void render_thread(void *p) { plat_thread_priority(PLAT_PRIO_HIGHEST); const uint64_t frame_ns = (uint64_t)r->param.sample_count * 1000000000ull / (uint64_t)r->param.sample_rate; const double frame = (double)r->param.sample_count / r->param.sample_rate; - uint64_t next = kern_now_ns(); + // When the device pulls a period (1024 samples on PipeWire, 4-5 frames) the ring drops below its target + // and the frames would otherwise render back to back. On hardware the DSP renders one frame every 5 ms and + // the game's audio thread works in between; AUX effects need that time to fill the return ring, or mix_aux + // reads stale samples and the frame clicks at its seams. Device-paced frames stay at least 4/5 of a frame + // apart, which still refills a period well before the device drains it. Linux only: on Windows SDL pulls + // 480-sample periods, and a sink dump (TOTK_AUDIO_DUMP) of the bursts there showed no more seam spikes than + // chance, so a wait on this thread buys nothing. +#ifdef __linux__ + const uint64_t gap_ns = frame_ns * 4 / 5; +#else + const uint64_t gap_ns = 0; +#endif + uint64_t next = kern_now_ns(), last = 0; while (plat_cas32(&r->running, 0, 0)) { uint64_t now = kern_now_ns(); int64_t pace = audio_mixer_pace_ns(r, now); @@ -362,6 +372,10 @@ static void render_thread(void *p) { plat_wait_ns((uint64_t)pace); continue; } + if (pace == 0 && last && now - last < gap_ns) { + plat_wait_ns(gap_ns - (now - last)); + continue; + } if (pace == 0) { next = now + frame_ns; // where the timer goes on if the device stops pacing } else { @@ -369,11 +383,12 @@ static void render_thread(void *p) { else if (now - next > 100000000ull) next = now; // fell far behind (debugger, sleep): resynchronise next += frame_ns; } + last = kern_now_ns(); plat_mutex_lock(&r->lock); if (r->state == 0) { voices_prepare(r); audio_mixer_render(r, frame); - for (int i = 0; i < r->param.voice_count; i++) advance_voice(r, &r->voices[i], frame); + for (int i = 0; i < r->param.voice_count; i++) advance_voice(r, &r->voices[i]); r->elapsed_frames++; uint64_t given = r->stop_released + r->empty_played + r->flushed; if ((rt_trace & RT_TRACE_IPC) && given != r->logged_released && r->elapsed_frames % 1000 == 0) { diff --git a/runtime/audren.h b/runtime/audren.h index 5d6f0a43..b2fccba0 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -184,5 +184,6 @@ void audio_mixer_render(AudioRenderer *r, double seconds); // after audio_voic // many ns (the ring is at its target), -1 leaves the pace to the render thread's timer (no device, renderer stopped, // device stalled). int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now); +uint64_t audio_voice_consumed(const AudioRenderer *r, ServerVoice *v, int frames); // source samples over one frame; advances v->fraction int audio_buffer_empty(const AudrenWaveBuffer *b); // no samples to decode: played as soon as it is reached void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // IAudioDevice diff --git a/runtime/boot.c b/runtime/boot.c index a92830db..992bf33b 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -4,7 +4,7 @@ // The boot driver: the guest main thread runs the game's own nn::init::Start // with three host callbacks, the second of which runs DT_INIT of main, subsdk0 and sdk; Start then calls nnMain. // --boot checks two checkpoints on the way and ends at the first nvnBootstrapLoader call, at the return address of -// each in main (1.1.1, 1.2.1, 1.4.3): +// each in main (1.1.0, 1.2.1, 1.4.3): // SetFocusHandlingMode main+0xeb8c14 (1.2.1: +0xe7a954, 1.4.3: +0xbdb48c) in nnMain, every module's constructor // flag reads 2 // nvnBootstrapLoader main+0xebcdec (1.2.1: +0xe7eb2c, 1.4.3: +0xbe0360) in the graphics bring-up; logs the @@ -15,27 +15,29 @@ #include #include "cfg.h" +#include "cheats.h" #include "fs.h" #include "gfx.h" +#include "mods.h" #include "nvn_api.h" #include "platform/platform.h" #include "vk.h" #include "platform/platform.h" #define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_" -// main: return addresses of nnMain's SetFocusHandlingMode call (1.1.1 and 1.2.1 from this port's analysis of main; +// main: return addresses of nnMain's SetFocusHandlingMode call (1.1.0 and 1.2.1 from this port's analysis of main; // 1.4.3 from MacNativeGames' scripts/verify_boot.py) and of the first nvnBootstrapLoader call, per game version. -#define CHECKPOINT_RET (RT_V143 ? 0xbdb48cull : RT_V111 ? 0xeb8c14ull : 0xe7a954ull) -#define NVN_RET (RT_V143 ? 0xbe0360ull : RT_V111 ? 0xebcdecull : 0xe7eb2cull) +#define CHECKPOINT_RET (RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull) +#define NVN_RET (RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull) #define CTOR_DONE 2 // a module's constructor flag after its DT_INIT ran the init_array -// The init_array sizes (1.1.1 and 1.2.1: counted in the modules; 1.4.3: DT_INIT_ARRAYSZ); MacNativeGames' boot check +// The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.4.3: DT_INIT_ARRAYSZ); MacNativeGames' boot check // expects the same. The three tables carry the same rows, so cb_module_inits walks them by the size of one. typedef struct { const char *name; uint64_t entries; } InitCount; -static const InitCount inits_111[] = {{"main", 10668}, {"subsdk0", 45}, {"sdk", 116}}; +static const InitCount inits_110[] = {{"main", 10668}, {"subsdk0", 45}, {"sdk", 116}}; static const InitCount inits_121[] = {{"main", 10673}, {"subsdk0", 45}, {"sdk", 116}}; static const InitCount inits_143[] = {{"main", 10717}, {"subsdk0", 45}, {"sdk", 83}}; -#define expected_inits (RT_V143 ? inits_143 : RT_V111 ? inits_111 : inits_121) +#define expected_inits (RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121) static volatile int32_t inits_done, checkpoint_seen; uint64_t rt_guest_start_ns; @@ -342,8 +344,10 @@ void rt_boot(void) { PlatThread *warm = plat_thread_new(prewarm_main, NULL, "prewarm"); ldr_load(rt_config()->exefs); rt_patches_init(); + mods_exefs_switch(); // after the patch sets' settings: an enabled mod's ExeFS patches on frame_step_init(); res_init(); + cheats_init(); // after the patch sets: a cheat's set follows its setting cb_addr[0] = hostfn_register("", cb_loader_ready); cb_addr[1] = hostfn_register("", cb_module_inits); cb_addr[2] = hostfn_register("", cb_module_finis); diff --git a/runtime/camera_speed.c b/runtime/camera_speed.c new file mode 100644 index 00000000..30b21ce6 --- /dev/null +++ b/runtime/camera_speed.c @@ -0,0 +1,159 @@ +// Camera speed: TOTK_STICK_SENS (0.1 to 3, default 1, read at start) multiplies the game's own right-stick camera speeds. +// The game's Options menu offers five camera sensitivities, Very Low to Very High; their turn rates are the array +// StickSensitivity {0.8, 1.2, 1.6, 2.0, 2.4} in Component/PlayerCameraParam/PlayerCamera.game__component__ +// PlayerCameraParam.bgyml inside Pack/Actor/PlayerCamera.pack.zs. romfs.c serves a copy of that pack with every entry +// multiplied (cam_pack_build), so the setting raises the camera's top speed as well as its speed at a partial tilt, at +// any of the five sensitivities. The mouse camera reaches the game as a right-stick deflection (input.c, mouse_camera), +// which divides it by the same factor (cam_speed_applied), so TOTK_MOUSE_SENS alone sets the mouse's speed. +// +// The pack is a SARC archive compressed with the dictionary pack.zsdic of Pack/ZsDic.pack.zs. A float in a BYML array +// is stored in the array itself, so the multiplied values take the bytes of the old ones and no offset moves. +#include +#include +#include +#include + +#include "cfg.h" +#include "host.h" + +#define MEMBER "Component/PlayerCameraParam/PlayerCamera.game__component__PlayerCameraParam.bgyml" +#define MAX_RAW (64u << 20) + +static float applied = 1.0f; + +float cam_speed_wanted(void) { + double v = cfg_double("TOTK_STICK_SENS"); + return (float)(v < 0.1 ? 0.1 : v > 3 ? 3 : v); +} + +float cam_speed_applied(void) { return applied; } + +static int u32_at(const uint8_t *p, size_t n, size_t off, uint32_t *v) { + if (off > n || n - off < 4) return 0; + memcpy(v, p + off, 4); + return 1; +} + +// SARC member `name` of the archive p[0..n): its start and size; 0 when it is not there. +static int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) { + uint32_t data; + uint16_t head, count, bom; + if (n < 0x14 || memcmp(p, "SARC", 4)) return 0; + memcpy(&head, p + 4, 2); + memcpy(&bom, p + 6, 2); + if (bom != 0xfeff || !u32_at(p, n, 12, &data) || head + 12u > n || memcmp(p + head, "SFAT", 4)) return 0; + memcpy(&count, p + head + 6, 2); + size_t names = head + 12 + (size_t)count * 16; + if (names + 8 > n || memcmp(p + names, "SFNT", 4)) return 0; + names += 8; + for (unsigned i = 0; i < count; i++) { + uint32_t attr, lo, hi; + size_t e = head + 12 + (size_t)i * 16; + if (!u32_at(p, n, e + 4, &attr) || !u32_at(p, n, e + 8, &lo) || !u32_at(p, n, e + 12, &hi)) return 0; + size_t s = names + (size_t)(attr & 0xffffff) * 4, len = strlen(name); + if (!(attr >> 24) || s + len + 1 > n || memcmp(p + s, name, len + 1)) continue; + if (lo > hi || data + (size_t)hi > n) return 0; + *at = data + lo; + *size = hi - lo; + return 1; + } + return 0; +} + +// The StickSensitivity floats of the BYML p[0..n), multiplied by k; their count, 0 when the file has no such array. +static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) { + uint32_t keys, root, head; + if (n < 16 || memcmp(p, "YB", 2) || !u32_at(p, n, 4, &keys) || !u32_at(p, n, 12, &root) || + !u32_at(p, n, root, &head) || (head & 0xff) != 0xc1 || !u32_at(p, n, keys, &head) || (head & 0xff) != 0xc2) + return 0; + uint32_t nkeys = head >> 8; + if (!u32_at(p, n, root, &head)) return 0; + for (uint32_t i = 0; i < head >> 8; i++) { + uint32_t e, at, arr, ah; + if (!u32_at(p, n, root + 4 + 8 * (size_t)i, &e) || (e & 0xffffff) >= nkeys || + !u32_at(p, n, keys + 4 + 4 * (size_t)(e & 0xffffff), &at)) + return 0; + const char *name = (const char *)p + keys + at; + if (keys + at + sizeof "StickSensitivity" > n || strcmp(name, "StickSensitivity")) continue; + if (e >> 24 != 0xc0 || !u32_at(p, n, root + 8 + 8 * (size_t)i, &arr) || !u32_at(p, n, arr, &ah) || + (ah & 0xff) != 0xc0) + return 0; + uint32_t count = ah >> 8; + size_t vals = arr + 4 + ((count + 3) & ~3u); + if (!count || count > 8 || vals + 4 * (size_t)count > n) return 0; + for (uint32_t j = 0; j < count; j++) + if (p[arr + 4 + j] != 0xd2) return 0; + for (uint32_t j = 0; j < count; j++) { + float v; + memcpy(&v, p + vals + 4 * j, 4); + was[j] = v; + v *= k; + memcpy(p + vals + 4 * j, &v, 4); + } + return (int)count; + } + return 0; +} + +int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n) { + float k = cam_speed_wanted(); + *out = NULL; + if (k == 1.0f) return 0; + int ok = 0; + uint8_t *dics = NULL, *raw = NULL; + ZSTD_DDict *dd = NULL; + ZSTD_CDict *cd = NULL; + void *z = NULL; + // The dictionaries: Pack/ZsDic.pack.zs is a SARC compressed without one. + unsigned long long dn = ZSTD_getFrameContentSize(zsdic, zsdic_n); + size_t dic_at, dic_n; + if (dn == ZSTD_CONTENTSIZE_UNKNOWN || dn == ZSTD_CONTENTSIZE_ERROR || dn > MAX_RAW || !(dics = malloc((size_t)dn)) || + ZSTD_decompress(dics, (size_t)dn, zsdic, zsdic_n) != dn || + !sarc_member(dics, (size_t)dn, "pack.zsdic", &dic_at, &dic_n)) { + rt_log("camera speed: no pack.zsdic in Pack/ZsDic.pack.zs; the camera keeps its own speed"); + goto done; + } + unsigned long long rn = ZSTD_getFrameContentSize(in, n); + unsigned id = ZSTD_getDictID_fromFrame(in, n); + dd = ZSTD_createDDict(dics + dic_at, dic_n); + if (!dd || id != ZSTD_getDictID_fromDDict(dd) || rn == ZSTD_CONTENTSIZE_UNKNOWN || + rn == ZSTD_CONTENTSIZE_ERROR || rn > MAX_RAW || !(raw = malloc((size_t)rn))) { + rt_log("camera speed: Pack/Actor/PlayerCamera.pack.zs is not a pack.zsdic frame with a size; the camera keeps its " + "own speed"); + goto done; + } + ZSTD_DCtx *dctx = ZSTD_createDCtx(); + size_t got = dctx ? ZSTD_decompress_usingDDict(dctx, raw, (size_t)rn, in, n, dd) : 0; + ZSTD_freeDCtx(dctx); + size_t at, size; + float was[8]; + int count = !ZSTD_isError(got) && got == rn && sarc_member(raw, (size_t)rn, MEMBER, &at, &size) + ? scale_sensitivity(raw + at, size, k, was) : 0; + if (!count) { + rt_log("camera speed: no StickSensitivity array in %s; the camera keeps its own speed", MEMBER); + goto done; + } + cd = ZSTD_createCDict(dics + dic_at, dic_n, 9); + size_t cap = ZSTD_compressBound((size_t)rn), zn = 0; + ZSTD_CCtx *cctx = cd ? ZSTD_createCCtx() : NULL; + if (cctx && (z = malloc(cap))) zn = ZSTD_compress_usingCDict(cctx, z, cap, raw, (size_t)rn, cd); + ZSTD_freeCCtx(cctx); + if (!z || !zn || ZSTD_isError(zn)) goto done; + *out = z; + *out_n = zn; + z = NULL; + ok = 1; + rt_log("camera speed: x%.2f, StickSensitivity %.2f %.2f %.2f %.2f %.2f -> %.2f %.2f %.2f %.2f %.2f", k, was[0], + count > 1 ? was[1] : 0, count > 2 ? was[2] : 0, count > 3 ? was[3] : 0, count > 4 ? was[4] : 0, was[0] * k, + count > 1 ? was[1] * k : 0, count > 2 ? was[2] * k : 0, count > 3 ? was[3] * k : 0, + count > 4 ? was[4] * k : 0); +done: + free(z); + free(raw); + free(dics); + ZSTD_freeDDict(dd); + ZSTD_freeCDict(cd); + return ok; +} + +void cam_pack_served(void) { applied = cam_speed_wanted(); } diff --git a/runtime/cfg.c b/runtime/cfg.c index d82cfd22..8bbc0ebf 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -30,27 +30,35 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "AB", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", + "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", - "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", - "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", - "FLIGHT_THUMBS", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "GPL", "GPL_DRAW_BUILD", "GPU", - "GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", - "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_DETAIL", "LOD_GUARD", "LOD_STEADY", "MEMORY", "MENU_LANG", "MENU_TEST", - "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", - "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PATCH_OUTPUT", "PATCH_SCENE", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", + "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", + "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", + "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", + "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", + "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", + "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", + "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", + "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_DETAIL", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", + "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", + "MV_SURVEY", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", - "PROFILE_MAIN", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", + "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "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", - "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", + "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", + "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", + "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", + "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", + "SWKBD_ANSWER", + "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "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", + "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", + "ZSTD_CHECK", "ZSTD_NATIVE", }; // The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections). @@ -62,12 +70,20 @@ static const struct { const char *key, *section; } sections[] = { {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, {"LOD_DETAIL", "display"}, {"LOD_STEADY", "display"}, {"COLOR_PRESET", "display"}, {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, - {"COLOR_GAMMA", "display"}, + {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, + {"HDR_UI_NITS", "display"}, {"HDR_HUE", "display"}, {"SHADOW_RES", "display"}, + {"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, + {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, + {"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"CONTACT_SHADOWS", "display"}, + {"PCSS", "display"}, + {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"}, {"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, - {"VOLUME", "audio"}, + {"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"}, {"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"}, - {"PAD_LAYOUT", "input"}, {"STICK_DEADZONE", "input"}, {"MOUSE_SENS", "input"}, {"INPUT_RECORD", "input"}, + {"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"}, + {"STICK_SENS", "input"}, + {"MOUSE_SENS", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"}, {"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"}, {"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"}, {"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"}, @@ -82,8 +98,9 @@ static const struct { const char *key, *section; } sections[] = { {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, - {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, + {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_EXACT", "diagnostics"}, + {"MV_SURVEY", "diagnostics"}, }; // The settings the menu shows: the value in effect when the key is unset, and what the key accepts. The only place a @@ -98,37 +115,57 @@ static const CfgMeta metas[] = { {"PERF_OVERLAY", "off", "off|fps|full", LIVE}, {"OUTPUT_RES", "1920x1080", NULL, RESTART}, {"RENDER_SCALE", "game", "game|50|67|75|100|125|150|200", RESTART}, {"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE}, + {"DLAA", "0", "0|1", RESTART}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE}, + {"DLSS_PRESET", "default", "default|k|j", LIVE}, + {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE}, {"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, - {"ANISO", "16", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features + {"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features {"LOD_DETAIL", "0", "0|1", RESTART}, {"LOD_STEADY", "0", "0|1", RESTART}, // patch sets lod and steady (1.2.1) // colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses {"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE}, {"COLOR_CONTRAST", "100", "70..150", LIVE}, {"COLOR_SATURATION", "100", "50..200", LIVE}, {"COLOR_GAMMA", "1", "0.8..1.2", LIVE}, + // HDR output (vk_hdr.c): on at start (the tonemap shader is swapped as shaders load); the nits apply at once + {"HDR", "0", "0|1", RESTART}, {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "80..500", LIVE}, + {"HDR_UI_NITS", "203", "80..500", LIVE}, {"HDR_HUE", "80", "0..100", LIVE}, {"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART}, + {"SHADOW_RES", "game", "game|256|512|768|1024|1536|2048", RESTART}, + {"SHADOW_SCALE", "1", "1..4", RESTART}, + {"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART}, + {"PCSS", "0", "0..200", 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}, + {"MODS_REBUILD", "auto", "auto|off", RESTART}, {"HIGHFPS_FIX", "1", "0|1", RESTART}, {"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR", RESTART}, {"OPERATION_MODE", "docked", "docked|handheld", RESTART}, {"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c) // Sound, Controls, Menu - {"VOLUME", "100", "0..100", LIVE}, + {"VOLUME", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART}, {"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE}, - {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox|xbox_prompts", LIVE}, - {"STICK_DEADZONE", "8", "0..50", LIVE}, {"MOUSE_SENS", "1", "0..10", LIVE}, - {"MENU_LANG", "", "en|ru", LIVE}, + {"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox", LIVE}, + {"PROMPTS", "nintendo", "nintendo|xbox", RESTART}, + {"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART}, + {"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE}, + {"MOUSE_SENS", "1", "0..10", LIVE}, + {"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE}, + {"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE}, + {"AMIIBO_DIR", "", NULL, LIVE}, + // Discord Rich Presence (discord_rpc.c): off by default, the Menu tab of the settings menu switches it. + {"DISCORD_RPC", "0", "0|1", LIVE}, + {"DISCORD_APP_ID", "1567232599846293504", NULL, LIVE}, + {"DISCORD_IMAGE", "totk_512x512", NULL, LIVE}, // Advanced: scheduling - {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART}, + {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "1", "0|1", RESTART}, {"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART}, // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, - {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "", "0|1", RESTART}, + {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, // textures and memory {"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART}, - {"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART}, {"ASTC_BC", "0", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART}, + {"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART}, // render fixes {"LOD_GUARD", "1", "0|1", RESTART}, {"FIX_C0", "1", "0|1", RESTART}, {"FIX_ZERO", "1", "0|1", RESTART}, {"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART}, @@ -141,13 +178,14 @@ static const CfgMeta metas[] = { // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, + {"PASS_CRUMBS", "", "0|1", RESTART}, // vk_crumbs.c pass marks; unset: VK_AMD_buffer_marker only {"INPUT_RECORD", "", NULL, RESTART}, }; static int is_known(const char *key) { for (size_t i = 0; i < sizeof known / sizeof *known; i++) if (!strcmp(known[i], key)) return 1; - return 0; + return !strncmp(key, "CHEAT_", 6); // cheats.c and the cheat patch sets (tools/cheats.py) } static void clear(void) { @@ -185,6 +223,24 @@ static void add(char *key, char *value, int line) { entries[n_entries++] = (CfgEntry){strdup(key), strdup(value), 0, 0}; } +// 0.1.9 had the Xbox prompts as a pad layout: pad_layout = xbox_prompts is pad_layout = nintendo with prompts = xbox. +// Read as those two, which the menu shows and the next cfg_save writes; a prompts line of the file's own wins. The +// value still works from the environment (input_pad_layout_pick, input_prompts_pick). +static void migrate(void) { + CfgEntry *layout = NULL, *prompts = NULL; + for (int i = 0; i < n_entries; i++) { + if (!strcmp(entries[i].key, "PAD_LAYOUT")) layout = &entries[i]; + if (!strcmp(entries[i].key, "PROMPTS")) prompts = &entries[i]; + } + if (!layout || strcasecmp(layout->value, "xbox_prompts")) return; + free(layout->value); + layout->value = strdup("nintendo"); + layout->dirty = 1; + if (!prompts && n_entries < CFG_MAX_KEYS) entries[n_entries++] = (CfgEntry){strdup("PROMPTS"), strdup("xbox"), 1, 0}; + rt_log("cfg: totk.ini pad_layout = xbox_prompts is read as pad_layout = nintendo and prompts = %s", + prompts ? prompts->value : "xbox"); +} + int cfg_load(const char *path) { clear(); uint64_t size = 0; @@ -208,6 +264,7 @@ int cfg_load(const char *path) { p = eol + 1; } free(data); + migrate(); return 1; } @@ -466,6 +523,7 @@ static void puts_(Out *o, const char *s) { put(o, s, strlen(s)); } static const char *section_of(const char *key) { for (size_t i = 0; i < sizeof sections / sizeof *sections; i++) if (!strcmp(sections[i].key, key)) return sections[i].section; + if (!strncmp(key, "CHEAT_", 6)) return "cheats"; // cheats.c: one key per cheat of the cheat file return NULL; } diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index ca7449b2..3aafcbfa 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -1,4 +1,5 @@ // --probe-cfg: totk.ini parsing, the environment override and a missing file (cfg.c). Needs no game files. +#include #include #include #include @@ -272,6 +273,22 @@ int probe_cfg(void) { "pad stick: a square gate's corner stays on the unit circle"); input_pad_stick(0, 32767 / 2 + 1, 0.2f, st); CHECK(abs(st[1] - 32767 * 3 / 8) <= 3, "pad stick: half deflection past a 20% zone is (0.5 - 0.2) / 0.8 of full"); + plat_setenv("TOTK_STICK_DEADZONE", "12"); + plat_setenv("TOTK_LSTICK_DEADZONE", NULL); + plat_setenv("TOTK_RSTICK_DEADZONE", NULL); + CHECK(fabsf(input_pad_deadzone(0) - 0.12f) < 1e-6f && fabsf(input_pad_deadzone(1) - 0.12f) < 1e-6f, + "pad dead zone: both sticks follow stick_deadzone while lstick/rstick are unset"); + plat_setenv("TOTK_RSTICK_DEADZONE", "20"); + CHECK(fabsf(input_pad_deadzone(0) - 0.12f) < 1e-6f && fabsf(input_pad_deadzone(1) - 0.20f) < 1e-6f, + "pad dead zone: rstick_deadzone sets the right stick alone"); + plat_setenv("TOTK_LSTICK_DEADZONE", "80"); + CHECK(fabsf(input_pad_deadzone(0) - 0.5f) < 1e-6f, "pad dead zone: clamped to 50%"); + plat_setenv("TOTK_STICK_DEADZONE", NULL); + plat_setenv("TOTK_LSTICK_DEADZONE", NULL); + plat_setenv("TOTK_RSTICK_DEADZONE", NULL); + CHECK(input_stick_percent(32767, 0) > 99.99 && input_stick_percent(32767, 0) < 100.01 && + input_stick_percent(0, 0) == 0 && fabs(input_stick_percent(3000, -4000) - 5000 * 100.0 / 32767) < 1e-9, + "stick percent: an axis's end is 100%, the centre 0%, round in between"); // Gamepad layouts (input_map.c) CHECK(input_pad_layout_pick("auto", 1) == PAD_LAYOUT_NINTENDO && input_pad_layout_pick(NULL, 0) == PAD_LAYOUT_XBOX, "pad layout: automatic is Nintendo for an east button labelled A, Xbox otherwise"); @@ -279,7 +296,30 @@ int probe_cfg(void) { "pad layout: a set layout wins over the labels"); CHECK(input_pad_layout_pick("xbox_prompts", 0) == PAD_LAYOUT_NINTENDO && input_pad_layout_pick("Xbox_Prompts", 1) == PAD_LAYOUT_NINTENDO, - "pad layout: Xbox prompts keep the Nintendo positions (only the prompts change, romfs.c overlay_init)"); + "pad layout: 0.1.9's xbox_prompts keeps the Nintendo positions (only the prompts change, romfs.c overlay_init)"); + // Button prompts (input_map.c) + CHECK(input_prompts_pick(NULL, "auto") == PROMPTS_NINTENDO && input_prompts_pick("", "xbox") == PROMPTS_NINTENDO && + input_prompts_pick("Xbox", "nintendo") == PROMPTS_XBOX && input_prompts_pick("nintendo", "xbox") == PROMPTS_NINTENDO, + "prompts: the game's own unless set to Xbox"); + CHECK(input_prompts_pick(NULL, "xbox_prompts") == PROMPTS_XBOX && input_prompts_pick("nintendo", "xbox_prompts") == PROMPTS_NINTENDO, + "prompts: 0.1.9's pad_layout = xbox_prompts means Xbox while prompts is unset"); + CHECK(!input_prompts_folder(PROMPTS_NINTENDO, "nintendo") && !input_prompts_folder(PROMPTS_NINTENDO, "xbox"), + "prompts: the game's own need no folder"); + CHECK(EQ(input_prompts_folder(PROMPTS_XBOX, "nintendo"), "romfs-patches-xbox") && + EQ(input_prompts_folder(PROMPTS_XBOX, "xbox_prompts"), "romfs-patches-xbox") && + EQ(input_prompts_folder(PROMPTS_XBOX, "xbox"), "romfs-patches-xbox-label") && + EQ(input_prompts_folder(PROMPTS_XBOX, "auto"), "romfs-patches-xbox-label") && + EQ(input_prompts_folder(PROMPTS_XBOX, NULL), "romfs-patches-xbox-label"), + "prompts: the face buttons by position for the Nintendo layout, by label for xbox and auto"); + // romfs-patches-xbox shows A as B (tests/romfs_prompts_test.py checks the folders' contents). It is right exactly + // where the layout puts A on the east button, which an Xbox pad labels B. + input_map_reset(); + static const char *const layouts[] = {"nintendo", "xbox", "auto", "xbox_prompts"}; + for (int i = 0; i < 4; i++) { + int a_east = input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_EAST] = 1}, input_pad_layout_pick(layouts[i], 0)) == 1ull << B_A; + CHECK(a_east == EQ(input_prompts_folder(PROMPTS_XBOX, layouts[i]), "romfs-patches-xbox"), + "prompts: the folder names A as B exactly when the layout puts A on the button labelled B"); + } uint8_t pd[PAD_COUNT] = {0}; pd[PAD_SOUTH] = pd[PAD_NORTH] = 1; CHECK(input_map_pad_buttons(pd, PAD_LAYOUT_NINTENDO) == (1ull << B_B | 1ull << B_X) && @@ -370,6 +410,24 @@ int probe_cfg(void) { plat_setenv("TOTK_FREE_CORES", "1"); CHECK(cfg_bool("TOTK_FREE_CORES") == 1, "free_cores 1: on"); plat_setenv("TOTK_FREE_CORES", NULL); + // 0.1.9's pad_layout = xbox_prompts (cfg.c migrate): read as the Nintendo layout with the Xbox prompts, saved so + plat_setenv("TOTK_PAD_LAYOUT", NULL); + plat_setenv("TOTK_PROMPTS", NULL); + static const char old_layout[] = "[input]\r\npad_layout = xbox_prompts\r\n"; + CHECK(plat_file_write(path, old_layout, sizeof old_layout - 1) && cfg_load(path) == 1 && + EQ(cfg_get("TOTK_PAD_LAYOUT"), "nintendo") && EQ(cfg_get("TOTK_PROMPTS"), "xbox"), + "0.1.9's pad_layout = xbox_prompts reads as nintendo with prompts = xbox"); + CHECK(cfg_save_to(path) == 1, "the read pad layout and prompts save"); + uint64_t saved_n = 0; + char *saved = (char *)plat_file_read(path, &saved_n); + CHECK(saved && strstr(saved, "pad_layout = nintendo") && strstr(saved, "prompts = xbox") && !strstr(saved, "xbox_prompts"), + "the next save writes the pad layout and the prompts"); + free(saved); + static const char old_layout_own[] = "[input]\r\npad_layout = xbox_prompts\r\nprompts = nintendo\r\n"; + CHECK(plat_file_write(path, old_layout_own, sizeof old_layout_own - 1) && cfg_load(path) == 1 && + EQ(cfg_get("TOTK_PAD_LAYOUT"), "nintendo") && EQ(cfg_get("TOTK_PROMPTS"), "nintendo"), + "a prompts line of the file's own wins over 0.1.9's pad_layout = xbox_prompts"); + plat_file_delete(path); cfg_load("cfg-probe-missing.ini"); int listed = 0; for (int i = 0; cfg_meta_at(i); i++) { @@ -383,9 +441,10 @@ int probe_cfg(void) { int ok = 0; if (!m->values) ok = 1; else if (strstr(m->values, "..")) { - long lo = strtol(m->values, NULL, 10), hi = strtol(strstr(m->values, "..") + 2, NULL, 10); + // strtod: ranges such as 0.2..0.9 have fractional ends + double lo = strtod(m->values, NULL), hi = strtod(strstr(m->values, "..") + 2, NULL); double d = strtod(m->def, NULL); - ok = d >= (double)lo && d <= (double)hi; + ok = d >= lo && d <= hi; } else { size_t dl = strlen(m->def); for (const char *v = m->values; *v;) { @@ -397,6 +456,11 @@ int probe_cfg(void) { if (!ok) fprintf(stderr, "FAIL cfg: default '%s' of %s is not one of its values '%s'\n", m->def, m->key, m->values), fails++; } CHECK(listed >= 30, "the defaults table was walked"); + // cfg_meta answers with the first row of a key: a second row would be dead, and Reset would visit the key twice + for (int i = 0; cfg_meta_at(i); i++) + for (int j = i + 1; cfg_meta_at(j); j++) + if (!strcmp(cfg_meta_at(i)->key, cfg_meta_at(j)->key)) + fprintf(stderr, "FAIL cfg: %s has two rows in the defaults table\n", cfg_meta_at(i)->key), fails++; printf("probe-cfg: %s\n", fails ? "FAIL" : "ok"); return fails ? 1 : 0; } diff --git a/runtime/cheats.c b/runtime/cheats.c new file mode 100644 index 00000000..53f1d6f3 --- /dev/null +++ b/runtime/cheats.c @@ -0,0 +1,667 @@ +// Cheats of the mods' Atmosphere cheat files (cheats.h). Each enabled mod the port can use (mods.h) may hold +// .txt in its cheats folder, the build id the first 8 bytes of main's NSO build id, as Atmosphere and the +// emulators name it; the file name's case does not matter. Each cheat's switch is the setting TOTK_CHEAT_ +// (cheat_ under [cheats] in totk.ini), the hash FNV-1a 32 of "/", so two mods' +// cheats of one name keep their own switches. +// +// Code cheats (opcode 0 writes into main's code) cannot be applied here: the game's code is lifted C. tools/cheats.py +// turns the code cheats of every mod into patch sets cheat_ when the game is built and writes +// build-cache\cheats.status next to totk.exe, one line per cheat: name hash, content hash, kind, buttons, note +// (tools/cheats.py has the format). A cheat whose status line is missing or has another content hash was added or +// edited after the build: a data cheat runs anyway, a code cheat waits for the setup to run again. +// +// Data cheats run in a VM after Atmosphere's dmnt cheat VM (opcodes 0-A; the 0xC.. and 0xF.. extensions are not +// supported), every presented frame while their switch is on, with registers cleared before each cheat. Memory is the +// guest's, mapped at its own addresses: main's image is written with its pages made writable for the write (read-only +// data included, as dmnt writes through the debugger), everything else after mem_query says the guest may read or write +// it. Buttons are input_poll's HidNpadButton bits, the same as the cheat format's key masks; they read 0 while the menu +// is open. +#include +#include +#include +#include +#include + +#include "cfg.h" +#include "cheats.h" +#include "host.h" +#include "input.h" +#include "mods.h" +#include "platform/platform.h" + +#ifdef _WIN32 +#define SEP "\\" +#else +#define SEP "/" +#endif + +enum { STATUS_MAX = 160, OPS_PER_RUN = 1 << 20 }; + +typedef struct { + CheatInfo info; + uint32_t *words; + int n; + int dup; // 1 for the first cheat of its name in its file, 2 for the next ... + uint32_t content; // FNV-1a 32 of its words as tools/cheats.py writes them + int set; // patch set, -1 none + volatile int32_t on; + char note[STATUS_MAX]; +} Cheat; + +static Cheat *g_cheats; +static int g_n, g_cap, g_vm; // g_vm: some cheat needs cheats_frame +static char g_file[64]; // ".txt" +static char g_dir[PLAT_PATH_MAX]; // the folder of totk.exe, with a trailing separator +static int g_files; // mods whose file was read +static uint64_t g_main, g_main_size, g_code_end; // main's image; its code with the padding up to .rodata + +static uint32_t fnv(const char *s, size_t n) { + uint32_t h = 0x811C9DC5u; + for (size_t i = 0; i < n; i++) h = (h ^ (uint8_t)s[i]) * 0x01000193u; + return h; +} + +// The folder of the executable, with a trailing separator. +static int exe_dir(char *out, size_t n) { + if (!plat_exe_path(out, n)) return 0; + char *cut = plat_last_sep(out); + if (!cut) return 0; + cut[1] = 0; + return 1; +} + +static int build_id(char out[17]) { + char path[PLAT_PATH_MAX]; + snprintf(path, sizeof path, "%s/main", rt_config()->exefs); + PlatFile *f = plat_file_open(path); + if (!f) return 0; + uint8_t head[0x48]; + int ok = plat_file_pread(f, head, sizeof head, 0) == sizeof head && !memcmp(head, "NSO0", 4); + plat_file_close(f); + for (int i = 0; ok && i < 8; i++) snprintf(out + 2 * i, 3, "%02X", head[0x40 + i]); + return ok; +} + +// ---- opcodes + +// Words of the opcode at w (left words remain), 0 when it is cut short. +static int op_len(const uint32_t *w, int left) { + uint32_t f = w[0]; + int t = f >> 28, width = (f >> 24) & 0xF, n; + switch (t) { + case 0: case 1: n = width == 8 ? 4 : 3; break; + case 2: case 8: n = 1; break; + case 3: n = width == 0 ? 2 : 1; break; + case 4: case 6: n = 3; break; + case 5: case 7: n = 2; break; + case 9: n = 1 + (((f >> 8) & 0xF) ? (width == 8 ? 2 : 1) : 0); break; + case 0xA: n = ((f >> 8) & 0xF) == 2 || ((f >> 8) & 0xF) == 4 || ((f >> 8) & 0xF) == 5 ? 2 : 1; break; + default: n = 1; break; + } + return n <= left ? n : 0; +} + +// NULL when the VM runs the opcode, else why not (tools/cheats.py vm_ok). +static const char *vm_unsupported(const uint32_t *w, char *buf, int n) { + uint32_t f = w[0]; + int t = f >> 28, width = (f >> 24) & 0xF; + if (t > 0xA || (t == 5 && ((f >> 12) & 0xF) > 1) || (t == 0xA && ((f >> 8) & 0xF) > 5)) { + snprintf(buf, n, "opcode %08X is not supported", f); + return buf; + } + if ((t <= 1 || t == 5 || t == 6 || t == 7 || t == 9 || t == 0xA) && width != 1 && width != 2 && width != 4 && + width != 8) { + snprintf(buf, n, "opcode %08X has a bad width", f); + return buf; + } + return NULL; +} + +// The kind of a cheat the build did not see: heading, data, rebuild (it writes main's code) or unsupported. +static CheatKind classify(Cheat *c) { + int ops = 0, ends = 0; + for (int i = 0; i < c->n;) { + int len = op_len(c->words + i, c->n - i); + if (!len) { + snprintf(c->note, sizeof c->note, "opcode %08X is cut short", c->words[i]); + return CHEAT_UNSUPPORTED; + } + uint32_t f = c->words[i]; + ops++; + ends += f >> 28 == 2; + if (f >> 28 == 0 && ((f >> 20) & 0xF) == 0) { + uint64_t off = ((uint64_t)(f & 0xFF) << 32) | c->words[i + 1]; + if (off < g_code_end - g_main) return CHEAT_REBUILD; + } + if (vm_unsupported(c->words + i, c->note, sizeof c->note)) return CHEAT_UNSUPPORTED; + i += len; + } + return ops == ends && ops <= 1 ? CHEAT_HEADING : CHEAT_DATA; +} + +// ---- guest memory + +static uint64_t region(int m) { + const RtGuestLayout *l = mem_layout(); + switch (m) { + case 0: return g_main; + case 1: return l->heap.base; + case 2: return l->alias.base; + case 3: return l->aslr.base; + default: return 0; + } +} + +static int in_main(uint64_t a, unsigned n) { return a >= g_main && a + n <= g_main + g_main_size; } + +static int guest_ok(uint64_t a, unsigned n, uint32_t perm) { + if (in_main(a, n)) return 1; + RtMemoryInfo mi; + if (mem_query(a, &mi) || !mi.state || (mi.perm & perm) != perm) return 0; + return a + n <= mi.base + mi.size; +} + +static int rd(uint64_t a, int width, uint64_t *v) { + if (!a || !guest_ok(a, (unsigned)width, 1)) return 0; + uint64_t x = 0; + memcpy(&x, (const void *)(uintptr_t)a, (size_t)width); + *v = x; + return 1; +} + +// Writes n bytes into main's image a page at a time: a read-only page is writable for its part of the copy only. +static void wr_main(uint64_t a, const uint8_t *from, int n) { + while (n > 0) { + uint64_t page = a & ~0xFFFull, end; + int k = (int)(page + 0x1000 - a); + if (k > n) k = n; + int prot = plat_mem_state(page, page + 0x1000, &end); + if (prot != PLAT_PROT_READ && prot != PLAT_PROT_RW) return; + if (prot == PLAT_PROT_READ && !plat_mem_protect(page, 0x1000, PLAT_PROT_RW)) return; + memcpy((void *)(uintptr_t)a, from, (size_t)k); + if (prot == PLAT_PROT_READ) plat_mem_protect(page, 0x1000, PLAT_PROT_READ); + a += (uint64_t)k; + from += k; + n -= k; + } +} + +static void wr(uint64_t a, int width, uint64_t v) { + uint64_t now; + if (!rd(a, width, &now) || !memcmp(&now, &v, (size_t)width)) return; // unchanged: no write, no protection change + void *p = (void *)(uintptr_t)a; + if (in_main(a, (unsigned)width)) { + wr_main(a, (const uint8_t *)&v, width); + } else if (guest_ok(a, (unsigned)width, 2)) { + memcpy(p, &v, (size_t)width); // a frozen region thaws on the write (memory.c) + } +} + +static uint64_t masked(int width, uint64_t v) { return width >= 8 ? v : v & ((1ull << (8 * width)) - 1); } + +static uint64_t arith(int op, uint64_t a, uint64_t b) { + switch (op) { + case 0: return a + b; + case 1: return a - b; + case 2: return a * b; + case 3: return b < 64 ? a << b : 0; + case 4: return b < 64 ? a >> b : 0; + case 5: return a & b; + case 6: return a | b; + case 7: return ~a; + case 8: return a ^ b; + default: return a; // 9: move + } +} + +static int compare(int cond, uint64_t a, uint64_t b) { + switch (cond) { + case 1: return a > b; + case 2: return a >= b; + case 3: return a < b; + case 4: return a <= b; + case 5: return a == b; + case 6: return a != b; + default: return 0; + } +} + +// From i (after a failed condition, or an else reached while running), the opcode to continue at: after the +// matching else (else_ok) or end. +static int skip(const uint32_t *w, int n, int i, int else_ok) { + int depth = 0; + while (i < n) { + int len = op_len(w + i, n - i); + if (!len) return n; + uint32_t f = w[i]; + int t = f >> 28; + i += len; + if (t == 1 || t == 8) depth++; + else if (t == 2 && ((f >> 24) & 0xF) == 0 && depth-- == 0) return i; + else if (t == 2 && depth == 0 && else_ok) return i; + } + return n; +} + +static uint64_t imm64(const uint32_t *w) { return (uint64_t)w[0] << 32 | w[1]; } + +static void run(const Cheat *c, uint64_t keys) { + uint64_t r[16] = {0}; + int loop[16] = {0}; + const uint32_t *w = c->words; + int n = c->n, budget = OPS_PER_RUN; + for (int i = 0; i < n && budget-- > 0;) { + int len = op_len(w + i, n - i); + if (!len) return; + uint32_t f = w[i]; + int t = f >> 28, width = (f >> 24) & 0xF, x = (f >> 20) & 0xF, y = (f >> 16) & 0xF, z = (f >> 12) & 0xF; + uint64_t rel = ((uint64_t)(f & 0xFF) << 32) | (len > 1 ? w[i + 1] : 0), v; + switch (t) { + case 0: // 0TMR00AA AAAAAAAA V..: store a value at region M + A + register R + wr(region(x) + rel + r[y], width, width == 8 ? imm64(w + i + 2) : w[i + 2]); + break; + case 1: // 1TMC00AA AAAAAAAA V..: if (region M + A) V + if (!rd(region(x) + rel, width, &v) || !compare(y, v, width == 8 ? imm64(w + i + 2) : w[i + 2])) { + i = skip(w, n, i + len, 1); + continue; + } + break; + case 2: // 20000000 end, 21000000 else: an else reached while running skips to the end + if (width == 1) { + i = skip(w, n, i + len, 0); + continue; + } + break; + case 3: // 300R0000 VVVVVVVV loop start, 310R0000 loop end + if (width == 0) { + r[y] = w[i + 1]; + loop[y] = i + len; + } else if (r[y] && --r[y]) { + i = loop[y]; + continue; + } + break; + case 4: // 400R0000 V V: register R = V + r[y] = imm64(w + i + 1); + break; + case 5: // 5TMRI0AA AAAAAAAA: R = (I ? R : region M) + A, read + if (!rd(z ? r[y] + rel : region(x) + rel, width, &v)) return; // a broken pointer chain: nothing more + r[y] = v; + break; + case 6: { // 6T0RIor0 V V: store V at R (+ register r); I: R += width + uint64_t a = r[y] + (((f >> 8) & 0xF) ? r[(f >> 4) & 0xF] : 0); + wr(a, width, imm64(w + i + 1)); + if (z) r[y] += (uint64_t)width; + break; + } + case 7: // 7T0RC000 VVVVVVVV: R = R V + r[y] = masked(width, arith(z, r[y], w[i + 1])); + break; + case 8: // 8kkkkkkk: if the buttons are held + if ((keys & (f & 0x0FFFFFFF)) != (f & 0x0FFFFFFF)) { + i = skip(w, n, i + len, 1); + continue; + } + break; + case 9: { // 9TCRS0s0 / 9TCRS100 V..: R = S (s or V) + uint64_t b = (f >> 8) & 0xF ? (width == 8 ? imm64(w + i + 1) : w[i + 1]) : r[(f >> 4) & 0xF]; + r[y] = masked(width, arith(x, r[z], b)); + break; + } + case 0xA: { // ATSRIOxa (aaaaaaaa): store register S at an address of type O + int o = (f >> 8) & 0xF, m = (f >> 4) & 0xF; + uint64_t imm = ((uint64_t)(f & 0xF) << 32) | (len > 1 ? w[i + 1] : 0), a; + switch (o) { + case 0: a = r[y]; break; + case 1: a = r[y] + r[m]; break; + case 2: a = r[y] + imm; break; + case 3: a = region(m) + r[y]; break; + case 4: a = region(m) + imm; break; + default: a = region(m) + r[y] + imm; break; + } + wr(a, width, r[x]); + if (z) r[y] += (uint64_t)width; + break; + } + default: + return; + } + i += len; + } +} + +// ---- the file + +static char *read_text(const char *path) { + uint64_t n = 0; + char *s = (char *)plat_file_read(path, &n); + if (s && n >= 3 && !memcmp(s, "\xEF\xBB\xBF", 3)) memmove(s, s + 3, (size_t)n - 2); // a UTF-8 BOM + return s; +} + +static void add_word(Cheat *c, uint32_t v, int *cap) { + if (c->n == *cap) { + *cap = *cap ? *cap * 2 : 16; + c->words = (uint32_t *)realloc(c->words, sizeof *c->words * (size_t)*cap); + if (!c->words) rt_fatal("cheats: out of memory"); + } + c->words[c->n++] = v; +} + +// Appends the cheats of one mod's file; mod: the mod's folder name (kept). +static void parse(char *s, const char *mod) { + int first = g_n, wcap = 0; + for (char *line = s; line && *line;) { + char *end = strchr(line, '\n'), *next = end ? end + 1 : NULL; + if (end) *end = 0; + while (*line == ' ' || *line == '\t') line++; + size_t len = strlen(line); + while (len && (line[len - 1] == '\r' || line[len - 1] == ' ' || line[len - 1] == '\t')) line[--len] = 0; + if (len >= 2 && (line[0] == '[' || line[0] == '{') && (line[len - 1] == ']' || line[len - 1] == '}')) { + if (g_n == g_cap) { + g_cap = g_cap ? g_cap * 2 : 32; + g_cheats = (Cheat *)realloc(g_cheats, sizeof *g_cheats * (size_t)g_cap); + if (!g_cheats) rt_fatal("cheats: out of memory"); + } + Cheat *c = &g_cheats[g_n++]; + memset(c, 0, sizeof *c); + line[len - 1] = 0; + char *name = line + 1; + while (*name == ' ' || *name == '\t') name++; + size_t k = strlen(name); + while (k && (name[k - 1] == ' ' || name[k - 1] == '\t')) name[--k] = 0; + c->info.name = strdup(name); + c->info.mod = mod; + c->info.kind = line[0] == '{' ? CHEAT_MASTER : CHEAT_HEADING; + c->set = -1; + c->dup = 1; // a repeated name gets "#2", "#3" ... (tools/cheats.py parse) + for (int j = first; j < g_n - 1; j++) c->dup += !strcmp(g_cheats[j].info.name, c->info.name); + wcap = 0; + } else if (g_n > first) { + for (char *tok = strtok(line, " \t"); tok; tok = strtok(NULL, " \t")) { + char *e; + unsigned long v = strtoul(tok, &e, 16); + if (e - tok == 8 && !*e) add_word(&g_cheats[g_n - 1], (uint32_t)v, &wcap); + } + } + line = next; + } +} + +// The file g_file in folder, its name in any case, into out; 0 when there is none. +typedef struct { + const char *dir; + char *out; + size_t cap; + int found; +} FindFile; +static int find_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + FindFile *f = (FindFile *)ctx; + if (kind == PLAT_FS_FILE && !strcasecmp(name, g_file) && + snprintf(f->out, f->cap, "%s/%s", f->dir, name) < (int)f->cap) + f->found = 1; + return !f->found; +} +static int find_file(const char *folder, char *out, size_t cap) { + FindFile f = {folder, out, cap, 0}; + plat_fs_list(folder, find_cb, &f); + return f.found; +} + +// The status line of key, its fields split in place; 0 when none. +static int status_of(char *status, const char *key, char **fields) { + for (char *line = status; line && *line;) { + char *end = strchr(line, '\n'), *next = end ? end + 1 : NULL; + if (!strncmp(line, key, 8) && line[8] == '\t') { + static char copy[512]; + size_t n = end ? (size_t)(end - line) : strlen(line); + if (n >= sizeof copy) n = sizeof copy - 1; + memcpy(copy, line, n); + copy[n] = 0; + if (n && copy[n - 1] == '\r') copy[n - 1] = 0; + int k = 0; + fields[k++] = copy; + for (char *p = copy; *p && k < 5; p++) + if (*p == '\t') *p = 0, fields[k++] = p + 1; + while (k < 5) fields[k++] = (char *)""; + return 1; + } + line = next; + } + return 0; +} + +// The menu's font has no numero sign (U+2116), which cheat names often start with: they show "No" instead. +static void plain_name(char *s) { + for (char *p; (p = strstr(s, "\xE2\x84\x96")) != NULL;) { + p[0] = 'N', p[1] = 'o'; + memmove(p + 2, p + 3, strlen(p + 3) + 1); + } +} + +// ---- rebuild: code cheats and ExeFS patches the build lacks +// The setup writes build-cache\rebuild.txt (installer/steps.py write_rebuild_command), the command that starts its +// window in rebuild mode on this install folder, one argument per line. When an enabled mod has code cheats this build +// lacks (CHEAT_REBUILD) or ExeFS patch files it lacks (mods_exefs_pending), the game runs it and exits; the setup lifts +// and compiles them in and starts the game again. +// build-cache\rebuild.skip holds the key of the cheats a rebuild was started for: a rebuild that failed or was +// cancelled leaves them unbuilt, and the same set does not start it again. TOTK_MODS_REBUILD=off never starts it. +// Scripted runs (--seconds, --boot) never start it. +static void rebuild_if_needed(const char *dir) { + uint32_t key = 0x811C9DC5u; + int need = 0; + for (int i = 0; i < g_n; i++) { + const Cheat *c = &g_cheats[i]; + if (c->info.kind != CHEAT_REBUILD) continue; + need++; + key = (key ^ fnv(c->info.key, strlen(c->info.key))) * 0x01000193u; + key = (key ^ c->content) * 0x01000193u; + } + int patches = mods_exefs_pending(&key); + if (!need && !patches) return; + char cmd_path[PLAT_PATH_MAX + 64], skip_path[PLAT_PATH_MAX + 64], skip[16] = "", want[16]; + snprintf(cmd_path, sizeof cmd_path, "%sbuild-cache" SEP "rebuild.txt", dir); + snprintf(skip_path, sizeof skip_path, "%sbuild-cache" SEP "rebuild.skip", dir); + snprintf(want, sizeof want, "%08X", key); + char *old = read_text(skip_path); + if (old) snprintf(skip, sizeof skip, "%.8s", old); + free(old); + const char *why = !strcmp(cfg_value("TOTK_MODS_REBUILD"), "off") ? "mods_rebuild = off" + : rt_config()->seconds || rt_config()->boot ? "a scripted run" + : !strcmp(skip, want) ? "the last rebuild for them failed or was cancelled" + : NULL; + char *cmd = why ? NULL : read_text(cmd_path); + if (!why && !cmd) why = "no build-cache/rebuild.txt (a build without the setup)"; + if (why) { + rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; no rebuild: %s", need, + patches, why); + free(cmd); + return; + } + const char *argv[32]; + int argc = 0; + for (char *line = cmd; line && *line && argc < 31;) { + char *end = strchr(line, '\n'), *next = end ? end + 1 : NULL; + if (end) *end = 0; + size_t n = strlen(line); + if (n && line[n - 1] == '\r') line[n - 1] = 0; + if (*line) argv[argc++] = line; + line = next; + } + argv[argc] = NULL; + if (argc < 2 || !plat_file_write(skip_path, want, strlen(want))) { + rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; cannot start the " + "rebuild (%s)", need, patches, cmd_path); + free(cmd); + return; + } + rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; starting the rebuild: %s", + need, patches, argv[0]); + int ok = plat_spawn(argv, dir); + free(cmd); + if (!ok) { + rt_log("cheats: the rebuild did not start; the game starts with the code it has"); + return; + } + prof_write(); // plat_exit runs no atexit handlers + plat_exit(0); +} + +void cheats_init(void) { + const RtLoaded *m = ldr_count() ? ldr_module(0) : NULL; + char dir[PLAT_PATH_MAX], bid[17], path[2400]; // path: a mod's cheats folder (ModInfo.cheats) + the file + if (!m || !exe_dir(dir, sizeof dir) || !build_id(bid)) return; + snprintf(g_dir, sizeof g_dir, "%s", dir); + g_main = m->base; + g_main_size = m->size; + g_code_end = m->base + ((m->text_end + 0xFFF) & ~0xFFFull); + snprintf(g_file, sizeof g_file, "%s.txt", bid); + mods_scan(); + for (int k = 0; k < mods_count(); k++) { // the Mods tab's order, top first + const ModInfo *mod = mods_at(k); + if (!mod->enabled || mod->problem != MOD_OK || !mod->cheats[0] || !find_file(mod->cheats, path, sizeof path)) + continue; + char *text = read_text(path); + if (!text) { + rt_log("cheats: %s: cannot read %s", mod->name, path); + continue; + } + parse(text, strdup(mod->name)); + free(text); + g_files++; + } + if (!g_files) { + rt_log("cheats: no enabled mod holds cheats/%s", g_file); + rebuild_if_needed(dir); // an enabled mod's ExeFS patches may still wait for it + return; + } + snprintf(path, sizeof path, "%sbuild-cache" SEP "cheats.status", dir); + char *status = read_text(path); + int counts[CHEAT_UNSUPPORTED + 1] = {0}, seen_dups = 0; + for (int i = 0; i < g_n; i++) { + Cheat *c = &g_cheats[i]; + char named[900]; + snprintf(named, sizeof named, c->dup > 1 ? "%s/%s#%d" : "%s/%s", c->info.mod, c->info.name, c->dup); + seen_dups += c->dup > 1; + char key[9], content[9]; + snprintf(key, sizeof key, "%08X", fnv(named, strlen(named))); + size_t tn = (size_t)c->n * 9 + 1, tl = 0; + char *t = (char *)malloc(tn); + if (!t) rt_fatal("cheats: out of memory"); + t[0] = 0; + for (int k = 0; k < c->n; k++) tl += (size_t)snprintf(t + tl, tn - tl, k ? " %08X" : "%08X", c->words[k]); + c->content = fnv(t, tl); + snprintf(content, sizeof content, "%08X", c->content); + free(t); + snprintf(c->info.key, sizeof c->info.key, "TOTK_CHEAT_%s", key); + int master = c->info.kind == CHEAT_MASTER; + char *f[5]; + CheatKind kind; + if (status && status_of(status, key, f) && !strcmp(f[1], content)) { + const char *k = f[2]; + kind = !strcmp(k, "code") ? CHEAT_CODE : !strcmp(k, "key") ? CHEAT_KEY : !strcmp(k, "master") ? CHEAT_MASTER + : !strcmp(k, "restore") ? CHEAT_RESTORE : !strcmp(k, "heading") ? CHEAT_HEADING + : !strcmp(k, "data") ? CHEAT_DATA : CHEAT_UNSUPPORTED; + c->info.buttons = (uint32_t)strtoul(f[3], NULL, 16); + snprintf(c->note, sizeof c->note, "%s", f[4]); + if (kind == CHEAT_CODE || kind == CHEAT_KEY || kind == CHEAT_MASTER) { + char set[32]; + snprintf(set, sizeof set, "cheat_%08x", fnv(named, strlen(named))); + c->set = rt_patch_set(set); + if (c->set < 0 && kind != CHEAT_MASTER) kind = CHEAT_REBUILD; + } + } else { + kind = classify(c); + } + if (master && kind == CHEAT_DATA) kind = CHEAT_MASTER; // a data master code: the VM, always on + c->info.kind = kind; + c->info.note = c->note; + const char *v = cfg_get(c->info.key); + c->on = kind == CHEAT_MASTER || (v && *v && strcmp(v, "0") != 0); + if (kind == CHEAT_KEY && c->set >= 0) rt_patch_switch(c->set, 0); // on while the buttons are held + if (kind == CHEAT_KEY || kind == CHEAT_DATA || (kind == CHEAT_MASTER && c->set < 0)) g_vm = 1; + counts[kind]++; + } + free(status); + rebuild_if_needed(dir); + for (int i = 0; i < g_n; i++) plain_name((char *)g_cheats[i].info.name); // the hashes above read the file's bytes + int on = 0; + for (int i = 0; i < g_n; i++) on += g_cheats[i].on && g_cheats[i].info.kind != CHEAT_HEADING; + rt_log("cheats: %s in %d mods: %d cheats, %d on; %d code, %d while buttons are held, %d data, %d master, " + "%d restore, %d headings, %d wait for a rebuild, %d not supported%s", + g_file, g_files, g_n, on, counts[CHEAT_CODE], counts[CHEAT_KEY], counts[CHEAT_DATA], counts[CHEAT_MASTER], + counts[CHEAT_RESTORE], counts[CHEAT_HEADING], counts[CHEAT_REBUILD], counts[CHEAT_UNSUPPORTED], + seen_dups ? " (repeated names numbered)" : ""); +} + +int cheats_rebuild_pending(void) { + char path[PLAT_PATH_MAX + 64]; + snprintf(path, sizeof path, "%sbuild-cache" SEP "rebuild.txt", g_dir); + uint32_t key = 0; + int need = mods_exefs_pending(&key); + for (int i = 0; i < g_n && !need; i++) need = g_cheats[i].info.kind == CHEAT_REBUILD; + return need && g_dir[0] && plat_file_exists(path); +} + +void cheats_rebuild_allow(void) { + char path[PLAT_PATH_MAX + 64]; + snprintf(path, sizeof path, "%sbuild-cache" SEP "rebuild.skip", g_dir); + plat_fs_unlink(path); +} + +int cheats_count(void) { return g_n; } + +const CheatInfo *cheats_info(int i) { return i >= 0 && i < g_n ? &g_cheats[i].info : NULL; } + +int cheats_on(int i) { return i >= 0 && i < g_n && g_cheats[i].on; } + +const char *cheats_file(void) { return g_file; } + +void cheats_set(int i, int on) { + if (i < 0 || i >= g_n) return; + Cheat *c = &g_cheats[i]; + if (c->info.kind != CHEAT_CODE && c->info.kind != CHEAT_KEY && c->info.kind != CHEAT_DATA) return; + plat_xchg32(&c->on, on != 0); + if (c->info.kind == CHEAT_CODE) rt_patch_switch(c->set, on); // CHEAT_KEY: cheats_frame follows the buttons + cfg_set(c->info.key, on ? "1" : "0"); + rt_log("cheats: %s/%s %s", c->info.mod, c->info.name, on ? "on" : "off"); +} + +void cheats_frame(uint64_t frame) { + if (!g_vm || frame < 60) return; // the game polls its input (and starts SDL) before this + uint64_t keys = 0; + int32_t lx, ly, rx, ry; + input_poll(&keys, &lx, &ly, &rx, &ry); // 0 while the menu is open + for (int i = 0; i < g_n; i++) { + Cheat *c = &g_cheats[i]; + switch (c->info.kind) { + case CHEAT_KEY: + if (c->set >= 0) { + int want = c->on && (keys & c->info.buttons) == c->info.buttons; + if (rt_patch_enabled(c->set) != want) rt_patch_switch(c->set, want); + } + break; + case CHEAT_DATA: + if (c->on) run(c, keys); + break; + case CHEAT_MASTER: + if (c->set < 0) run(c, keys); + break; + default: + break; + } + } +} + +const char *cheats_buttons(uint32_t mask, char *buf, int n) { + static const char *const names[] = {"A", "B", "X", "Y", "Left stick press", "Right stick press", "L", "R", "ZL", + "ZR", "+", "-", "D-pad left", "D-pad up", "D-pad right", "D-pad down", + "Left stick left", "Left stick up", "Left stick right", "Left stick down", + "Right stick left", "Right stick up", "Right stick right", "Right stick down"}; + int k = 0; + buf[0] = 0; + for (int b = 0; b < 24; b++) + if (mask & (1u << b)) k += snprintf(buf + k, (size_t)(n > k ? n - k : 0), "%s%s", k ? " + " : "", names[b]); + return buf; +} diff --git a/runtime/cheats.h b/runtime/cheats.h new file mode 100644 index 00000000..bbefa12c --- /dev/null +++ b/runtime/cheats.h @@ -0,0 +1,48 @@ +// Cheats of the mods' Atmosphere cheat files (cheats.c): mods\\cheats\.txt of each enabled mod, +// switched from the menu's Cheats tab. A cheat that writes the game's instructions is a code patch set that tools/cheats.py built into the game +// (switched with rt_patch_switch); one that writes data runs in the cheat VM every presented frame. +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + CHEAT_HEADING, // no opcodes: a title or a group label + CHEAT_CODE, // a patch set + CHEAT_KEY, // a patch set on while the buttons are held + CHEAT_DATA, // the cheat VM + CHEAT_MASTER, // {master}: always on + CHEAT_RESTORE, // writes the original instructions back: nothing to do + CHEAT_REBUILD, // writes instructions, but the game was built without this version of it + CHEAT_UNSUPPORTED, // note says why +} CheatKind; + +typedef struct { + const char *name; // UTF-8, as in the file + const char *mod; // the mod's folder name; cheats come grouped by mod, in the Mods tab's order + const char *note; // why it is unsupported ("" otherwise) + CheatKind kind; + uint32_t buttons; // CHEAT_KEY: HidNpadButton mask + char key[24]; // its setting, TOTK_CHEAT_/"> +} CheatInfo; + +void cheats_init(void); // after rt_patches_init: reads the mods' files and their build status +int cheats_count(void); +// 1 when a cheat of an enabled mod waits for a rebuild (CHEAT_REBUILD) and the setup left its rebuild command. +int cheats_rebuild_pending(void); +// The next start rebuilds again for cheats whose last rebuild failed or was cancelled (the menu restarts after it). +void cheats_rebuild_allow(void); +const CheatInfo *cheats_info(int i); +int cheats_on(int i); // the player's switch (1 for a master cheat) +void cheats_set(int i, int on); // switches at once and sets the cheat's setting (the menu saves totk.ini) +const char *cheats_file(void); // ".txt", the name the files have for this game version +// Every presented frame (the present path): the data cheats' VM and the button state of CHEAT_KEY cheats. +void cheats_frame(uint64_t frame); +// The button names of a mask ("ZL + Right stick left"), into buf. +const char *cheats_buttons(uint32_t mask, char *buf, int n); + +#ifdef __cplusplus +} +#endif diff --git a/runtime/contact_shadow_spv.h b/runtime/contact_shadow_spv.h new file mode 100644 index 00000000..7d506c8f --- /dev/null +++ b/runtime/contact_shadow_spv.h @@ -0,0 +1,156 @@ +// Generated by tools/build_shaders.py from runtime/shaders/contact_shadow.comp; do not edit. +// source sha256 b452a1920e59c4d83b2c994b0c6573cc1e479f7032a1310a7432d11c5c2e9908 +#pragma once +#include +static const uint32_t contact_shadow_spv[] = { + 0x07230203, 0x00010300, 0x000d000b, 0x000001b1, 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x00000031, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x00000069, + 0x00060010, 0x00000004, 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00030047, 0x00000027, + 0x00000002, 0x00050048, 0x00000027, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000027, + 0x00000001, 0x00000023, 0x00000010, 0x00050048, 0x00000027, 0x00000002, 0x00000023, 0x00000020, + 0x00050048, 0x00000027, 0x00000003, 0x00000023, 0x00000030, 0x00040047, 0x00000069, 0x0000000b, + 0x0000001c, 0x00040047, 0x00000088, 0x00000021, 0x00000000, 0x00040047, 0x00000088, 0x00000022, + 0x00000000, 0x00040047, 0x0000013f, 0x00000021, 0x00000001, 0x00040047, 0x0000013f, 0x00000022, + 0x00000000, 0x00040047, 0x0000015c, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, 0x00030021, + 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, + 0x00000002, 0x00040017, 0x0000000a, 0x00000006, 0x00000003, 0x0004002b, 0x00000006, 0x0000001b, + 0x40000000, 0x0004002b, 0x00000006, 0x0000001d, 0x3f800000, 0x00040015, 0x00000020, 0x00000020, + 0x00000000, 0x0004002b, 0x00000020, 0x00000021, 0x00000000, 0x00040017, 0x00000026, 0x00000006, + 0x00000004, 0x0006001e, 0x00000027, 0x00000026, 0x00000026, 0x00000026, 0x00000026, 0x00040020, + 0x00000028, 0x00000009, 0x00000027, 0x0004003b, 0x00000028, 0x00000029, 0x00000009, 0x00040015, + 0x0000002a, 0x00000020, 0x00000001, 0x0004002b, 0x0000002a, 0x0000002b, 0x00000000, 0x00040020, + 0x0000002c, 0x00000009, 0x00000006, 0x0004002b, 0x00000020, 0x00000030, 0x00000001, 0x0004002b, + 0x00000020, 0x0000003f, 0x00000002, 0x0004002b, 0x00000006, 0x00000043, 0x3f000000, 0x0004002b, + 0x00000006, 0x00000059, 0x4253ee82, 0x0004002b, 0x00000006, 0x0000005b, 0x3d897143, 0x0004002b, + 0x00000006, 0x0000005c, 0x3bbf4590, 0x0005002c, 0x00000007, 0x0000005d, 0x0000005b, 0x0000005c, + 0x00040017, 0x00000064, 0x0000002a, 0x00000002, 0x00040017, 0x00000067, 0x00000020, 0x00000003, + 0x00040020, 0x00000068, 0x00000001, 0x00000067, 0x0004003b, 0x00000068, 0x00000069, 0x00000001, + 0x00040017, 0x0000006a, 0x00000020, 0x00000002, 0x00020014, 0x0000006e, 0x0004002b, 0x0000002a, + 0x00000072, 0x00000002, 0x00090019, 0x00000085, 0x00000006, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x00000086, 0x00000085, 0x00040020, 0x00000087, + 0x00000000, 0x00000086, 0x0004003b, 0x00000087, 0x00000088, 0x00000000, 0x0004002b, 0x00000006, + 0x0000008f, 0x00000000, 0x0004002b, 0x00000006, 0x00000092, 0x3f7fbe77, 0x0004002b, 0x00000006, + 0x0000009e, 0x42a00000, 0x00040020, 0x000000a8, 0x00000009, 0x00000026, 0x0004002b, 0x0000002a, + 0x000000b4, 0x00000001, 0x0004002b, 0x00000020, 0x000000b5, 0x00000003, 0x0004002b, 0x00000006, + 0x000000bb, 0x3ecccccd, 0x0004002b, 0x00000006, 0x000000bc, 0x40400000, 0x0004002b, 0x00000006, + 0x000000c0, 0x3e19999a, 0x0004002b, 0x00000006, 0x000000c2, 0x3c23d70a, 0x0004002b, 0x00000006, + 0x000000cb, 0x3e800000, 0x0004002b, 0x0000002a, 0x000000da, 0x00000010, 0x0004002b, 0x00000006, + 0x000000e6, 0xbd4ccccd, 0x0005002c, 0x00000007, 0x000000f0, 0x0000008f, 0x0000008f, 0x00040017, + 0x000000f1, 0x0000006e, 0x00000002, 0x0005002c, 0x00000007, 0x000000f8, 0x0000001d, 0x0000001d, + 0x0004002b, 0x00000006, 0x00000114, 0x3ca3d70a, 0x0004002b, 0x00000006, 0x00000116, 0x3b03126f, + 0x0004002b, 0x00000006, 0x0000012b, 0x42200000, 0x0004002b, 0x0000002a, 0x00000137, 0x00000003, + 0x00090019, 0x0000013d, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000002, + 0x00000008, 0x00040020, 0x0000013e, 0x00000000, 0x0000013d, 0x0004003b, 0x0000013e, 0x0000013f, + 0x00000000, 0x0004002b, 0x00000020, 0x0000015b, 0x00000008, 0x0006002c, 0x00000067, 0x0000015c, + 0x0000015b, 0x0000015b, 0x00000030, 0x0005002c, 0x00000007, 0x000001ad, 0x00000043, 0x00000043, + 0x0004002b, 0x00000006, 0x000001af, 0x3dcccccd, 0x0004002b, 0x00000006, 0x000001b0, 0x3d800000, + 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x000300f7, + 0x0000015d, 0x00000000, 0x000300fb, 0x00000021, 0x0000015e, 0x000200f8, 0x0000015e, 0x0004003d, + 0x00000067, 0x0000006b, 0x00000069, 0x0007004f, 0x0000006a, 0x0000006c, 0x0000006b, 0x0000006b, + 0x00000000, 0x00000001, 0x0004007c, 0x00000064, 0x0000006d, 0x0000006c, 0x00050051, 0x0000002a, + 0x00000071, 0x0000006d, 0x00000000, 0x00060041, 0x0000002c, 0x00000073, 0x00000029, 0x00000072, + 0x00000021, 0x0004003d, 0x00000006, 0x00000074, 0x00000073, 0x0004006e, 0x0000002a, 0x00000075, + 0x00000074, 0x000500af, 0x0000006e, 0x00000076, 0x00000071, 0x00000075, 0x000400a8, 0x0000006e, + 0x00000077, 0x00000076, 0x000300f7, 0x00000079, 0x00000000, 0x000400fa, 0x00000077, 0x00000078, + 0x00000079, 0x000200f8, 0x00000078, 0x00050051, 0x0000002a, 0x0000007b, 0x0000006d, 0x00000001, + 0x00060041, 0x0000002c, 0x0000007c, 0x00000029, 0x00000072, 0x00000030, 0x0004003d, 0x00000006, + 0x0000007d, 0x0000007c, 0x0004006e, 0x0000002a, 0x0000007e, 0x0000007d, 0x000500af, 0x0000006e, + 0x0000007f, 0x0000007b, 0x0000007e, 0x000200f9, 0x00000079, 0x000200f8, 0x00000079, 0x000700f5, + 0x0000006e, 0x00000080, 0x00000076, 0x0000015e, 0x0000007f, 0x00000078, 0x000300f7, 0x00000082, + 0x00000000, 0x000400fa, 0x00000080, 0x00000081, 0x00000082, 0x000200f8, 0x00000081, 0x000200f9, + 0x0000015d, 0x000200f8, 0x00000082, 0x0004003d, 0x00000086, 0x00000089, 0x00000088, 0x00040064, + 0x00000085, 0x0000008b, 0x00000089, 0x0007005f, 0x00000026, 0x0000008c, 0x0000008b, 0x0000006d, + 0x00000002, 0x0000002b, 0x00050051, 0x00000006, 0x0000008d, 0x0000008c, 0x00000000, 0x000500bc, + 0x0000006e, 0x00000090, 0x0000008d, 0x0000008f, 0x000500be, 0x0000006e, 0x00000093, 0x0000008d, + 0x00000092, 0x000500a6, 0x0000006e, 0x00000094, 0x00000090, 0x00000093, 0x000300f7, 0x00000096, + 0x00000000, 0x000400fa, 0x00000094, 0x00000095, 0x00000096, 0x000200f8, 0x00000095, 0x000200f9, + 0x0000015d, 0x000200f8, 0x00000096, 0x00060041, 0x0000002c, 0x0000009a, 0x00000029, 0x0000002b, + 0x0000003f, 0x0004003d, 0x00000006, 0x0000009b, 0x0000009a, 0x00050085, 0x00000006, 0x0000009c, + 0x0000008d, 0x0000009b, 0x000500ba, 0x0000006e, 0x0000009f, 0x0000009c, 0x0000009e, 0x000300f7, + 0x000000a1, 0x00000000, 0x000400fa, 0x0000009f, 0x000000a0, 0x000000a1, 0x000200f8, 0x000000a0, + 0x000200f9, 0x0000015d, 0x000200f8, 0x000000a1, 0x0004006f, 0x00000007, 0x000000a5, 0x0000006d, + 0x00050081, 0x00000007, 0x000000a7, 0x000000a5, 0x000001ad, 0x00050041, 0x000000a8, 0x000000a9, + 0x00000029, 0x00000072, 0x0004003d, 0x00000026, 0x000000aa, 0x000000a9, 0x0007004f, 0x00000007, + 0x000000ab, 0x000000aa, 0x000000aa, 0x00000002, 0x00000003, 0x00050085, 0x00000007, 0x000000ac, + 0x000000a7, 0x000000ab, 0x0005008e, 0x00000007, 0x00000167, 0x000000ac, 0x0000001b, 0x00050083, + 0x00000007, 0x00000169, 0x00000167, 0x000000f8, 0x00050051, 0x00000006, 0x0000016b, 0x00000169, + 0x00000000, 0x00050085, 0x00000006, 0x0000016d, 0x0000016b, 0x0000009c, 0x00060041, 0x0000002c, + 0x0000016e, 0x00000029, 0x0000002b, 0x00000021, 0x0004003d, 0x00000006, 0x0000016f, 0x0000016e, + 0x00050088, 0x00000006, 0x00000170, 0x0000016d, 0x0000016f, 0x00050051, 0x00000006, 0x00000172, + 0x00000169, 0x00000001, 0x0004007f, 0x00000006, 0x00000173, 0x00000172, 0x00050085, 0x00000006, + 0x00000175, 0x00000173, 0x0000009c, 0x00060041, 0x0000002c, 0x00000176, 0x00000029, 0x0000002b, + 0x00000030, 0x0004003d, 0x00000006, 0x00000177, 0x00000176, 0x00050088, 0x00000006, 0x00000178, + 0x00000175, 0x00000177, 0x0004007f, 0x00000006, 0x0000017a, 0x0000009c, 0x00060050, 0x0000000a, + 0x0000017b, 0x00000170, 0x00000178, 0x0000017a, 0x00060041, 0x0000002c, 0x000000b6, 0x00000029, + 0x000000b4, 0x000000b5, 0x0004003d, 0x00000006, 0x000000b7, 0x000000b6, 0x00050085, 0x00000006, + 0x000000ba, 0x0000009c, 0x000001af, 0x0008000c, 0x00000006, 0x000000bd, 0x00000001, 0x0000002b, + 0x000000ba, 0x000000bb, 0x000000bc, 0x00050085, 0x00000006, 0x000000be, 0x000000b7, 0x000000bd, + 0x00050085, 0x00000006, 0x000000c3, 0x0000009c, 0x000000c2, 0x00050081, 0x00000006, 0x000000c4, + 0x000000c0, 0x000000c3, 0x00050085, 0x00000006, 0x000000c8, 0x000000be, 0x000001b0, 0x00050094, + 0x00000006, 0x0000017f, 0x000000a5, 0x0000005d, 0x0006000c, 0x00000006, 0x00000180, 0x00000001, + 0x0000000a, 0x0000017f, 0x00050085, 0x00000006, 0x00000181, 0x00000059, 0x00000180, 0x0006000c, + 0x00000006, 0x00000182, 0x00000001, 0x0000000a, 0x00000181, 0x00050081, 0x00000006, 0x000000d0, + 0x000000cb, 0x00000182, 0x00050085, 0x00000006, 0x000000d1, 0x000000c8, 0x000000d0, 0x000200f9, + 0x000000d4, 0x000200f8, 0x000000d4, 0x000700f5, 0x00000006, 0x000001a7, 0x000000d1, 0x000000a1, + 0x00000129, 0x000000d7, 0x000700f5, 0x0000002a, 0x000001a6, 0x0000002b, 0x000000a1, 0x00000126, + 0x000000d7, 0x000500b1, 0x0000006e, 0x000000db, 0x000001a6, 0x000000da, 0x000400f6, 0x000000d6, + 0x000000d7, 0x00000000, 0x000400fa, 0x000000db, 0x000000d5, 0x000000d6, 0x000200f8, 0x000000d5, + 0x00050041, 0x000000a8, 0x000000de, 0x00000029, 0x000000b4, 0x0004003d, 0x00000026, 0x000000df, + 0x000000de, 0x0008004f, 0x0000000a, 0x000000e0, 0x000000df, 0x000000df, 0x00000000, 0x00000001, + 0x00000002, 0x0005008e, 0x0000000a, 0x000000e2, 0x000000e0, 0x000001a7, 0x00050081, 0x0000000a, + 0x000000e3, 0x0000017b, 0x000000e2, 0x00050051, 0x00000006, 0x000000e5, 0x000000e3, 0x00000002, + 0x000500be, 0x0000006e, 0x000000e7, 0x000000e5, 0x000000e6, 0x000300f7, 0x000000e9, 0x00000000, + 0x000400fa, 0x000000e7, 0x000000e8, 0x000000e9, 0x000200f8, 0x000000e8, 0x000200f9, 0x000000d6, + 0x000200f8, 0x000000e9, 0x0004007f, 0x00000006, 0x00000188, 0x000000e5, 0x00050051, 0x00000006, + 0x0000018a, 0x000000e3, 0x00000000, 0x00050085, 0x00000006, 0x0000018b, 0x00000043, 0x0000018a, + 0x00050085, 0x00000006, 0x0000018e, 0x0000018b, 0x0000016f, 0x00050088, 0x00000006, 0x00000190, + 0x0000018e, 0x00000188, 0x00050081, 0x00000006, 0x00000191, 0x00000043, 0x00000190, 0x00050051, + 0x00000006, 0x00000193, 0x000000e3, 0x00000001, 0x00050085, 0x00000006, 0x00000194, 0x00000043, + 0x00000193, 0x00050085, 0x00000006, 0x00000197, 0x00000194, 0x00000177, 0x00050088, 0x00000006, + 0x00000199, 0x00000197, 0x00000188, 0x00050083, 0x00000006, 0x0000019a, 0x00000043, 0x00000199, + 0x00050050, 0x00000007, 0x0000019b, 0x00000191, 0x0000019a, 0x000500b8, 0x000000f1, 0x000000f2, + 0x0000019b, 0x000000f0, 0x0004009a, 0x0000006e, 0x000000f3, 0x000000f2, 0x000400a8, 0x0000006e, + 0x000000f4, 0x000000f3, 0x000300f7, 0x000000f6, 0x00000000, 0x000400fa, 0x000000f4, 0x000000f5, + 0x000000f6, 0x000200f8, 0x000000f5, 0x000500ba, 0x000000f1, 0x000000f9, 0x0000019b, 0x000000f8, + 0x0004009a, 0x0000006e, 0x000000fa, 0x000000f9, 0x000200f9, 0x000000f6, 0x000200f8, 0x000000f6, + 0x000700f5, 0x0000006e, 0x000000fb, 0x000000f3, 0x000000e9, 0x000000fa, 0x000000f5, 0x000300f7, + 0x000000fd, 0x00000000, 0x000400fa, 0x000000fb, 0x000000fc, 0x000000fd, 0x000200f8, 0x000000fc, + 0x000200f9, 0x000000d6, 0x000200f8, 0x000000fd, 0x0004003d, 0x00000086, 0x00000100, 0x00000088, + 0x0007004f, 0x00000007, 0x00000104, 0x000000aa, 0x000000aa, 0x00000000, 0x00000001, 0x00050085, + 0x00000007, 0x00000105, 0x0000019b, 0x00000104, 0x0004006e, 0x00000064, 0x00000106, 0x00000105, + 0x00040064, 0x00000085, 0x00000107, 0x00000100, 0x0007005f, 0x00000026, 0x00000108, 0x00000107, + 0x00000106, 0x00000002, 0x0000002b, 0x00050051, 0x00000006, 0x00000109, 0x00000108, 0x00000000, + 0x00050085, 0x00000006, 0x0000010c, 0x00000109, 0x0000009b, 0x00050083, 0x00000006, 0x00000112, + 0x00000188, 0x0000010c, 0x00050085, 0x00000006, 0x00000117, 0x0000009c, 0x00000116, 0x00050081, + 0x00000006, 0x00000118, 0x00000114, 0x00000117, 0x000500ba, 0x0000006e, 0x00000119, 0x00000112, + 0x00000118, 0x000500b8, 0x0000006e, 0x0000011c, 0x00000112, 0x000000c4, 0x000500a7, 0x0000006e, + 0x0000011d, 0x00000119, 0x0000011c, 0x000300f7, 0x0000011f, 0x00000000, 0x000400fa, 0x0000011d, + 0x0000011e, 0x0000011f, 0x000200f8, 0x0000011e, 0x0004006f, 0x00000006, 0x00000121, 0x000001a6, + 0x00050085, 0x00000006, 0x00000122, 0x00000121, 0x000001b0, 0x00050083, 0x00000006, 0x00000123, + 0x0000001d, 0x00000122, 0x000200f9, 0x000000d6, 0x000200f8, 0x0000011f, 0x000200f9, 0x000000d7, + 0x000200f8, 0x000000d7, 0x00050080, 0x0000002a, 0x00000126, 0x000001a6, 0x000000b4, 0x00050081, + 0x00000006, 0x00000129, 0x000001a7, 0x000000c8, 0x000200f9, 0x000000d4, 0x000200f8, 0x000000d6, + 0x000b00f5, 0x00000006, 0x000001aa, 0x0000008f, 0x000000d4, 0x0000008f, 0x000000e8, 0x0000008f, + 0x000000fc, 0x00000123, 0x0000011e, 0x0008000c, 0x00000006, 0x0000012d, 0x00000001, 0x00000031, + 0x0000012b, 0x0000009e, 0x0000009c, 0x00050083, 0x00000006, 0x0000012e, 0x0000001d, 0x0000012d, + 0x00060041, 0x0000002c, 0x00000130, 0x00000029, 0x0000002b, 0x000000b5, 0x0004003d, 0x00000006, + 0x00000131, 0x00000130, 0x00050085, 0x00000006, 0x00000133, 0x00000131, 0x000001aa, 0x00050085, + 0x00000006, 0x00000135, 0x00000133, 0x0000012e, 0x00050083, 0x00000006, 0x00000136, 0x0000001d, + 0x00000135, 0x00060041, 0x0000002c, 0x00000138, 0x00000029, 0x00000137, 0x00000021, 0x0004003d, + 0x00000006, 0x00000139, 0x00000138, 0x000500ba, 0x0000006e, 0x0000013a, 0x00000139, 0x00000043, + 0x000300f7, 0x0000013c, 0x00000000, 0x000400fa, 0x0000013a, 0x0000013b, 0x0000013c, 0x000200f8, + 0x0000013b, 0x0004003d, 0x0000013d, 0x00000140, 0x0000013f, 0x00070050, 0x00000026, 0x00000143, + 0x00000136, 0x00000136, 0x00000136, 0x0000001d, 0x00040063, 0x00000140, 0x0000006d, 0x00000143, + 0x000200f9, 0x0000015d, 0x000200f8, 0x0000013c, 0x000500be, 0x0000006e, 0x00000146, 0x00000136, + 0x00000092, 0x000300f7, 0x00000148, 0x00000000, 0x000400fa, 0x00000146, 0x00000147, 0x00000148, + 0x000200f8, 0x00000147, 0x000200f9, 0x0000015d, 0x000200f8, 0x00000148, 0x0004003d, 0x0000013d, + 0x0000014c, 0x0000013f, 0x00050062, 0x00000026, 0x0000014e, 0x0000014c, 0x0000006d, 0x0004003d, + 0x0000013d, 0x0000014f, 0x0000013f, 0x0008004f, 0x0000000a, 0x00000152, 0x0000014e, 0x0000014e, + 0x00000000, 0x00000001, 0x00000002, 0x0005008e, 0x0000000a, 0x00000154, 0x00000152, 0x00000136, + 0x00050051, 0x00000006, 0x00000156, 0x0000014e, 0x00000003, 0x00050051, 0x00000006, 0x00000157, + 0x00000154, 0x00000000, 0x00050051, 0x00000006, 0x00000158, 0x00000154, 0x00000001, 0x00050051, + 0x00000006, 0x00000159, 0x00000154, 0x00000002, 0x00070050, 0x00000026, 0x0000015a, 0x00000157, + 0x00000158, 0x00000159, 0x00000156, 0x00040063, 0x0000014f, 0x0000006d, 0x0000015a, 0x000200f9, + 0x0000015d, 0x000200f8, 0x0000015d, 0x000100fd, 0x00010038, +}; diff --git a/runtime/cpu.h b/runtime/cpu.h index a3be39f4..207f4901 100644 --- a/runtime/cpu.h +++ b/runtime/cpu.h @@ -109,7 +109,7 @@ EXCL(16, uint16_t) EXCL(32, uint32_t) EXCL(64, uint64_t) -// Exclusive pair of two X registers: one 16-byte-aligned 128-bit access (cmpxchg16b; x86-64-v3 has CX16). +// Exclusive pair of two X registers: one 16-byte-aligned 128-bit access (cmpxchg16b; x86-64-v2 has CX16). // A pair of two W registers is a 64-bit exclusive and uses ldx64/stx64. static inline __uint128_t ldxp128(Cpu *c, uint64_t a) { __uint128_t v = __atomic_load_n((__uint128_t *)(uintptr_t)a, __ATOMIC_ACQUIRE); diff --git a/runtime/discord_rpc.c b/runtime/discord_rpc.c new file mode 100644 index 00000000..878deb91 --- /dev/null +++ b/runtime/discord_rpc.c @@ -0,0 +1,259 @@ +// Discord Rich Presence: "Playing TotK Recomp" with the elapsed play time in the player's Discord status, through +// the local IPC of a running Discord client (platform.h's plat_ipc_*). Nothing leaves the machine but the activity +// itself, and nothing is sent while TOTK_DISCORD_RPC is off (the default: the status is information about the +// player's session). TOTK_DISCORD_APP_ID names the application whose name and artwork Discord shows — the default +// is the project's own application, registered as "TotK Recomp"; a player who wants their own name or +// art registers an application at discord.com/developers and puts its id in totk.ini — and TOTK_DISCORD_IMAGE the +// key of its uploaded art asset. +// +// One host thread owns the connection and never holds a virtual core (kernel.c: host work runs without one). It +// re-reads the settings when cfg_version changes (a menu change reaches it within half a second), connects to the +// first of Discord's discord-ipc-0..9 pipes that answers, and writes the activity again every 60 s: that write is +// the keep-alive, and one that fails closes the connection and tries again 15 s later. Discord clears the status by +// itself when the process ends and the pipe closes, so there is no shutdown path. Every answer frame is read and +// discarded: unread, they would fill the pipe's buffer and block the client's writes. +// +// The protocol is the documented IPC one (discord-rpc's src/rpc_connection.h): a frame is , with Handshake 0, Frame 1, Close 2, Ping 3 and Pong 4. The handshake +// (opcode 0) carries {"v":1,"client_id":...} and is answered by a Ready frame (opcode 1); every command, +// SET_ACTIVITY here, is a frame (opcode 1) answered by one, and a Ping the client sends is echoed as a Pong. A +// client that speaks another numbering (an early arRPC did) answers the handshake with a Close. --probe-discord +// runs the same exchange once, without the game and without the setting being on. +#include +#include +#include +#include + +#include "cfg.h" +#include "host.h" +#include "platform/platform.h" + +enum { OP_HANDSHAKE = 0, OP_FRAME = 1, OP_CLOSE = 2, OP_PING = 3, OP_PONG = 4 }; + +#define APP_ID_CAP 24 // a Discord snowflake: at most 20 digits +#define IMAGE_KEY_CAP 64 +#define ANSWER_CAP 8192 // the Ready frame carries the client's config; a few KiB + +// A settings value that fits the JSON below without escaping: the app id is digits, an asset key letters, digits, +// '_' and '-' (what Discord's developer portal accepts for an asset key). +static int valid_app_id(const char *s) { + size_t n = strlen(s); + if (!n || n > 20) return 0; + for (const char *p = s; *p; p++) + if (*p < '0' || *p > '9') return 0; + return 1; +} + +static int valid_image_key(const char *s) { + size_t n = strlen(s); + if (!n || n >= IMAGE_KEY_CAP) return 0; + for (const char *p = s; *p; p++) + if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && *p != '_' && *p != '-') + return 0; + return 1; +} + +// The setting's value, or its default when the value does not fit the protocol (logged: the player sees why their +// own app does not show). Called on the presence thread and by the probe, after cfg_version changed or once. +static void read_setting(const char *name, char *out, size_t cap, int (*valid)(const char *)) { + const char *v = cfg_value(name); + if (valid(v)) { + snprintf(out, cap, "%s", v); + return; + } + snprintf(out, cap, "%s", cfg_meta(name)->def); + rt_log("discord: %s does not fit what the Rich Presence protocol takes; using %s", name + strlen("TOTK_"), out); +} + +static void le32(uint8_t *at, uint32_t v) { + at[0] = (uint8_t)v, at[1] = (uint8_t)(v >> 8), at[2] = (uint8_t)(v >> 16), at[3] = (uint8_t)(v >> 24); +} + +static int write_frame(PlatIpc *p, uint32_t op, const char *json) { + uint8_t head[8]; + le32(head, op); + le32(head + 4, (uint32_t)strlen(json)); + return plat_ipc_write(p, head, sizeof head) && plat_ipc_write(p, json, strlen(json)); +} + +// One frame off the wire, its 8-byte header in as many reads as the peer made, then its payload; a payload longer +// than cap-1 fills buf with its first cap-1 bytes and the rest is still consumed. *op gets the opcode; buf is +// NUL-terminated. 1 on a whole frame, 0 when the deadline passed, -1 when the connection broke. +static int read_one_frame(PlatIpc *p, uint32_t *op, char *buf, size_t cap, uint64_t deadline) { + uint8_t head[8]; + size_t got = 0; + while (got < sizeof head) { + uint32_t left = deadline > plat_now_ms() ? (uint32_t)(deadline - plat_now_ms()) : 0; + int n = plat_ipc_read(p, head + got, sizeof head - got, left); + if (n < 0) return -1; + if (!n) { + if (plat_now_ms() >= deadline) return 0; + continue; + } + got += (size_t)n; + } + *op = head[0] | head[1] << 8 | head[2] << 16 | (uint32_t)head[3] << 24; + uint32_t len = head[4] | head[5] << 8 | head[6] << 16 | (uint32_t)head[7] << 24; + size_t taken = 0; + while (taken < len) { + size_t want = len - taken > cap - 1 ? cap - 1 : len - taken; + uint32_t left = deadline > plat_now_ms() ? (uint32_t)(deadline - plat_now_ms()) : 0; + int n = plat_ipc_read(p, buf, want, left); + if (n < 0) return -1; + if (!n) { + if (plat_now_ms() >= deadline) return 0; + continue; + } + taken += (size_t)n; + } + buf[taken < cap - 1 ? taken : cap - 1] = 0; + return 1; +} + +// The next frame that is not a Ping: a Ping the client sent to see we are alive is echoed as a Pong with its own +// payload, a Close (the client told us it is done) counts as a broken connection so the caller connects again +// later. Same return values as read_one_frame; the deadline covers the whole exchange. +static int read_frame(PlatIpc *p, uint32_t *op, char *buf, size_t cap, uint32_t ms) { + uint64_t deadline = plat_now_ms() + ms; + for (;;) { + int r = read_one_frame(p, op, buf, cap, deadline); + if (r != 1) return r; + if (*op == OP_PING) { + uint8_t head[8]; + size_t n = strlen(buf); + le32(head, OP_PONG); + le32(head + 4, (uint32_t)n); + if (!plat_ipc_write(p, head, sizeof head) || !plat_ipc_write(p, buf, n)) return -1; + continue; + } + if (*op == OP_CLOSE) return -1; + return 1; + } +} + +// The first of Discord's pipes that completes a handshake. NULL when no client is running or none answered in time; +// the caller tries again later. +static PlatIpc *connect_client(const char *app_id) { + for (int i = 0; i < 10; i++) { + char name[32], json[160]; + snprintf(name, sizeof name, "discord-ipc-%d", i); + PlatIpc *p = plat_ipc_connect(name); + if (!p) continue; + snprintf(json, sizeof json, "{\"v\":1,\"client_id\":\"%s\"}", app_id); + if (!write_frame(p, OP_HANDSHAKE, json)) { + plat_ipc_close(p); + continue; + } + uint32_t op = 0; + char answer[ANSWER_CAP]; + if (read_frame(p, &op, answer, sizeof answer, 2000) == 1 && op == OP_FRAME) return p; // the Ready event + plat_ipc_close(p); // this pipe answered but not with a Ready frame in 2 s: the next may belong to a client + } + return NULL; +} + +static uint32_t next_nonce(void) { + static volatile int32_t n; + return (uint32_t)plat_inc32(&n); +} + +// The activity as one SET_ACTIVITY frame: the game update and the port version as the details line ("Game 1.4.3 · +// Recomp 0.3.7": the port version without its +commit suffix, the art's hover text keeps the full one; the dot goes +// as a JSON escape), the process's start as the timestamp Discord counts the elapsed time from, and the application's +// art asset. 1 when the client answered (a slow answer that ran out of the 2 s read is fine: the next drain takes +// it), 0 when the connection broke. +static int send_activity(PlatIpc *p, const char *image) { + char json[512]; + int v = rt_game_version; + uint64_t start = (plat_unix_time_ns() - plat_process_uptime_ns()) / 1000000000ull; + int port_len = (int)strcspn(rt_port_version, "+"); + snprintf(json, sizeof json, + "{\"cmd\":\"SET_ACTIVITY\",\"nonce\":\"%u\",\"args\":{\"pid\":%lu,\"activity\":{\"details\":\"Game " + "%d.%d.%d \\u00b7 Recomp %.*s\",\"timestamps\":{\"start\":%llu},\"assets\":{\"large_image\":\"%s\"," + "\"large_text\":\"TotK Recomp %s\"}}}}", + next_nonce(), (unsigned long)plat_process_id(), v / 100, v / 10 % 10, v % 10, port_len, rt_port_version, + (unsigned long long)start, image, rt_port_version); + if (!write_frame(p, OP_FRAME, json)) return 0; + uint32_t op = 0; + char answer[ANSWER_CAP]; + return read_frame(p, &op, answer, sizeof answer, 2000) >= 0; +} + +// ---- the presence thread + +static void discord_main(void *arg) { + (void)arg; + unsigned seen = 0; + char app_id[APP_ID_CAP], image[IMAGE_KEY_CAP]; + read_setting("TOTK_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id); + read_setting("TOTK_DISCORD_IMAGE", image, sizeof image, valid_image_key); + PlatIpc *ipc = NULL; + uint64_t retry_at = 0, send_at = 0; + int said_no_client = 0; + for (;;) { + unsigned v = cfg_version(); + if (v != seen) { // the settings changed (the menu writes them): pick the new ones up at once + seen = v; + read_setting("TOTK_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id); + read_setting("TOTK_DISCORD_IMAGE", image, sizeof image, valid_image_key); + if (ipc) plat_ipc_close(ipc), ipc = NULL; // the new app id needs a new handshake; the image is resent + said_no_client = 0; + } + if (!cfg_bool("TOTK_DISCORD_RPC")) { + if (ipc) plat_ipc_close(ipc), ipc = NULL; + plat_sleep_ms(500); + continue; + } + if (!ipc && plat_now_ms() >= retry_at) { + ipc = connect_client(app_id); + if (!ipc) { + retry_at = plat_now_ms() + 15000; + if (!said_no_client) + rt_log("discord: no client is running; trying again every 15 s while the setting is on"); + said_no_client = 1; + } else { + rt_log("discord: connected (app %s)", app_id); + send_at = 0; + said_no_client = 0; + } + } + if (ipc && plat_now_ms() >= send_at) { + if (send_activity(ipc, image)) { + send_at = plat_now_ms() + 60000; + } else { + rt_log("discord: the connection broke; reconnecting"); + plat_ipc_close(ipc); + ipc = NULL; + retry_at = plat_now_ms() + 15000; + } + } + plat_sleep_ms(500); + } +} + +void discord_init(void) { + if (!plat_thread_spawn(discord_main, NULL, "discord", 0)) rt_log("discord: cannot start the presence thread"); +} + +// ---- --probe-discord: the exchange above without the game and the setting; needs no game files + +int probe_discord(void) { + static int fails; +#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL discord: %s\n", what); } } while (0) + char app_id[APP_ID_CAP], image[IMAGE_KEY_CAP]; + read_setting("TOTK_DISCORD_APP_ID", app_id, sizeof app_id, valid_app_id); + read_setting("TOTK_DISCORD_IMAGE", image, sizeof image, valid_image_key); + PlatIpc *p = connect_client(app_id); + if (!p) { + fprintf(stderr, "discord: probe skipped: no client on discord-ipc-0..9\n"); + return 0; + } + fprintf(stderr, "discord: connected (app %s)\n", app_id); + CHECK(send_activity(p, image), "SET_ACTIVITY written and answered"); + fprintf(stderr, "discord: activity set; the status shows it for ten seconds, then the connection closes\n"); + plat_sleep_ms(10000); + plat_ipc_close(p); + fprintf(stderr, "discord: probe %s\n", fails ? "FAILED" : "passed"); + return fails != 0; +#undef CHECK +} diff --git a/runtime/dispatch.c b/runtime/dispatch.c index 80dad448..b284cf6c 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -52,6 +52,7 @@ void rt_fatal(const char *fmt, ...) { double proc, used, limit; plat_commit_info(&proc, &used, &limit); rt_log("fatal: memory commit: this process %.0f MB, the system %.0f of %.0f MB", proc, used, limit); + kern_free_cores_note(); fflush(stdout); fflush(stderr); _Exit(70); diff --git a/runtime/dlaa_mask_spv.h b/runtime/dlaa_mask_spv.h new file mode 100644 index 00000000..1a21adbf --- /dev/null +++ b/runtime/dlaa_mask_spv.h @@ -0,0 +1,66 @@ +// Generated by tools/make_dlaa_mask.py: DLSS's reactive mask pass (vk_dlaa.c mask_pass). Do not edit. +#pragma once +#include +static const uint32_t dlaa_mask_spv[] = { + 0x07230203, 0x00010000, 0x00000000, 0x00000055, 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x00000031, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000002, 0x6e69616d, 0x00000000, 0x0000001b, + 0x00060010, 0x00000002, 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00040047, 0x0000001b, + 0x0000000b, 0x0000001c, 0x00040047, 0x00000018, 0x00000022, 0x00000000, 0x00040047, 0x00000018, + 0x00000021, 0x00000000, 0x00040047, 0x00000019, 0x00000022, 0x00000000, 0x00040047, 0x00000019, + 0x00000021, 0x00000001, 0x00040047, 0x0000001a, 0x00000022, 0x00000000, 0x00040047, 0x0000001a, + 0x00000021, 0x00000002, 0x00030047, 0x0000001a, 0x00000019, 0x00030047, 0x00000015, 0x00000002, + 0x00050048, 0x00000015, 0x00000000, 0x00000023, 0x00000000, 0x00020013, 0x00000003, 0x00030021, + 0x00000004, 0x00000003, 0x00020014, 0x00000005, 0x00030016, 0x00000006, 0x00000020, 0x00040015, + 0x00000007, 0x00000020, 0x00000001, 0x00040015, 0x00000008, 0x00000020, 0x00000000, 0x00040017, + 0x00000009, 0x00000006, 0x00000002, 0x00040017, 0x0000000a, 0x00000006, 0x00000003, 0x00040017, + 0x0000000b, 0x00000006, 0x00000004, 0x00040017, 0x0000000c, 0x00000007, 0x00000002, 0x00040017, + 0x0000000d, 0x00000008, 0x00000002, 0x00040017, 0x0000000e, 0x00000008, 0x00000003, 0x00090019, + 0x0000000f, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, + 0x0003001b, 0x00000010, 0x0000000f, 0x00090019, 0x00000011, 0x00000006, 0x00000001, 0x00000000, + 0x00000000, 0x00000000, 0x00000002, 0x0000000f, 0x00040020, 0x00000012, 0x00000000, 0x00000010, + 0x00040020, 0x00000013, 0x00000000, 0x00000011, 0x00040020, 0x00000014, 0x00000001, 0x0000000e, + 0x0003001e, 0x00000015, 0x0000000b, 0x00040020, 0x00000016, 0x00000009, 0x00000015, 0x00040020, + 0x00000017, 0x00000009, 0x0000000b, 0x0004002b, 0x00000007, 0x0000001d, 0x00000000, 0x0004002b, + 0x00000006, 0x0000001e, 0x00000000, 0x0004002b, 0x00000006, 0x0000001f, 0x3f800000, 0x0004002b, + 0x00000006, 0x00000020, 0x38d1b717, 0x0004003b, 0x00000012, 0x00000018, 0x00000000, 0x0004003b, + 0x00000012, 0x00000019, 0x00000000, 0x0004003b, 0x00000013, 0x0000001a, 0x00000000, 0x0004003b, + 0x00000014, 0x0000001b, 0x00000001, 0x0004003b, 0x00000016, 0x0000001c, 0x00000009, 0x00050036, + 0x00000003, 0x00000002, 0x00000000, 0x00000004, 0x000200f8, 0x00000021, 0x00050041, 0x00000017, + 0x00000024, 0x0000001c, 0x0000001d, 0x0004003d, 0x0000000b, 0x00000025, 0x00000024, 0x0004003d, + 0x0000000e, 0x00000026, 0x0000001b, 0x0007004f, 0x0000000d, 0x00000027, 0x00000026, 0x00000026, + 0x00000000, 0x00000001, 0x00050051, 0x00000008, 0x00000028, 0x00000026, 0x00000000, 0x00040070, + 0x00000006, 0x00000029, 0x00000028, 0x00050051, 0x00000008, 0x0000002a, 0x00000026, 0x00000001, + 0x00040070, 0x00000006, 0x0000002b, 0x0000002a, 0x00050051, 0x00000006, 0x0000002c, 0x00000025, + 0x00000000, 0x00050051, 0x00000006, 0x0000002d, 0x00000025, 0x00000001, 0x000500b8, 0x00000005, + 0x0000002e, 0x00000029, 0x0000002c, 0x000500b8, 0x00000005, 0x0000002f, 0x0000002b, 0x0000002d, + 0x000500a7, 0x00000005, 0x00000030, 0x0000002e, 0x0000002f, 0x000300f7, 0x00000023, 0x00000000, + 0x000400fa, 0x00000030, 0x00000022, 0x00000023, 0x000200f8, 0x00000022, 0x0004007c, 0x0000000c, + 0x00000031, 0x00000027, 0x0004003d, 0x00000010, 0x00000032, 0x00000018, 0x00040064, 0x0000000f, + 0x00000033, 0x00000032, 0x0007005f, 0x0000000b, 0x00000034, 0x00000033, 0x00000031, 0x00000002, + 0x0000001d, 0x0008004f, 0x0000000a, 0x00000035, 0x00000034, 0x00000034, 0x00000000, 0x00000001, + 0x00000002, 0x0004003d, 0x00000010, 0x00000036, 0x00000019, 0x00040064, 0x0000000f, 0x00000037, + 0x00000036, 0x0007005f, 0x0000000b, 0x00000038, 0x00000037, 0x00000031, 0x00000002, 0x0000001d, + 0x0008004f, 0x0000000a, 0x00000039, 0x00000038, 0x00000038, 0x00000000, 0x00000001, 0x00000002, + 0x00050083, 0x0000000a, 0x0000003a, 0x00000039, 0x00000035, 0x0006000c, 0x0000000a, 0x0000003b, + 0x00000001, 0x00000004, 0x0000003a, 0x00050051, 0x00000006, 0x0000003c, 0x0000003b, 0x00000000, + 0x00050051, 0x00000006, 0x0000003d, 0x0000003b, 0x00000001, 0x00050051, 0x00000006, 0x0000003e, + 0x0000003b, 0x00000002, 0x0007000c, 0x00000006, 0x0000003f, 0x00000001, 0x00000028, 0x0000003c, + 0x0000003d, 0x0007000c, 0x00000006, 0x00000040, 0x00000001, 0x00000028, 0x0000003f, 0x0000003e, + 0x00050051, 0x00000006, 0x00000041, 0x00000039, 0x00000000, 0x00050051, 0x00000006, 0x00000042, + 0x00000039, 0x00000001, 0x00050051, 0x00000006, 0x00000043, 0x00000039, 0x00000002, 0x0007000c, + 0x00000006, 0x00000044, 0x00000001, 0x00000028, 0x00000041, 0x00000042, 0x0007000c, 0x00000006, + 0x00000045, 0x00000001, 0x00000028, 0x00000044, 0x00000043, 0x00050051, 0x00000006, 0x00000046, + 0x00000035, 0x00000000, 0x00050051, 0x00000006, 0x00000047, 0x00000035, 0x00000001, 0x00050051, + 0x00000006, 0x00000048, 0x00000035, 0x00000002, 0x0007000c, 0x00000006, 0x00000049, 0x00000001, + 0x00000028, 0x00000046, 0x00000047, 0x0007000c, 0x00000006, 0x0000004a, 0x00000001, 0x00000028, + 0x00000049, 0x00000048, 0x0007000c, 0x00000006, 0x0000004b, 0x00000001, 0x00000028, 0x00000045, + 0x0000004a, 0x0007000c, 0x00000006, 0x0000004c, 0x00000001, 0x00000028, 0x0000004b, 0x00000020, + 0x00050088, 0x00000006, 0x0000004d, 0x00000040, 0x0000004c, 0x00050051, 0x00000006, 0x0000004e, + 0x00000025, 0x00000002, 0x00050051, 0x00000006, 0x0000004f, 0x00000025, 0x00000003, 0x00050083, + 0x00000006, 0x00000050, 0x0000004d, 0x0000004e, 0x00050085, 0x00000006, 0x00000051, 0x00000050, + 0x0000004f, 0x0008000c, 0x00000006, 0x00000052, 0x00000001, 0x0000002b, 0x00000051, 0x0000001e, + 0x0000001f, 0x00070050, 0x0000000b, 0x00000053, 0x00000052, 0x00000052, 0x00000052, 0x00000052, + 0x0004003d, 0x00000011, 0x00000054, 0x0000001a, 0x00040063, 0x00000054, 0x00000031, 0x00000053, + 0x000200f9, 0x00000023, 0x000200f8, 0x00000023, 0x000100fd, 0x00010038 +}; diff --git a/runtime/dlaa_mv_spv.h b/runtime/dlaa_mv_spv.h new file mode 100644 index 00000000..93243f1d --- /dev/null +++ b/runtime/dlaa_mv_spv.h @@ -0,0 +1,162 @@ +// Generated by tools/build_shaders.py from runtime/shaders/dlaa_mv.comp; do not edit. +// source sha256 e96a6d2d0cec26c47a76280363077c5982919bedc984f20ae9f1d185dc6ae674 +#pragma once +#include +static const uint32_t dlaa_mv_spv[] = { + 0x07230203, 0x00010300, 0x000d000b, 0x0000019a, 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x00000031, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x00000036, + 0x00060010, 0x00000004, 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00040047, 0x00000036, + 0x0000000b, 0x0000001c, 0x00030047, 0x00000041, 0x00000002, 0x00050048, 0x00000041, 0x00000000, + 0x00000023, 0x00000000, 0x00050048, 0x00000041, 0x00000001, 0x00000023, 0x00000008, 0x00050048, + 0x00000041, 0x00000002, 0x00000023, 0x00000010, 0x00050048, 0x00000041, 0x00000003, 0x00000023, + 0x00000014, 0x00040047, 0x0000005d, 0x00000021, 0x00000000, 0x00040047, 0x0000005d, 0x00000022, + 0x00000000, 0x00040047, 0x00000086, 0x00000006, 0x00000010, 0x00030047, 0x00000087, 0x00000002, + 0x00050048, 0x00000087, 0x00000000, 0x00000023, 0x00000000, 0x00040047, 0x00000089, 0x00000021, + 0x00000002, 0x00040047, 0x00000089, 0x00000022, 0x00000000, 0x00040047, 0x0000009d, 0x00000006, + 0x00000010, 0x00030047, 0x0000009e, 0x00000002, 0x00050048, 0x0000009e, 0x00000000, 0x00000023, + 0x00000000, 0x00040047, 0x000000a0, 0x00000021, 0x00000003, 0x00040047, 0x000000a0, 0x00000022, + 0x00000000, 0x00030047, 0x000000e7, 0x00000019, 0x00040047, 0x000000e7, 0x00000021, 0x00000001, + 0x00040047, 0x000000e7, 0x00000022, 0x00000000, 0x00040047, 0x000000f6, 0x00000021, 0x00000004, + 0x00040047, 0x000000f6, 0x00000022, 0x00000000, 0x00030047, 0x00000101, 0x00000019, 0x00040047, + 0x00000101, 0x00000021, 0x00000007, 0x00040047, 0x00000101, 0x00000022, 0x00000000, 0x00030047, + 0x0000012a, 0x00000018, 0x00040047, 0x0000012a, 0x00000021, 0x00000006, 0x00040047, 0x0000012a, + 0x00000022, 0x00000000, 0x00030047, 0x0000013b, 0x00000019, 0x00040047, 0x0000013b, 0x00000021, + 0x00000005, 0x00040047, 0x0000013b, 0x00000022, 0x00000000, 0x00040047, 0x00000146, 0x0000000b, + 0x00000019, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, + 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004, 0x00040018, 0x00000009, 0x00000007, + 0x00000004, 0x0004002b, 0x00000006, 0x00000015, 0x3f800000, 0x0004002b, 0x00000006, 0x00000016, + 0x00000000, 0x00040015, 0x0000002f, 0x00000020, 0x00000001, 0x00040017, 0x00000030, 0x0000002f, + 0x00000002, 0x00040015, 0x00000033, 0x00000020, 0x00000000, 0x00040017, 0x00000034, 0x00000033, + 0x00000003, 0x00040020, 0x00000035, 0x00000001, 0x00000034, 0x0004003b, 0x00000035, 0x00000036, + 0x00000001, 0x00040017, 0x00000037, 0x00000033, 0x00000002, 0x00020014, 0x0000003b, 0x0004002b, + 0x00000033, 0x0000003c, 0x00000000, 0x00040017, 0x00000040, 0x00000006, 0x00000002, 0x0006001e, + 0x00000041, 0x00000040, 0x00000040, 0x00000006, 0x00000006, 0x00040020, 0x00000042, 0x00000009, + 0x00000041, 0x0004003b, 0x00000042, 0x00000043, 0x00000009, 0x0004002b, 0x0000002f, 0x00000044, + 0x00000000, 0x00040020, 0x00000045, 0x00000009, 0x00000006, 0x0004002b, 0x00000033, 0x0000004d, + 0x00000001, 0x00090019, 0x0000005a, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, + 0x00000001, 0x00000000, 0x0003001b, 0x0000005b, 0x0000005a, 0x00040020, 0x0000005c, 0x00000000, + 0x0000005b, 0x0004003b, 0x0000005c, 0x0000005d, 0x00000000, 0x0004002b, 0x00000006, 0x00000067, + 0x3f000000, 0x0004002b, 0x0000002f, 0x0000006a, 0x00000001, 0x00040020, 0x0000006b, 0x00000009, + 0x00000040, 0x0004002b, 0x00000006, 0x00000075, 0x40000000, 0x0004002b, 0x00000033, 0x00000085, + 0x00000010, 0x0004001c, 0x00000086, 0x00000007, 0x00000085, 0x0003001e, 0x00000087, 0x00000086, + 0x00040020, 0x00000088, 0x00000002, 0x00000087, 0x0004003b, 0x00000088, 0x00000089, 0x00000002, + 0x0004002b, 0x0000002f, 0x0000008a, 0x00000003, 0x0004002b, 0x0000002f, 0x0000008b, 0x00000004, + 0x0004002b, 0x0000002f, 0x0000008c, 0x00000005, 0x0004002b, 0x0000002f, 0x0000008d, 0x00000006, + 0x00040020, 0x0000008f, 0x00000002, 0x00000007, 0x0004001c, 0x0000009d, 0x00000007, 0x00000085, + 0x0003001e, 0x0000009e, 0x0000009d, 0x00040020, 0x0000009f, 0x00000002, 0x0000009e, 0x0004003b, + 0x0000009f, 0x000000a0, 0x00000002, 0x0005002c, 0x00000040, 0x000000b4, 0x00000016, 0x00000016, + 0x0004002b, 0x00000006, 0x000000b9, 0x2b8cbccc, 0x0004002b, 0x00000006, 0x000000c7, 0x358637bd, + 0x00090019, 0x000000e5, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000002, + 0x00000007, 0x00040020, 0x000000e6, 0x00000000, 0x000000e5, 0x0004003b, 0x000000e6, 0x000000e7, + 0x00000000, 0x0004002b, 0x0000002f, 0x000000ee, 0x00000002, 0x0004003b, 0x0000005c, 0x000000f6, + 0x00000000, 0x0004002b, 0x00000006, 0x000000fc, 0x3e808081, 0x00090019, 0x000000ff, 0x00000006, + 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x0000000f, 0x00040020, 0x00000100, + 0x00000000, 0x000000ff, 0x0004003b, 0x00000100, 0x00000101, 0x00000000, 0x0004002b, 0x0000002f, + 0x00000119, 0xffffffff, 0x0004003b, 0x00000100, 0x0000012a, 0x00000000, 0x0005002c, 0x00000030, + 0x00000131, 0x00000044, 0x00000044, 0x0004003b, 0x00000100, 0x0000013b, 0x00000000, 0x0004002b, + 0x00000033, 0x00000145, 0x00000008, 0x0006002c, 0x00000034, 0x00000146, 0x00000145, 0x00000145, + 0x0000004d, 0x0005002c, 0x00000040, 0x00000197, 0x00000067, 0x00000067, 0x0005002c, 0x00000030, + 0x00000199, 0x0000006a, 0x0000006a, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, + 0x000200f8, 0x00000005, 0x000300f7, 0x00000147, 0x00000000, 0x000300fb, 0x0000003c, 0x00000148, + 0x000200f8, 0x00000148, 0x0004003d, 0x00000034, 0x00000038, 0x00000036, 0x0007004f, 0x00000037, + 0x00000039, 0x00000038, 0x00000038, 0x00000000, 0x00000001, 0x0004007c, 0x00000030, 0x0000003a, + 0x00000039, 0x00050051, 0x0000002f, 0x0000003f, 0x0000003a, 0x00000000, 0x00060041, 0x00000045, + 0x00000046, 0x00000043, 0x00000044, 0x0000003c, 0x0004003d, 0x00000006, 0x00000047, 0x00000046, + 0x0004006e, 0x0000002f, 0x00000048, 0x00000047, 0x000500af, 0x0000003b, 0x00000049, 0x0000003f, + 0x00000048, 0x000400a8, 0x0000003b, 0x0000004a, 0x00000049, 0x000300f7, 0x0000004c, 0x00000000, + 0x000400fa, 0x0000004a, 0x0000004b, 0x0000004c, 0x000200f8, 0x0000004b, 0x00050051, 0x0000002f, + 0x0000004f, 0x0000003a, 0x00000001, 0x00060041, 0x00000045, 0x00000050, 0x00000043, 0x00000044, + 0x0000004d, 0x0004003d, 0x00000006, 0x00000051, 0x00000050, 0x0004006e, 0x0000002f, 0x00000052, + 0x00000051, 0x000500af, 0x0000003b, 0x00000053, 0x0000004f, 0x00000052, 0x000200f9, 0x0000004c, + 0x000200f8, 0x0000004c, 0x000700f5, 0x0000003b, 0x00000054, 0x00000049, 0x00000148, 0x00000053, + 0x0000004b, 0x000300f7, 0x00000056, 0x00000000, 0x000400fa, 0x00000054, 0x00000055, 0x00000056, + 0x000200f8, 0x00000055, 0x000200f9, 0x00000147, 0x000200f8, 0x00000056, 0x0004003d, 0x0000005b, + 0x0000005e, 0x0000005d, 0x00040064, 0x0000005a, 0x00000060, 0x0000005e, 0x0007005f, 0x00000007, + 0x00000061, 0x00000060, 0x0000003a, 0x00000002, 0x00000044, 0x00050051, 0x00000006, 0x00000062, + 0x00000061, 0x00000000, 0x0004006f, 0x00000040, 0x00000066, 0x0000003a, 0x00050081, 0x00000040, + 0x00000069, 0x00000066, 0x00000197, 0x00050041, 0x0000006b, 0x0000006c, 0x00000043, 0x0000006a, + 0x0004003d, 0x00000040, 0x0000006d, 0x0000006c, 0x00050083, 0x00000040, 0x0000006e, 0x00000069, + 0x0000006d, 0x00050051, 0x00000006, 0x00000071, 0x0000006e, 0x00000000, 0x00050088, 0x00000006, + 0x00000074, 0x00000071, 0x00000047, 0x00050085, 0x00000006, 0x00000076, 0x00000074, 0x00000075, + 0x00050083, 0x00000006, 0x00000077, 0x00000076, 0x00000015, 0x00050051, 0x00000006, 0x00000079, + 0x0000006e, 0x00000001, 0x00060041, 0x00000045, 0x0000007a, 0x00000043, 0x00000044, 0x0000004d, + 0x0004003d, 0x00000006, 0x0000007b, 0x0000007a, 0x00050088, 0x00000006, 0x0000007c, 0x00000079, + 0x0000007b, 0x00050085, 0x00000006, 0x0000007d, 0x0000007c, 0x00000075, 0x00050083, 0x00000006, + 0x0000007e, 0x00000015, 0x0000007d, 0x00050085, 0x00000006, 0x00000080, 0x00000062, 0x00000075, + 0x00050083, 0x00000006, 0x00000081, 0x00000080, 0x00000015, 0x00070050, 0x00000007, 0x00000082, + 0x00000077, 0x0000007e, 0x00000081, 0x00000015, 0x00060041, 0x0000008f, 0x00000090, 0x00000089, + 0x00000044, 0x0000008a, 0x0004003d, 0x00000007, 0x00000091, 0x00000090, 0x00060041, 0x0000008f, + 0x00000093, 0x00000089, 0x00000044, 0x0000008b, 0x0004003d, 0x00000007, 0x00000094, 0x00000093, + 0x00060041, 0x0000008f, 0x00000096, 0x00000089, 0x00000044, 0x0000008c, 0x0004003d, 0x00000007, + 0x00000097, 0x00000096, 0x00060041, 0x0000008f, 0x00000099, 0x00000089, 0x00000044, 0x0000008d, + 0x0004003d, 0x00000007, 0x0000009a, 0x00000099, 0x00070050, 0x00000009, 0x00000167, 0x00000091, + 0x00000094, 0x00000097, 0x0000009a, 0x00040054, 0x00000009, 0x00000168, 0x00000167, 0x00060041, + 0x0000008f, 0x000000a2, 0x000000a0, 0x00000044, 0x0000008a, 0x0004003d, 0x00000007, 0x000000a3, + 0x000000a2, 0x00060041, 0x0000008f, 0x000000a5, 0x000000a0, 0x00000044, 0x0000008b, 0x0004003d, + 0x00000007, 0x000000a6, 0x000000a5, 0x00060041, 0x0000008f, 0x000000a8, 0x000000a0, 0x00000044, + 0x0000008c, 0x0004003d, 0x00000007, 0x000000a9, 0x000000a8, 0x00060041, 0x0000008f, 0x000000ab, + 0x000000a0, 0x00000044, 0x0000008d, 0x0004003d, 0x00000007, 0x000000ac, 0x000000ab, 0x00070050, + 0x00000009, 0x00000183, 0x000000a3, 0x000000a6, 0x000000a9, 0x000000ac, 0x00040054, 0x00000009, + 0x00000184, 0x00000183, 0x0006000c, 0x00000009, 0x000000b0, 0x00000001, 0x00000022, 0x00000168, + 0x00050091, 0x00000007, 0x000000b2, 0x000000b0, 0x00000082, 0x00050051, 0x00000006, 0x000000b7, + 0x000000b2, 0x00000003, 0x0006000c, 0x00000006, 0x000000b8, 0x00000001, 0x00000004, 0x000000b7, + 0x000500ba, 0x0000003b, 0x000000ba, 0x000000b8, 0x000000b9, 0x000300f7, 0x000000bc, 0x00000000, + 0x000400fa, 0x000000ba, 0x000000bb, 0x000000bc, 0x000200f8, 0x000000bb, 0x00070050, 0x00000007, + 0x000000c2, 0x000000b7, 0x000000b7, 0x000000b7, 0x000000b7, 0x00050088, 0x00000007, 0x000000c3, + 0x000000b2, 0x000000c2, 0x00050091, 0x00000007, 0x000000c4, 0x00000184, 0x000000c3, 0x00050051, + 0x00000006, 0x000000c6, 0x000000c4, 0x00000003, 0x000500ba, 0x0000003b, 0x000000c8, 0x000000c6, + 0x000000c7, 0x000300f7, 0x000000ca, 0x00000000, 0x000400fa, 0x000000c8, 0x000000c9, 0x000000ca, + 0x000200f8, 0x000000c9, 0x0007004f, 0x00000040, 0x000000cd, 0x000000c4, 0x000000c4, 0x00000000, + 0x00000001, 0x00050050, 0x00000040, 0x000000d0, 0x000000c6, 0x000000c6, 0x00050088, 0x00000040, + 0x000000d1, 0x000000cd, 0x000000d0, 0x00050051, 0x00000006, 0x000000d4, 0x000000d1, 0x00000000, + 0x00050085, 0x00000006, 0x000000d5, 0x000000d4, 0x00000067, 0x00050081, 0x00000006, 0x000000d6, + 0x000000d5, 0x00000067, 0x00050085, 0x00000006, 0x000000d9, 0x000000d6, 0x00000047, 0x00050051, + 0x00000006, 0x000000db, 0x000000d1, 0x00000001, 0x00050085, 0x00000006, 0x000000dc, 0x000000db, + 0x00000067, 0x00050083, 0x00000006, 0x000000dd, 0x00000067, 0x000000dc, 0x00050085, 0x00000006, + 0x000000e0, 0x000000dd, 0x0000007b, 0x00050050, 0x00000040, 0x000000e1, 0x000000d9, 0x000000e0, + 0x00050083, 0x00000040, 0x000000e4, 0x000000e1, 0x0000006e, 0x000200f9, 0x000000ca, 0x000200f8, + 0x000000ca, 0x000700f5, 0x00000040, 0x00000190, 0x000000b4, 0x000000bb, 0x000000e4, 0x000000c9, + 0x000200f9, 0x000000bc, 0x000200f8, 0x000000bc, 0x000700f5, 0x00000040, 0x0000018f, 0x000000b4, + 0x00000056, 0x00000190, 0x000000ca, 0x0004003d, 0x000000e5, 0x000000e8, 0x000000e7, 0x00050051, + 0x00000006, 0x000000eb, 0x0000018f, 0x00000000, 0x00050051, 0x00000006, 0x000000ec, 0x0000018f, + 0x00000001, 0x00070050, 0x00000007, 0x000000ed, 0x000000eb, 0x000000ec, 0x00000016, 0x00000016, + 0x00040063, 0x000000e8, 0x0000003a, 0x000000ed, 0x00050041, 0x00000045, 0x000000ef, 0x00000043, + 0x000000ee, 0x0004003d, 0x00000006, 0x000000f0, 0x000000ef, 0x000500bc, 0x0000003b, 0x000000f1, + 0x000000f0, 0x00000016, 0x000300f7, 0x000000f3, 0x00000000, 0x000400fa, 0x000000f1, 0x000000f2, + 0x000000f3, 0x000200f8, 0x000000f2, 0x000200f9, 0x00000147, 0x000200f8, 0x000000f3, 0x0004003d, + 0x0000005b, 0x000000f7, 0x000000f6, 0x00040064, 0x0000005a, 0x000000f9, 0x000000f7, 0x0007005f, + 0x00000007, 0x000000fa, 0x000000f9, 0x0000003a, 0x00000002, 0x00000044, 0x00050051, 0x00000006, + 0x000000fb, 0x000000fa, 0x00000001, 0x000500ba, 0x0000003b, 0x000000fd, 0x000000fb, 0x000000fc, + 0x000600a9, 0x00000006, 0x000000fe, 0x000000fd, 0x00000015, 0x00000016, 0x0004003d, 0x000000ff, + 0x00000102, 0x00000101, 0x00070050, 0x00000007, 0x00000105, 0x000000fe, 0x000000fe, 0x000000fe, + 0x000000fe, 0x00040063, 0x00000102, 0x0000003a, 0x00000105, 0x00050041, 0x00000045, 0x00000107, + 0x00000043, 0x0000008a, 0x0004003d, 0x00000006, 0x00000108, 0x00000107, 0x000500ba, 0x0000003b, + 0x00000109, 0x00000108, 0x00000067, 0x000300f7, 0x0000010b, 0x00000000, 0x000400fa, 0x00000109, + 0x0000010a, 0x0000010b, 0x000200f8, 0x0000010a, 0x00050081, 0x00000040, 0x0000010f, 0x0000006e, + 0x0000018f, 0x0006000c, 0x00000040, 0x00000110, 0x00000001, 0x00000008, 0x0000010f, 0x0004006e, + 0x00000030, 0x00000111, 0x00000110, 0x00050041, 0x0000006b, 0x00000113, 0x00000043, 0x00000044, + 0x0004003d, 0x00000040, 0x00000114, 0x00000113, 0x0004006e, 0x00000030, 0x00000115, 0x00000114, + 0x00050082, 0x00000030, 0x00000117, 0x00000115, 0x00000199, 0x000200f9, 0x0000011a, 0x000200f8, + 0x0000011a, 0x000700f5, 0x00000006, 0x00000193, 0x00000016, 0x0000010a, 0x00000196, 0x0000011d, + 0x000700f5, 0x0000002f, 0x00000191, 0x00000119, 0x0000010a, 0x0000013a, 0x0000011d, 0x000500b3, + 0x0000003b, 0x00000120, 0x00000191, 0x0000006a, 0x000400f6, 0x0000011c, 0x0000011d, 0x00000000, + 0x000400fa, 0x00000120, 0x0000011b, 0x0000011c, 0x000200f8, 0x0000011b, 0x000200f9, 0x00000122, + 0x000200f8, 0x00000122, 0x000700f5, 0x00000006, 0x00000196, 0x00000193, 0x0000011b, 0x00000136, + 0x00000123, 0x000700f5, 0x0000002f, 0x00000194, 0x00000119, 0x0000011b, 0x00000138, 0x00000123, + 0x000500b3, 0x0000003b, 0x00000128, 0x00000194, 0x0000006a, 0x000400f6, 0x00000124, 0x00000123, + 0x00000000, 0x000400fa, 0x00000128, 0x00000123, 0x00000124, 0x000200f8, 0x00000123, 0x0004003d, + 0x000000ff, 0x0000012b, 0x0000012a, 0x00050050, 0x00000030, 0x0000012f, 0x00000194, 0x00000191, + 0x00050080, 0x00000030, 0x00000130, 0x00000111, 0x0000012f, 0x0008000c, 0x00000030, 0x00000133, + 0x00000001, 0x0000002d, 0x00000130, 0x00000131, 0x00000117, 0x00050062, 0x00000007, 0x00000134, + 0x0000012b, 0x00000133, 0x00050051, 0x00000006, 0x00000135, 0x00000134, 0x00000000, 0x0007000c, + 0x00000006, 0x00000136, 0x00000001, 0x00000028, 0x00000196, 0x00000135, 0x00050080, 0x0000002f, + 0x00000138, 0x00000194, 0x0000006a, 0x000200f9, 0x00000122, 0x000200f8, 0x00000124, 0x000200f9, + 0x0000011d, 0x000200f8, 0x0000011d, 0x00050080, 0x0000002f, 0x0000013a, 0x00000191, 0x0000006a, + 0x000200f9, 0x0000011a, 0x000200f8, 0x0000011c, 0x000200f9, 0x0000010b, 0x000200f8, 0x0000010b, + 0x000700f5, 0x00000006, 0x00000192, 0x00000016, 0x000000f3, 0x00000193, 0x0000011c, 0x0004003d, + 0x000000ff, 0x0000013c, 0x0000013b, 0x0007000c, 0x00000006, 0x00000140, 0x00000001, 0x00000028, + 0x000000fe, 0x00000192, 0x00050085, 0x00000006, 0x00000143, 0x00000140, 0x000000f0, 0x00070050, + 0x00000007, 0x00000144, 0x00000143, 0x00000143, 0x00000143, 0x00000143, 0x00040063, 0x0000013c, + 0x0000003a, 0x00000144, 0x000200f9, 0x00000147, 0x000200f8, 0x00000147, 0x000100fd, 0x00010038, +}; diff --git a/runtime/fma.c b/runtime/fma.c new file mode 100644 index 00000000..dbebdc12 --- /dev/null +++ b/runtime/fma.c @@ -0,0 +1,246 @@ +// Correctly rounded fma/fmaf for x86-64 builds without FMA3: the -march=x86-64-v2 build (docs/building.md). +// +// runtime/simd.h lowers AArch64 FMADD/FMSUB and the RECPS/RSQRTS helpers to __builtin_elementwise_fma, which clang +// turns into a call to fma/fmaf when the target has no FMA instruction. The C runtime is not a substitute: on CPUs +// without FMA3 the UCRT's software fallback is documented as rounding incorrectly by up to half an ulp, while the FP +// lowering needs the single rounding the ARM instruction gives. This file supplies it. +// +// Adapted from musl src/math/fma.c and src/math/fmaf.c (MIT, Copyright (c) 2005-2020 Rich Felker and contributors). +// The result is rounded in the MXCSR rounding mode the caller set (simd.h simd_rm_enter), and MXCSR FTZ and DAZ are +// honoured as the hardware FMA would, so runtime/simd.h's fast paths keep their assumptions: denormal inputs read as +// zero and denormal results as zero under FPCR.FZ, without a trip through the slow path. +// +// The definitions exist only where the compiler would emit the calls. A -march=x86-64-v3 build defines __FMA__ and +// emits vfmadd instead, so this file contributes nothing there. +#include + +#if defined(__x86_64__) && !defined(__FMA__) + +#include +#include + +#define MXCSR_DAZ 0x0040u +#define MXCSR_FTZ 0x8000u + +static double fma_flush_d(double x) { + union { double f; uint64_t i; } u = {x}; + if (!(u.i & 0x7ff0000000000000ull) && (u.i & 0x000fffffffffffffull)) u.i &= 0x8000000000000000ull; + return u.f; +} + +static float fma_flush_f(float x) { + union { float f; uint32_t i; } u = {x}; + if (!(u.i & 0x7f800000u) && (u.i & 0x007fffffu)) u.i &= 0x80000000u; + return u.f; +} + +// ---- float: the exact product fits in a double, so one extra term and a round-to-odd step decide the float +// rounding. Halfway and tiny results are the only ones where rounding the double first can change the float result. +float fmaf(float x, float y, float z) { + unsigned csr = _mm_getcsr(); + if (csr & MXCSR_DAZ) x = fma_flush_f(x), y = fma_flush_f(y), z = fma_flush_f(z); + double xy = (double)x * y; // exact + union { double r; uint64_t i; } u = {xy + z}; + int e = u.i >> 52 & 0x7ff; + int halfway = (u.i & 0x1fffffff) == 0x10000000; + int tiny = e <= 0x3ff - 126 && e >= 0x3ff - 149; + if (halfway || tiny) { + if (e != 0x7ff) { + // r+t is the exact sum in nearest rounding; otherwise t keeps its sign and whether it is zero, which + // is all the round-to-odd step needs. An odd sticky bit makes the final conversion round once, in + // whichever mode MXCSR asks for. + int s = u.i >> 63; + double t = s == (xy < z) ? xy - u.r + z : z - u.r + xy; + if (t) { + u.i -= s ^ (t < 0); + u.i |= 1; + } + } + } + float r = (float)u.r; + if (csr & MXCSR_FTZ) r = fma_flush_f(r); + return r; +} + +// ---- double: the significands are multiplied and aligned as integers, then converted back with one rounding. +#define ASUINT64(x) ((union { double f; uint64_t i; }){x}).i +#define ZEROINFNAN (0x7ff - 0x3ff - 52 - 1) + +struct fma_num { uint64_t m; int e; int sign; }; + +static struct fma_num fma_normalize(double x) { + uint64_t ix = ASUINT64(x); + int e = ix >> 52; + int sign = e & 0x800; + e &= 0x7ff; + if (!e) { + ix = ASUINT64(x * 0x1p63); + e = ix >> 52 & 0x7ff; + e = e ? e - 63 : 0x800; + } + ix &= (1ull << 52) - 1; + ix |= 1ull << 52; + ix <<= 1; + e -= 0x3ff + 52 + 1; + return (struct fma_num){ix, e, sign}; +} + +static void fma_mul(uint64_t *hi, uint64_t *lo, uint64_t x, uint64_t y) { + uint64_t xlo = (uint32_t)x, xhi = x >> 32; + uint64_t ylo = (uint32_t)y, yhi = y >> 32; + uint64_t t1 = xlo * ylo; + uint64_t t2 = xlo * yhi + xhi * ylo; + uint64_t t3 = xhi * yhi; + *lo = t1 + (t2 << 32); + *hi = t3 + (t2 >> 32) + (t1 > *lo); +} + +// scalbn from musl src/math/scalbn.c (MIT): the fma's final scaling, kept here so the result does not depend on the +// C runtime's version. Its multiplications honour MXCSR like any other host FP operation. +static double fma_scalbn(double x, int n) { + union { double f; uint64_t i; } u; + double y = x; + if (n > 1023) { + y *= 0x1p1023; + n -= 1023; + if (n > 1023) { + y *= 0x1p1023; + n -= 1023; + if (n > 1023) n = 1023; + } + } else if (n < -1022) { + // Make sure the final n < -53, so the subnormal range rounds only once. + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; + if (n < -1022) { + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; + if (n < -1022) n = -1022; + } + } + u.i = (uint64_t)(0x3ff + n) << 52; + return y * u.f; +} + +double fma(double x, double y, double z) { +#pragma STDC FENV_ACCESS ON + unsigned csr = _mm_getcsr(); + if (csr & MXCSR_DAZ) x = fma_flush_d(x), y = fma_flush_d(y), z = fma_flush_d(z); + + struct fma_num nx = fma_normalize(x), ny = fma_normalize(y), nz = fma_normalize(z); + if (nx.e >= ZEROINFNAN || ny.e >= ZEROINFNAN) return x * y + z; + if (nz.e >= ZEROINFNAN) { + if (nz.e > ZEROINFNAN) return x * y; // z == 0 + return z; + } + + uint64_t rhi, rlo, zhi, zlo; + fma_mul(&rhi, &rlo, nx.m, ny.m); + + // Align the exponents: shift z left by kz and r right by kr with kz + kr == d. + int e = nx.e + ny.e; + int d = nz.e - e; + if (d > 0) { + if (d < 64) { + zlo = nz.m << d; + zhi = nz.m >> (64 - d); + } else { + zlo = 0; + zhi = nz.m; + e = nz.e - 64; + d -= 64; + if (d == 0) { + } else if (d < 64) { + rlo = rhi << (64 - d) | rlo >> d | !!(rlo << (64 - d)); + rhi = rhi >> d; + } else { + rlo = 1; + rhi = 0; + } + } + } else { + zhi = 0; + d = -d; + if (d == 0) { + zlo = nz.m; + } else if (d < 64) { + zlo = nz.m >> d | !!(nz.m << (64 - d)); + } else { + zlo = 1; + } + } + + int sign = nx.sign ^ ny.sign; + int samesign = !(sign ^ nz.sign); + int nonzero = 1; + if (samesign) { + rlo += zlo; + rhi += zhi + (rlo < zlo); + } else { + uint64_t t = rlo; + rlo -= zlo; + rhi = rhi - zhi - (t < rlo); + if (rhi >> 63) { + rlo = -rlo; + rhi = -rhi - !!rlo; + sign = !sign; + } + nonzero = !!rhi; + } + + // Keep the top 63 bits in rhi; the last bit is sticky. + if (nonzero) { + e += 64; + d = __builtin_clzll(rhi) - 1; + rhi = rhi << d | rlo >> (64 - d) | !!(rlo << d); + } else if (rlo) { + d = __builtin_clzll(rlo) - 1; + if (d < 0) + rhi = rlo >> 1 | (rlo & 1); + else + rhi = rlo << d; + } else { + return x * y + z; // exact +-0 + } + e -= d; + + int64_t i = rhi; + if (sign) i = -i; + double r = i; // |r| is in [0x1p62, 0x1p63] + + if (e < -1022 - 62) { + // The result is subnormal before rounding. + if (e == -1022 - 63) { + double c = 0x1p63; + if (sign) c = -c; + if (r == c) { + // The smallest normal after rounding: whether it underflows depends on the architecture. A double + // to float conversion imitates the before-rounding rule the guest has. + float fltmin = 0x0.ffffff8p-63 * FLT_MIN * r; + r = DBL_MIN / FLT_MIN * fltmin; + return (csr & MXCSR_FTZ) ? fma_flush_d(r) : r; + } + // One bit is lost when scaled: add another top bit so the conversion rounds only once if inexact. + if (rhi << 53) { + i = rhi >> 1 | (rhi & 1) | 1ull << 62; + if (sign) i = -i; + r = i; + r = 2 * r - c; + // Raise underflow portably, so the compiler cannot optimize the expression away. + double tiny = DBL_MIN / FLT_MIN * r; + r += (double)(tiny * tiny) * (r - r); + } + } else { + // Only round once when scaled. + d = 10; + i = (rhi >> d | !!(rhi << (64 - d))) << d; + if (sign) i = -i; + r = i; + } + } + r = fma_scalbn(r, e); + if (csr & MXCSR_FTZ) r = fma_flush_d(r); + return r; +} + +#endif diff --git a/runtime/folder_pick.h b/runtime/folder_pick.h new file mode 100644 index 00000000..d701ef8d --- /dev/null +++ b/runtime/folder_pick.h @@ -0,0 +1,18 @@ +// The host's own folder dialog, for menu pages that point at a folder (the Amiibo tab): folder_pick_win.cpp with Win32, +// folder_pick_sdl.c with SDL. The menu has no text entry, so a folder is chosen here or by walking the page's list. +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// done gets the chosen folder (UTF-8) or NULL when the player cancelled or the host has no dialog. +typedef void (*FolderPickDone)(const char *path, void *ctx); + +// Opens the dialog at start (may be relative or missing) and returns at once; done runs later on another thread, once. +// The caller keeps one dialog open at a time. +void folder_pick(const char *start, FolderPickDone done, void *ctx); + +#ifdef __cplusplus +} +#endif diff --git a/runtime/folder_pick_sdl.c b/runtime/folder_pick_sdl.c new file mode 100644 index 00000000..74a48a1c --- /dev/null +++ b/runtime/folder_pick_sdl.c @@ -0,0 +1,42 @@ +// The SDL front end of folder_pick.h: SDL_ShowOpenFolderDialog (the desktop portal, else zenity), started on the window +// thread, which runs SDL's event loop that delivers the portal's answer. +#include +#include +#include + +#include + +#include "folder_pick.h" +#include "host.h" +#include "input_sdl.h" + +typedef struct { + char start[PATH_MAX]; // absolute, or empty: the dialog's own default + FolderPickDone done; + void *ctx; +} PickReq; + +static void SDLCALL on_pick(void *userdata, const char *const *list, int filter) { + (void)filter; + PickReq *r = userdata; + if (!list) rt_log("folder_pick: the folder dialog failed (%s)", SDL_GetError()); + r->done(list && list[0] ? list[0] : NULL, r->ctx); + free(r); +} + +static void show(void *arg) { + PickReq *r = arg; + SDL_ShowOpenFolderDialog(on_pick, r, gfx_window_sdl(), r->start[0] ? r->start : NULL, false); +} + +void folder_pick(const char *start, FolderPickDone done, void *ctx) { + PickReq *r = calloc(1, sizeof *r); + if (!r) { + done(NULL, ctx); + return; + } + r->done = done; + r->ctx = ctx; + if (start && *start && !realpath(start, r->start)) r->start[0] = 0; + gfx_window_sdl_run(show, r); +} diff --git a/runtime/folder_pick_win.cpp b/runtime/folder_pick_win.cpp new file mode 100644 index 00000000..9f6f40a5 --- /dev/null +++ b/runtime/folder_pick_win.cpp @@ -0,0 +1,84 @@ +// The Win32 front end of folder_pick.h: the shell's folder dialog (IFileOpenDialog with FOS_PICKFOLDERS), owned by the +// game window, on a thread of its own so the present thread that draws the menu keeps running. +#include +#include +#include +#include + +extern "C" { +#include "folder_pick.h" +#include "host.h" +#include "input_win.h" +#include "platform/platform.h" +} + +struct PickReq { + std::string start; + FolderPickDone done; + void *ctx; +}; + +static std::wstring wide(const std::string &utf8) { + int n = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0); + std::wstring w(n > 0 ? n : 1, L'\0'); + if (n > 0) MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &w[0], n); + w.resize(wcslen(w.c_str())); + return w; +} + +static std::string utf8(const wchar_t *w) { + int n = WideCharToMultiByte(CP_UTF8, 0, w, -1, NULL, 0, NULL, NULL); + std::string s(n > 0 ? n : 1, '\0'); + if (n > 0) WideCharToMultiByte(CP_UTF8, 0, w, -1, &s[0], n, NULL, NULL); + s.resize(strlen(s.c_str())); + return s; +} + +// The dialog wants an absolute folder that exists; anything else leaves it at the shell's last folder. +static void set_start(IFileOpenDialog *d, const std::string &start) { + wchar_t full[MAX_PATH * 2]; + if (start.empty() || !GetFullPathNameW(wide(start).c_str(), MAX_PATH * 2, full, NULL)) return; + IShellItem *item = NULL; + if (SUCCEEDED(SHCreateItemFromParsingName(full, NULL, IID_PPV_ARGS(&item)))) { + d->SetFolder(item); + item->Release(); + } +} + +static void pick_main(void *arg) { + PickReq *r = (PickReq *)arg; + std::string chosen; + HRESULT co = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + IFileOpenDialog *d = NULL; + HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&d)); + if (SUCCEEDED(hr)) { + FILEOPENDIALOGOPTIONS opt = 0; + d->GetOptions(&opt); + d->SetOptions(opt | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST); + set_start(d, r->start); + hr = d->Show(gfx_window_handle()); + IShellItem *item = NULL; + if (SUCCEEDED(hr) && SUCCEEDED(hr = d->GetResult(&item))) { + PWSTR path = NULL; + if (SUCCEEDED(hr = item->GetDisplayName(SIGDN_FILESYSPATH, &path))) { + chosen = utf8(path); + CoTaskMemFree(path); + } + item->Release(); + } + d->Release(); + } + if (FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_CANCELLED)) rt_log("folder_pick: the folder dialog failed (0x%08lx)", (unsigned long)hr); + r->done(chosen.empty() ? NULL : chosen.c_str(), r->ctx); + if (SUCCEEDED(co)) CoUninitialize(); + delete r; +} + +extern "C" void folder_pick(const char *start, FolderPickDone done, void *ctx) { + PickReq *r = new PickReq{start ? start : "", done, ctx}; + if (!plat_thread_spawn(pick_main, r, "folder dialog", 0)) { + rt_log("folder_pick: cannot start the dialog thread"); + done(NULL, ctx); + delete r; + } +} diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 61f82115..84ddca10 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -11,8 +11,20 @@ // game speed is right only when frames take exactly their interval. With the set realtime both read the patch page // instead: +0 ratio = seconds * 30, +4 seconds (1.4.3: +8 interval = seconds * 60, the producer's nominal interval // before its half-step coefficient), the moving average of the last 5 display intervals, at most 1/15 s -// (slower frames slow the game down, as on the console), no lower bound, so a higher frame-rate cap only changes the -// pacer (frame_step_cap_ns, TOTK_FPS_LIMIT). The set cloth gives hair and cloth the same clock and needs realtime. +// (slower frames slow the game down, as on the console) and at least 1/240 s, so a higher frame-rate cap only changes +// the pacer (frame_step_cap_ns, TOTK_FPS_LIMIT). The set cloth gives hair and cloth the same clock and needs realtime. +// +// The floor: with a shorter step (ratio below 0.125) 1.4.3 aborts within seconds of some save loads (issue #241, a +// 280 Hz monitor): Havok's hkDefaultMemoryImpl reports "Out of memory" for the buffer the physics pre-solve (profiler +// scope "TtPreSolve") resizes in main+0x1261630. Forced steps on the Depths save: 1/240 s plays, 1/244 s and shorter +// abort; the field, sky island and fight saves play at 1/280 s, the town save aborts after about 800 frames. The +// same short steps also crash a physics worker reading that buffer's contacts: an access violation in main+0x1292560 +// or its caller main+0x1286870 (narrowphase contact setup, reached from the grid task main+0x12f1ea0), reading a +// Havok body's shape at body+0x78 as null or garbage (issues #29 and #92, 240 Hz monitors with fps_limit = display). +// Steps drawn from 1/252..1/230 s crash the Depths load there in seconds; 1/240..1/230 s plays. The frame limit +// stops at 240 too (LIMIT_MAX_HZ), so the floor only catches the average dipping under 1/240 s at that limit: a +// present late by less than a period keeps the pacer's phase (nvn_api_obj.c, pace), so the next display interval is +// shorter than the period. No limit makes the game run slower than the clock. // // The sample is the display time the pacer gives each present (nvnQueuePresentTexture), so the step follows the // frames as shown, pacing waits included. With the step on, the pacer ignores the game's interval and holds a frame @@ -47,6 +59,7 @@ #define STEP_MAX_S (1.0 / 15.0) // longest step; a longer frame slows the game down #define STEP_RESET_S 0.25 // a gap this long (loading, a debugger) restarts the average #define STEP_FIRST_S (1.0 / 60.0) +#define STEP_MIN_S (1.0 / 240.0) // shortest step; a shorter one makes the game's physics run out of memory static _Atomic uint64_t *step_word; // patch page +0: ratio (low float), seconds (high float) static double step_now = STEP_FIRST_S; // the step the patch page holds: what the game steps each frame @@ -90,14 +103,15 @@ void frame_step_init(void) { int frame_step_realtime(void) { return step_word != NULL; } -// The frame limit (menu, TOTK_FPS_LIMIT, live): 30, 60, 90, 120, 144, any whole rate from 30 to 360, or display (the -// refresh rate of the window's monitor). Unset, the older TOTK_FPS60=0 still means 30. A limit of 30 switches the set +// The frame limit (menu, TOTK_FPS_LIMIT, live): 30, 60, 90, 120, 144, any whole rate from 30 to 240, or display (the +// refresh rate of the window's monitor, at most 240). A higher number gives 240, the rate of the shortest step the +// game survives (STEP_MIN_S). Unset, the older TOTK_FPS60=0 still means 30. A limit of 30 switches the set // fps60 off (the shipping minimum interval 2), every other limit switches it on; the controller reads its minimum // interval from the patched sites each frame. With the real-time step the pacer holds frames to the limit; above 60 -// only the pacer changes, as the step has no lower bound. Without it the game steps whole 60 Hz vsyncs and the pacer +// only the pacer changes, as the step's floor is the limit's ceiling. Without it the game steps whole 60 Hz vsyncs and the pacer // follows the game's interval, so the limit stops at 60. #define LIMIT_MIN_HZ 30 -#define LIMIT_MAX_HZ 360 +#define LIMIT_MAX_HZ 240 // 1 / STEP_MIN_S static int fps60_set = -2; static unsigned limit_seen; static int limit_read; @@ -109,8 +123,13 @@ static int limit_parse(void) { if (!strcmp(e, "display")) return 0; char *end; long n = strtol(e, &end, 10); - if (!*end && n >= LIMIT_MIN_HZ && n <= LIMIT_MAX_HZ) return (int)n; - rt_log("frame rate: TOTK_FPS_LIMIT=%s is not 30..360 or display; 60", e); + if (!*end && n > LIMIT_MAX_HZ) { + rt_log("frame rate: TOTK_FPS_LIMIT=%s is above %d, where the game's physics runs out of memory; %d", e, + LIMIT_MAX_HZ, LIMIT_MAX_HZ); + return LIMIT_MAX_HZ; + } + if (!*end && n >= LIMIT_MIN_HZ) return (int)n; + rt_log("frame rate: TOTK_FPS_LIMIT=%s is not %d..%d or display; 60", e, LIMIT_MIN_HZ, LIMIT_MAX_HZ); return 60; } @@ -161,13 +180,13 @@ static uint64_t read_ptr(uint64_t a) { uint64_t p = 0; return read_guest(a, &p, 8) ? p : 0; } -// 1.2.1: [main+0x4728538] (1.1.1: [main+0x4630a68], 1.4.3: [main+0x3add238]) +0x1e8 scene, its component 13 lists the +// 1.2.1: [main+0x4728538] (1.1.0: [main+0x4630a68], 1.4.3: [main+0x3add238]) +0x1e8 scene, its component 13 lists the // resident actors; the one named "Player" has its position at +0x2b4 (MacNativeGames world_survey.c, game_version.h -// TOTK_GAME_V121/V143). The 1.1.1 slot was measured from its own dereference chain: 181 scene accesses in that build +// TOTK_GAME_V121/V143). The 1.1.0 slot was measured from its own dereference chain: 181 scene accesses in that build // read it, and one of them reproduces this walk (the 14-component check, component 13, the 0x70 stride and the name // compare) instruction for instruction. static int player_pos(float xyz[3]) { - uint64_t module = read_ptr(MOD_MAIN_BASE + (RT_V143 ? 0x3add238 : RT_V111 ? 0x4630a68 : 0x4728538)), + uint64_t module = read_ptr(MOD_MAIN_BASE + (RT_V143 ? 0x3add238 : RT_V110 ? 0x4630a68 : 0x4728538)), scene = module ? read_ptr(module + 0x1e8) : 0; int32_t count = 0; if (!scene || !read_guest(scene + 0x50, &count, 4) || count < 14 || count > 256) return 0; @@ -258,6 +277,7 @@ void frame_step_present(uint64_t display_ns, int interval) { double sum = 0; for (unsigned i = 0; i < sample_count; i++) sum += samples[i]; step = sum / sample_count; + if (step < STEP_MIN_S) step = STEP_MIN_S; step_store((float)step); } } diff --git a/runtime/fs.h b/runtime/fs.h index a780e1e1..21d9f8cf 100644 --- a/runtime/fs.h +++ b/runtime/fs.h @@ -38,8 +38,15 @@ uint64_t romfs_file_count(void); // files in the index const char *romfs_entry(uint64_t i, uint64_t *offset, uint64_t *size); // path, image offset and size of index row i const char *romfs_image_path(void); // the romfs.bin path fs_init opened size_t romfs_overlay_count(void); -int romfs_xbox_prompts_ready(void); // romfs-patches-xbox was there at start -int romfs_xbox_prompts_active(void); // and was loaded (pad_layout = xbox_prompts at start) +int romfs_patches_loaded(void); // a romfs-patches manifest gave at least one file +// The button prompts (input_prompts_folder): the folder loaded at start, NULL for the game's own prompts; whether a +// folder had a manifest at start; and why the folder the settings asked for at start was not loaded (ROMFS_PROMPTS_OK +// for any other folder: it was not asked for). +enum { ROMFS_PROMPTS_OK, ROMFS_PROMPTS_MISSING, ROMFS_PROMPTS_FAILED, ROMFS_PROMPTS_SHAPE, ROMFS_PROMPTS_MODS, + ROMFS_PROMPTS_PATCHES }; +const char *romfs_prompts(void); +int romfs_prompts_ready(const char *folder); +int romfs_prompts_off(const char *folder); const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_added); // RomFS path of overlay k void fs_trace(const char *what, const char *path); // TOTK_TRACE=fs: one line per open diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 90eaa8dc..3af390a7 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -125,12 +125,30 @@ static void make_dirs(const char *p) { if (!make_dir(buf)) rt_fatal("fs: cannot create directory %s", buf); } -// Remove a directory tree; a link (reparse point, symbolic link) is removed as a link, never followed. 0 also when +// The first path remove_tree could not delete and the OS error code, for the message of a fatal. +static char remove_failed[1100]; +static int remove_error; +static void remove_note(const char *path) { + if (*remove_failed) return; + snprintf(remove_failed, sizeof remove_failed, "%s", path); + remove_error = plat_fs_os_error(); +} + +// Remove a directory tree; a link (symbolic link, junction) is removed as a link, never followed. 0 also when // nothing is there. static int remove_tree(const char *path) { int kind = plat_fs_kind(path); if (kind == PLAT_FS_MISSING) return 0; - if (kind == PLAT_FS_FILE) return plat_fs_unlink(path) ? -1 : 0; + if (kind == PLAT_FS_FILE) { + if (!plat_fs_unlink(path)) return 0; + remove_note(path); + return -1; + } + if (kind == PLAT_FS_LINK) { // a link to a file goes by unlink, a link to a directory by rmdir + if (!plat_fs_unlink(path) || !plat_fs_rmdir(path)) return 0; + remove_note(path); + return -1; + } int bad = 0; if (kind == PLAT_FS_DIR) { Listing l; @@ -142,7 +160,23 @@ static int remove_tree(const char *path) { } listing_free(l.e, l.n); } - return bad | (plat_fs_rmdir(path) ? -1 : 0); + if (plat_fs_rmdir(path)) { + remove_note(path); + bad |= -1; + } + return bad; +} + +// remove_tree, tried again every 50 ms for up to 2 s while another process (the previous run still exiting, a virus +// scanner, a sync client) holds a file in the tree. The failing path and its OS error stay in remove_failed. +#define MOVE_TRIES 40 // 2 s, also move_dir's +static int remove_tree_retry(const char *path) { + for (int i = 0;; i++) { + *remove_failed = 0; + if (!remove_tree(path)) return 0; + if (i == MOVE_TRIES) return -1; + plat_sleep_ms(50); + } } // ---- save transactions @@ -190,7 +224,6 @@ static int exists(const char *p) { return plat_fs_kind(p) != PLAT_FS_MISSING; } // Rename a directory, durably; retried every 50 ms, tries times in all, while another process (a virus scanner, the // indexer) holds a file in it. 0 on success. -#define MOVE_TRIES 40 // 2 s static int move_dir(const char *from, const char *to, int tries) { for (int i = 0; i < tries; i++) { int e = plat_fs_rename(from, to, 1); @@ -446,7 +479,8 @@ static void save_recover(void) { did = swap_in() ? "a finished commit still waits in save-tx; the game loads it from there" : "completed a finished commit"; if (exists(work_root)) { - if (remove_tree(work_root)) rt_fatal("fs: cannot delete the old save working copy"); + if (remove_tree_retry(work_root)) + rt_fatal("fs: cannot delete the old save working copy: %s (OS error %d)", remove_failed, remove_error); if (!strcmp(did, "nothing to recover")) did = "deleted the last run's working copy (changes after its last Commit)"; } @@ -1575,6 +1609,15 @@ static void probe_save_tx(const uint64_t *uid) { CALL(S_UNMOUNT, U("ro")); CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && tx_commit() == 0 && tx_read_committed("tx_a.sav") == -1, "Commit of deletions"); + // a run that left read-only folders and files in the working copy (attributes another emulator's save carried): + // the next start deletes them and names the failing path if it cannot + char *ro_dir = join(work_root, "ro_dir"), *ro_file = join(ro_dir, "ro.sav"); + CHECK(!plat_fs_mkdir(ro_dir) && plat_file_write(ro_file, "x", 1) && plat_file_set_readonly(ro_file, 1) && + plat_file_set_readonly(ro_dir, 1), "read-only leftovers in the working copy"); + probe_restart(uid); + CHECK(!exists(ro_dir), "the next start deleted a working copy with read-only folders"); + free(ro_dir); + free(ro_file); printf("save tx: uncommitted writes visible to the game and dropped by a restart and by Unmount, commits kept; a " "crash at each of %d commit steps recovers to the old save %d times, the new one %d times, mixed %d " "times; %d backups\n", steps, olds, news, mixed, backups); diff --git a/runtime/gfx.h b/runtime/gfx.h index b875bffb..614e8f65 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -35,6 +35,8 @@ typedef struct { // the translator's a uint8_t attr_type[32], frag_out_type[8]; // GFX_TYPE_* uint16_t local[3]; uint32_t shared_bytes; uint8_t depth_minus_w; + uint8_t mvprev; // VS: also write last frame's position (patch 0015, + // vk_mvprev.c); packed as bit 7 of the depth_minus_w byte } GfxShaderAnswers; typedef struct GfxShader GfxShader; // SPIR-V words + parsed reflection; owned by the cache const GfxShader *gfx_shader_get(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, @@ -73,6 +75,12 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out); // malloc'd; recorde const GfxShader *gfx_shader_load_private(const uint8_t iid[32], int *owned); void gfx_shader_release_private(const GfxShader *s); const uint32_t *gfx_shader_spirv(const GfxShader *s, uint64_t *words); +// A shader of the runtime's own (SPIR-V and a .res.json text as the translator writes them), outside the cache and +// never freed; its key is the sha256 of both. NULL when the SPIR-V is malformed. +const GfxShader *gfx_shader_own(const uint32_t *spirv, uint64_t words, const char *res_json); +// vk_hdr.c: with TOTK_HDR=1, replaces a fragment shader's SPIR-V (malloc'd, *words long) by the HDR tonemap when it is +// the game's tonemap; gfx_shader.c calls it for every fragment shader entering the cache. +void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words); uint64_t gfx_shader_failures(void); // failed translations; feeds GfxStats.shader_failures // Reflection: parsed once per shader from the translator's .res.json and the SPIR-V before it enters the cache. enum { GFX_RES_UBO, GFX_RES_SSBO, GFX_RES_TEX, GFX_RES_IMG, GFX_RES_KINDS }; @@ -94,7 +102,8 @@ int gfx_shader_stage(const GfxShader *s); // GFX_STAGE_VS, GFX int gfx_shader_resources(const GfxShader *s, int kind, const GfxShaderRes **res); // count of GFX_RES_* kind; array in *res uint32_t gfx_shader_slot_mask(const GfxShader *s); // texture/image slots whose type its translation read uint32_t gfx_shader_inputs(const GfxShader *s, GfxShaderIo io[32]); // Input Locations (VS attributes, FS varyings) as a mask -uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]); // Output Locations (VS varyings, FS colour targets) +uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]); +uint32_t gfx_shader_mvprev_banks(const GfxShader *s); // a last-frame variant: the banks (bit n: cn) its position reads // Output Locations (VS varyings, FS colour targets) void gfx_shader_local_size(const GfxShader *s, uint32_t local[3]); // CS: the LocalSize execution mode; 1, 1, 1 otherwise // ---- back end (gfx_null.c, vk_*.c). Called by the NVN front end (nvn_api_*.c, nvn_ext.c) on guest threads. diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 6a47dcad..9b5afbbf 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -23,9 +23,9 @@ #include "gfx.h" #include "gfx_wsys.h" #include "nvn_api.h" +#include "platform/platform.h" #include "ui/menu.h" #include "vk.h" -#include "platform/platform.h" #define FRAMES 2 #define MAX_IMAGES 16 @@ -40,6 +40,7 @@ void vk_queue_unlock(void) { plat_rmutex_unlock(&g_ql); } // ---- window state shared with the back end static VkSurfaceKHR g_surface; static volatile int32_t g_dirty; // the window was resized; swapchain stale +static volatile int32_t g_restart; // gfx_window_request_restart: closing the window starts the game again static volatile int32_t g_focused = 1; static volatile int32_t g_focus_msgs; static PlatRMutex g_start_lock; // gfx_window_start / vsync start @@ -47,25 +48,43 @@ static int g_started; void gfx_window_mark_dirty(void) { plat_xchg32(&g_dirty, 1); } -// The window gained or lost the focus: the game hears it as nn::oe's focus state and a notification message. +// The window gained or lost the focus: the game hears it as nn::oe's focus state and a notification message. The line +// carries the local wall time, the only one in the log: when a GPU hang takes the whole system down, the log ends +// without an error, and this time set against the driver's entry in the Windows event log (nvlddmkm 153, Display 4101) +// tells whether an alt-tab or an overlay came before the hang or followed it. void gfx_window_focus_changed(int in) { plat_xchg32(&g_focused, in); oe_set_focus(in); // pinned to 1 by TOTK_ASSUME_FOCUS inside oe_push_message(OE_MSG_FOCUS_STATE_CHANGED); plat_inc32(&g_focus_msgs); - rt_log("gfx: window %s the focus", in ? "gained" : "lost"); + struct tm t; + plat_local_time(&t); + rt_log("gfx: window %s the focus at %04d-%02d-%02d %02d:%02d:%02d local time", in ? "gained" : "lost", + t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); } // The window was closed: the process ends with it, as in MNG, once a save Commit still running in the background is -// done. +// done. After gfx_window_request_restart the executable then starts again with the same command line and environment +// (plat_restart), and the new process waits until this one is gone (main.c) before it moves the log and opens the +// RomFS and the saves. void gfx_window_closed(void) { - rt_log("gfx: window closed; exiting"); + int restart = plat_cas32(&g_restart, 0, 0) != 0; + rt_log("gfx: window closed; %s", restart ? "restarting" : "exiting"); fs_save_flush(); vk_pipeline_save(); prof_write(); // plat_exit runs no atexit handlers + if (restart) { + plat_setenv("TOTK_REEXEC", NULL); // the new process counts its own address-space retries (memory.c) + if (!plat_restart()) rt_log("gfx: the restart could not start the game again"); + } plat_exit(0); } +void gfx_window_request_restart(void) { + plat_xchg32(&g_restart, 1); + gfx_window_request_close(); +} + // TOTK_FULLSCREEN=1: borderless window over the monitor. TOTK_WINDOW_SIZE=x: client area of the window (the // swapchain follows the window; the game image is scaled to it). int gfx_cfg_fullscreen(void) { return cfg_bool("TOTK_FULLSCREEN"); } @@ -93,6 +112,7 @@ double gfx_window_refresh_hz(void) { return plat_cas32(&g_rate_mhz, 0, 0) / 1000 static PlatRMutex g_pl; static VkSwapchainKHR g_sc; static VkFormat g_sc_fmt; +static int g_sc_hdr; // HDR10 (TOTK_HDR, vk_hdr.c): PQ code values in Rec. 2020 static VkExtent2D g_sc_ext; static int g_scaler; // the last present: 0 the game image 1:1, 1 scaled by the blit, 2 by FSR static uint32_t g_sc_n; @@ -292,9 +312,9 @@ static int create_swapchain(void) { if (nf > 64) nf = 64; vkGetPhysicalDeviceSurfaceFormatsKHR(c->phys, g_surface, &nf, fmts); if (!nf) return 0; - // UNORM: the blit writes the bytes the game wrote (no sRGB encode). + // UNORM: the blit writes the bytes the game wrote (no sRGB encode). TOTK_HDR: HDR10 when the surface offers it. VkSurfaceFormatKHR sf = fmts[0]; - int found = 0; + int hdr = vk_hdr_surface_format(fmts, nf, &sf), found = hdr; for (uint32_t i = 0; i < nf && !found; i++) if (fmts[i].format == VK_FORMAT_B8G8R8A8_UNORM && fmts[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { sf = fmts[i]; found = 1; } for (uint32_t i = 0; i < nf && !found; i++) @@ -328,6 +348,9 @@ static int create_swapchain(void) { } if (old) vkDestroySwapchainKHR(c->device, old, NULL); g_sc_fmt = sf.format; + g_sc_hdr = hdr; + vk_hdr_set_active(hdr); + if (hdr) vk_hdr_metadata(g_sc, 1); g_sc_ext = ext; g_sc_n = MAX_IMAGES; vkGetSwapchainImagesKHR(c->device, g_sc, &g_sc_n, g_sc_img); @@ -411,12 +434,14 @@ static int ensure_thumbs(void) { } g_th_ptr = info.pMappedData; } - if (g_th_img && g_th_fmt != g_sc_fmt) { + // an HDR10 swapchain blits into RGBA8: its PQ codes, flat-looking but readable as bytes + VkFormat tf = g_sc_hdr ? VK_FORMAT_R8G8B8A8_UNORM : g_sc_fmt; + if (g_th_img && g_th_fmt != tf) { vmaDestroyImage(c->vma, g_th_img, g_th_img_alloc); // the previous present's fence was waited for: idle g_th_img = VK_NULL_HANDLE; } if (!g_th_img) { - VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = g_sc_fmt, + VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = tf, .extent = {FLIGHT_THUMB_W, FLIGHT_THUMB_H, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, @@ -428,7 +453,7 @@ static int ensure_thumbs(void) { rt_log("gfx: flight thumbnails off: no %ux%u image", FLIGHT_THUMB_W, FLIGHT_THUMB_H); return 0; } - g_th_fmt = g_sc_fmt; + g_th_fmt = tf; } return 1; } @@ -502,7 +527,8 @@ static void save_capture(const char *path) { uint8_t *px = malloc(n); if (!px) return; int bgra = g_sc_fmt == VK_FORMAT_B8G8R8A8_UNORM || g_sc_fmt == VK_FORMAT_B8G8R8A8_SRGB; - for (size_t i = 0; i < n; i += 4) { + if (g_sc_hdr) vk_hdr_to_srgb8(g_cap_ptr, px, n / 4, g_sc_fmt == VK_FORMAT_A2R10G10B10_UNORM_PACK32); + else for (size_t i = 0; i < n; i += 4) { px[i] = g_cap_ptr[i + (bgra ? 2 : 0)]; px[i + 1] = g_cap_ptr[i + 1]; px[i + 2] = g_cap_ptr[i + (bgra ? 0 : 2)]; @@ -554,6 +580,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) goto out; // lost, not ready: skip the frame if (r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1); vkResetFences(c->device, 1, &g_fence[slot]); + if (g_sc_hdr) vk_hdr_metadata(g_sc, 0); // a changed peak or white goes to the display if (g_th_pending[slot]) { // that fence has signalled: the thumbnail its submission wrote is complete g_th_tag[g_th_pending[slot] % (uint64_t)g_th_n] = g_th_pending[slot]; g_th_pending[slot] = 0; @@ -568,7 +595,8 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w window_settings_live(); // The menu overlay: laid out before recording (its first frame starts the ImGui back end and uploads the font) const char *menu_shot = NULL; - MenuTarget mt = {c->instance, c->phys, c->device, c->gfx_family, c->gfx_queue, g_sc_fmt, g_sc_ext.width, + MenuTarget mt = {c->instance, c->phys, c->device, c->gfx_family, c->gfx_queue, + g_sc_hdr ? VK_FORMAT_R8G8B8A8_UNORM : g_sc_fmt, g_sc_ext.width, g_sc_ext.height, cw, ch, g_scaler}; int overlay = g_sc_view[idx] && menu_overlay_frame(__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), &mt, &menu_shot); @@ -596,8 +624,10 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w VK_PIPELINE_STAGE_2_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); } - // Colour adjustment (TOTK_COLOR_*, vk_color.c): an adjusted copy of the crop takes the game image's place below - VkImage graded = general ? vk_color_record(cb, slot, src, fmt, rect) : VK_NULL_HANDLE; + // Colour adjustment (TOTK_COLOR_*, vk_color.c), or on an HDR10 swapchain the PQ encode (vk_hdr.c; the adjustment is + // for SDR): a copy of the crop takes the game image's place below + VkImage graded = !general ? VK_NULL_HANDLE : g_sc_hdr ? vk_hdr_encode(cb, slot, src, fmt, rect) + : vk_color_record(cb, slot, src, fmt, rect); const int grect[4] = {0, 0, cw, ch}; VkImage psrc = graded ? graded : src; VkFormat pfmt = graded ? VK_FORMAT_R16G16B16A16_SFLOAT : fmt; @@ -623,18 +653,24 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w VkImageLayout last = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; VkPipelineStageFlags2 last_stage = VK_PIPELINE_STAGE_2_TRANSFER_BIT; VkAccessFlags2 last_write = VK_ACCESS_2_TRANSFER_WRITE_BIT; + // HDR10: the menu goes into vk_hdr.c's RGBA8 image, which is then blended onto the swapchain image at the HUD's white + VkImageView menu_view = overlay && g_sc_hdr ? vk_hdr_ui_target(cb, g_sc_ext.width, g_sc_ext.height) : VK_NULL_HANDLE; + if (overlay && g_sc_hdr && !menu_view) overlay = 0; if (overlay) { sync_barrier(cb, dst, last, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, last_stage, last_write, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT); - VkRenderingAttachmentInfo ca = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .imageView = g_sc_view[idx], + VkRenderingAttachmentInfo ca = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = menu_view ? menu_view : g_sc_view[idx], .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, - .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, .storeOp = VK_ATTACHMENT_STORE_OP_STORE}; + .loadOp = menu_view ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE}; VkRenderingInfo ri = {.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, g_sc_ext}, .layerCount = 1, .colorAttachmentCount = 1, .pColorAttachments = &ca}; vkCmdBeginRendering(cb, &ri); menu_overlay_draw(cb); vkCmdEndRendering(cb); + if (menu_view) vk_hdr_ui_compose(cb, g_sc_view[idx], g_sc_fmt, g_sc_ext.width, g_sc_ext.height); last = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; last_stage = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT; last_write = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT; @@ -728,6 +764,8 @@ void gfx_window_release_vk(void) { menu_vk_release(); vk_fsr_release(); vk_color_release(); + vk_hdr_release(); + vk_contact_release(); free_capture(); free_thumbs(); for (int i = 0; i < FRAMES; i++) { diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index a38d6741..881fa86d 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -86,6 +86,7 @@ struct GfxShader { uint32_t in_mask, out_mask; // user Locations of Input / Output variables GfxShaderIo in[32], out[32]; uint32_t local[3]; + uint32_t mvprev_banks; // .res.json "mvprev_banks" (patch 0015), 0 none }; static PlatMutex cache_lock = PLAT_MUTEX_INIT; // memory cache @@ -589,6 +590,26 @@ static GfxShader *disk_load_from(const uint8_t key[32], const uint8_t *iid, int s->reflection = (char *)res; s->reflection_size = res_size; reflect(s); + if (s->stage == GFX_STAGE_FS) vk_hdr_shader_swap(&s->spirv, &s->words); // the in-memory copy; the cache keeps its own + return s; +} + +const GfxShader *gfx_shader_own(const uint32_t *spirv, uint64_t words, const char *res_json) { + if (words < 5 || spirv[0] != 0x07230203 || !spirv_complete(spirv, words) || + !reflection_valid(res_json, strlen(res_json))) + return NULL; + GfxShader *s = calloc(1, sizeof *s); + if (!s) return NULL; + size_t rn = strlen(res_json); + uint8_t *buf = malloc(words * 4 + rn); + s->spirv = malloc(words * 4), s->reflection = malloc(rn + 1); + if (!buf || !s->spirv || !s->reflection) rt_fatal("gfx_shader: out of memory"); + memcpy(buf, spirv, words * 4), memcpy(buf + words * 4, res_json, rn); + rt_sha256(buf, words * 4 + rn, s->key); + free(buf); + memcpy(s->spirv, spirv, words * 4), memcpy(s->reflection, res_json, rn + 1); + s->words = words, s->reflection_size = rn; + reflect(s); return s; } @@ -758,18 +779,81 @@ out: free(loc), free(packed), free(ttype), free(tcomps), free(pointee); } +// Every gradient sample (OpImageSampleExplicitLod with Grad) whose two gradients are each built from two components gets +// them transposed, dx = (a, c) and dy = (b, d) from dx = (a, b) and dy = (c, d), by two new OpCompositeConstruct before +// it. TotK's shaders fill a TXD's gradient registers per coordinate (du/dx, du/dy, dv/dx, dv/dy), and the translator +// (Ryujinx, InstEmitTexture EmitTxd) builds dx = (first two registers), dy = (last two) = (du/dx, du/dy) and +// (dv/dx, dv/dy): a mip level chosen from the wrong derivatives, too sharp on terrain faces turned across the screen, +// which crawled in patches that followed the faces (1.4.3, the Depths). TOTK_TXD_TRANSPOSE=0 turns it off. +// Applied to the SPIR-V after it is loaded, so the shader cache is untouched. +enum { OP_COMPOSITE_CONSTRUCT = 80, OP_SAMPLE_EXPLICIT = 88, IO_GRAD = 4 }; + +static void patch_txd_transpose(GfxShader *s) { + static int on = -1; + static volatile int32_t n_logged; + if (on < 0) { + const char *e = cfg_get("TOTK_TXD_TRANSPOSE"); + on = !(e && *e == '0'); + } + uint32_t *w = s->spirv; + uint64_t n = s->words; + if (!on || n < 5) return; + uint32_t bound = w[3] < (1u << 20) ? w[3] : (1u << 20); + uint64_t *cc = calloc(bound, sizeof *cc); // word index of a 2-constituent OpCompositeConstruct defining the id + int found = 0; + if (!cc) return; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) goto out; + if (op == OP_COMPOSITE_CONSTRUCT && len == 5 && w[i + 2] < bound) cc[w[i + 2]] = i; + if (op == OP_SAMPLE_EXPLICIT && len >= 8 && w[i + 5] == IO_GRAD && w[i + 6] < bound && w[i + 7] < bound && + cc[w[i + 6]] && cc[w[i + 7]] && w[cc[w[i + 6]] + 1] == w[cc[w[i + 7]] + 1]) + found++; + i += len; + } + if (!found) goto out; + uint32_t *o = malloc((n + (uint64_t)found * 10) * sizeof *o); + if (!o) goto out; + uint64_t k = 0; + uint32_t next = w[3]; + for (uint64_t i = 0; i < 5; i++) o[k++] = w[i]; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (op == OP_SAMPLE_EXPLICIT && len >= 8 && w[i + 5] == IO_GRAD && w[i + 6] < bound && w[i + 7] < bound && + cc[w[i + 6]] && cc[w[i + 7]] && w[cc[w[i + 6]] + 1] == w[cc[w[i + 7]] + 1]) { + const uint32_t *dx = w + cc[w[i + 6]], *dy = w + cc[w[i + 7]]; // [hdr, type, id, a, b], [hdr, type, id, c, d] + uint32_t ndx = next++, ndy = next++; + o[k++] = 5u << 16 | OP_COMPOSITE_CONSTRUCT, o[k++] = dx[1], o[k++] = ndx, o[k++] = dx[3], o[k++] = dy[3]; + o[k++] = 5u << 16 | OP_COMPOSITE_CONSTRUCT, o[k++] = dx[1], o[k++] = ndy, o[k++] = dx[4], o[k++] = dy[4]; + for (uint32_t j = 0; j < len; j++) o[k++] = w[i + j]; + o[k - len + 6] = ndx, o[k - len + 7] = ndy; + } else { + for (uint32_t j = 0; j < len; j++) o[k++] = w[i + j]; + } + i += len; + } + o[3] = next; + free(s->spirv); + s->spirv = o, s->words = k; + if (plat_inc32(&n_logged) <= 4) rt_log("gfx_shader: %d gradient samples now read their derivatives per coordinate", found); +out: + free(cc); +} + static void reflect(GfxShader *s) { static const char *const kinds[GFX_RES_KINDS] = {"cbufs", "sbufs", "textures", "images"}; const char *js = s->reflection; char stage[8]; json_str(js, js + strlen(js), "stage", stage, sizeof stage); s->stage = !strcmp(stage, "cs") ? GFX_STAGE_CS : !strcmp(stage, "ps") ? GFX_STAGE_FS : GFX_STAGE_VS; + s->mvprev_banks = (uint32_t)json_int(js, js + strlen(js), "mvprev_banks", 0); for (int k = 0; k < GFX_RES_KINDS; k++) { int count = json_objects(js, kinds[k], NULL, s, k); s->res[k] = calloc(count ? count : 1, sizeof *s->res[k]); json_objects(js, kinds[k], add_res, s, k); } reflect_spirv(s); + patch_txd_transpose(s); } // ---- use masks (see the cache key at the top) @@ -974,7 +1058,7 @@ static size_t pack_answers(uint8_t *o, const GfxShaderAnswers *a) { memcpy(p, a->frag_out_type, 8), p += 8; for (int i = 0; i < 3; i++) *p++ = (uint8_t)a->local[i], *p++ = (uint8_t)(a->local[i] >> 8); put_u32(&p, a->shared_bytes); - *p++ = a->depth_minus_w; + *p++ = (uint8_t)(a->depth_minus_w | (a->mvprev ? 0x80 : 0)); return (size_t)(p - o); } @@ -1006,6 +1090,7 @@ static void mask_answers(const GfxShaderAnswers *a, const UseMask *m, GfxShaderA if (m->fouts >> i & 1) o->frag_out_type[i] = a->frag_out_type[i]; memcpy(o->local, a->local, sizeof o->local); o->shared_bytes = a->shared_bytes; + o->mvprev = a->mvprev; // another translation, not an answer the use mask covers } // The cache key of an input id under the current translator. @@ -1085,6 +1170,7 @@ static size_t build_request(char *o0, const char *hex, const char *stage, const if (!strcmp(stage, "ps") && (control[0x718] & 1)) o += sprintf(o, ",\"early_fragment_tests\":true"); int si = !strcmp(stage, "vs") ? 0 : !strcmp(stage, "cs") ? 2 : 1; // gfx_shader_set_host answers if (!host_f64) o += sprintf(o, ",\"softfloat64\":true"); + if (a && a->mvprev && !strcmp(stage, "vs")) o += sprintf(o, ",\"mvprev\":true"); if (host_sbuf) o += sprintf(o, ",\"sbuf_align\":%u", host_sbuf); if (host_sg[si] != 32) o += sprintf(o, ",\"subgroup\":%u", host_sg[si]); if (host_gather) o += sprintf(o, ",\"gather_bias\":%u", host_gather); @@ -1287,8 +1373,18 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co if (!req) return plat_file_delete(code_path), fail("out of memory", hex); build_request(req, hex, r->stage_name, code_path, r->c1, r->c1_len, a, control); int ok = svc_request(req, reply, sizeof reply); + // TOTK_XLATE_KEEP=1: a last-frame variant's job stays as .job (one request line) with its code, to run the + // translator on by hand (totk_xlate --serve < .job) + static int keep = -1; + if (keep < 0) keep = rt_knob("XLATE_KEEP", "0"); + int kept = keep && a && a->mvprev; + if (kept) { + char job_path[PATH_CAP + 80]; + snprintf(job_path, sizeof job_path, "%s/%s.job", cache_dir, hex); + write_file(job_path, req, strlen(req)); + } free(req); - plat_file_delete(code_path); + if (!kept) plat_file_delete(code_path); if (!ok) return fail("translator unavailable", hex); if (strncmp(reply, "ok ", 3)) { plat_file_delete(spv_path), plat_file_delete(res_path); @@ -1643,6 +1739,8 @@ uint32_t gfx_shader_inputs(const GfxShader *s, GfxShaderIo io[32]) { return s->in_mask; } +uint32_t gfx_shader_mvprev_banks(const GfxShader *s) { return s->mvprev_banks; } + uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]) { if (io) memcpy(io, s->out, sizeof s->out); return s->out_mask; @@ -1732,7 +1830,7 @@ static void unpack_answers(const uint8_t *p, GfxShaderAnswers *a) { memcpy(a->frag_out_type, p, 8), p += 8; for (int i = 0; i < 3; i++) a->local[i] = (uint16_t)(p[0] | p[1] << 8), p += 2; memcpy(&a->shared_bytes, p, 4), p += 4; - a->depth_minus_w = *p; + a->depth_minus_w = *p & 0x7f, a->mvprev = *p >> 7; } // Reads a record file's body (after its header) into *body; 0 when absent, and a file with another header is removed. diff --git a/runtime/gfx_shader_probe.c b/runtime/gfx_shader_probe.c index 753353ed..2023b0d4 100644 --- a/runtime/gfx_shader_probe.c +++ b/runtime/gfx_shader_probe.c @@ -53,9 +53,9 @@ static int hexval(char c) { return c <= '9' ? c - '0' : (c | 32) - 'a' + 10; } // parent of totk.exe that has one with a jobs.json, the running game version's first, then the others' (any census // serves a probe). 1 with that folder in dir. int probe_census_dir(char *dir, size_t n) { - const char *const versions[3] = {RT_V111 ? "v111" : RT_V143 ? "v143" : "v121", - RT_V111 ? "v121" : RT_V143 ? "v121" : "v143", - RT_V111 ? "v143" : RT_V143 ? "v111" : "v111"}; + const char *const versions[3] = {RT_V110 ? "v110" : RT_V143 ? "v143" : "v121", + RT_V110 ? "v121" : RT_V143 ? "v121" : "v143", + RT_V110 ? "v143" : RT_V143 ? "v110" : "v110"}; for (int v = 0; v < 3; v++) { char exe[PLAT_PATH_MAX * 2]; int len = plat_exe_path(exe, sizeof exe); diff --git a/runtime/gfx_window_probe.c b/runtime/gfx_window_probe.c index 0757171b..336bd617 100644 --- a/runtime/gfx_window_probe.c +++ b/runtime/gfx_window_probe.c @@ -131,6 +131,7 @@ static int present_and_check(Source *s, int first, int i, int *w_out, int *h_out } int probe_window(void) { + vk_hdr_off(); // the checks read 8-bit swapchain pixels if (!menu_font_check()) return fail("the menu's font check takes a damaged font or refuses a good one"); gfx_window_start(); VkCtx *c = vk_ctx(); diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c index ef869c34..eef6efe6 100644 --- a/runtime/gfx_window_sdl.c +++ b/runtime/gfx_window_sdl.c @@ -64,6 +64,8 @@ double gfx_window_sdl_density(void) { return __atomic_load_n(&g_density_milli, _ SDL_Window *gfx_window_sdl(void) { return g_win; } +void gfx_window_sdl_run(void (*fn)(void *arg), void *arg) { run_on_window(fn, arg); } + // ---- settings: size and fullscreen static char g_applied_size[32]; static int g_applied_fs; diff --git a/runtime/gfx_window_win.c b/runtime/gfx_window_win.c index 6c026127..51dafe04 100644 --- a/runtime/gfx_window_win.c +++ b/runtime/gfx_window_win.c @@ -279,6 +279,24 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) { return DefWindowProcW(h, m, w, l); } +// Brings the game window to the front. Windows refuses SetForegroundWindow to a process that was not the last to get +// input, which is the case for a game started by a frontend; attaching this thread's input queue to the foreground +// window's thread for the call is the documented way around it. The window is not left topmost. +static void raise_window(HWND h) { + HWND fg = GetForegroundWindow(); + DWORD me = GetCurrentThreadId(), other = fg ? GetWindowThreadProcessId(fg, NULL) : 0; + int attached = other && other != me && AttachThreadInput(me, other, TRUE); + ShowWindow(h, IsIconic(h) ? SW_RESTORE : SW_SHOW); + BringWindowToTop(h); + SetForegroundWindow(h); + SetActiveWindow(h); + if (attached) AttachThreadInput(me, other, FALSE); + if (GetForegroundWindow() != h) { // still refused: a topmost toggle puts it above the others without keeping it there + SetWindowPos(h, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + SetWindowPos(h, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + } +} + static void window_main(void *arg) { (void)arg; HINSTANCE inst = GetModuleHandleW(NULL); @@ -293,10 +311,10 @@ static void window_main(void *arg) { int cw, ch, ow, oh; cfg_window_size(&cw, &ch); outer_size(NULL, cw, ch, &ow, &oh); - // The window opens behind the user's windows without taking focus: it shows the game's blank loading frames for a - // while, and must not cover what the player works on meanwhile. The game still counts itself in focus until the - // first WM_ACTIVATEAPP, so loading goes on, and keyboard input waits for the foreground (input_win.c). Its taskbar - // button flashes until it is brought up, and TOTK_FULLSCREEN waits for that first activation. + // The window opens behind the user's windows first and is then raised (raise_window): a game started by a frontend + // has no other way to reach it. The game counts itself in focus until the first WM_ACTIVATEAPP, so loading goes on, + // and keyboard input waits for the foreground (input_win.c). If Windows still refuses the foreground, the taskbar + // button flashes, and TOTK_FULLSCREEN waits for the first activation. // TOTK_BACKGROUND_WINDOW=1 (test runs): no flashing, never fullscreen at start, and F11 keeps the window's place. int background = g_background = cfg_get("TOTK_BACKGROUND_WINDOW") && atoi(cfg_get("TOTK_BACKGROUND_WINDOW")); g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"TotK Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, ow, oh, NULL, @@ -315,6 +333,7 @@ static void window_main(void *arg) { outer_size(g_hwnd, cw, ch, &ow, &oh); SetWindowPos(g_hwnd, HWND_BOTTOM, 0, 0, ow, oh, SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW); if (!background) { + raise_window(g_hwnd); // a frontend's player may have no way to reach a window left behind FLASHWINFO fw = {.cbSize = sizeof fw, .hwnd = g_hwnd, .dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG}; FlashWindowEx(&fw); g_fs_pending = cfg_fullscreen(); diff --git a/runtime/hdr_encode_spv.h b/runtime/hdr_encode_spv.h new file mode 100644 index 00000000..d2e5073f --- /dev/null +++ b/runtime/hdr_encode_spv.h @@ -0,0 +1,97 @@ +// Generated by tools/build_shaders.py from runtime/shaders/hdr_encode.comp; do not edit. +// source sha256 08e1d94f9041b32d4183286db39941eec9cde41412babb197c0303fd64bb07a7 +#pragma once +#include +static const uint32_t hdr_encode_spv[] = { + 0x07230203, 0x00010300, 0x000d000b, 0x000000dd, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, + 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000004c, 0x00060010, 0x00000004, + 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00040047, 0x0000004c, 0x0000000b, 0x0000001c, + 0x00030047, 0x00000053, 0x00000002, 0x00050048, 0x00000053, 0x00000000, 0x00000023, 0x00000000, + 0x00050048, 0x00000053, 0x00000001, 0x00000023, 0x00000008, 0x00050048, 0x00000053, 0x00000002, + 0x00000023, 0x00000010, 0x00040047, 0x00000064, 0x00000021, 0x00000000, 0x00040047, 0x00000064, + 0x00000022, 0x00000000, 0x00030047, 0x0000009c, 0x00000019, 0x00040047, 0x0000009c, 0x00000021, + 0x00000001, 0x00040047, 0x0000009c, 0x00000022, 0x00000000, 0x00040047, 0x000000a7, 0x0000000b, + 0x00000019, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, + 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000003, 0x0004002b, 0x00000006, 0x00000015, + 0x3d6147ae, 0x0004002b, 0x00000006, 0x0000001b, 0x4019999a, 0x0006002c, 0x00000007, 0x0000001c, + 0x0000001b, 0x0000001b, 0x0000001b, 0x0004002b, 0x00000006, 0x0000001f, 0x3d25aee6, 0x0006002c, + 0x00000007, 0x00000020, 0x0000001f, 0x0000001f, 0x0000001f, 0x00020014, 0x00000021, 0x00040017, + 0x00000022, 0x00000021, 0x00000003, 0x0004002b, 0x00000006, 0x0000002c, 0x00000000, 0x0004002b, + 0x00000006, 0x0000002d, 0x3f800000, 0x0004002b, 0x00000006, 0x00000031, 0x3e232000, 0x0006002c, + 0x00000007, 0x00000032, 0x00000031, 0x00000031, 0x00000031, 0x0004002b, 0x00000006, 0x00000034, + 0x3f560000, 0x0004002b, 0x00000006, 0x00000035, 0x4196d000, 0x0004002b, 0x00000006, 0x0000003a, + 0x41958000, 0x0004002b, 0x00000006, 0x00000040, 0x429db000, 0x0006002c, 0x00000007, 0x00000041, + 0x00000040, 0x00000040, 0x00000040, 0x00040015, 0x00000045, 0x00000020, 0x00000001, 0x00040017, + 0x00000046, 0x00000045, 0x00000002, 0x00040015, 0x00000049, 0x00000020, 0x00000000, 0x00040017, + 0x0000004a, 0x00000049, 0x00000003, 0x00040020, 0x0000004b, 0x00000001, 0x0000004a, 0x0004003b, + 0x0000004b, 0x0000004c, 0x00000001, 0x00040017, 0x0000004d, 0x00000049, 0x00000002, 0x00040017, + 0x00000052, 0x00000006, 0x00000004, 0x0005001e, 0x00000053, 0x00000046, 0x00000046, 0x00000052, + 0x00040020, 0x00000054, 0x00000009, 0x00000053, 0x0004003b, 0x00000054, 0x00000055, 0x00000009, + 0x0004002b, 0x00000045, 0x00000056, 0x00000001, 0x00040020, 0x00000057, 0x00000009, 0x00000046, + 0x00040017, 0x0000005a, 0x00000021, 0x00000002, 0x00090019, 0x00000061, 0x00000006, 0x00000001, + 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x00000062, 0x00000061, + 0x00040020, 0x00000063, 0x00000000, 0x00000062, 0x0004003b, 0x00000063, 0x00000064, 0x00000000, + 0x0004002b, 0x00000045, 0x00000067, 0x00000000, 0x0006002c, 0x00000007, 0x0000006e, 0x0000002c, + 0x0000002c, 0x0000002c, 0x0004002b, 0x00000045, 0x00000070, 0x00000002, 0x0004002b, 0x00000049, + 0x00000071, 0x00000002, 0x00040020, 0x00000072, 0x00000009, 0x00000006, 0x0004002b, 0x00000006, + 0x00000075, 0x3f000000, 0x00040018, 0x00000080, 0x00000007, 0x00000003, 0x0004002b, 0x00000006, + 0x00000081, 0x3f209d49, 0x0004002b, 0x00000006, 0x00000082, 0x3d8d844d, 0x0004002b, 0x00000006, + 0x00000083, 0x3c86594b, 0x0006002c, 0x00000007, 0x00000084, 0x00000081, 0x00000082, 0x00000083, + 0x0004002b, 0x00000006, 0x00000085, 0x3ea89a02, 0x0004002b, 0x00000006, 0x00000086, 0x3f6b645a, + 0x0004002b, 0x00000006, 0x00000087, 0x3db43958, 0x0006002c, 0x00000007, 0x00000088, 0x00000085, + 0x00000086, 0x00000087, 0x0004002b, 0x00000006, 0x00000089, 0x3d315b57, 0x0004002b, 0x00000006, + 0x0000008a, 0x3c3ac711, 0x0004002b, 0x00000006, 0x0000008b, 0x3f65460b, 0x0006002c, 0x00000007, + 0x0000008c, 0x00000089, 0x0000008a, 0x0000008b, 0x0006002c, 0x00000080, 0x0000008d, 0x00000084, + 0x00000088, 0x0000008c, 0x0004002b, 0x00000049, 0x00000091, 0x00000000, 0x0004002b, 0x00000049, + 0x00000095, 0x00000001, 0x00090019, 0x0000009a, 0x00000006, 0x00000001, 0x00000000, 0x00000000, + 0x00000000, 0x00000002, 0x00000002, 0x00040020, 0x0000009b, 0x00000000, 0x0000009a, 0x0004003b, + 0x0000009b, 0x0000009c, 0x00000000, 0x0004002b, 0x00000049, 0x000000a6, 0x00000008, 0x0006002c, + 0x0000004a, 0x000000a7, 0x000000a6, 0x000000a6, 0x00000095, 0x0006002c, 0x00000007, 0x000000d1, + 0x0000002d, 0x0000002d, 0x0000002d, 0x0006002c, 0x00000007, 0x000000d3, 0x00000015, 0x00000015, + 0x00000015, 0x0006002c, 0x00000007, 0x000000d6, 0x00000034, 0x00000034, 0x00000034, 0x0004002b, + 0x00000006, 0x000000d7, 0x3d9e8391, 0x0006002c, 0x00000007, 0x000000d8, 0x000000d7, 0x000000d7, + 0x000000d7, 0x0004002b, 0x00000006, 0x000000d9, 0x3f72a76f, 0x0006002c, 0x00000007, 0x000000da, + 0x000000d9, 0x000000d9, 0x000000d9, 0x0004002b, 0x00000006, 0x000000db, 0x38d1b717, 0x0006002c, + 0x00000007, 0x000000dc, 0x000000db, 0x000000db, 0x000000db, 0x00050036, 0x00000002, 0x00000004, + 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x000300f7, 0x000000a8, 0x00000000, 0x000300fb, + 0x00000091, 0x000000a9, 0x000200f8, 0x000000a9, 0x0004003d, 0x0000004a, 0x0000004e, 0x0000004c, + 0x0007004f, 0x0000004d, 0x0000004f, 0x0000004e, 0x0000004e, 0x00000000, 0x00000001, 0x0004007c, + 0x00000046, 0x00000050, 0x0000004f, 0x00050041, 0x00000057, 0x00000058, 0x00000055, 0x00000056, + 0x0004003d, 0x00000046, 0x00000059, 0x00000058, 0x000500af, 0x0000005a, 0x0000005b, 0x00000050, + 0x00000059, 0x0004009a, 0x00000021, 0x0000005c, 0x0000005b, 0x000300f7, 0x0000005e, 0x00000000, + 0x000400fa, 0x0000005c, 0x0000005d, 0x0000005e, 0x000200f8, 0x0000005d, 0x000200f9, 0x000000a8, + 0x000200f8, 0x0000005e, 0x0004003d, 0x00000062, 0x00000065, 0x00000064, 0x00050041, 0x00000057, + 0x00000068, 0x00000055, 0x00000067, 0x0004003d, 0x00000046, 0x00000069, 0x00000068, 0x00050080, + 0x00000046, 0x0000006a, 0x00000050, 0x00000069, 0x00040064, 0x00000061, 0x0000006b, 0x00000065, + 0x0007005f, 0x00000052, 0x0000006c, 0x0000006b, 0x0000006a, 0x00000002, 0x00000067, 0x0008004f, + 0x00000007, 0x0000006d, 0x0000006c, 0x0000006c, 0x00000000, 0x00000001, 0x00000002, 0x0007000c, + 0x00000007, 0x0000006f, 0x00000001, 0x00000028, 0x0000006d, 0x0000006e, 0x00060041, 0x00000072, + 0x00000073, 0x00000055, 0x00000070, 0x00000071, 0x0004003d, 0x00000006, 0x00000074, 0x00000073, + 0x000500ba, 0x00000021, 0x00000076, 0x00000074, 0x00000075, 0x000300f7, 0x00000078, 0x00000000, + 0x000400fa, 0x00000076, 0x00000077, 0x00000078, 0x000200f8, 0x00000077, 0x0008000c, 0x00000007, + 0x0000007c, 0x00000001, 0x0000002b, 0x0000006f, 0x0000006e, 0x000000d1, 0x00050085, 0x00000007, + 0x000000b2, 0x0000007c, 0x000000d8, 0x00050081, 0x00000007, 0x000000b5, 0x0000007c, 0x000000d3, + 0x00050085, 0x00000007, 0x000000b7, 0x000000b5, 0x000000da, 0x0007000c, 0x00000007, 0x000000b8, + 0x00000001, 0x0000001a, 0x000000b7, 0x0000001c, 0x000500ba, 0x00000022, 0x000000ba, 0x0000007c, + 0x00000020, 0x000600a9, 0x00000007, 0x000000bb, 0x000000ba, 0x000000b8, 0x000000b2, 0x000200f9, + 0x00000078, 0x000200f8, 0x00000078, 0x000700f5, 0x00000007, 0x000000d0, 0x0000006f, 0x0000005e, + 0x000000bb, 0x00000077, 0x00050091, 0x00000007, 0x0000008f, 0x0000008d, 0x000000d0, 0x0007000c, + 0x00000007, 0x00000090, 0x00000001, 0x00000028, 0x0000008f, 0x0000006e, 0x00060041, 0x00000072, + 0x00000092, 0x00000055, 0x00000070, 0x00000091, 0x0004003d, 0x00000006, 0x00000093, 0x00000092, + 0x0005008e, 0x00000007, 0x00000094, 0x00000090, 0x00000093, 0x00060041, 0x00000072, 0x00000096, + 0x00000055, 0x00000070, 0x00000095, 0x0004003d, 0x00000006, 0x00000097, 0x00000096, 0x00060050, + 0x00000007, 0x00000098, 0x00000097, 0x00000097, 0x00000097, 0x0007000c, 0x00000007, 0x00000099, + 0x00000001, 0x00000025, 0x00000094, 0x00000098, 0x0004003d, 0x0000009a, 0x0000009d, 0x0000009c, + 0x00050085, 0x00000007, 0x000000c1, 0x00000099, 0x000000dc, 0x0008000c, 0x00000007, 0x000000c4, + 0x00000001, 0x0000002b, 0x000000c1, 0x0000006e, 0x000000d1, 0x0007000c, 0x00000007, 0x000000c5, + 0x00000001, 0x0000001a, 0x000000c4, 0x00000032, 0x0005008e, 0x00000007, 0x000000c7, 0x000000c5, + 0x00000035, 0x00050081, 0x00000007, 0x000000c9, 0x000000d6, 0x000000c7, 0x0005008e, 0x00000007, + 0x000000cb, 0x000000c5, 0x0000003a, 0x00050081, 0x00000007, 0x000000cd, 0x000000d1, 0x000000cb, + 0x00050088, 0x00000007, 0x000000ce, 0x000000c9, 0x000000cd, 0x0007000c, 0x00000007, 0x000000cf, + 0x00000001, 0x0000001a, 0x000000ce, 0x00000041, 0x00050051, 0x00000006, 0x000000a2, 0x000000cf, + 0x00000000, 0x00050051, 0x00000006, 0x000000a3, 0x000000cf, 0x00000001, 0x00050051, 0x00000006, + 0x000000a4, 0x000000cf, 0x00000002, 0x00070050, 0x00000052, 0x000000a5, 0x000000a2, 0x000000a3, + 0x000000a4, 0x0000002d, 0x00040063, 0x0000009d, 0x00000050, 0x000000a5, 0x000200f9, 0x000000a8, + 0x000200f8, 0x000000a8, 0x000100fd, 0x00010038, +}; diff --git a/runtime/hdr_fullscreen_spv.h b/runtime/hdr_fullscreen_spv.h new file mode 100644 index 00000000..93e809ba --- /dev/null +++ b/runtime/hdr_fullscreen_spv.h @@ -0,0 +1,36 @@ +// Generated by tools/build_shaders.py from runtime/shaders/hdr_fullscreen.vert; do not edit. +// source sha256 3474de7753260cdbc5df61bbd96074c566f98c9be15be4791ae7cbea32542b03 +#pragma once +#include +static const uint32_t hdr_fullscreen_spv[] = { + 0x07230203, 0x00010300, 0x000d000b, 0x0000002c, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, + 0x0008000f, 0x00000000, 0x00000004, 0x6e69616d, 0x00000000, 0x00000009, 0x0000000c, 0x0000001d, + 0x00040047, 0x00000009, 0x0000001e, 0x00000000, 0x00040047, 0x0000000c, 0x0000000b, 0x0000002a, + 0x00030047, 0x0000001b, 0x00000002, 0x00050048, 0x0000001b, 0x00000000, 0x0000000b, 0x00000000, + 0x00050048, 0x0000001b, 0x00000001, 0x0000000b, 0x00000001, 0x00050048, 0x0000001b, 0x00000002, + 0x0000000b, 0x00000003, 0x00050048, 0x0000001b, 0x00000003, 0x0000000b, 0x00000004, 0x00020013, + 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, + 0x00000007, 0x00000006, 0x00000002, 0x00040020, 0x00000008, 0x00000003, 0x00000007, 0x0004003b, + 0x00000008, 0x00000009, 0x00000003, 0x00040015, 0x0000000a, 0x00000020, 0x00000001, 0x00040020, + 0x0000000b, 0x00000001, 0x0000000a, 0x0004003b, 0x0000000b, 0x0000000c, 0x00000001, 0x0004002b, + 0x0000000a, 0x0000000e, 0x00000001, 0x0004002b, 0x0000000a, 0x00000010, 0x00000002, 0x00040017, + 0x00000017, 0x00000006, 0x00000004, 0x00040015, 0x00000018, 0x00000020, 0x00000000, 0x0004002b, + 0x00000018, 0x00000019, 0x00000001, 0x0004001c, 0x0000001a, 0x00000006, 0x00000019, 0x0006001e, + 0x0000001b, 0x00000017, 0x00000006, 0x0000001a, 0x0000001a, 0x00040020, 0x0000001c, 0x00000003, + 0x0000001b, 0x0004003b, 0x0000001c, 0x0000001d, 0x00000003, 0x0004002b, 0x0000000a, 0x0000001e, + 0x00000000, 0x0004002b, 0x00000006, 0x00000020, 0x40000000, 0x0004002b, 0x00000006, 0x00000022, + 0x3f800000, 0x0004002b, 0x00000006, 0x00000025, 0x00000000, 0x00040020, 0x00000029, 0x00000003, + 0x00000017, 0x0005002c, 0x00000007, 0x0000002b, 0x00000022, 0x00000022, 0x00050036, 0x00000002, + 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003d, 0x0000000a, 0x0000000d, + 0x0000000c, 0x000500c4, 0x0000000a, 0x0000000f, 0x0000000d, 0x0000000e, 0x000500c7, 0x0000000a, + 0x00000011, 0x0000000f, 0x00000010, 0x0004006f, 0x00000006, 0x00000012, 0x00000011, 0x000500c7, + 0x0000000a, 0x00000014, 0x0000000d, 0x00000010, 0x0004006f, 0x00000006, 0x00000015, 0x00000014, + 0x00050050, 0x00000007, 0x00000016, 0x00000012, 0x00000015, 0x0003003e, 0x00000009, 0x00000016, + 0x0004003d, 0x00000007, 0x0000001f, 0x00000009, 0x0005008e, 0x00000007, 0x00000021, 0x0000001f, + 0x00000020, 0x00050083, 0x00000007, 0x00000024, 0x00000021, 0x0000002b, 0x00050051, 0x00000006, + 0x00000026, 0x00000024, 0x00000000, 0x00050051, 0x00000006, 0x00000027, 0x00000024, 0x00000001, + 0x00070050, 0x00000017, 0x00000028, 0x00000026, 0x00000027, 0x00000025, 0x00000022, 0x00050041, + 0x00000029, 0x0000002a, 0x0000001d, 0x0000001e, 0x0003003e, 0x0000002a, 0x00000028, 0x000100fd, + 0x00010038, +}; diff --git a/runtime/hdr_tonemap_spv.h b/runtime/hdr_tonemap_spv.h new file mode 100644 index 00000000..6a740ef4 --- /dev/null +++ b/runtime/hdr_tonemap_spv.h @@ -0,0 +1,290 @@ +// Generated by tools/build_shaders.py from runtime/shaders/hdr_tonemap.frag; do not edit. +// source sha256 a09ae2b13d844bb6a136d2c3cf38b3ec571191775a4c2f0b43a586bd355381fa +#pragma once +#include +static const uint32_t hdr_tonemap_spv[] = { + 0x07230203, 0x00010300, 0x0008000b, 0x0000018a, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, + 0x0007000f, 0x00000004, 0x00000004, 0x6e69616d, 0x00000000, 0x00000053, 0x00000181, 0x00030010, + 0x00000004, 0x00000007, 0x00030003, 0x00000002, 0x000001c2, 0x00040005, 0x00000004, 0x6e69616d, + 0x00000000, 0x00050005, 0x0000000a, 0x76727563, 0x31662865, 0x0000003b, 0x00030005, 0x00000009, + 0x00000078, 0x00050005, 0x00000010, 0x76727563, 0x76283365, 0x003b3366, 0x00030005, 0x0000000f, + 0x00000078, 0x00050005, 0x00000014, 0x6378616d, 0x33667628, 0x0000003b, 0x00030005, 0x00000013, + 0x00000063, 0x00030005, 0x00000016, 0x00003079, 0x00040005, 0x0000002e, 0x61726170, 0x0000006d, + 0x00040005, 0x00000034, 0x61726170, 0x0000006d, 0x00040005, 0x00000039, 0x61726170, 0x0000006d, + 0x00030005, 0x0000004b, 0x00000078, 0x00050005, 0x0000004f, 0x745f7066, 0x6263745f, 0x0000385f, + 0x00030005, 0x00000053, 0x00007675, 0x00050005, 0x0000005c, 0x745f7066, 0x6263745f, 0x0000415f, + 0x00030005, 0x00000064, 0x00000077, 0x00030005, 0x00000067, 0x00003143, 0x00040006, 0x00000067, + 0x00000000, 0x00000076, 0x00040005, 0x00000069, 0x635f7066, 0x00000031, 0x00030005, 0x0000006f, + 0x00000067, 0x00030005, 0x00000071, 0x00003343, 0x00040006, 0x00000071, 0x00000000, 0x00000076, + 0x00040005, 0x00000073, 0x635f7066, 0x00000033, 0x00040005, 0x00000076, 0x656d6167, 0x00000000, + 0x00060005, 0x00000079, 0x70707553, 0x4274726f, 0x65666675, 0x00000072, 0x00040006, 0x00000079, + 0x00000000, 0x00646170, 0x00040006, 0x00000079, 0x00000001, 0x00726468, 0x00060005, 0x0000007b, + 0x70707573, 0x5f74726f, 0x66667562, 0x00007265, 0x00040005, 0x00000089, 0x6b616570, 0x00000000, + 0x00030005, 0x00000094, 0x006d756c, 0x00030005, 0x000000a6, 0x00726473, 0x00040005, 0x000000ab, + 0x6c5f7962, 0x00006d75, 0x00040005, 0x000000b2, 0x61726170, 0x0000006d, 0x00030005, 0x000000bb, + 0x00000063, 0x00040005, 0x000000bd, 0x61726170, 0x0000006d, 0x00030005, 0x000000c7, 0x00000073, + 0x00040005, 0x000000c8, 0x61726170, 0x0000006d, 0x00030005, 0x000000cc, 0x0000006e, 0x00030005, + 0x000000d1, 0x0000006d, 0x00040005, 0x000000d2, 0x61726170, 0x0000006d, 0x00030005, 0x000000d5, + 0x00000064, 0x00040005, 0x000000e4, 0x64617267, 0x00000065, 0x00040005, 0x000000fa, 0x5f726473, + 0x00000063, 0x00030005, 0x00000115, 0x00006d73, 0x00040005, 0x00000116, 0x61726170, 0x0000006d, + 0x00030005, 0x00000119, 0x00006473, 0x00030005, 0x00000139, 0x00657568, 0x00030005, 0x0000013d, + 0x00006379, 0x00030005, 0x00000144, 0x00007379, 0x00040005, 0x00000159, 0x61726170, 0x0000006d, + 0x00030005, 0x00000160, 0x00706f74, 0x00040005, 0x00000161, 0x61726170, 0x0000006d, 0x00040005, + 0x00000164, 0x65656e6b, 0x00000000, 0x00030005, 0x0000016c, 0x00000072, 0x00040005, 0x00000181, + 0x6f6c6f63, 0x00000072, 0x00040047, 0x0000004f, 0x00000021, 0x00000001, 0x00040047, 0x0000004f, + 0x00000022, 0x00000000, 0x00040047, 0x00000053, 0x0000001e, 0x00000000, 0x00040047, 0x0000005c, + 0x00000021, 0x00000002, 0x00040047, 0x0000005c, 0x00000022, 0x00000000, 0x00040047, 0x00000066, + 0x00000006, 0x00000010, 0x00030047, 0x00000067, 0x00000002, 0x00050048, 0x00000067, 0x00000000, + 0x00000023, 0x00000000, 0x00040047, 0x00000069, 0x00000021, 0x00000003, 0x00040047, 0x00000069, + 0x00000022, 0x00000000, 0x00040047, 0x00000070, 0x00000006, 0x00000010, 0x00030047, 0x00000071, + 0x00000002, 0x00050048, 0x00000071, 0x00000000, 0x00000023, 0x00000000, 0x00040047, 0x00000073, + 0x00000021, 0x00000004, 0x00040047, 0x00000073, 0x00000022, 0x00000000, 0x00040047, 0x00000078, + 0x00000006, 0x00000010, 0x00030047, 0x00000079, 0x00000002, 0x00050048, 0x00000079, 0x00000000, + 0x00000023, 0x00000000, 0x00050048, 0x00000079, 0x00000001, 0x00000023, 0x000005a0, 0x00040047, + 0x0000007b, 0x00000021, 0x00000000, 0x00040047, 0x0000007b, 0x00000022, 0x00000000, 0x00040047, + 0x00000181, 0x0000001e, 0x00000000, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, + 0x00030016, 0x00000006, 0x00000020, 0x00040020, 0x00000007, 0x00000007, 0x00000006, 0x00040021, + 0x00000008, 0x00000006, 0x00000007, 0x00040017, 0x0000000c, 0x00000006, 0x00000003, 0x00040020, + 0x0000000d, 0x00000007, 0x0000000c, 0x00040021, 0x0000000e, 0x0000000c, 0x0000000d, 0x00040021, + 0x00000012, 0x00000006, 0x0000000d, 0x0004002b, 0x00000006, 0x00000017, 0x3e28aeed, 0x0004002b, + 0x00000006, 0x00000019, 0x3e3851ec, 0x00020014, 0x0000001a, 0x0004002b, 0x00000006, 0x0000001f, + 0x3f800000, 0x0004002b, 0x00000006, 0x00000026, 0x3f55d445, 0x00040015, 0x0000002f, 0x00000020, + 0x00000000, 0x0004002b, 0x0000002f, 0x00000030, 0x00000000, 0x0004002b, 0x0000002f, 0x00000035, + 0x00000001, 0x0004002b, 0x0000002f, 0x0000003a, 0x00000002, 0x00090019, 0x0000004c, 0x00000006, + 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x0000004d, + 0x0000004c, 0x00040020, 0x0000004e, 0x00000000, 0x0000004d, 0x0004003b, 0x0000004e, 0x0000004f, + 0x00000000, 0x00040017, 0x00000051, 0x00000006, 0x00000004, 0x00040020, 0x00000052, 0x00000001, + 0x00000051, 0x0004003b, 0x00000052, 0x00000053, 0x00000001, 0x00040017, 0x00000054, 0x00000006, + 0x00000002, 0x0004002b, 0x00000006, 0x00000059, 0x461c4000, 0x0006002c, 0x0000000c, 0x0000005a, + 0x00000059, 0x00000059, 0x00000059, 0x0004003b, 0x0000004e, 0x0000005c, 0x00000000, 0x00040020, + 0x00000063, 0x00000007, 0x00000051, 0x0004002b, 0x0000002f, 0x00000065, 0x00001000, 0x0004001c, + 0x00000066, 0x00000051, 0x00000065, 0x0003001e, 0x00000067, 0x00000066, 0x00040020, 0x00000068, + 0x00000002, 0x00000067, 0x0004003b, 0x00000068, 0x00000069, 0x00000002, 0x00040015, 0x0000006a, + 0x00000020, 0x00000001, 0x0004002b, 0x0000006a, 0x0000006b, 0x00000000, 0x00040020, 0x0000006c, + 0x00000002, 0x00000051, 0x0004001c, 0x00000070, 0x00000051, 0x00000065, 0x0003001e, 0x00000071, + 0x00000070, 0x00040020, 0x00000072, 0x00000002, 0x00000071, 0x0004003b, 0x00000072, 0x00000073, + 0x00000002, 0x0004002b, 0x0000002f, 0x00000077, 0x0000005a, 0x0004001c, 0x00000078, 0x00000051, + 0x00000077, 0x0004001e, 0x00000079, 0x00000078, 0x00000051, 0x00040020, 0x0000007a, 0x00000002, + 0x00000079, 0x0004003b, 0x0000007a, 0x0000007b, 0x00000002, 0x0004002b, 0x0000006a, 0x0000007c, + 0x00000001, 0x00040020, 0x0000007d, 0x00000002, 0x00000006, 0x0004002b, 0x00000006, 0x00000080, + 0x00000000, 0x0004002b, 0x00000006, 0x00000097, 0x3e99096c, 0x0006002c, 0x0000000c, 0x000000c5, + 0x00000080, 0x00000080, 0x00000080, 0x0004002b, 0x00000006, 0x000000d8, 0x3f2aaaab, 0x0006002c, + 0x0000000c, 0x00000108, 0x0000001f, 0x0000001f, 0x0000001f, 0x0004002b, 0x00000006, 0x0000013f, + 0x3e59b3d0, 0x0004002b, 0x00000006, 0x00000140, 0x3f371759, 0x0004002b, 0x00000006, 0x00000141, + 0x3d93dd98, 0x0006002c, 0x0000000c, 0x00000142, 0x0000013f, 0x00000140, 0x00000141, 0x0004002b, + 0x00000006, 0x0000014a, 0x3727c5ac, 0x0004002b, 0x00000006, 0x00000157, 0x3f000000, 0x0004002b, + 0x00000006, 0x00000158, 0x3fc00000, 0x00040020, 0x00000180, 0x00000003, 0x00000051, 0x0004003b, + 0x00000180, 0x00000181, 0x00000003, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, + 0x000200f8, 0x00000005, 0x0004003b, 0x0000000d, 0x0000004b, 0x00000007, 0x0004003b, 0x00000063, + 0x00000064, 0x00000007, 0x0004003b, 0x00000063, 0x0000006f, 0x00000007, 0x0004003b, 0x00000007, + 0x00000076, 0x00000007, 0x0004003b, 0x00000007, 0x00000082, 0x00000007, 0x0004003b, 0x00000007, + 0x00000089, 0x00000007, 0x0004003b, 0x00000007, 0x0000008d, 0x00000007, 0x0004003b, 0x00000007, + 0x00000094, 0x00000007, 0x0004003b, 0x00000007, 0x000000a6, 0x00000007, 0x0004003b, 0x0000000d, + 0x000000ab, 0x00000007, 0x0004003b, 0x0000000d, 0x000000ae, 0x00000007, 0x0004003b, 0x00000007, + 0x000000b2, 0x00000007, 0x0004003b, 0x0000000d, 0x000000bb, 0x00000007, 0x0004003b, 0x0000000d, + 0x000000bd, 0x00000007, 0x0004003b, 0x00000007, 0x000000c7, 0x00000007, 0x0004003b, 0x0000000d, + 0x000000c8, 0x00000007, 0x0004003b, 0x0000000d, 0x000000cc, 0x00000007, 0x0004003b, 0x00000007, + 0x000000d1, 0x00000007, 0x0004003b, 0x0000000d, 0x000000d2, 0x00000007, 0x0004003b, 0x00000007, + 0x000000d5, 0x00000007, 0x0004003b, 0x00000007, 0x000000e4, 0x00000007, 0x0004003b, 0x0000000d, + 0x000000fa, 0x00000007, 0x0004003b, 0x0000000d, 0x000000fd, 0x00000007, 0x0004003b, 0x00000007, + 0x00000115, 0x00000007, 0x0004003b, 0x0000000d, 0x00000116, 0x00000007, 0x0004003b, 0x00000007, + 0x00000119, 0x00000007, 0x0004003b, 0x00000007, 0x00000139, 0x00000007, 0x0004003b, 0x00000007, + 0x0000013d, 0x00000007, 0x0004003b, 0x00000007, 0x00000144, 0x00000007, 0x0004003b, 0x0000000d, + 0x00000159, 0x00000007, 0x0004003b, 0x00000007, 0x00000160, 0x00000007, 0x0004003b, 0x0000000d, + 0x00000161, 0x00000007, 0x0004003b, 0x00000007, 0x00000164, 0x00000007, 0x0004003b, 0x00000007, + 0x0000016c, 0x00000007, 0x0004003d, 0x0000004d, 0x00000050, 0x0000004f, 0x0004003d, 0x00000051, + 0x00000055, 0x00000053, 0x0007004f, 0x00000054, 0x00000056, 0x00000055, 0x00000055, 0x00000000, + 0x00000001, 0x00050057, 0x00000051, 0x00000057, 0x00000050, 0x00000056, 0x0008004f, 0x0000000c, + 0x00000058, 0x00000057, 0x00000057, 0x00000000, 0x00000001, 0x00000002, 0x0007000c, 0x0000000c, + 0x0000005b, 0x00000001, 0x00000025, 0x00000058, 0x0000005a, 0x0004003d, 0x0000004d, 0x0000005d, + 0x0000005c, 0x0004003d, 0x00000051, 0x0000005e, 0x00000053, 0x0007004f, 0x00000054, 0x0000005f, + 0x0000005e, 0x0000005e, 0x00000000, 0x00000001, 0x00050057, 0x00000051, 0x00000060, 0x0000005d, + 0x0000005f, 0x0008004f, 0x0000000c, 0x00000061, 0x00000060, 0x00000060, 0x00000000, 0x00000001, + 0x00000002, 0x00050081, 0x0000000c, 0x00000062, 0x0000005b, 0x00000061, 0x0003003e, 0x0000004b, + 0x00000062, 0x00060041, 0x0000006c, 0x0000006d, 0x00000069, 0x0000006b, 0x0000006b, 0x0004003d, + 0x00000051, 0x0000006e, 0x0000006d, 0x0003003e, 0x00000064, 0x0000006e, 0x00060041, 0x0000006c, + 0x00000074, 0x00000073, 0x0000006b, 0x0000006b, 0x0004003d, 0x00000051, 0x00000075, 0x00000074, + 0x0003003e, 0x0000006f, 0x00000075, 0x00060041, 0x0000007d, 0x0000007e, 0x0000007b, 0x0000007c, + 0x00000030, 0x0004003d, 0x00000006, 0x0000007f, 0x0000007e, 0x000500ba, 0x0000001a, 0x00000081, + 0x0000007f, 0x00000080, 0x000300f7, 0x00000084, 0x00000000, 0x000400fa, 0x00000081, 0x00000083, + 0x00000087, 0x000200f8, 0x00000083, 0x00060041, 0x0000007d, 0x00000085, 0x0000007b, 0x0000007c, + 0x00000030, 0x0004003d, 0x00000006, 0x00000086, 0x00000085, 0x0003003e, 0x00000082, 0x00000086, + 0x000200f9, 0x00000084, 0x000200f8, 0x00000087, 0x0003003e, 0x00000082, 0x0000001f, 0x000200f9, + 0x00000084, 0x000200f8, 0x00000084, 0x0004003d, 0x00000006, 0x00000088, 0x00000082, 0x0003003e, + 0x00000076, 0x00000088, 0x00060041, 0x0000007d, 0x0000008a, 0x0000007b, 0x0000007c, 0x00000035, + 0x0004003d, 0x00000006, 0x0000008b, 0x0000008a, 0x000500ba, 0x0000001a, 0x0000008c, 0x0000008b, + 0x0000001f, 0x000300f7, 0x0000008f, 0x00000000, 0x000400fa, 0x0000008c, 0x0000008e, 0x00000092, + 0x000200f8, 0x0000008e, 0x00060041, 0x0000007d, 0x00000090, 0x0000007b, 0x0000007c, 0x00000035, + 0x0004003d, 0x00000006, 0x00000091, 0x00000090, 0x0003003e, 0x0000008d, 0x00000091, 0x000200f9, + 0x0000008f, 0x000200f8, 0x00000092, 0x0003003e, 0x0000008d, 0x0000001f, 0x000200f9, 0x0000008f, + 0x000200f8, 0x0000008f, 0x0004003d, 0x00000006, 0x00000093, 0x0000008d, 0x0003003e, 0x00000089, + 0x00000093, 0x00050041, 0x00000007, 0x00000095, 0x0000004b, 0x00000030, 0x0004003d, 0x00000006, + 0x00000096, 0x00000095, 0x00050085, 0x00000006, 0x00000098, 0x00000096, 0x00000097, 0x00050041, + 0x00000007, 0x00000099, 0x0000004b, 0x00000035, 0x0004003d, 0x00000006, 0x0000009a, 0x00000099, + 0x00050041, 0x00000007, 0x0000009b, 0x00000064, 0x00000030, 0x0004003d, 0x00000006, 0x0000009c, + 0x0000009b, 0x00050085, 0x00000006, 0x0000009d, 0x0000009a, 0x0000009c, 0x00050081, 0x00000006, + 0x0000009e, 0x00000098, 0x0000009d, 0x00050041, 0x00000007, 0x0000009f, 0x0000004b, 0x0000003a, + 0x0004003d, 0x00000006, 0x000000a0, 0x0000009f, 0x00050041, 0x00000007, 0x000000a1, 0x00000064, + 0x00000035, 0x0004003d, 0x00000006, 0x000000a2, 0x000000a1, 0x00050085, 0x00000006, 0x000000a3, + 0x000000a0, 0x000000a2, 0x00050081, 0x00000006, 0x000000a4, 0x0000009e, 0x000000a3, 0x0007000c, + 0x00000006, 0x000000a5, 0x00000001, 0x00000028, 0x000000a4, 0x00000080, 0x0003003e, 0x00000094, + 0x000000a5, 0x0004003d, 0x00000006, 0x000000a7, 0x00000094, 0x0004007f, 0x00000006, 0x000000a8, + 0x000000a7, 0x0006000c, 0x00000006, 0x000000a9, 0x00000001, 0x0000001b, 0x000000a8, 0x00050083, + 0x00000006, 0x000000aa, 0x0000001f, 0x000000a9, 0x0003003e, 0x000000a6, 0x000000aa, 0x0004003d, + 0x00000006, 0x000000ac, 0x00000094, 0x000500ba, 0x0000001a, 0x000000ad, 0x000000ac, 0x00000080, + 0x000300f7, 0x000000b0, 0x00000000, 0x000400fa, 0x000000ad, 0x000000af, 0x000000b8, 0x000200f8, + 0x000000af, 0x0004003d, 0x0000000c, 0x000000b1, 0x0000004b, 0x0004003d, 0x00000006, 0x000000b3, + 0x00000094, 0x0003003e, 0x000000b2, 0x000000b3, 0x00050039, 0x00000006, 0x000000b4, 0x0000000a, + 0x000000b2, 0x0004003d, 0x00000006, 0x000000b5, 0x00000094, 0x00050088, 0x00000006, 0x000000b6, + 0x000000b4, 0x000000b5, 0x0005008e, 0x0000000c, 0x000000b7, 0x000000b1, 0x000000b6, 0x0003003e, + 0x000000ae, 0x000000b7, 0x000200f9, 0x000000b0, 0x000200f8, 0x000000b8, 0x0004003d, 0x0000000c, + 0x000000b9, 0x0000004b, 0x0003003e, 0x000000ae, 0x000000b9, 0x000200f9, 0x000000b0, 0x000200f8, + 0x000000b0, 0x0004003d, 0x0000000c, 0x000000ba, 0x000000ae, 0x0003003e, 0x000000ab, 0x000000ba, + 0x0004003d, 0x0000000c, 0x000000bc, 0x000000ab, 0x0004003d, 0x0000000c, 0x000000be, 0x0000004b, + 0x0003003e, 0x000000bd, 0x000000be, 0x00050039, 0x0000000c, 0x000000bf, 0x00000010, 0x000000bd, + 0x0004003d, 0x00000006, 0x000000c0, 0x000000a6, 0x0004003d, 0x00000006, 0x000000c1, 0x000000a6, + 0x00050085, 0x00000006, 0x000000c2, 0x000000c0, 0x000000c1, 0x00060050, 0x0000000c, 0x000000c3, + 0x000000c2, 0x000000c2, 0x000000c2, 0x0008000c, 0x0000000c, 0x000000c4, 0x00000001, 0x0000002e, + 0x000000bc, 0x000000bf, 0x000000c3, 0x0007000c, 0x0000000c, 0x000000c6, 0x00000001, 0x00000028, + 0x000000c4, 0x000000c5, 0x0003003e, 0x000000bb, 0x000000c6, 0x0004003d, 0x0000000c, 0x000000c9, + 0x000000bb, 0x0003003e, 0x000000c8, 0x000000c9, 0x00050039, 0x00000006, 0x000000ca, 0x00000014, + 0x000000c8, 0x0007000c, 0x00000006, 0x000000cb, 0x00000001, 0x00000028, 0x000000ca, 0x0000001f, + 0x0003003e, 0x000000c7, 0x000000cb, 0x0004003d, 0x0000000c, 0x000000cd, 0x000000bb, 0x0004003d, + 0x00000006, 0x000000ce, 0x000000c7, 0x00060050, 0x0000000c, 0x000000cf, 0x000000ce, 0x000000ce, + 0x000000ce, 0x00050088, 0x0000000c, 0x000000d0, 0x000000cd, 0x000000cf, 0x0003003e, 0x000000cc, + 0x000000d0, 0x0004003d, 0x0000000c, 0x000000d3, 0x000000cc, 0x0003003e, 0x000000d2, 0x000000d3, + 0x00050039, 0x00000006, 0x000000d4, 0x00000014, 0x000000d2, 0x0003003e, 0x000000d1, 0x000000d4, + 0x00050041, 0x00000007, 0x000000d6, 0x000000cc, 0x00000030, 0x0004003d, 0x00000006, 0x000000d7, + 0x000000d6, 0x00050085, 0x00000006, 0x000000d9, 0x000000d7, 0x000000d8, 0x00050041, 0x00000007, + 0x000000da, 0x000000cc, 0x00000035, 0x0004003d, 0x00000006, 0x000000db, 0x000000da, 0x00050041, + 0x00000007, 0x000000dc, 0x000000cc, 0x0000003a, 0x0004003d, 0x00000006, 0x000000dd, 0x000000dc, + 0x00050081, 0x00000006, 0x000000de, 0x000000db, 0x000000dd, 0x00050041, 0x00000007, 0x000000df, + 0x00000064, 0x0000003a, 0x0004003d, 0x00000006, 0x000000e0, 0x000000df, 0x00050085, 0x00000006, + 0x000000e1, 0x000000de, 0x000000e0, 0x00050081, 0x00000006, 0x000000e2, 0x000000d9, 0x000000e1, + 0x00050083, 0x00000006, 0x000000e3, 0x000000e2, 0x0000001f, 0x0003003e, 0x000000d5, 0x000000e3, + 0x00050041, 0x00000007, 0x000000e5, 0x0000006f, 0x00000035, 0x0004003d, 0x00000006, 0x000000e6, + 0x000000e5, 0x0004003d, 0x00000006, 0x000000e7, 0x000000d5, 0x0004003d, 0x00000006, 0x000000e8, + 0x000000d5, 0x00050085, 0x00000006, 0x000000e9, 0x000000e7, 0x000000e8, 0x00050083, 0x00000006, + 0x000000ea, 0x0000001f, 0x000000e9, 0x00050085, 0x00000006, 0x000000eb, 0x000000e6, 0x000000ea, + 0x00050041, 0x00000007, 0x000000ec, 0x0000006f, 0x00000030, 0x0004003d, 0x00000006, 0x000000ed, + 0x000000ec, 0x00050081, 0x00000006, 0x000000ee, 0x000000eb, 0x000000ed, 0x0003003e, 0x000000e4, + 0x000000ee, 0x0004003d, 0x00000006, 0x000000ef, 0x000000c7, 0x0004003d, 0x0000000c, 0x000000f0, + 0x000000cc, 0x0004003d, 0x00000006, 0x000000f1, 0x000000d1, 0x00060050, 0x0000000c, 0x000000f2, + 0x000000f1, 0x000000f1, 0x000000f1, 0x00050083, 0x0000000c, 0x000000f3, 0x000000f0, 0x000000f2, + 0x0004003d, 0x00000006, 0x000000f4, 0x000000e4, 0x0005008e, 0x0000000c, 0x000000f5, 0x000000f3, + 0x000000f4, 0x0004003d, 0x00000006, 0x000000f6, 0x000000d1, 0x00060050, 0x0000000c, 0x000000f7, + 0x000000f6, 0x000000f6, 0x000000f6, 0x00050081, 0x0000000c, 0x000000f8, 0x000000f5, 0x000000f7, + 0x0005008e, 0x0000000c, 0x000000f9, 0x000000f8, 0x000000ef, 0x0003003e, 0x000000bb, 0x000000f9, + 0x0004003d, 0x00000006, 0x000000fb, 0x00000094, 0x000500ba, 0x0000001a, 0x000000fc, 0x000000fb, + 0x00000080, 0x000300f7, 0x000000ff, 0x00000000, 0x000400fa, 0x000000fc, 0x000000fe, 0x00000105, + 0x000200f8, 0x000000fe, 0x0004003d, 0x0000000c, 0x00000100, 0x0000004b, 0x0004003d, 0x00000006, + 0x00000101, 0x000000a6, 0x0004003d, 0x00000006, 0x00000102, 0x00000094, 0x00050088, 0x00000006, + 0x00000103, 0x00000101, 0x00000102, 0x0005008e, 0x0000000c, 0x00000104, 0x00000100, 0x00000103, + 0x0003003e, 0x000000fd, 0x00000104, 0x000200f9, 0x000000ff, 0x000200f8, 0x00000105, 0x0004003d, + 0x0000000c, 0x00000106, 0x0000004b, 0x0003003e, 0x000000fd, 0x00000106, 0x000200f9, 0x000000ff, + 0x000200f8, 0x000000ff, 0x0004003d, 0x0000000c, 0x00000107, 0x000000fd, 0x0004003d, 0x0000000c, + 0x00000109, 0x0000004b, 0x0004007f, 0x0000000c, 0x0000010a, 0x00000109, 0x0006000c, 0x0000000c, + 0x0000010b, 0x00000001, 0x0000001b, 0x0000010a, 0x00050083, 0x0000000c, 0x0000010c, 0x00000108, + 0x0000010b, 0x0004003d, 0x00000006, 0x0000010d, 0x000000a6, 0x0004003d, 0x00000006, 0x0000010e, + 0x000000a6, 0x00050085, 0x00000006, 0x0000010f, 0x0000010d, 0x0000010e, 0x00060050, 0x0000000c, + 0x00000110, 0x0000010f, 0x0000010f, 0x0000010f, 0x0008000c, 0x0000000c, 0x00000111, 0x00000001, + 0x0000002e, 0x00000107, 0x0000010c, 0x00000110, 0x00060050, 0x0000000c, 0x00000112, 0x00000080, + 0x00000080, 0x00000080, 0x00060050, 0x0000000c, 0x00000113, 0x0000001f, 0x0000001f, 0x0000001f, + 0x0008000c, 0x0000000c, 0x00000114, 0x00000001, 0x0000002b, 0x00000111, 0x00000112, 0x00000113, + 0x0003003e, 0x000000fa, 0x00000114, 0x0004003d, 0x0000000c, 0x00000117, 0x000000fa, 0x0003003e, + 0x00000116, 0x00000117, 0x00050039, 0x00000006, 0x00000118, 0x00000014, 0x00000116, 0x0003003e, + 0x00000115, 0x00000118, 0x00050041, 0x00000007, 0x0000011a, 0x000000fa, 0x00000030, 0x0004003d, + 0x00000006, 0x0000011b, 0x0000011a, 0x00050085, 0x00000006, 0x0000011c, 0x0000011b, 0x000000d8, + 0x00050041, 0x00000007, 0x0000011d, 0x000000fa, 0x00000035, 0x0004003d, 0x00000006, 0x0000011e, + 0x0000011d, 0x00050041, 0x00000007, 0x0000011f, 0x000000fa, 0x0000003a, 0x0004003d, 0x00000006, + 0x00000120, 0x0000011f, 0x00050081, 0x00000006, 0x00000121, 0x0000011e, 0x00000120, 0x00050041, + 0x00000007, 0x00000122, 0x00000064, 0x0000003a, 0x0004003d, 0x00000006, 0x00000123, 0x00000122, + 0x00050085, 0x00000006, 0x00000124, 0x00000121, 0x00000123, 0x00050081, 0x00000006, 0x00000125, + 0x0000011c, 0x00000124, 0x00050083, 0x00000006, 0x00000126, 0x00000125, 0x0000001f, 0x0003003e, + 0x00000119, 0x00000126, 0x0004003d, 0x0000000c, 0x00000127, 0x000000fa, 0x0004003d, 0x00000006, + 0x00000128, 0x00000115, 0x00060050, 0x0000000c, 0x00000129, 0x00000128, 0x00000128, 0x00000128, + 0x00050083, 0x0000000c, 0x0000012a, 0x00000127, 0x00000129, 0x00050041, 0x00000007, 0x0000012b, + 0x0000006f, 0x00000035, 0x0004003d, 0x00000006, 0x0000012c, 0x0000012b, 0x0004003d, 0x00000006, + 0x0000012d, 0x00000119, 0x0004003d, 0x00000006, 0x0000012e, 0x00000119, 0x00050085, 0x00000006, + 0x0000012f, 0x0000012d, 0x0000012e, 0x00050083, 0x00000006, 0x00000130, 0x0000001f, 0x0000012f, + 0x00050085, 0x00000006, 0x00000131, 0x0000012c, 0x00000130, 0x00050041, 0x00000007, 0x00000132, + 0x0000006f, 0x00000030, 0x0004003d, 0x00000006, 0x00000133, 0x00000132, 0x00050081, 0x00000006, + 0x00000134, 0x00000131, 0x00000133, 0x0005008e, 0x0000000c, 0x00000135, 0x0000012a, 0x00000134, + 0x0004003d, 0x00000006, 0x00000136, 0x00000115, 0x00060050, 0x0000000c, 0x00000137, 0x00000136, + 0x00000136, 0x00000136, 0x00050081, 0x0000000c, 0x00000138, 0x00000135, 0x00000137, 0x0003003e, + 0x000000fa, 0x00000138, 0x00060041, 0x0000007d, 0x0000013a, 0x0000007b, 0x0000007c, 0x0000003a, + 0x0004003d, 0x00000006, 0x0000013b, 0x0000013a, 0x0008000c, 0x00000006, 0x0000013c, 0x00000001, + 0x0000002b, 0x0000013b, 0x00000080, 0x0000001f, 0x0003003e, 0x00000139, 0x0000013c, 0x0004003d, + 0x0000000c, 0x0000013e, 0x000000bb, 0x00050094, 0x00000006, 0x00000143, 0x0000013e, 0x00000142, + 0x0003003e, 0x0000013d, 0x00000143, 0x0004003d, 0x0000000c, 0x00000145, 0x000000fa, 0x00050094, + 0x00000006, 0x00000146, 0x00000145, 0x00000142, 0x0003003e, 0x00000144, 0x00000146, 0x0004003d, + 0x00000006, 0x00000147, 0x00000139, 0x000500ba, 0x0000001a, 0x00000148, 0x00000147, 0x00000080, + 0x0004003d, 0x00000006, 0x00000149, 0x00000144, 0x000500ba, 0x0000001a, 0x0000014b, 0x00000149, + 0x0000014a, 0x000500a7, 0x0000001a, 0x0000014c, 0x00000148, 0x0000014b, 0x000300f7, 0x0000014e, + 0x00000000, 0x000400fa, 0x0000014c, 0x0000014d, 0x0000014e, 0x000200f8, 0x0000014d, 0x0004003d, + 0x0000000c, 0x0000014f, 0x000000bb, 0x0004003d, 0x0000000c, 0x00000150, 0x000000fa, 0x0007000c, + 0x0000000c, 0x00000151, 0x00000001, 0x00000028, 0x00000150, 0x000000c5, 0x0004003d, 0x00000006, + 0x00000152, 0x0000013d, 0x0004003d, 0x00000006, 0x00000153, 0x00000144, 0x00050088, 0x00000006, + 0x00000154, 0x00000152, 0x00000153, 0x0005008e, 0x0000000c, 0x00000155, 0x00000151, 0x00000154, + 0x0004003d, 0x00000006, 0x00000156, 0x00000139, 0x0004003d, 0x0000000c, 0x0000015a, 0x000000bb, + 0x0003003e, 0x00000159, 0x0000015a, 0x00050039, 0x00000006, 0x0000015b, 0x00000014, 0x00000159, + 0x0008000c, 0x00000006, 0x0000015c, 0x00000001, 0x00000031, 0x00000157, 0x00000158, 0x0000015b, + 0x00050085, 0x00000006, 0x0000015d, 0x00000156, 0x0000015c, 0x00060050, 0x0000000c, 0x0000015e, + 0x0000015d, 0x0000015d, 0x0000015d, 0x0008000c, 0x0000000c, 0x0000015f, 0x00000001, 0x0000002e, + 0x0000014f, 0x00000155, 0x0000015e, 0x0003003e, 0x000000bb, 0x0000015f, 0x000200f9, 0x0000014e, + 0x000200f8, 0x0000014e, 0x0004003d, 0x0000000c, 0x00000162, 0x000000bb, 0x0003003e, 0x00000161, + 0x00000162, 0x00050039, 0x00000006, 0x00000163, 0x00000014, 0x00000161, 0x0003003e, 0x00000160, + 0x00000163, 0x0004003d, 0x00000006, 0x00000165, 0x00000089, 0x00050085, 0x00000006, 0x00000166, + 0x00000157, 0x00000165, 0x0003003e, 0x00000164, 0x00000166, 0x0004003d, 0x00000006, 0x00000167, + 0x00000160, 0x0004003d, 0x00000006, 0x00000168, 0x00000164, 0x000500ba, 0x0000001a, 0x00000169, + 0x00000167, 0x00000168, 0x000300f7, 0x0000016b, 0x00000000, 0x000400fa, 0x00000169, 0x0000016a, + 0x0000016b, 0x000200f8, 0x0000016a, 0x0004003d, 0x00000006, 0x0000016d, 0x00000089, 0x0004003d, + 0x00000006, 0x0000016e, 0x00000164, 0x00050083, 0x00000006, 0x0000016f, 0x0000016d, 0x0000016e, + 0x0003003e, 0x0000016c, 0x0000016f, 0x0004003d, 0x00000006, 0x00000170, 0x00000164, 0x0004003d, + 0x00000006, 0x00000171, 0x0000016c, 0x0004003d, 0x00000006, 0x00000172, 0x00000160, 0x0004003d, + 0x00000006, 0x00000173, 0x00000164, 0x00050083, 0x00000006, 0x00000174, 0x00000172, 0x00000173, + 0x0004007f, 0x00000006, 0x00000175, 0x00000174, 0x0004003d, 0x00000006, 0x00000176, 0x0000016c, + 0x00050088, 0x00000006, 0x00000177, 0x00000175, 0x00000176, 0x0006000c, 0x00000006, 0x00000178, + 0x00000001, 0x0000001b, 0x00000177, 0x00050083, 0x00000006, 0x00000179, 0x0000001f, 0x00000178, + 0x00050085, 0x00000006, 0x0000017a, 0x00000171, 0x00000179, 0x00050081, 0x00000006, 0x0000017b, + 0x00000170, 0x0000017a, 0x0004003d, 0x00000006, 0x0000017c, 0x00000160, 0x00050088, 0x00000006, + 0x0000017d, 0x0000017b, 0x0000017c, 0x0004003d, 0x0000000c, 0x0000017e, 0x000000bb, 0x0005008e, + 0x0000000c, 0x0000017f, 0x0000017e, 0x0000017d, 0x0003003e, 0x000000bb, 0x0000017f, 0x000200f9, + 0x0000016b, 0x000200f8, 0x0000016b, 0x0004003d, 0x0000000c, 0x00000182, 0x000000bb, 0x0007000c, + 0x0000000c, 0x00000183, 0x00000001, 0x00000028, 0x00000182, 0x000000c5, 0x0004003d, 0x00000006, + 0x00000184, 0x00000076, 0x0005008e, 0x0000000c, 0x00000185, 0x00000183, 0x00000184, 0x00050051, + 0x00000006, 0x00000186, 0x00000185, 0x00000000, 0x00050051, 0x00000006, 0x00000187, 0x00000185, + 0x00000001, 0x00050051, 0x00000006, 0x00000188, 0x00000185, 0x00000002, 0x00070050, 0x00000051, + 0x00000189, 0x00000186, 0x00000187, 0x00000188, 0x00000080, 0x0003003e, 0x00000181, 0x00000189, + 0x000100fd, 0x00010038, 0x00050036, 0x00000006, 0x0000000a, 0x00000000, 0x00000008, 0x00030037, + 0x00000007, 0x00000009, 0x000200f8, 0x0000000b, 0x0004003b, 0x00000007, 0x00000016, 0x00000007, + 0x0004003b, 0x00000007, 0x0000001c, 0x00000007, 0x0003003e, 0x00000016, 0x00000017, 0x0004003d, + 0x00000006, 0x00000018, 0x00000009, 0x000500bc, 0x0000001a, 0x0000001b, 0x00000018, 0x00000019, + 0x000300f7, 0x0000001e, 0x00000000, 0x000400fa, 0x0000001b, 0x0000001d, 0x00000024, 0x000200f8, + 0x0000001d, 0x0004003d, 0x00000006, 0x00000020, 0x00000009, 0x0004007f, 0x00000006, 0x00000021, + 0x00000020, 0x0006000c, 0x00000006, 0x00000022, 0x00000001, 0x0000001b, 0x00000021, 0x00050083, + 0x00000006, 0x00000023, 0x0000001f, 0x00000022, 0x0003003e, 0x0000001c, 0x00000023, 0x000200f9, + 0x0000001e, 0x000200f8, 0x00000024, 0x0004003d, 0x00000006, 0x00000025, 0x00000016, 0x0004003d, + 0x00000006, 0x00000027, 0x00000009, 0x00050083, 0x00000006, 0x00000028, 0x00000027, 0x00000019, + 0x00050085, 0x00000006, 0x00000029, 0x00000026, 0x00000028, 0x00050081, 0x00000006, 0x0000002a, + 0x00000025, 0x00000029, 0x0003003e, 0x0000001c, 0x0000002a, 0x000200f9, 0x0000001e, 0x000200f8, + 0x0000001e, 0x0004003d, 0x00000006, 0x0000002b, 0x0000001c, 0x000200fe, 0x0000002b, 0x00010038, + 0x00050036, 0x0000000c, 0x00000010, 0x00000000, 0x0000000e, 0x00030037, 0x0000000d, 0x0000000f, + 0x000200f8, 0x00000011, 0x0004003b, 0x00000007, 0x0000002e, 0x00000007, 0x0004003b, 0x00000007, + 0x00000034, 0x00000007, 0x0004003b, 0x00000007, 0x00000039, 0x00000007, 0x00050041, 0x00000007, + 0x00000031, 0x0000000f, 0x00000030, 0x0004003d, 0x00000006, 0x00000032, 0x00000031, 0x0003003e, + 0x0000002e, 0x00000032, 0x00050039, 0x00000006, 0x00000033, 0x0000000a, 0x0000002e, 0x00050041, + 0x00000007, 0x00000036, 0x0000000f, 0x00000035, 0x0004003d, 0x00000006, 0x00000037, 0x00000036, + 0x0003003e, 0x00000034, 0x00000037, 0x00050039, 0x00000006, 0x00000038, 0x0000000a, 0x00000034, + 0x00050041, 0x00000007, 0x0000003b, 0x0000000f, 0x0000003a, 0x0004003d, 0x00000006, 0x0000003c, + 0x0000003b, 0x0003003e, 0x00000039, 0x0000003c, 0x00050039, 0x00000006, 0x0000003d, 0x0000000a, + 0x00000039, 0x00060050, 0x0000000c, 0x0000003e, 0x00000033, 0x00000038, 0x0000003d, 0x000200fe, + 0x0000003e, 0x00010038, 0x00050036, 0x00000006, 0x00000014, 0x00000000, 0x00000012, 0x00030037, + 0x0000000d, 0x00000013, 0x000200f8, 0x00000015, 0x00050041, 0x00000007, 0x00000041, 0x00000013, + 0x00000030, 0x0004003d, 0x00000006, 0x00000042, 0x00000041, 0x00050041, 0x00000007, 0x00000043, + 0x00000013, 0x00000035, 0x0004003d, 0x00000006, 0x00000044, 0x00000043, 0x00050041, 0x00000007, + 0x00000045, 0x00000013, 0x0000003a, 0x0004003d, 0x00000006, 0x00000046, 0x00000045, 0x0007000c, + 0x00000006, 0x00000047, 0x00000001, 0x00000028, 0x00000044, 0x00000046, 0x0007000c, 0x00000006, + 0x00000048, 0x00000001, 0x00000028, 0x00000042, 0x00000047, 0x000200fe, 0x00000048, 0x00010038, +}; diff --git a/runtime/hdr_ui_spv.h b/runtime/hdr_ui_spv.h new file mode 100644 index 00000000..1854ac8a --- /dev/null +++ b/runtime/hdr_ui_spv.h @@ -0,0 +1,73 @@ +// Generated by tools/build_shaders.py from runtime/shaders/hdr_ui.frag; do not edit. +// source sha256 59cfbb1ab5393fdb743016cbf75a085ee8a05929c7ea67a69b4ab77f26a14303 +#pragma once +#include +static const uint32_t hdr_ui_spv[] = { + 0x07230203, 0x00010300, 0x000d000b, 0x000000be, 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001, + 0x0007000f, 0x00000004, 0x00000004, 0x6e69616d, 0x00000000, 0x0000004f, 0x00000068, 0x00030010, + 0x00000004, 0x00000007, 0x00040047, 0x0000004b, 0x00000021, 0x00000000, 0x00040047, 0x0000004b, + 0x00000022, 0x00000000, 0x00040047, 0x0000004f, 0x0000001e, 0x00000000, 0x00040047, 0x00000068, + 0x0000001e, 0x00000000, 0x00030047, 0x0000007b, 0x00000002, 0x00050048, 0x0000007b, 0x00000000, + 0x00000023, 0x00000000, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, + 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000003, 0x0004002b, 0x00000006, + 0x00000015, 0x3d6147ae, 0x0004002b, 0x00000006, 0x0000001b, 0x4019999a, 0x0006002c, 0x00000007, + 0x0000001c, 0x0000001b, 0x0000001b, 0x0000001b, 0x0004002b, 0x00000006, 0x0000001f, 0x3d25aee6, + 0x0006002c, 0x00000007, 0x00000020, 0x0000001f, 0x0000001f, 0x0000001f, 0x00020014, 0x00000021, + 0x00040017, 0x00000022, 0x00000021, 0x00000003, 0x0004002b, 0x00000006, 0x0000002c, 0x00000000, + 0x0004002b, 0x00000006, 0x0000002d, 0x3f800000, 0x0004002b, 0x00000006, 0x00000031, 0x3e232000, + 0x0006002c, 0x00000007, 0x00000032, 0x00000031, 0x00000031, 0x00000031, 0x0004002b, 0x00000006, + 0x00000034, 0x3f560000, 0x0004002b, 0x00000006, 0x00000035, 0x4196d000, 0x0004002b, 0x00000006, + 0x0000003a, 0x41958000, 0x0004002b, 0x00000006, 0x00000040, 0x429db000, 0x0006002c, 0x00000007, + 0x00000041, 0x00000040, 0x00000040, 0x00000040, 0x00040017, 0x00000045, 0x00000006, 0x00000004, + 0x00090019, 0x00000048, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, + 0x00000000, 0x0003001b, 0x00000049, 0x00000048, 0x00040020, 0x0000004a, 0x00000000, 0x00000049, + 0x0004003b, 0x0000004a, 0x0000004b, 0x00000000, 0x00040017, 0x0000004d, 0x00000006, 0x00000002, + 0x00040020, 0x0000004e, 0x00000001, 0x0000004d, 0x0004003b, 0x0000004e, 0x0000004f, 0x00000001, + 0x00040015, 0x00000052, 0x00000020, 0x00000000, 0x00040020, 0x00000067, 0x00000003, 0x00000045, + 0x0004003b, 0x00000067, 0x00000068, 0x00000003, 0x00040018, 0x00000069, 0x00000007, 0x00000003, + 0x0004002b, 0x00000006, 0x0000006a, 0x3f209d49, 0x0004002b, 0x00000006, 0x0000006b, 0x3d8d844d, + 0x0004002b, 0x00000006, 0x0000006c, 0x3c86594b, 0x0006002c, 0x00000007, 0x0000006d, 0x0000006a, + 0x0000006b, 0x0000006c, 0x0004002b, 0x00000006, 0x0000006e, 0x3ea89a02, 0x0004002b, 0x00000006, + 0x0000006f, 0x3f6b645a, 0x0004002b, 0x00000006, 0x00000070, 0x3db43958, 0x0006002c, 0x00000007, + 0x00000071, 0x0000006e, 0x0000006f, 0x00000070, 0x0004002b, 0x00000006, 0x00000072, 0x3d315b57, + 0x0004002b, 0x00000006, 0x00000073, 0x3c3ac711, 0x0004002b, 0x00000006, 0x00000074, 0x3f65460b, + 0x0006002c, 0x00000007, 0x00000075, 0x00000072, 0x00000073, 0x00000074, 0x0006002c, 0x00000069, + 0x00000076, 0x0000006d, 0x00000071, 0x00000075, 0x0006002c, 0x00000007, 0x00000079, 0x0000002c, + 0x0000002c, 0x0000002c, 0x0003001e, 0x0000007b, 0x00000045, 0x00040020, 0x0000007c, 0x00000009, + 0x0000007b, 0x0004003b, 0x0000007c, 0x0000007d, 0x00000009, 0x00040015, 0x0000007e, 0x00000020, + 0x00000001, 0x0004002b, 0x0000007e, 0x0000007f, 0x00000000, 0x0004002b, 0x00000052, 0x00000080, + 0x00000000, 0x00040020, 0x00000081, 0x00000009, 0x00000006, 0x0006002c, 0x00000007, 0x000000b2, + 0x0000002d, 0x0000002d, 0x0000002d, 0x0006002c, 0x00000007, 0x000000b4, 0x00000015, 0x00000015, + 0x00000015, 0x0006002c, 0x00000007, 0x000000b7, 0x00000034, 0x00000034, 0x00000034, 0x0004002b, + 0x00000006, 0x000000b8, 0x3d9e8391, 0x0006002c, 0x00000007, 0x000000b9, 0x000000b8, 0x000000b8, + 0x000000b8, 0x0004002b, 0x00000006, 0x000000ba, 0x3f72a76f, 0x0006002c, 0x00000007, 0x000000bb, + 0x000000ba, 0x000000ba, 0x000000ba, 0x0004002b, 0x00000006, 0x000000bc, 0x38d1b717, 0x0006002c, + 0x00000007, 0x000000bd, 0x000000bc, 0x000000bc, 0x000000bc, 0x00050036, 0x00000002, 0x00000004, + 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003d, 0x00000049, 0x0000004c, 0x0000004b, + 0x0004003d, 0x0000004d, 0x00000050, 0x0000004f, 0x00050057, 0x00000045, 0x00000051, 0x0000004c, + 0x00000050, 0x00050051, 0x00000006, 0x00000056, 0x00000051, 0x00000003, 0x000500bc, 0x00000021, + 0x00000057, 0x00000056, 0x0000002c, 0x000300f7, 0x00000059, 0x00000000, 0x000400fa, 0x00000057, + 0x00000058, 0x00000059, 0x000200f8, 0x00000058, 0x000100fc, 0x000200f8, 0x00000059, 0x0008004f, + 0x00000007, 0x0000005d, 0x00000051, 0x00000051, 0x00000000, 0x00000001, 0x00000002, 0x00060050, + 0x00000007, 0x00000060, 0x00000056, 0x00000056, 0x00000056, 0x00050088, 0x00000007, 0x00000061, + 0x0000005d, 0x00000060, 0x0008000c, 0x00000007, 0x00000064, 0x00000001, 0x0000002b, 0x00000061, + 0x00000079, 0x000000b2, 0x00050085, 0x00000007, 0x00000091, 0x00000064, 0x000000b9, 0x00050081, + 0x00000007, 0x00000094, 0x00000064, 0x000000b4, 0x00050085, 0x00000007, 0x00000096, 0x00000094, + 0x000000bb, 0x0007000c, 0x00000007, 0x00000097, 0x00000001, 0x0000001a, 0x00000096, 0x0000001c, + 0x000500ba, 0x00000022, 0x00000099, 0x00000064, 0x00000020, 0x000600a9, 0x00000007, 0x0000009a, + 0x00000099, 0x00000097, 0x00000091, 0x00050091, 0x00000007, 0x00000078, 0x00000076, 0x0000009a, + 0x0007000c, 0x00000007, 0x0000007a, 0x00000001, 0x00000028, 0x00000078, 0x00000079, 0x00060041, + 0x00000081, 0x00000082, 0x0000007d, 0x0000007f, 0x00000080, 0x0004003d, 0x00000006, 0x00000083, + 0x00000082, 0x0005008e, 0x00000007, 0x00000084, 0x0000007a, 0x00000083, 0x00050085, 0x00000007, + 0x000000a0, 0x00000084, 0x000000bd, 0x0008000c, 0x00000007, 0x000000a3, 0x00000001, 0x0000002b, + 0x000000a0, 0x00000079, 0x000000b2, 0x0007000c, 0x00000007, 0x000000a4, 0x00000001, 0x0000001a, + 0x000000a3, 0x00000032, 0x0005008e, 0x00000007, 0x000000a6, 0x000000a4, 0x00000035, 0x00050081, + 0x00000007, 0x000000a8, 0x000000b7, 0x000000a6, 0x0005008e, 0x00000007, 0x000000aa, 0x000000a4, + 0x0000003a, 0x00050081, 0x00000007, 0x000000ac, 0x000000b2, 0x000000aa, 0x00050088, 0x00000007, + 0x000000ad, 0x000000a8, 0x000000ac, 0x0007000c, 0x00000007, 0x000000ae, 0x00000001, 0x0000001a, + 0x000000ad, 0x00000041, 0x00050051, 0x00000006, 0x00000089, 0x000000ae, 0x00000000, 0x00050051, + 0x00000006, 0x0000008a, 0x000000ae, 0x00000001, 0x00050051, 0x00000006, 0x0000008b, 0x000000ae, + 0x00000002, 0x00070050, 0x00000045, 0x0000008c, 0x00000089, 0x0000008a, 0x0000008b, 0x00000056, + 0x0003003e, 0x00000068, 0x0000008c, 0x000100fd, 0x00010038, +}; diff --git a/runtime/host.h b/runtime/host.h index 06526922..f5528f18 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -57,6 +57,9 @@ typedef struct { int have_gpu, have_vram, vram_exact; uint64_t gpu_ns, gpu_parts, gpu_missed; // busy time, submission parts timed, parts left untimed uint64_t vram_used, vram_budget; + int have_idle; // calibrated timestamps: the idle split below is filled + uint64_t idle_ns, starved_ns, queued_ns; // GPU idle between timed parts; of it, before the part was submitted; submit to start + uint64_t idle_gaps, max_gap_ns; // gaps over 0.5 ms; the longest } PerfGpu; extern void (*perf_gpu_stats)(PerfGpu *out); // The performance overlay (perfmon.c): mode 0 off, 1 fps, 2 full. While it is on, perf_present hands every frame to @@ -184,14 +187,16 @@ typedef struct { const char *file, *sha256; } RtFileHash; extern const RtFileHash rt_exefs_sha256[]; // tools/data/expected-.json "exefs_sha256" extern const int rt_exefs_sha256_count; extern const char rt_default_exefs[], rt_default_romfs[], rt_default_profile[]; -extern const int rt_game_version; // the game build: 111 (1.1.1), 121 (1.2.1) or 143 (1.4.3) +extern const int rt_game_version; // the game build: 110 (1.1.0), 121 (1.2.1) or 143 (1.4.3) extern const char rt_port_version[]; // VERSION at the build's root: 0.1.0, 0.1.0+973c626 in a release -#define RT_V111 (rt_game_version == 111) // 1.1.1: its own boot constants and data slots +#define RT_V110 (rt_game_version == 110) // 1.1.0: its own boot constants and data slots #define RT_V143 (rt_game_version >= 143) // ---- kernel (kernel.c) typedef struct RtThread RtThread; Cpu *kern_cpu(void); // current guest thread's Cpu, NULL on host threads +void kern_free_cores_note(void); // for crash reports: logs that free_cores = 1 lifted the + // virtual core gate, nothing when the gate is on uint32_t kern_main_thread(Cpu **cpu); // main thread object and Cpu for the calling host thread // (main.npdm: 1 MiB stack at the top of the stack region, // priority 44, core 0); on the main thread: its handle @@ -242,6 +247,13 @@ void account_uid(uint64_t out[2]); // the local user's n // ---- movies (ov_movie.c) void movie_tick(Cpu *c); // at every present, on the presenting guest thread: hands // decoded movie frames over when due, signals their events +// ---- camera speed (camera_speed.c): TOTK_STICK_SENS +float cam_speed_wanted(void); // the factor TOTK_STICK_SENS asks for, 0.1 to 3 +float cam_speed_applied(void); // the factor in effect: 1 until romfs.c serves the pack +int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, void **out, size_t *out_n); + // Pack/Actor/PlayerCamera.pack.zs with the camera speeds + // multiplied, malloc'd; 0 = factor 1 or cannot (logged) +void cam_pack_served(void); // romfs.c serves the multiplied pack // ---- game resolution (resolution.c): TOTK_OUTPUT_RES, TOTK_RENDER_SCALE uint64_t res_heap_extra_bytes(void); // added to the application pool (mem_init), 2 MiB steps uint64_t res_heap_extra_kib(void); // added to the HeapDef texture heap and its parent; 0 = none @@ -249,6 +261,10 @@ int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n); // // the grown heaps, malloc'd; 0 = cannot (logged) void res_heapdef_served(void); // romfs.c serves the grown table void res_init(void); // after rt_patches_init: the patch page and the sets +float res_dlss_lod_bias(void); // TOTK_DLSS upscaling: texture LOD bias (<= 0) +int res_dlss_mode(const char *mode); // TOTK_DLSS changed while running: new scene size +void res_fn_hit(uint64_t addr, uint64_t x0); // main.c fnprof_hit: catches the renderer at start +void res_present(Cpu *c); // nvnQueuePresentTexture: applies a pending scene size void res_sizes(unsigned out[2], unsigned scene[2]); // the output and scene sizes in effect (the game's own // until res_init switched the sets on) int res_is_16x9(unsigned w, unsigned h); // within 1% of 16:9 (the test res_init and romfs.c share) @@ -295,6 +311,9 @@ void rt_boot(void); // boot.c // process exit code. int probe_cfg(void); // cfg_probe.c int probe_dpi(void); // dpi_probe_win.c (Windows only) +int probe_mods(void); // mods_probe.c +void discord_init(void); // discord_rpc.c: the Rich Presence thread, game runs only +int probe_discord(void); // discord_rpc.c int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void), probe_fs(void); int probe_gfx_abi(void); // gfx_abi_probe.c int probe_vkdevice(void); // vk_device.c diff --git a/runtime/hostfn.c b/runtime/hostfn.c index e57a7d41..27aa0c15 100644 --- a/runtime/hostfn.c +++ b/runtime/hostfn.c @@ -121,7 +121,7 @@ __attribute__((noinline, noreturn, cold)) static void bad_entry(Cpu *c) { } // kern_host_track: the call counted in the thread's host depth. Not a tail call, so the host stack holds this frame -// while fn runs; longjmp out of fn (ExitThread) leaves the depth raised on a thread that ends. +// while fn runs; the jump out of fn (ExitThread) leaves the depth raised on a thread that ends. __attribute__((noinline)) static void hostfn_tracked(Cpu *c, GuestFn fn) { void *t = c->thread; int *d = kern_host_depth(t); diff --git a/runtime/input.c b/runtime/input.c index 86391739..e51a1e54 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -236,7 +236,56 @@ static float mouse_sens_get(void) { } -// The mouse camera as a right-stick sample, held for the whole presented frame. Under `lock`. +// ---- walk key: the keyboard's stick samples scaled down while a key is held (TOTK_WALK_KEY, TOTK_WALK_SCALE) +// A keyboard key is all or nothing, so every keyboard-driven stick sample is +/-32767 and the game only ever sees a +// full tilt. Holding this key scales those two samples, which gives the half tilt a gamepad gets from its analog range, +// so the keyboard can walk as well as run. yuzu and eden do the same with their modifier and modifier_scale settings. +// Only the keyboard is scaled: the gamepad's axes and the mouse camera sample their own values. +static int walk_key = KEY_NONE; // TOTK_WALK_KEY as a KEY_*, KEY_NONE = off; live (cfg_version) +static float walk_scale = 0.51f; // TOTK_WALK_SCALE, 0.2 to 0.9; live (cfg_version) +static unsigned walk_seen = ~0u; + +// The ini takes the key names the Controls tab writes; a hand-written line may spell them in any case. +static int name_matches(const char *name, const char *key) { + for (; *name && *key; name++, key++) { + unsigned char a = (unsigned char)*name, b = (unsigned char)*key; + if (a >= 'A' && a <= 'Z') a = (unsigned char)(a - 'A' + 'a'); + if (b >= 'A' && b <= 'Z') b = (unsigned char)(b - 'A' + 'a'); + if (a != b) return 0; + } + return *name == *key; +} + +static void walk_get(void) { + unsigned ver = cfg_version(); + if (ver == walk_seen) return; + walk_seen = ver; + const char *name = cfg_value("TOTK_WALK_KEY"); // "none" (the default) turns the key off + int key = KEY_NONE; + if (*name && !name_matches(name, "none")) { + for (int k = KEY_A; k < KEY_COUNT && key == KEY_NONE; k++) + if (input_key_name(k) && name_matches(name, input_key_name(k))) key = k; + if (key == KEY_NONE) rt_log("input: walk_key: unknown key '%s', the keyboard sticks keep their full tilt", name); + } + // 0.51 rather than 0.5: a keyboard sample is 32767, and 32767 * 0.5 = 16383 falls one short of stick_bits' + // STICK_DIR (16384), which would leave the game's stick-direction bits clear while the walk key is held. The value + // in the settings table carries that default; this code only rejects what the table's range does not allow. + const char *sv = cfg_value("TOTK_WALK_SCALE"); + char *end; + double d = strtod(sv, &end); + double s = end == sv || *end || !(d >= 0.2 && d <= 0.9) ? strtod(cfg_meta("TOTK_WALK_SCALE")->def, NULL) : d; + if (key != walk_key || (float)s != walk_scale) { + walk_key = key; + walk_scale = (float)s; + rt_log("input: walk key %s, keyboard sticks at %.0f%% of their tilt (mouse and gamepad unchanged)", + key != KEY_NONE ? input_key_name(key) : "off", s * 100); + } +} + +// The mouse camera as a right-stick sample, held for the whole presented frame. Under `lock`. The sample is divided by +// the camera speed factor the game runs with (camera_speed.c, cam_speed_applied), which multiplies every right-stick +// turn rate, so TOTK_MOUSE_SENS alone sets the mouse's speed; below 1 the sample stays within the stick's range, so the +// mouse's top speed drops with the factor. static void mouse_camera(int32_t out[2]) { static uint64_t frame = ~0ull, t0; static int32_t held[2]; @@ -259,20 +308,23 @@ static void mouse_camera(int32_t out[2]) { held[0] = held[1] = 0; // frames stopped: do not hold a deflection } } - out[0] = held[0]; - out[1] = held[1]; + double k = cam_speed_applied(), x = held[0] / k, y = held[1] / k, len = sqrt(x * x + y * y); + if (len > STICK_MAX) x *= STICK_MAX / len, y *= STICK_MAX / len; + out[0] = (int32_t)lround(x); + out[1] = (int32_t)lround(y); } -static float stick_dead = -1; // TOTK_STICK_DEADZONE as a fraction of full deflection; live (cfg_version) +static float stick_dead[2] = {-1, -1}; // input_pad_deadzone of the left and right stick; live (cfg_version) static unsigned stick_dead_seen; -static float stick_dead_get(void) { +static float stick_dead_get(int s) { unsigned ver = cfg_version(); - if (stick_dead >= 0 && ver == stick_dead_seen) return stick_dead; - stick_dead_seen = ver; - double v = cfg_double("TOTK_STICK_DEADZONE"); - stick_dead = (float)(v < 0 ? 0 : v > 50 ? 50 : v) / 100.f; - return stick_dead; + if (stick_dead[0] < 0 || ver != stick_dead_seen) { + stick_dead_seen = ver; + stick_dead[0] = input_pad_deadzone(0); + stick_dead[1] = input_pad_deadzone(1); + } + return stick_dead[s]; } // One gamepad stick through the dead zone; SDL's Y grows downwards, the Switch's upwards. @@ -425,12 +477,26 @@ static int pad_read(uint64_t *b, int32_t sl[2], int32_t sr[2]) { uint8_t pd[PAD_COUNT] = {0}; if (!pad_physical(pd)) return 0; *b |= input_map_pad_buttons(pd, pad_layout_get()); - float dead = stick_dead_get(); - stick(SDL_GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTY, dead, sl); - stick(SDL_GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTY, dead, sr); + stick(SDL_GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTY, stick_dead_get(0), sl); + stick(SDL_GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTY, stick_dead_get(1), sr); return 1; } +int input_pad_stick_percent(double pct[2]) { + int ok = 0; + pct[0] = pct[1] = 0; + plat_mutex_lock(&lock); + if (state == 1 && pad && SDL_GamepadConnected(pad)) { // the game's poll keeps the state fresh (SDL_UpdateGamepads) + pct[0] = input_stick_percent(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX), + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY)); + pct[1] = input_stick_percent(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX), + SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTY)); + ok = 1; + } + plat_mutex_unlock(&lock); + return ok; +} + static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { uint64_t b = 0; int32_t sl[2] = {0, 0}, sr[2] = {0, 0}; @@ -456,9 +522,13 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx was_captured = cap; b |= input_map_buttons(down); int32_t k[2]; + walk_get(); + int walk = walk_key != KEY_NONE && down[walk_key]; input_map_stick(down, 0, k); + if (walk) k[0] = (int32_t)(k[0] * walk_scale), k[1] = (int32_t)(k[1] * walk_scale); pick(&sl[0], &sl[1], k[0], k[1]); input_map_stick(down, 1, k); + if (walk) k[0] = (int32_t)(k[0] * walk_scale), k[1] = (int32_t)(k[1] * walk_scale); pick(&sr[0], &sr[1], k[0], k[1]); mouse_camera(k); pick(&sr[0], &sr[1], k[0], k[1]); @@ -486,6 +556,12 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx // g_block: 0 off, 1 text entry open, 2 closed, waiting for the release. The cursor is released while it is open. static volatile int32_t g_block; static volatile int32_t g_menu; // the in-game menu, the same way (input_menu) +// When g_block and g_menu last went to 2 (plat_now_ns). The wait for the release ends after RELEASE_WAIT_NS even with +// something held: an input that never reads as released (a button another program holds down, a key whose release +// went to another window, a stick that rests outside its dead zone) would otherwise keep the game deaf until restart +// (#462, #494). A press that closed the menu is let go well within the wait; one still held after it is meant. +static volatile uint64_t g_block_closed, g_menu_closed; +#define RELEASE_WAIT_NS 1000000000ull int input_cursor_allowed(void) { const char *in = cfg_get("TOTK_INPUT"); @@ -493,6 +569,7 @@ int input_cursor_allowed(void) { } void input_block(int on) { + if (!on) __atomic_store_n(&g_block_closed, plat_now_ns(), __ATOMIC_RELEASE); plat_xchg32(&g_block, on ? 1 : 2); ikb_cursor_update(); } @@ -500,20 +577,35 @@ void input_block(int on) { // The in-game menu (ui/menu.cpp) blocks the game's input the same way: g_menu 0 closed, 1 open, 2 closed, waiting for // the release. The cursor is released while it is open, for the menu's mouse. void input_menu(int open) { + if (!open) __atomic_store_n(&g_menu_closed, plat_now_ns(), __ATOMIC_RELEASE); plat_xchg32(&g_menu, open ? 1 : 2); if (!open) ikb_menu_closed(); // the capture before Esc comes back ikb_cursor_update(); } -// 1 while flag (g_block, g_menu) blocks the game's input; state 2 ends at the first poll with nothing held. -static int blocked(volatile int32_t *flag, uint64_t buttons) { +// 1 while flag (g_block, g_menu) blocks the game's input; state 2 ends at the first poll with nothing held, or with +// something still held RELEASE_WAIT_NS after closed. A stick axis counts as held as well: with the walk key the +// keyboard's sticks move part way without setting a direction bit, so a gate that watched the buttons alone would let +// that movement through the moment the menu or the text entry closes. +static int blocked(volatile int32_t *flag, volatile uint64_t *closed, const char *what, uint64_t buttons, + const int32_t *axes) { int32_t k = *flag; - return k && !(k == 2 && !buttons && plat_cas32(flag, 0, 2) == 2); + if (k != 2) return k != 0; + int held = buttons != 0; + for (int i = 0; i < 4; i++) held |= axes[i] != 0; + if (held && plat_now_ns() - __atomic_load_n(closed, __ATOMIC_ACQUIRE) < RELEASE_WAIT_NS) return 1; + if (plat_cas32(flag, 0, 2) != 2) return *flag != 0; // reopened meanwhile + if (held) + rt_log("input: the %s closed 1 s ago and input is still held (buttons %#llx, sticks %d %d %d %d); the game " + "gets it from now on", what, (unsigned long long)buttons, axes[0], axes[1], axes[2], axes[3]); + return 0; } void input_poll(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { poll_merged(buttons, lx, ly, rx, ry); - int text = blocked(&g_block, *buttons), menu = blocked(&g_menu, *buttons); + const int32_t axes[4] = {*lx, *ly, *rx, *ry}; + int text = blocked(&g_block, &g_block_closed, "text entry", *buttons, axes), + menu = blocked(&g_menu, &g_menu_closed, "menu", *buttons, axes); if (!text && !menu) return; *buttons = 0; *lx = *ly = *rx = *ry = 0; } diff --git a/runtime/input.h b/runtime/input.h index 8fe24377..36ca52a0 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -21,6 +21,10 @@ int input_overlay_gate(int *held, uint64_t *buttons); // L3+R3: 1 in the poll int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly); // The gamepad layout in effect (PAD_LAYOUT_*, input_map.h; TOTK_PAD_LAYOUT, live); -1 without a gamepad. Any thread. int input_pad_layout(void); +// How far the open gamepad's left (pct[0]) and right (pct[1]) sticks are from the centre now, in percent of full +// deflection, before the dead zone: the menu shows it on the dead zone rows, so a drifting stick's resting value can be +// read off. 0 (and both 0) without a connected gamepad. Any thread. +int input_pad_stick_percent(double pct[2]); // The label on a face button of the open gamepad (input_map.h PAD_SOUTH..PAD_NORTH, as SDL reports it); PAD_LABEL_NONE for another // button, an unknown label or no gamepad. Any thread. enum { PAD_LABEL_NONE, PAD_LABEL_A, PAD_LABEL_B, PAD_LABEL_X, PAD_LABEL_Y, PAD_LABEL_CROSS, PAD_LABEL_CIRCLE, diff --git a/runtime/input_map.c b/runtime/input_map.c index 49220eac..5ed1e005 100644 --- a/runtime/input_map.c +++ b/runtime/input_map.c @@ -288,6 +288,18 @@ int input_pad_layout_pick(const char *setting, int east_labelled_a) { return east_labelled_a ? PAD_LAYOUT_NINTENDO : PAD_LAYOUT_XBOX; } +int input_prompts_pick(const char *prompts, const char *pad_layout) { + if (prompts && *prompts) return !strcasecmp(prompts, "xbox") ? PROMPTS_XBOX : PROMPTS_NINTENDO; + return pad_layout && !strcasecmp(pad_layout, "xbox_prompts") ? PROMPTS_XBOX : PROMPTS_NINTENDO; +} + +const char *input_prompts_folder(int prompts, const char *pad_layout) { + if (prompts != PROMPTS_XBOX) return NULL; + // The folders are read at start, before a pad is open: auto counts as Xbox, as for a pad without Nintendo labels. + // A Nintendo-labelled pad under auto plays by label too (its A is on the button labelled A), so it matches as well. + return input_pad_layout_pick(pad_layout, 0) == PAD_LAYOUT_NINTENDO ? "romfs-patches-xbox" : "romfs-patches-xbox-label"; +} + uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT], int layout) { const PadBinding *t = input_map.pads[layout]; uint64_t b = 0; @@ -334,6 +346,16 @@ void input_pad_stick(int32_t x, int32_t y, float dead, int32_t out[2]) { out[1] = (int32_t)lrint(fmax(-1, fmin(1, fy * k)) * STICK_MAX); } +float input_pad_deadzone(int stick) { + const char *own = stick ? "TOTK_RSTICK_DEADZONE" : "TOTK_LSTICK_DEADZONE", *set = cfg_get(own); + double v = cfg_double(set && *set ? own : "TOTK_STICK_DEADZONE"); + return (float)(v < 0 ? 0 : v > 50 ? 50 : v) / 100.f; +} + +double input_stick_percent(int32_t x, int32_t y) { + return sqrt((double)x * x + (double)y * y) * 100.0 / STICK_MAX; +} + // Minus and Plus held together open or close the menu. *held: latched from the poll where both go down until neither is // down (the caller keeps it). Every poll of the latch reports neither to the game, so releasing one button first does not // leak the other; Minus alone before Plus reaches the game unchanged. With the menu open the game sees a neutral pad. diff --git a/runtime/input_map.h b/runtime/input_map.h index 6e66b4ab..c400320f 100644 --- a/runtime/input_map.h +++ b/runtime/input_map.h @@ -80,11 +80,23 @@ int input_map_bind(const char *button, const char *list); // Applies the TOTK_BIND_ settings (cfg_get) over the defaults: at start, and again when one of them changes. void input_map_configure(void); -// The layout for a TOTK_PAD_LAYOUT value: "nintendo" or "xbox" (any case); "xbox_prompts" is the Nintendo layout with -// the A/B and X/Y prompts swapped (runtime/romfs.c overlay_init), so the buttons map as "nintendo". Anything else -// ("auto", NULL) picks Nintendo when the pad's east button is labelled A (a Switch Pro Controller, a GuliKit) and Xbox -// otherwise. +// The layout for a TOTK_PAD_LAYOUT value: "nintendo" or "xbox" (any case); "xbox_prompts", 0.1.9's name for the Nintendo +// layout with the Xbox prompts, maps the buttons as "nintendo". Anything else ("auto", NULL) picks Nintendo when the +// pad's east button is labelled A (a Switch Pro Controller, a GuliKit) and Xbox otherwise. int input_pad_layout_pick(const char *setting, int east_labelled_a); + +// Button prompts (TOTK_PROMPTS): the controller whose buttons the game's prompts show. Nintendo: the game's own. Xbox: +// LB RB LT RT, Menu, View and a d-pad cross for the other buttons, and the face buttons named by the label of the +// button the pad layout presses (runtime/romfs.c overlay_init, tools/make_romfs_patches.py --prompts). +enum { PROMPTS_NINTENDO, PROMPTS_XBOX }; +// The prompts for a TOTK_PROMPTS value ("nintendo" or "xbox", any case). Unset or empty: Xbox while the pad layout +// setting is "xbox_prompts", else Nintendo. +int input_prompts_pick(const char *prompts, const char *pad_layout); +// The folder next to romfs-index.bin that holds those prompts for a TOTK_PAD_LAYOUT value, NULL for the game's own. The +// Nintendo layout plays an Xbox pad by position, so its folder names the face buttons the other way round +// (romfs-patches-xbox: the right button does A and is labelled B); xbox and auto put every Switch button on the button +// labelled with its letter (auto picks the layout per pad by its labels), so they share romfs-patches-xbox-label. +const char *input_prompts_folder(int prompts, const char *pad_layout); // down[pad] != 0 for the gamepad inputs held. Returns the Switch button bits (0..15) the layout's pad table presses. uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT], int layout); @@ -100,6 +112,12 @@ void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t // 0..0.5): inside the circle the stick reads 0, and the rest of the travel is scaled to start at 0 at the edge, so a // drifting stick rests at 0 and full deflection still reaches full. The result stays inside the unit square. void input_pad_stick(int32_t x, int32_t y, float dead, int32_t out[2]); +// The dead zone of gamepad stick 0 (left) or 1 (right) for input_pad_stick, as a fraction of full deflection: +// TOTK_LSTICK_DEADZONE or TOTK_RSTICK_DEADZONE when set, else TOTK_STICK_DEADZONE (both sticks), percent clamped to 0..50. +float input_pad_deadzone(int stick); +// How far a stick (x, y in -32768..32767) is from the centre, in percent of full deflection (100 at an axis's end; a +// square gate's corner reads more). +double input_stick_percent(int32_t x, int32_t y); // ---- the remapping rows' edits, on a copy of input_map. Pure. // action's keys (table order) / its gamepad buttons in layout, at most max; returns how many. diff --git a/runtime/input_sdl.h b/runtime/input_sdl.h index bfbb44b8..ec26db86 100644 --- a/runtime/input_sdl.h +++ b/runtime/input_sdl.h @@ -11,6 +11,7 @@ extern "C" { // gfx_window_sdl.c SDL_Window *gfx_window_sdl(void); // the game window, NULL before gfx_window_start; any thread double gfx_window_sdl_density(void); // pixels per window unit (mouse positions are in units); any thread +void gfx_window_sdl_run(void (*fn)(void *arg), void *arg); // runs fn on the window thread and waits; any thread // input_sdl.c void input_sdl_window(SDL_Window *w); // the game window was created diff --git a/runtime/ipc.c b/runtime/ipc.c index a78ae7ac..d47a3620 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -11,6 +11,7 @@ #include "audren.h" #include "host.h" #include "ipc.h" +#include "nfp.h" #include "platform/platform.h" #define CMIF_IN_MAGIC 0x49434653u // "SFCI" @@ -25,6 +26,7 @@ enum { T_CLOSE = 2, T_REQUEST = 4, T_CONTROL = 5, T_REQUEST_CTX = 6, T_CONTROL_C // Services sm: hands out. Anything else is a named fatal. static const struct { const char *name; IpcHandler handler; } services[] = { {"audren:u", audren_manager_handler}, + {"nfp:user", nfp_user_manager_handler}, }; static volatile int32_t g_live; diff --git a/runtime/kernel.c b/runtime/kernel.c index cd75797b..e8fb48b4 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -8,10 +8,6 @@ // Cpu. The SDK's nn::os runs as lifted code; only the kernel under it is the host. Every wait hangs on one global lock // G with one condition variable per thread; wake order is the kernel's: lower priority value first, then first come. // Semantics follow the public Horizon kernel (KConditionVariable, KAddressArbiter, KSynchronization). -#ifdef _WIN32 -#define __USE_MINGW_SETJMP_NON_SEH // setjmp(b) = _setjmp(b, NULL): longjmp restores registers, no SEH unwinding -#endif -#include #include #include #include @@ -77,7 +73,7 @@ struct RtThread { uint64_t cseq; RtThread *cnext; uint8_t *tlr, *tls; - jmp_buf exit_jmp; // the thread start; ExitThread longjmps to it + PlatJmp exit_jmp; // the thread start; ExitThread jumps back to it // fibers PlatFiber *base_fiber; // the host thread as a host fiber, after the first switch Fiber *fiber; // current guest fiber, NULL on the thread's own context @@ -296,7 +292,13 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) { // and gives it up only where Horizon would switch threads (waits, sleep, yield, exit, blocking host calls); a free // core goes to the best waiting thread. A waiter better than a holder asks one holder to give its core up (core_kick), // which it does at its next SVC or indirect call (kern_checkpoint, rt_dispatch), as Horizon runs the better thread at -// once; guest code without either keeps the core until it reaches one. TOTK_FREE_CORES=1 turns the gate off. +// once; guest code without either keeps the core until it reaches one. +// +// free_cores = 1 (TOTK_FREE_CORES) turns the gate off, for experiments only: the game does not run that way. A thread +// that holds no core reports its ideal core to GetCurrentProcessorNumber, so the game's job workers all report core 0 +// and run its per-core code at once: on 1.2.1 two of them add the same per-core NVN control memory, which retires the +// command handle the other just recorded, and CallCommands finds no recording at start (#212); 1.4.3 corrupts a heap +// in main+0x82e00 or hangs at the title menu. The start log and every crash report say so (kern_free_cores_note). static RtThread *g_core_owner[NCORE], *g_core_wait; static int g_cores_on = -1, g_held_max; @@ -307,14 +309,21 @@ static int cores_enabled(void) { if (g_cores_on < 0) { g_cores_on = !cfg_bool("TOTK_FREE_CORES"); // "0" keeps the gate, like unset if (!g_cores_on) - rt_log("kernel: virtual core gate off (TOTK_FREE_CORES): guest threads run without the limit of one per " - "core that the game's per-core data needs; for experiments only, the game can fail"); + rt_log("kernel: WARNING: free_cores = 1 (TOTK_FREE_CORES) turns the virtual core gate off. This is an " + "experimental mode and the game crashes or hangs in it, usually at start. Set free_cores = 0 in " + "totk.ini to play"); diag_init(); ap_init(); } return g_cores_on; } +void kern_free_cores_note(void) { + if (!g_cores_on) + rt_log("fatal: free_cores = 1 was on: the game crashes in this experimental mode; set free_cores = 0 in " + "totk.ini before reporting this crash"); +} + static int core_allowed(const RtThread *t, int c) { return (int)((t->mask >> c) & 1); } // ---- asynchronous preemption (TOTK_ASYNC_PREEMPT, on unless 0) @@ -406,6 +415,18 @@ static int ap_disabled(const RtThread *t) { return g_ap > 0 && ap_tlr(t)[0]; } // in kern_ap_yield and jumps back to kern_ap_rip(), whose value it first stores in the return slot it reserved at // entry, so a stack walk sees the interrupted function as its caller. No ret: the jump keeps call/return pairs // balanced. The waiter never writes the owner's stack. +// An x86-64-v2 build (no __AVX__) saves XMM0-15 with SSE moves into the same 32-byte slots instead: its game code +// keeps nothing in the upper YMM halves, and the CPUs it is for have no VEX encoding, so a vmovdqa there faults with +// an illegal instruction the first time a core is interrupted. +#ifdef __AVX__ +#define AP_VMOV "vmovdqa" +#define AP_V "%ymm" +#define AP_VZEROUPPER " vzeroupper\n" +#else +#define AP_VMOV "movdqa" +#define AP_V "%xmm" +#define AP_VZEROUPPER "" +#endif #ifdef _WIN32 __asm__(".text\n" ".globl kern_ap_stub\n" @@ -437,48 +458,48 @@ __asm__(".text\n" ".seh_setframe %rbp, 0\n" ".seh_endprologue\n" " andq $-32, %rsp\n" - " subq $576, %rsp\n" // 0: shadow space, 32: MXCSR saved, 36: MXCSR default, 64: YMM0-15 - " vmovdqa %ymm0, 64(%rsp)\n" - " vmovdqa %ymm1, 96(%rsp)\n" - " vmovdqa %ymm2, 128(%rsp)\n" - " vmovdqa %ymm3, 160(%rsp)\n" - " vmovdqa %ymm4, 192(%rsp)\n" - " vmovdqa %ymm5, 224(%rsp)\n" - " vmovdqa %ymm6, 256(%rsp)\n" - " vmovdqa %ymm7, 288(%rsp)\n" - " vmovdqa %ymm8, 320(%rsp)\n" - " vmovdqa %ymm9, 352(%rsp)\n" - " vmovdqa %ymm10, 384(%rsp)\n" - " vmovdqa %ymm11, 416(%rsp)\n" - " vmovdqa %ymm12, 448(%rsp)\n" - " vmovdqa %ymm13, 480(%rsp)\n" - " vmovdqa %ymm14, 512(%rsp)\n" - " vmovdqa %ymm15, 544(%rsp)\n" + " subq $576, %rsp\n" // 0: shadow space, 32: MXCSR saved, 36: MXCSR default, 64: YMM0-15 (XMM0-15 without AVX) + " " AP_VMOV " " AP_V "0, 64(%rsp)\n" + " " AP_VMOV " " AP_V "1, 96(%rsp)\n" + " " AP_VMOV " " AP_V "2, 128(%rsp)\n" + " " AP_VMOV " " AP_V "3, 160(%rsp)\n" + " " AP_VMOV " " AP_V "4, 192(%rsp)\n" + " " AP_VMOV " " AP_V "5, 224(%rsp)\n" + " " AP_VMOV " " AP_V "6, 256(%rsp)\n" + " " AP_VMOV " " AP_V "7, 288(%rsp)\n" + " " AP_VMOV " " AP_V "8, 320(%rsp)\n" + " " AP_VMOV " " AP_V "9, 352(%rsp)\n" + " " AP_VMOV " " AP_V "10, 384(%rsp)\n" + " " AP_VMOV " " AP_V "11, 416(%rsp)\n" + " " AP_VMOV " " AP_V "12, 448(%rsp)\n" + " " AP_VMOV " " AP_V "13, 480(%rsp)\n" + " " AP_VMOV " " AP_V "14, 512(%rsp)\n" + " " AP_VMOV " " AP_V "15, 544(%rsp)\n" " stmxcsr 32(%rsp)\n" " movl $0x1f80, 36(%rsp)\n" " ldmxcsr 36(%rsp)\n" " cld\n" - " vzeroupper\n" + AP_VZEROUPPER " call kern_ap_rip\n" " movq %rax, 72(%rbp)\n" // the return slot: rbp + 8 pushes + pushfq " call kern_ap_yield\n" " ldmxcsr 32(%rsp)\n" - " vmovdqa 64(%rsp), %ymm0\n" - " vmovdqa 96(%rsp), %ymm1\n" - " vmovdqa 128(%rsp), %ymm2\n" - " vmovdqa 160(%rsp), %ymm3\n" - " vmovdqa 192(%rsp), %ymm4\n" - " vmovdqa 224(%rsp), %ymm5\n" - " vmovdqa 256(%rsp), %ymm6\n" - " vmovdqa 288(%rsp), %ymm7\n" - " vmovdqa 320(%rsp), %ymm8\n" - " vmovdqa 352(%rsp), %ymm9\n" - " vmovdqa 384(%rsp), %ymm10\n" - " vmovdqa 416(%rsp), %ymm11\n" - " vmovdqa 448(%rsp), %ymm12\n" - " vmovdqa 480(%rsp), %ymm13\n" - " vmovdqa 512(%rsp), %ymm14\n" - " vmovdqa 544(%rsp), %ymm15\n" + " " AP_VMOV " 64(%rsp), " AP_V "0\n" + " " AP_VMOV " 96(%rsp), " AP_V "1\n" + " " AP_VMOV " 128(%rsp), " AP_V "2\n" + " " AP_VMOV " 160(%rsp), " AP_V "3\n" + " " AP_VMOV " 192(%rsp), " AP_V "4\n" + " " AP_VMOV " 224(%rsp), " AP_V "5\n" + " " AP_VMOV " 256(%rsp), " AP_V "6\n" + " " AP_VMOV " 288(%rsp), " AP_V "7\n" + " " AP_VMOV " 320(%rsp), " AP_V "8\n" + " " AP_VMOV " 352(%rsp), " AP_V "9\n" + " " AP_VMOV " 384(%rsp), " AP_V "10\n" + " " AP_VMOV " 416(%rsp), " AP_V "11\n" + " " AP_VMOV " 448(%rsp), " AP_V "12\n" + " " AP_VMOV " 480(%rsp), " AP_V "13\n" + " " AP_VMOV " 512(%rsp), " AP_V "14\n" + " " AP_VMOV " 544(%rsp), " AP_V "15\n" " movq %rbp, %rsp\n" " popq %rbp\n" " popq %r11\n" @@ -516,48 +537,48 @@ __asm__(".text\n" " pushq %rbp\n" " movq %rsp, %rbp\n" " andq $-32, %rsp\n" - " subq $576, %rsp\n" // 32: MXCSR saved, 36: MXCSR default, 64: YMM0-15 - " vmovdqa %ymm0, 64(%rsp)\n" - " vmovdqa %ymm1, 96(%rsp)\n" - " vmovdqa %ymm2, 128(%rsp)\n" - " vmovdqa %ymm3, 160(%rsp)\n" - " vmovdqa %ymm4, 192(%rsp)\n" - " vmovdqa %ymm5, 224(%rsp)\n" - " vmovdqa %ymm6, 256(%rsp)\n" - " vmovdqa %ymm7, 288(%rsp)\n" - " vmovdqa %ymm8, 320(%rsp)\n" - " vmovdqa %ymm9, 352(%rsp)\n" - " vmovdqa %ymm10, 384(%rsp)\n" - " vmovdqa %ymm11, 416(%rsp)\n" - " vmovdqa %ymm12, 448(%rsp)\n" - " vmovdqa %ymm13, 480(%rsp)\n" - " vmovdqa %ymm14, 512(%rsp)\n" - " vmovdqa %ymm15, 544(%rsp)\n" + " subq $576, %rsp\n" // 32: MXCSR saved, 36: MXCSR default, 64: YMM0-15 (XMM0-15 without AVX) + " " AP_VMOV " " AP_V "0, 64(%rsp)\n" + " " AP_VMOV " " AP_V "1, 96(%rsp)\n" + " " AP_VMOV " " AP_V "2, 128(%rsp)\n" + " " AP_VMOV " " AP_V "3, 160(%rsp)\n" + " " AP_VMOV " " AP_V "4, 192(%rsp)\n" + " " AP_VMOV " " AP_V "5, 224(%rsp)\n" + " " AP_VMOV " " AP_V "6, 256(%rsp)\n" + " " AP_VMOV " " AP_V "7, 288(%rsp)\n" + " " AP_VMOV " " AP_V "8, 320(%rsp)\n" + " " AP_VMOV " " AP_V "9, 352(%rsp)\n" + " " AP_VMOV " " AP_V "10, 384(%rsp)\n" + " " AP_VMOV " " AP_V "11, 416(%rsp)\n" + " " AP_VMOV " " AP_V "12, 448(%rsp)\n" + " " AP_VMOV " " AP_V "13, 480(%rsp)\n" + " " AP_VMOV " " AP_V "14, 512(%rsp)\n" + " " AP_VMOV " " AP_V "15, 544(%rsp)\n" " stmxcsr 32(%rsp)\n" " movl $0x1f80, 36(%rsp)\n" " ldmxcsr 36(%rsp)\n" " cld\n" - " vzeroupper\n" + AP_VZEROUPPER " call kern_ap_rip\n" " movq %rax, 88(%rbp)\n" // the return slot: rbp + 10 pushes + pushfq " call kern_ap_yield\n" " ldmxcsr 32(%rsp)\n" - " vmovdqa 64(%rsp), %ymm0\n" - " vmovdqa 96(%rsp), %ymm1\n" - " vmovdqa 128(%rsp), %ymm2\n" - " vmovdqa 160(%rsp), %ymm3\n" - " vmovdqa 192(%rsp), %ymm4\n" - " vmovdqa 224(%rsp), %ymm5\n" - " vmovdqa 256(%rsp), %ymm6\n" - " vmovdqa 288(%rsp), %ymm7\n" - " vmovdqa 320(%rsp), %ymm8\n" - " vmovdqa 352(%rsp), %ymm9\n" - " vmovdqa 384(%rsp), %ymm10\n" - " vmovdqa 416(%rsp), %ymm11\n" - " vmovdqa 448(%rsp), %ymm12\n" - " vmovdqa 480(%rsp), %ymm13\n" - " vmovdqa 512(%rsp), %ymm14\n" - " vmovdqa 544(%rsp), %ymm15\n" + " " AP_VMOV " 64(%rsp), " AP_V "0\n" + " " AP_VMOV " 96(%rsp), " AP_V "1\n" + " " AP_VMOV " 128(%rsp), " AP_V "2\n" + " " AP_VMOV " 160(%rsp), " AP_V "3\n" + " " AP_VMOV " 192(%rsp), " AP_V "4\n" + " " AP_VMOV " 224(%rsp), " AP_V "5\n" + " " AP_VMOV " 256(%rsp), " AP_V "6\n" + " " AP_VMOV " 288(%rsp), " AP_V "7\n" + " " AP_VMOV " 320(%rsp), " AP_V "8\n" + " " AP_VMOV " 352(%rsp), " AP_V "9\n" + " " AP_VMOV " 384(%rsp), " AP_V "10\n" + " " AP_VMOV " 416(%rsp), " AP_V "11\n" + " " AP_VMOV " 448(%rsp), " AP_V "12\n" + " " AP_VMOV " 480(%rsp), " AP_V "13\n" + " " AP_VMOV " 512(%rsp), " AP_V "14\n" + " " AP_VMOV " 544(%rsp), " AP_V "15\n" " movq %rbp, %rsp\n" " popq %rbp\n" " popq %r11\n" @@ -1435,10 +1456,10 @@ static void thread_finish(RtThread *t) { unlock(); } -// ExitThread: back to the thread start with a non-unwinding longjmp; no SEH unwinding through lifted frames. -// (__builtin_setjmp is not usable: on Win64 LLVM saves the frame address, which is not rbp when the frame register -// is offset, and the landing code then reads its locals through the wrong base.) -static __attribute__((noreturn)) void exit_now(RtThread *t) { longjmp(t->exit_jmp, 1); } +// ExitThread: back to the thread start with plat_jmp_go, which unwinds nothing: no SEH unwinding through lifted +// frames. (__builtin_setjmp is not usable: on Win64 LLVM saves the frame address, which is not rbp when the frame +// register is offset, and the landing code then reads its locals through the wrong base.) +static __attribute__((noreturn)) void exit_now(RtThread *t) { plat_jmp_go(&t->exit_jmp); } // The calling host thread, for ap_try (TOTK_ASYNC_PREEMPT) and dg_sample (TOTK_CORE_DIAG). static PlatIntr *ap_open_self(void) { @@ -1460,7 +1481,7 @@ static void thread_main(void *p) { unlock(); Cpu *c = t->cpu; c->sp = t->stack_top & ~15ull; - if (!setjmp(t->exit_jmp)) { + if (!plat_jmp_save(&t->exit_jmp)) { rt_call(c, t->entry, 1, t->arg); char d[128]; rt_fatal("guest thread %#x: entry %s returned (Horizon threads end with ExitThread)", t->handle, @@ -1520,7 +1541,7 @@ static void guest_main(void *p) { void (*fn)(Cpu *) = (void (*)(Cpu *))p; Cpu *c; kern_main_thread(&c); - if (!setjmp(tcur->exit_jmp)) fn(c); + if (!plat_jmp_save(&tcur->exit_jmp)) fn(c); thread_finish(tcur); return; } @@ -1879,7 +1900,9 @@ int kern_svc(Cpu *c, uint32_t imm) { if (type != 0) { int32_t nv = val + 1; if (type == 2) { - if (cnt <= 0) nv = nw > 0 ? val - 1 : val + 1; + // Waking all (count <= 0) with waiters subtracts 2, not 1 (mesosphere + // KAddressArbiter::SignalAndModifyByWaitingCountIfEqual; yuzu, LAS Recomp and nxnative agree). + if (cnt <= 0) nv = nw > 0 ? val - 2 : val + 1; else nv = nw == 0 ? val + 1 : nw <= cnt ? val - 1 : val; } uint32_t e = (uint32_t)val; @@ -1943,8 +1966,13 @@ int kern_svc(Cpu *c, uint32_t imm) { int idx = -1; lock(); g_sync_calls++; + // Horizon's order (mesosphere KSynchronizationObject::Wait): a signalled object, then timeout 0, then a pending + // CancelSynchronization. The cancel is for the next wait that would block: a poll or a wait on a signalled + // object leaves it pending. A cancel that wakes this wait (CancelSynchronization wakes it with R_CANCELLED; a + // signal wakes it with -1 to look again) ends it cancelled, signalled objects or not. + uint32_t woke = 0; for (;;) { - if (t->cancel) { + if (woke == R_CANCELLED) { t->cancel = 0; r = R_CANCELLED; break; @@ -1960,6 +1988,15 @@ int kern_svc(Cpu *c, uint32_t imm) { r = R_OK; break; } + if (ns == 0) { + r = R_TIMEOUT; + break; + } + if (t->cancel) { + t->cancel = 0; + r = R_CANCELLED; + break; + } int64_t left = ns; if (ns > 0) { uint64_t spent = kern_now_ns() - start; @@ -1971,7 +2008,8 @@ int kern_svc(Cpu *c, uint32_t imm) { } t->wobj = n ? hs[0] | (uint64_t)n << 32 : 0; g_sync_blocks++; - if (block(t, W_SYNC, left) == R_TIMEOUT) { + woke = block(t, W_SYNC, left); + if (woke == R_TIMEOUT) { r = R_TIMEOUT; break; } @@ -1986,7 +2024,7 @@ int kern_svc(Cpu *c, uint32_t imm) { RtThread *t = h_get((uint32_t)x[0], O_THREAD); if (t) { t->cancel = 1; - if (t->wkind == W_SYNC) wake(t, (uint32_t)-1); + if (t->wkind == W_SYNC) wake(t, R_CANCELLED); } unlock(); x[0] = t ? R_OK : R_BAD_HANDLE; @@ -2047,10 +2085,19 @@ void kern_event_signal(uint32_t hw) { // System events of host services: the SDK's own nn::os::CreateSystemEvent creates the kernel event (SVC 0x45) on a // guest thread; the writable handle is kept, so any thread (host audio tick included) can signal it later. +// The guest may destroy such an event and attach the same SystemEventType again: the game does that with the nfp +// activate and deactivate events before each detection. nn::os::DestroySystemEvent zeroes the state byte at +0x28 +// (0 not initialised, 1 event, 2 inter-process event; nn::os::TryWaitSystemEvent aborts on 0) and closes both +// handles, so a kept handle counts only while that byte is nonzero. +#define SE_STATE 0x28 static struct { uint64_t ev; uint32_t wh; } g_se[512]; static int g_nse; static PlatMutex g_se_lock = PLAT_MUTEX_INIT; +static int se_live(uint64_t ev) { + return __atomic_load_n((const uint8_t *)(uintptr_t)(ev + SE_STATE), __ATOMIC_RELAXED) != 0; +} + static uint32_t se_find(uint64_t ev) { plat_mutex_lock_shared(&g_se_lock); uint32_t wh = 0; @@ -2060,8 +2107,9 @@ static uint32_t se_find(uint64_t ev) { return wh; } +// A bound event the guest destroyed since is created again and its table entry takes the new writable handle. void kern_bind_system_event(Cpu *c, uint64_t ev, int autoclear) { - if (se_find(ev)) return; + if (se_find(ev) && se_live(ev)) return; if (!tcur || !c) rt_fatal("kern_bind_system_event(%#llx) on a host thread", (unsigned long long)ev); static uint64_t create, getw; if (!create) { @@ -2073,15 +2121,21 @@ void kern_bind_system_event(Cpu *c, uint64_t ev, int autoclear) { if (r) rt_fatal("kern_bind_system_event: CreateSystemEvent(%#llx) failed %#x", (unsigned long long)ev, r); uint32_t wh = (uint32_t)rt_call(c, getw, 1, ev); plat_mutex_lock(&g_se_lock); - if (g_nse == (int)(sizeof g_se / sizeof *g_se)) rt_fatal("kern_bind_system_event: table full"); - g_se[g_nse].ev = ev; - g_se[g_nse++].wh = wh; + int i = 0; + while (i < g_nse && g_se[i].ev != ev) i++; + if (i == g_nse) { + if (g_nse == (int)(sizeof g_se / sizeof *g_se)) rt_fatal("kern_bind_system_event: table full"); + g_se[g_nse++].ev = ev; + } + g_se[i].wh = wh; plat_mutex_unlock(&g_se_lock); } -// An event not bound yet is bound on the spot with manual clear, which needs a guest thread. +// An event not bound yet is bound on the spot with manual clear, which needs a guest thread. A bound event the guest +// destroyed is not signalled: its handles are closed and the kernel hands their numbers to new objects. void kern_signal_system_event(uint64_t ev) { uint32_t wh = se_find(ev); + if (wh && !se_live(ev)) return; if (!wh) { kern_bind_system_event(kern_cpu(), ev, 0); wh = se_find(ev); diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index e226d4a5..191a1c28 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -305,9 +305,18 @@ static void test_arbiter(Cpu *c) { CHECK(sig(c, 2, 5, 0) == 0 && g_arb == 6, "signal type 2, no waiter, count 0: value %d (want 6)", (int32_t)g_arb); ArbJob jc = {.type = 2, .value = 6, .timeout = -1}; h = arb_spawn(c, &jc); - CHECK(sig(c, 2, 6, -1) == 0 && g_arb == 5, "signal type 2, 1 waiter, count -1: value %d (want 5)", (int32_t)g_arb); + // Waking all (count <= 0) with waiters subtracts 2 (mesosphere KAddressArbiter::SignalAndModifyByWaitingCountIfEqual). + CHECK(sig(c, 2, 6, -1) == 0 && g_arb == 4, "signal type 2, 1 waiter, count -1: value %d (want 4)", (int32_t)g_arb); join(c, h); - CHECK(ja.r == 0 && jb.r == 0 && jc.r == 0, "wait type 2 results %#x %#x %#x", ja.r, jb.r, jc.r); + g_arb = 6; + ArbJob jd = {.type = 2, .value = 6, .timeout = -1}, je = jd; + uint32_t hd = arb_spawn(c, &jd), he = arb_spawn(c, &je); + CHECK(sig(c, 2, 6, 0) == 0 && g_arb == 4, "signal type 2, 2 waiters, count 0: value %d (want 4)", (int32_t)g_arb); + join(c, hd); + join(c, he); + CHECK(ja.r == 0 && jb.r == 0 && jc.r == 0 && jd.r == 0 && je.r == 0, "wait type 2 results %#x %#x %#x %#x %#x", ja.r, + jb.r, jc.r, jd.r, je.r); + g_arb = 5; CHECK(sig(c, 2, 7, 1) == KERN_R_BAD_STATE, "signal type 2 with a different value"); // timeout, invalid type uint64_t t0 = kern_now_ns(); @@ -320,11 +329,17 @@ static void test_arbiter(Cpu *c) { } // ---- events -typedef struct { uint32_t h, r, idx; int64_t timeout; volatile int delay, go; } EvJob; +// pre: a first wait on h0 with timeout t0 (result r0, index idx0) before the wait on h. +typedef struct { uint32_t h, r, idx, h0, r0, idx0; int64_t timeout, t0; volatile int delay, go; int pre; } EvJob; static void event_waiter(Cpu *c) { EvJob *j = arg_p(c, 0); while (j->delay && !j->go) sleep_ms(c, 1); + if (j->pre) { + uint32_t h0[1] = {j->h0}; + j->r0 = sv(c, 0x18, 0, ga(h0), 1, (uint64_t)j->t0); + j->idx0 = (uint32_t)c->x[1]; + } uint32_t hs[1] = {j->h}; j->r = sv(c, 0x18, 0, ga(hs), 1, (uint64_t)j->timeout); j->idx = (uint32_t)c->x[1]; @@ -375,6 +390,26 @@ static void test_events(Cpu *c) { join(c, h); CHECK(g.r == KERN_R_CANCELLED, "wait after a cancel: %#x", g.r); + // A pending cancel is for the next WaitSynchronization that would block (mesosphere KSynchronizationObject::Wait: + // signalled objects first, then timeout 0, then the cancel): a poll times out and a wait on a signalled object + // succeeds, both leaving it pending; the next blocking wait ends cancelled. The blocking waits have a 500 ms + // timeout, so a cancel used up too early shows as a timeout instead of a hang. + CHECK(sv(c, 0x11, hw2, 0, 0, 0) == 0, "SignalEvent of the second event"); + EvJob p = {.h = hr, .timeout = 500000000, .delay = 1, .pre = 1, .h0 = hr, .t0 = 0}; + EvJob q = {.h = hr, .timeout = 500000000, .delay = 1, .pre = 1, .h0 = hr2, .t0 = -1}; + EvJob *pq[2] = {&p, &q}; + for (int i = 0; i < 2; i++) { + h = create(c, a_event_waiter, pq[i], 44, -2); + start(c, h); + CHECK(sv(c, 0x19, h, 0, 0, 0) == 0, "CancelSynchronization before the %s", i ? "signalled wait" : "poll"); + pq[i]->go = 1; + join(c, h); + } + CHECK(p.r0 == KERN_R_TIMEOUT, "poll (timeout 0) with a cancel pending: %#x (want timeout)", p.r0); + CHECK(p.r == KERN_R_CANCELLED, "blocking wait after the poll: %#x (want cancelled)", p.r); + CHECK(q.r0 == 0 && q.idx0 == 0, "wait on a signalled event with a cancel pending: %#x (want success)", q.r0); + CHECK(q.r == KERN_R_CANCELLED, "blocking wait after the signalled wait: %#x (want cancelled)", q.r); + uint32_t sw = kern_event_new(), sr = kern_event_readable(sw); CHECK(wait1(c, sr, 0) == KERN_R_TIMEOUT, "service event before the signal"); kern_event_signal(sw); @@ -382,7 +417,8 @@ static void test_events(Cpu *c) { uint32_t handles[] = {sr, sw, hr2, hw2, hr, hw}; for (unsigned i = 0; i < 6; i++) CHECK(sv(c, 0x16, handles[i], 0, 0, 0) == 0, "CloseHandle %#x", handles[i]); CHECK(sv(c, 0x16, hr, 0, 0, 0) == KERN_R_BAD_HANDLE, "CloseHandle twice"); - printf("events: signal, clear, reset, two handles, cancel while waiting and before, service events, 10 ms " + printf("events: signal, clear, reset, two handles, cancel while waiting and before, a pending cancel kept by a poll " + "and a signalled wait, service events, 10 ms " "timeout after %.2f ms: %s\n", ms, failures == before ? "ok" : "FAIL"); } diff --git a/runtime/loader.c b/runtime/loader.c index 8bd482fe..4aa030f5 100644 --- a/runtime/loader.c +++ b/runtime/loader.c @@ -552,8 +552,8 @@ uint64_t ldr_plt_stub(const char *module, const char *name) { } // ---- --probe-loader -// Expected values, measured on the 1.1.1, 1.2.1 and 1.4.3 modules; the 1.4.3 relocation totals match the ones -// MacNativeGames' port notes give for that version. 1.1.1 shares subsdk0 and sdk with 1.2.1 down to every count. +// Expected values, measured on the 1.1.0, 1.2.1 and 1.4.3 modules; the 1.4.3 relocation totals match the ones +// MacNativeGames' port notes give for that version. 1.1.0 shares subsdk0 and sdk with 1.2.1 down to every count. typedef struct { const char *name; uint64_t kinds[K_COUNT], total, sym_import, sym_own; @@ -564,7 +564,7 @@ static const Expect expect_121[] = { {"subsdk0", {1623, 12820, 1030, 3924}, 19397, 6576, 11198, 667, {9, 0, 654}, 4, 0x30, 45}, {"sdk", {10917, 13976, 1207, 8932}, 35032, 16, 24099, 13, {8, 0, 0}, 5, 0x30, 116}, }; -static const Expect expect_111[] = { +static const Expect expect_110[] = { {"main", {508604, 1412, 32, 639}, 510687, 2083, 0, 673, {0, 16, 645}, 12, 0x30, 10668}, {"subsdk0", {1623, 12820, 1030, 3924}, 19397, 6576, 11198, 667, {9, 0, 654}, 4, 0x30, 45}, {"sdk", {10917, 13976, 1207, 8932}, 35032, 16, 24099, 13, {8, 0, 0}, 5, 0x30, 116}, @@ -574,7 +574,7 @@ static const Expect expect_143[] = { {"subsdk0", {3424, 10927, 905, 3817}, 19073, 6557, 9092, 663, {9, 0, 654}, 0, 0x20, 45}, {"sdk", {15013, 10135, 1079, 7832}, 34059, 10, 19036, 8, {8, 0, 0}, 0, 0x400, 83}, }; -#define expect (RT_V143 ? expect_143 : RT_V111 ? expect_111 : expect_121) +#define expect (RT_V143 ? expect_143 : RT_V110 ? expect_110 : expect_121) static int failures; #define CHECK(cond, ...) do { if (!(cond)) { failures++; printf(" FAIL: " __VA_ARGS__); putchar('\n'); } } while (0) diff --git a/runtime/main.c b/runtime/main.c index 137bb264..01b1ffda 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -2,8 +2,10 @@ // // totk.exe [--exefs DIR] [--romfs DIR] [--profile DIR] // --selftest layout, dispatch table and host-function registry; needs no game files +// --probe-mods the mods folder scan, order and switches (mods.c); needs no game files // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values // --probe-kernel, --probe-ipc, --probe-services, --probe-fs host probes +// --probe-discord the Rich Presence IPC exchange; skips without a Discord client; no game files // --probe-dpi the process DPI awareness (per-monitor v2) and the primary monitor's size; no game files // --probe-gfx-abi NVN object layouts against nvn.h's reserved sizes; needs no game files // --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec graphics probes @@ -152,8 +154,8 @@ static RtConfig cfg; const RtConfig *rt_config(void) { return &cfg; } // ---- runtime services the generated code calls (cpu.h) -volatile int g_fnprof = 0; -void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0) { (void)addr, (void)lr, (void)x0; } +volatile int g_fnprof = 0; // resolution.c switches it on until the game sets its render size (res_fn_hit) +void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0) { (void)lr, res_fn_hit(addr, x0); } void rt_brk(Cpu *c, uint32_t imm, uint64_t pc) { char at[128], lr[128]; @@ -217,16 +219,21 @@ static void crash_handler(const PlatCrash *x) { if (plat_xchg32(&busy, 1)) plat_exit(71); char d[160]; uintptr_t rip = x->rip; - rt_logf("FATAL: %s (%#x) at host %p (totk+%#llx)", x->what, x->code, (void *)rip, (unsigned long long)x->offset); + rt_logf("FATAL: %s (%#x) at host %p (", x->what, x->code, (void *)rip); + if (x->module && !*x->module) rt_logf("outside every module)"); + else rt_logf("%s+%#llx)", x->module ? x->module : "totk", (unsigned long long)x->offset); if (x->access >= 0) rt_logf(", %s address %#llx", x->access == 1 ? "writing" : x->access == 8 ? "executing" : "reading", (unsigned long long)x->addr); rt_log_raw("\n", 1); - uint64_t f = lifted_function(rip); + uint64_t f = x->module ? 0 : lifted_function(rip); if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d)); - for (int i = 0; i < x->nframes; i++) { // abort and invalid parameter: where the host code called it + // abort and invalid parameter: where the host code called it; a fault in another module: the host calls that led + // into it + uintptr_t exe = plat_exe_base(); + for (int i = 0; i < x->nframes; i++) { uint64_t g = lifted_function(x->frames[i]); - rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - (rip - x->offset))); + rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - exe)); if (g) rt_logf(" in lifted function f_%llx", (unsigned long long)g); rt_log_raw("\n", 1); } @@ -246,6 +253,7 @@ static void crash_handler(const PlatCrash *x) { } else { rt_logf(" not on a guest thread\n"); } + kern_free_cores_note(); fflush(stderr); plat_exit(71); } @@ -259,11 +267,22 @@ static double now_s(void) { return (double)plat_now_ns() / 1e9; } static volatile int host_probe_hits; static void host_probe(Cpu *c) { host_probe_hits++; c->x[0] = 42; } +// The instruction set build_game.py --cpu compiled for: v3 (AVX2/FMA/BMI2, the default) or v2 (SSE4.2/POPCNT). +#ifndef RT_CPU_LEVEL +#define RT_CPU_LEVEL 3 +#endif + static void check_cpu(void) { __builtin_cpu_init(); +#if RT_CPU_LEVEL >= 3 int ok = __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") && __builtin_cpu_supports("bmi2"); printf("cpu: x86-64-v3 (AVX2, FMA, BMI2) %s\n", ok ? "present" : "MISSING"); CHECK(ok, "the lifted code is built for x86-64-v3"); +#else + int ok = __builtin_cpu_supports("sse4.2") && __builtin_cpu_supports("popcnt"); + printf("cpu: x86-64-v2 (SSE4.2, POPCNT) %s\n", ok ? "present" : "MISSING"); + CHECK(ok, "the lifted code is built for x86-64-v2"); +#endif } // Layout: ascending, no overlap, below 2^39, and inside the window mem_init reserved. @@ -426,11 +445,27 @@ static int selftest(void) { return failures ? 1 : 0; } +// --probe-crash-call and --probe-crash-null crash on purpose, to check the report of a fault outside this executable. +// call: strlen in the C runtime library (ucrtbase.dll, libc.so.6) reads address 8, the way a driver faults inside a +// call the host made; the report names the library and the host frames that called it. null: a call through a null +// function pointer, which leaves rip at 0, outside every module. Both end with exit code 71 and a FATAL line in the +// log. +static size_t (*volatile probe_strlen)(const char *) = strlen; +static void (*volatile probe_null)(void); + +static int probe_crash(int null) { + if (null) probe_null(); + else printf("%zu\n", probe_strlen((const char *)(uintptr_t)8)); + printf("probe-crash: still running, the crash did not happen\n"); + return 1; +} + static int usage(const char *argv0) { fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | " - "--probe-cfg | --probe-dpi | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " + "--probe-cfg | --probe-dpi | --probe-mods | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " - "--probe-vkexec | --probe-astc | --probe-movie | --prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); + "--probe-vkexec | --probe-astc | --probe-movie | --probe-crash-call | --probe-crash-null | --probe-discord | " + "--prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); return 2; } @@ -468,6 +503,9 @@ static void portable_paths(RtConfig *c) { int main(int argc, char **argv) { mem_init(); // first: claim the guest address space before anything else can land there; reads totk.ini + // A restart from the settings menu (gfx_present.c gfx_window_closed) starts this process while the one it replaces + // is still exiting: its log, RomFS and saves are free once it is gone. + if (plat_restart_wait(30000) < 0) rt_log("restart: the previous process still runs after 30 s; starting anyway"); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); plat_crash_hook(crash_handler); @@ -510,15 +548,20 @@ int main(int argc, char **argv) { log_open(!mode); if (!mode) { cfg.boot = cfg.seconds > 0; // --seconds starts the game like --boot, with its checkpoints + discord_init(); // the Rich Presence thread (discord_rpc.c); it reads totk.ini itself and idles while off rt_boot(); return 0; } if (!strcmp(mode, "--selftest")) return selftest(); if (!strcmp(mode, "--probe-loader")) return probe_loader(); + if (!strcmp(mode, "--probe-crash-call")) return probe_crash(0); + if (!strcmp(mode, "--probe-crash-null")) return probe_crash(1); if (!strcmp(mode, "--probe-cfg")) return probe_cfg(); #ifdef _WIN32 if (!strcmp(mode, "--probe-dpi")) return probe_dpi(); // the Win32 DPI awareness of totk.manifest #endif + if (!strcmp(mode, "--probe-mods")) return probe_mods(); + if (!strcmp(mode, "--probe-discord")) return probe_discord(); if (!strcmp(mode, "--probe-kernel")) return probe_kernel(); if (!strcmp(mode, "--probe-ipc")) return probe_ipc(); if (!strcmp(mode, "--probe-services")) return probe_services(); diff --git a/runtime/mods.c b/runtime/mods.c new file mode 100644 index 00000000..4f3ad08c --- /dev/null +++ b/runtime/mods.c @@ -0,0 +1,457 @@ +// Player mods: the scan of the mods folder, its order and switches (mods.h). +// +// One subfolder per mod, as emulators take them. The RomFS root is the first of /romfs, /romfslite (TKMM's +// name for it when it bypasses Atmosphere's RomFS build), /atmosphere/contents//romfs, +// <mod>/contents/<title>/romfs and <mod>/<title>/romfs that exists; the ExeFS and cheats folders likewise. A mod is +// listed whatever it holds; what the port cannot use is its problem, shown in the menu and logged, and never stops the +// start. romfs.c serves the RomFS files of the enabled mods, the top one winning a path. +// +// The order and the switches are totk.ini keys (cfg.c): TOTK_MODS_ORDER lists names top first, separated by '|' (a +// Windows file name cannot hold '|'; a folder whose name does on Linux is listed as unusable); a folder it does not name +// goes below the named ones, by name. TOTK_MODS_DISABLED lists the switched-off names. The menu changes the list in +// place and writes both keys; the scan's state is kept to tell the menu a restart is due. +// +// ExeFS patch files: an .ips or .pchtxt file in the exefs folder or in atmosphere/exefs_patches/<name>/ is for the game +// when its build id is main's (an IPS file's name, a pchtxt file's @nsobid line); others are another version's and +// counted. The setup builds every mod's files into patch sets and writes build-cache/exefs.status next to totk.exe +// (tools/exefs_patches.py has the format): a file whose line is missing or has another content hash waits for the +// rebuild at start (cheats.c), and its mod stays off until then. +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#include "cfg.h" +#include "host.h" +#include "mods.h" +#include "platform/platform.h" + +#define TITLE "0100F2C0115B6000" +#define MAX_MODS 512 + +static ModInfo *mods; +static int nmods; +static char dir[2048]; +static char start_order[MAX_MODS][256]; // names in the scan's order and their switches, for mods_changed +static int start_enabled[MAX_MODS]; + +// 1 when a file lies anywhere under path. +static int has_file_cb(const char *name, int kind, uint64_t size, void *ctx); +static int has_file(const char *path) { + struct { const char *path; int found; } c = {path, 0}; + plat_fs_list(path, has_file_cb, &c); + return c.found; +} +static int has_file_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + struct { const char *path; int found; } *c = ctx; + if (kind == PLAT_FS_FILE) { + c->found = 1; + } else if (kind == PLAT_FS_DIR) { + char sub[2100]; + if (snprintf(sub, sizeof sub, "%s/%s", c->path, name) < (int)sizeof sub) c->found = has_file(sub); + } + return !c->found; +} + +// The first of the emulator and Atmosphere places of <mod>/<part> that is a folder, into out ("" none). +static void find_root(const char *mod, const char *part, char *out, size_t cap) { + static const char *const forms[] = {"%s/%s", "%s/atmosphere/contents/" TITLE "/%s", "%s/contents/" TITLE "/%s", + "%s/" TITLE "/%s"}; + for (size_t i = 0; i < sizeof forms / sizeof *forms; i++) { + if (snprintf(out, cap, forms[i], mod, part) < (int)cap && plat_dir_exists(out)) return; + } + out[0] = 0; +} + +static int ends_with_ci(const char *s, const char *tail) { + size_t n = strlen(s), t = strlen(tail); + return n >= t && !strcasecmp(s + n - t, tail); +} + +// What the ExeFS folder holds: MOD_EXEFS for patches (.ips, .pchtxt), MOD_SUBSDK for code modules (subsdk* plugins, a +// replaced main). main.npdm alone is the process's metadata, not code: it counts as neither. +static int exefs_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + int *k = ctx; + if (kind != PLAT_FS_FILE) return 1; + if (ends_with_ci(name, ".ips") || ends_with_ci(name, ".pchtxt")) *k |= MOD_EXEFS; + else if (!strncasecmp(name, "subsdk", 6) || !strcasecmp(name, "main")) *k |= MOD_SUBSDK; + return 1; +} + +// MOD_CHEATS when the cheats folder holds an Atmosphere cheat file (<build id>.txt). +static int cheats_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + if (kind == PLAT_FS_FILE && ends_with_ci(name, ".txt")) *(int *)ctx |= MOD_CHEATS; + return 1; +} + +// 1 when name is one of the '|'-separated names of list. +static int listed(const char *list, const char *name) { + size_t n = strlen(name); + for (const char *p = list; p && *p;) { + const char *bar = strchr(p, '|'); + size_t len = bar ? (size_t)(bar - p) : strlen(p); + if (len == n && !memcmp(p, name, n)) return 1; + p = bar ? bar + 1 : NULL; + } + return 0; +} + +// The position of name in the '|'-separated list, or -1. +static int list_pos(const char *list, const char *name) { + size_t n = strlen(name); + int k = 0; + for (const char *p = list; p && *p; k++) { + const char *bar = strchr(p, '|'); + size_t len = bar ? (size_t)(bar - p) : strlen(p); + if (len == n && !memcmp(p, name, n)) return k; + p = bar ? bar + 1 : NULL; + } + return -1; +} + +static const char *order_list; // TOTK_MODS_ORDER while sorting +static int by_order(const void *a, const void *b) { + const ModInfo *x = a, *y = b; + int px = list_pos(order_list, x->name), py = list_pos(order_list, y->name); + if (px >= 0 && py >= 0) return px - py; + if (px >= 0 || py >= 0) return px >= 0 ? -1 : 1; + return strcmp(x->name, y->name); +} + +static const char *problem_name(ModProblem p) { + switch (p) { + case MOD_EMPTY: return "empty"; + case MOD_PLUGIN: return "has a code module (subsdk or main), not supported; the whole mod stays off"; + case MOD_EXEFS_LATER: return "ExeFS patches wait for a rebuild of the game code; the whole mod stays off"; + case MOD_EXEFS_UNSUPPORTED: return "an ExeFS patch file cannot be applied; the whole mod stays off"; + case MOD_EXEFS_OTHER: return "its ExeFS patches are for another game version"; + case MOD_BAD_NAME: return "its folder name holds '|', which the order list uses; rename the folder"; + default: return "ok"; + } +} + +// ---- ExeFS patch files + +static uint32_t fnv(const void *p, size_t n) { + uint32_t h = 0x811C9DC5u; + for (size_t i = 0; i < n; i++) h = (h ^ ((const uint8_t *)p)[i]) * 0x01000193u; + return h; +} + +const char *mods_main_build_id(void) { + static char bid[65]; + static int read; + if (read) return bid; + read = 1; + char path[2100]; + snprintf(path, sizeof path, "%s/main", rt_config()->exefs); + PlatFile *f = plat_file_open(path); + uint8_t head[0x60]; + int ok = f && plat_file_pread(f, head, sizeof head, 0) == sizeof head && !memcmp(head, "NSO0", 4); + if (f) plat_file_close(f); + for (int i = 0; ok && i < 32; i++) snprintf(bid + 2 * i, 3, "%02x", head[0x40 + i]); + return bid; +} + +// 1 when hex (any case; trailing zeros left out or not) names main's build id; also when main's is unknown. +static int same_build(const char *hex, size_t n) { + const char *bid = mods_main_build_id(); + if (!*bid) return 1; + if (!n || n > 64) return 0; + for (size_t i = 0; i < 64; i++) { + char c = i < n ? hex[i] : '0'; + if (c >= 'A' && c <= 'F') c = (char)(c - 'A' + 'a'); + if (c != bid[i]) return 0; + } + return 1; +} + +// build-cache/exefs.status next to totk.exe, read once; "" when there is none. +static const char *exefs_status(void) { + static char *text; + if (text) return text; + char path[2100]; + uint64_t n = 0; + if (plat_exe_path(path, sizeof path)) { + char *cut = strrchr(path, '/'), *back = strrchr(path, '\\'); + if (back > cut) cut = back; + if (cut) snprintf(cut + 1, sizeof path - (size_t)(cut + 1 - path), "build-cache/exefs.status"); + text = (char *)plat_file_read(path, &n); + } + if (!text) text = calloc(1, 1); + return text; +} + +// The status line of key: its kind, built-in sets and note, NUL-terminated copies; 0 when none. +static int status_line(const char *key, const char *content, char *kind, char *over, char *note) { + char want[20]; + snprintf(want, sizeof want, "%s\t%s\t", key, content); + for (const char *line = exefs_status(); *line;) { + const char *end = strchr(line, '\n'); + size_t len = end ? (size_t)(end - line) : strlen(line); + if (len > 18 && !memcmp(line, want, 18)) { + char copy[600]; + snprintf(copy, sizeof copy, "%.*s", (int)(len < sizeof copy ? len : sizeof copy - 1), line + 18); + char *t1 = strchr(copy, '\t'), *t2 = t1 ? strchr(t1 + 1, '\t') : NULL; + if (!t1 || !t2) return 0; + *t1 = *t2 = 0; + char *r = strchr(t2 + 1, '\r'); + if (r) *r = 0; + snprintf(kind, 16, "%s", copy); + snprintf(over, 256, "%s", strcmp(t1 + 1, "-") ? t1 + 1 : ""); + snprintf(note, 256, "%s", t2 + 1); + return 1; + } + line = end ? end + 1 : line + len; + } + return 0; +} + +typedef struct { + ModInfo *m; + const char *folder, *rel; // the folder listed and its path in the mod + int unsupported; +} ExefsWalk; + +// One file of an exefs folder: its build, then its status line. +static int exefs_file_cb(const char *leaf, int kind, uint64_t size, void *ctx) { + (void)size; + ExefsWalk *w = ctx; + ModInfo *m = w->m; + int ips = ends_with_ci(leaf, ".ips"); + if (kind != PLAT_FS_FILE || (!ips && !ends_with_ci(leaf, ".pchtxt"))) return 1; + m->kind |= MOD_EXEFS; + char path[4300], rel[600]; + snprintf(path, sizeof path, "%s/%s", w->folder, leaf); + snprintf(rel, sizeof rel, "%s/%s/%s", m->name, w->rel, leaf); + uint64_t n = 0; + char *data = (char *)plat_file_read(path, &n); + if (!data) { + rt_log("mods: %s: cannot read %s", m->name, path); + return 1; + } + int for_main; + if (ips) { + for_main = same_build(leaf, strlen(leaf) - 4); + } else { + char *id = NULL; // after "@nsobid-" at the start of a line + for (char *p = data; !id && p && data + n - p > 8;) { + if (!memcmp(p, "@nsobid-", 8)) id = p + 8; + p = memchr(p, '\n', (size_t)(data + n - p)); + if (p) p++; + } + size_t len = 0; + while (id && id + len < data + n && isxdigit((unsigned char)id[len])) len++; + for_main = id && same_build(id, len); + } + char key[9], content[9], kind_s[16], over[256], note[256]; + snprintf(key, sizeof key, "%08X", fnv(rel, strlen(rel))); + snprintf(content, sizeof content, "%08X", fnv(data, (size_t)n)); + free(data); + if (!for_main) { + m->other_version++; + return 1; + } + if (!status_line(key, content, kind_s, over, note)) { + m->pending++; + m->pending_key = (m->pending_key ^ fnv(key, 8)) * 0x01000193u; + m->pending_key = (m->pending_key ^ fnv(content, 8)) * 0x01000193u; + return 1; + } + if (!strcmp(kind_s, "unsupported")) { + if (!w->unsupported) snprintf(m->note, sizeof m->note, "%s: %s", leaf, note); + w->unsupported = 1; + } else if (!strcmp(kind_s, "code") && m->nsets < (int)(sizeof m->sets / sizeof *m->sets)) { + m->sets[m->nsets++] = (uint32_t)strtoul(key, NULL, 16); + size_t k = strlen(m->overrides); + if (*over) snprintf(m->overrides + k, sizeof m->overrides - k, "%s%s", k ? "," : "", over); + } else if (!strcmp(kind_s, "other")) { + m->other_version++; + } + return 1; +} + +// atmosphere/exefs_patches/<name>/ of a mod: each <name> folder's files. +static int exefs_patches_cb(const char *leaf, int kind, uint64_t size, void *ctx) { + (void)size; + ExefsWalk *w = ctx; + if (kind != PLAT_FS_DIR) return 1; + char folder[4300], rel[600]; + snprintf(folder, sizeof folder, "%s/%s", w->folder, leaf); + snprintf(rel, sizeof rel, "atmosphere/exefs_patches/%s", leaf); + ExefsWalk sub = {w->m, folder, rel, 0}; + plat_fs_list(folder, exefs_file_cb, &sub); + w->unsupported |= sub.unsupported; + return 1; +} + +// The ExeFS patch files of mod m (folder path): 1 when one cannot be applied. +static int exefs_check(ModInfo *m, const char *path) { + ExefsWalk w = {m, m->exefs, m->exefs + strlen(path) + 1, 0}; + if (m->exefs[0]) plat_fs_list(m->exefs, exefs_file_cb, &w); + char patches[2200]; + snprintf(patches, sizeof patches, "%s/atmosphere/exefs_patches", path); + ExefsWalk at = {m, patches, "", 0}; + if (plat_dir_exists(patches)) plat_fs_list(patches, exefs_patches_cb, &at); + return w.unsupported || at.unsupported; +} + +// One mod folder of the scan. +static int scan_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + const char *disabled = ctx; + if (kind != PLAT_FS_DIR) return 1; + if (nmods == MAX_MODS) { + rt_log("mods: more than %d folders in %s; the rest are left out", MAX_MODS, dir); + return 0; + } + ModInfo *m = &mods[nmods]; + memset(m, 0, sizeof *m); + char path[2100]; + if (snprintf(m->name, sizeof m->name, "%s", name) >= (int)sizeof m->name || + snprintf(path, sizeof path, "%s/%s", dir, name) >= (int)sizeof path) { + rt_log("mods: %s: name too long, left out", name); + return 1; + } + find_root(path, "romfs", m->romfs, sizeof m->romfs); + if (!m->romfs[0]) find_root(path, "romfslite", m->romfs, sizeof m->romfs); + find_root(path, "exefs", m->exefs, sizeof m->exefs); + find_root(path, "cheats", m->cheats, sizeof m->cheats); + if (m->romfs[0] && has_file(m->romfs)) m->kind |= MOD_ROMFS; + if (m->exefs[0]) plat_fs_list(m->exefs, exefs_cb, &m->kind); + if (m->cheats[0]) plat_fs_list(m->cheats, cheats_cb, &m->kind); + int unsupported = exefs_check(m, path); + // a mod whose code part the port cannot use is off as a whole: its RomFS files may expect that code + m->problem = strchr(m->name, '|') ? MOD_BAD_NAME + : m->kind & MOD_SUBSDK ? MOD_PLUGIN + : unsupported ? MOD_EXEFS_UNSUPPORTED + : m->pending ? MOD_EXEFS_LATER + : m->nsets || m->kind & (MOD_ROMFS | MOD_CHEATS) ? MOD_OK + : m->kind & MOD_EXEFS ? MOD_EXEFS_OTHER + : MOD_EMPTY; + m->enabled = !listed(disabled, m->name); + nmods++; + return 1; +} + +void mods_scan(void) { + static int done; + if (done) return; + done = 1; + const char *d = cfg_get("TOTK_MODS_DIR"); + if (d && *d) { + snprintf(dir, sizeof dir, "%s", d); + } else { + char exe[2048]; + if (!plat_exe_path(exe, sizeof exe)) return; + char *cut = strrchr(exe, '/'), *back = strrchr(exe, '\\'); + if (back > cut) cut = back; + if (!cut) return; + *cut = 0; + snprintf(dir, sizeof dir, "%s/mods", exe); + } + if (!plat_dir_exists(dir)) { + rt_log("mods: no folder %s", dir); + return; + } + mods = calloc(MAX_MODS, sizeof *mods); + if (!mods) return; + plat_fs_list(dir, scan_cb, (void *)cfg_get("TOTK_MODS_DISABLED")); + order_list = cfg_get("TOTK_MODS_ORDER"); + qsort(mods, nmods, sizeof *mods, by_order); + order_list = NULL; + for (int i = 0; i < nmods; i++) { + snprintf(start_order[i], sizeof start_order[i], "%s", mods[i].name); + start_enabled[i] = mods[i].enabled; + const ModInfo *m = &mods[i]; + rt_log("mods: %s: %s,%s%s%s%s%s, %s%s%s", m->name, m->enabled ? "on" : "off", m->kind & MOD_ROMFS ? " romfs" : "", + m->kind & MOD_EXEFS ? " exefs" : "", m->kind & MOD_SUBSDK ? " subsdk" : "", + m->kind & MOD_CHEATS ? " cheats" : "", m->kind ? "" : " no files", problem_name(m->problem), + m->note[0] ? ": " : "", m->note); + } + rt_log("mods: %d folders in %s", nmods, dir); +} + +void mods_exefs_switch(void) { + mods_scan(); + for (int i = 0; i < nmods; i++) { + const ModInfo *m = &mods[i]; + if (!start_enabled[i] || m->problem != MOD_OK || !m->nsets) continue; + for (int k = 0; k < m->nsets; k++) { + char name[32]; + snprintf(name, sizeof name, "mod_%08x", m->sets[k]); + int set = rt_patch_set(name); + if (set >= 0) rt_patch_switch(set, 1); + else rt_log("mods: %s: patch set %s is not in this build", m->name, name); + } + char over[256]; + snprintf(over, sizeof over, "%s", m->overrides); + for (char *s = strtok(over, ","); s; s = strtok(NULL, ",")) { + int set = rt_patch_set(s); + if (set >= 0) rt_patch_switch(set, 0); + rt_log("mods: %s replaces the port's patch set %s; it stays off", m->name, s); + } + rt_log("mods: %s: %d ExeFS patch files on", m->name, m->nsets); + } +} + +int mods_exefs_pending(uint32_t *key) { + int n = 0; + for (int i = 0; i < nmods; i++) { + if (!start_enabled[i] || mods[i].problem != MOD_EXEFS_LATER) continue; + n += (int)mods[i].pending; + *key = (*key ^ mods[i].pending_key) * 0x01000193u; + } + return n; +} + +const char *mods_dir(void) { return dir; } +int mods_count(void) { return nmods; } +ModInfo *mods_at(int i) { return i >= 0 && i < nmods ? &mods[i] : NULL; } + +// TOTK_MODS_ORDER as every listed name top first; TOTK_MODS_DISABLED as the switched-off names. A name holding '|' +// would split into two; it is left out of both (its mod is unusable anyway). +static void write_keys(void) { + size_t cap = (size_t)nmods * 257 + 1; + char *order = calloc(cap, 1), *off = calloc(cap, 1); + if (order && off) { + for (int i = 0; i < nmods; i++) { + if (mods[i].problem == MOD_BAD_NAME) continue; + if (*order) strcat(order, "|"); + strcat(order, mods[i].name); + if (!mods[i].enabled) { + if (*off) strcat(off, "|"); + strcat(off, mods[i].name); + } + } + cfg_set("TOTK_MODS_ORDER", order); + cfg_set("TOTK_MODS_DISABLED", off); + } + free(order); + free(off); +} + +void mods_set_enabled(int i, int on) { + if (i < 0 || i >= nmods) return; + mods[i].enabled = on != 0; + write_keys(); +} + +void mods_move(int i, int delta) { + int j = i + delta; + if (i < 0 || i >= nmods || j < 0 || j >= nmods || i == j) return; + ModInfo t = mods[i]; + mods[i] = mods[j]; + mods[j] = t; + write_keys(); +} + +int mods_changed(void) { + for (int i = 0; i < nmods; i++) + if (strcmp(mods[i].name, start_order[i]) || mods[i].enabled != start_enabled[i]) return 1; + return 0; +} diff --git a/runtime/mods.h b/runtime/mods.h new file mode 100644 index 00000000..98a513db --- /dev/null +++ b/runtime/mods.h @@ -0,0 +1,57 @@ +// Player mods (mods.c): one folder per mod under mods/ next to totk.exe (TOTK_MODS_DIR), unpacked as emulators take +// them: <mod>/romfs, <mod>/exefs and <mod>/cheats, the Atmosphere layout (atmosphere/contents/<title id>/romfs, +// atmosphere/exefs_patches/<name>/*.ips) or TKMM's merged output (romfs or romfslite, exefs, cheats). ExeFS patch +// files (.ips, .pchtxt) for the game's main are patch sets the setup builds into the game (tools/exefs_patches.py), +// switched with their mod (mods_exefs_switch). The list is scanned once at start (romfs.c overlay_init); +// its order and switches live in totk.ini [mods] (TOTK_MODS_ORDER: names top first, separated by '|'; +// TOTK_MODS_DISABLED: names switched off). The top mod wins a file two mods replace. +#pragma once +#include <stdint.h> + +enum { MOD_ROMFS = 1, MOD_EXEFS = 2, MOD_SUBSDK = 4, MOD_CHEATS = 8 }; // ModInfo.kind bits +typedef enum { + MOD_OK, + MOD_EMPTY, // no RomFS file, no ExeFS patch, no cheat file + MOD_PLUGIN, // has code modules (exefs/subsdk*, a replaced exefs/main): the port has no module loader, so the + // whole mod stays off (its RomFS files may expect that code) + MOD_EXEFS_LATER, // has ExeFS patches for this game's main that the game was built without: the rebuild at the + // next start builds them in; the whole mod stays off until then + MOD_EXEFS_UNSUPPORTED, // an ExeFS patch file the port cannot apply (note says why): the whole mod stays off + MOD_EXEFS_OTHER, // only ExeFS patches, all for another game version + MOD_BAD_NAME, // its folder name holds '|', the separator of the order keys +} ModProblem; + +typedef struct { + char name[256]; // folder name, UTF-8 + char romfs[2048]; // UTF-8 path of its RomFS root; "" none + char exefs[2048]; // UTF-8 path of its ExeFS folder; "" none + char cheats[2048]; // UTF-8 path of its cheats folder; "" none + int kind, enabled; + ModProblem problem; + uint32_t replaced, added, overridden; // romfs.c: files served (replacing / added), files a higher mod serves + uint32_t other_version; // files for another game version (RomFS *.Product.<n>.*, ExeFS patches + // for another build), not used + char overridden_by[256]; // the first higher mod that took one of its files + char note[256]; // MOD_EXEFS_UNSUPPORTED: why + uint32_t pending, pending_key; // ExeFS patch files the build lacks; FNV-1a of their names and contents + int nsets; + uint32_t sets[16]; // the built patch sets mod_<hash> of its ExeFS patch files + char overrides[256]; // the port's own patch sets its patches replace, comma separated +} ModInfo; + +void mods_scan(void); // once; later calls return at once +const char *mods_dir(void); // the folder scanned (default: <dir of totk.exe>/mods) +int mods_count(void); +ModInfo *mods_at(int i); // 0 = top of the order; NULL out of range +// Menu edits (present path). They change the list mods_at shows and write TOTK_MODS_ORDER / TOTK_MODS_DISABLED with +// cfg_set; romfs.c used the start state, so they apply at the next start. +void mods_set_enabled(int i, int on); +void mods_move(int i, int delta); // -1 up, +1 down; nothing at the ends +int mods_changed(void); // 1 when the order or a switch differs from the start +// After rt_patches_init: the patch sets of the enabled mods' ExeFS patches on, the port's own sets they replace off. +void mods_exefs_switch(void); +// The ExeFS patch files of the enabled mods that the build lacks, their FNV-1a mixed into *key (cheats.c starts the +// rebuild for them). +int mods_exefs_pending(uint32_t *key); +// The hex of the game main's build id (64 digits), "" when exefs/main cannot be read. +const char *mods_main_build_id(void); diff --git a/runtime/mods_probe.c b/runtime/mods_probe.c new file mode 100644 index 00000000..221cb2d7 --- /dev/null +++ b/runtime/mods_probe.c @@ -0,0 +1,130 @@ +// --probe-mods: the mods folder scan, its order and switches (mods.c). Builds a mods folder of its own next to totk.exe, +// scans it and removes it. Needs no game files. +#include <stdio.h> +#include <string.h> + +#include "cfg.h" +#include "host.h" +#include "mods.h" +#include "platform/platform.h" + +static int fails; +#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL mods: %s\n", what); } } while (0) +#define EQ(a, b) ((a) && !strcmp((a), (b))) + +// root/rel as a file of a few bytes (rel ends in '/': a folder), with every folder above it. +static void put(const char *root, const char *rel) { + char p[2048]; + snprintf(p, sizeof p, "%s/%s", root, rel); + char *slash = strrchr(p, '/'); + *slash = 0; + plat_file_mkdir_p(p); + *slash = '/'; + if (slash[1]) plat_file_write(p, "probe", 5); +} + +static int remove_cb(const char *name, int kind, uint64_t size, void *ctx); +static void remove_tree(const char *dir) { + plat_fs_list(dir, remove_cb, (void *)dir); + plat_fs_rmdir(dir); +} +static int remove_cb(const char *name, int kind, uint64_t size, void *ctx) { + (void)size; + char sub[2048]; + snprintf(sub, sizeof sub, "%s/%s", (const char *)ctx, name); + if (kind == PLAT_FS_DIR) remove_tree(sub); + else plat_fs_unlink(sub); + return 1; +} + +// s ends in tail, either path separator matching either. +static int ends_with(const char *s, const char *tail) { + size_t n = strlen(s), t = strlen(tail); + if (n < t) return 0; + for (size_t i = 0; i < t; i++) { + char a = s[n - t + i], b = tail[i]; + if (a == '\\') a = '/'; + if (b == '\\') b = '/'; + if (a != b) return 0; + } + return 1; +} + +int probe_mods(void) { + char root[1024]; + if (!plat_exe_path(root, sizeof root)) return 1; + char *cut = strrchr(root, '/'), *back = strrchr(root, '\\'); + if (back > cut) cut = back; + if (!cut) return 1; + strcpy(cut + 1, "mods-probe"); + remove_tree(root); + plat_file_mkdir_p(root); + static const char *const files[] = { + "A/romfs/x.bin", "B/atmosphere/contents/0100F2C0115B6000/romfs/x.bin", + "C/contents/0100F2C0115B6000/romfs/y.bin", "D/exefs/main.pchtxt", "E/exefs/subsdk9", "F/romfs/", + "G/romfs/z.bin", "G/exefs/1234.ips", "Ultra \xD0\x96/romfs/u.bin", + "H/romfs/h.bin", "H/exefs/subsdk9", "I/romfs/i.bin", "I/exefs/main", "J/cheats/0123456789ABCDEF.txt", + "K/romfslite/k.bin", "L/romfs/l.bin", "L/cheats/0123456789ABCDEF.txt", "L/exefs/main.npdm", + }; + for (size_t i = 0; i < sizeof files / sizeof *files; i++) put(root, files[i]); + // an IPS file for this game's main (any file counts as one when main cannot be read), in Atmosphere's layout + char ips[160]; + snprintf(ips, sizeof ips, "M/atmosphere/exefs_patches/x/%s.ips", *mods_main_build_id() ? mods_main_build_id() : "0"); + put(root, ips); + + cfg_set("TOTK_MODS_DIR", root); + cfg_set("TOTK_MODS_ORDER", "B|A|Gone"); + cfg_set("TOTK_MODS_DISABLED", "C"); + mods_scan(); + + static const char *const order[] = {"B", "A", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", + "Ultra \xD0\x96"}; + enum { N = sizeof order / sizeof *order }; + CHECK(mods_count() == N, "fourteen mod folders"); + int in_order = mods_count() == N; + for (int i = 0; in_order && i < N; i++) in_order = EQ(mods_at(i)->name, order[i]); + CHECK(in_order, "order: mods_order first, the rest by name"); + if (mods_count() == N && in_order) { + ModInfo *a = mods_at(1), *b = mods_at(0), *c = mods_at(2), *d = mods_at(3), *e = mods_at(4), *f = mods_at(5), + *g = mods_at(6), *hm = mods_at(7), *im = mods_at(8), *jm = mods_at(9), *km = mods_at(10), + *lm = mods_at(11), *mm = mods_at(12), *u = mods_at(13); + CHECK(hm->kind == (MOD_ROMFS | MOD_SUBSDK) && hm->problem == MOD_PLUGIN, "RomFS + subsdk plugin: unusable"); + CHECK(im->kind == (MOD_ROMFS | MOD_SUBSDK) && im->problem == MOD_PLUGIN, "RomFS + replaced exefs/main"); + CHECK(ends_with(b->romfs, "B/atmosphere/contents/0100F2C0115B6000/romfs"), "Atmosphere RomFS root"); + CHECK(ends_with(c->romfs, "C/contents/0100F2C0115B6000/romfs"), "contents RomFS root"); + CHECK(ends_with(a->romfs, "A/romfs") && a->kind == MOD_ROMFS && a->problem == MOD_OK, "plain RomFS mod"); + CHECK(!c->enabled && a->enabled && b->enabled && g->enabled && u->enabled, "mods_disabled switches C off"); + CHECK(d->kind == MOD_EXEFS && d->problem == MOD_EXEFS_OTHER && d->other_version == 1, + "ExeFS patch mod without a build id: another version's"); + CHECK(e->problem == MOD_PLUGIN, "subsdk-only mod is a plugin mod"); + CHECK(f->problem == MOD_EMPTY, "empty RomFS folder"); + CHECK(g->kind == (MOD_ROMFS | MOD_EXEFS) && g->problem == MOD_OK && g->other_version == 1, + "RomFS + another version's ExeFS patch: the RomFS is used"); + CHECK(mm->kind == MOD_EXEFS && mm->problem == MOD_EXEFS_LATER && mm->pending == 1, + "an ExeFS patch for main the build lacks waits for the rebuild"); + CHECK(jm->kind == MOD_CHEATS && jm->problem == MOD_OK && ends_with(jm->cheats, "J/cheats"), + "cheats-only mod"); + CHECK(km->kind == MOD_ROMFS && km->problem == MOD_OK && ends_with(km->romfs, "K/romfslite"), "TKMM romfslite"); + CHECK(lm->kind == (MOD_ROMFS | MOD_CHEATS) && lm->problem == MOD_OK, "RomFS + cheats, main.npdm is no code"); + CHECK(u->kind == MOD_ROMFS && ends_with(u->romfs, "Ultra \xD0\x96/romfs"), "Unicode folder name"); + CHECK(!mods_changed(), "nothing changed after the scan"); + + mods_move(1, -1); + CHECK(EQ(mods_at(0)->name, "A") && EQ(mods_at(1)->name, "B"), "move up"); + CHECK(EQ(cfg_get("TOTK_MODS_ORDER"), "A|B|C|D|E|F|G|H|I|J|K|L|M|Ultra \xD0\x96"), + "order written without the gone name"); + mods_move(0, -1); + CHECK(EQ(mods_at(0)->name, "A"), "move up at the top does nothing"); + mods_move(N - 1, 1); + CHECK(EQ(mods_at(N - 1)->name, "Ultra \xD0\x96"), "move down at the bottom does nothing"); + mods_set_enabled(2, 1); + const char *dis = cfg_get("TOTK_MODS_DISABLED"); + CHECK(mods_at(2)->enabled && (!dis || !*dis), "switching C on empties mods_disabled"); + mods_set_enabled(0, 0); + CHECK(EQ(cfg_get("TOTK_MODS_DISABLED"), "A"), "switching A off writes it"); + CHECK(mods_changed(), "changes seen"); + } + remove_tree(root); + printf("mods: %s\n", fails ? "FAIL" : "ok"); + return fails ? 1 : 0; +} diff --git a/runtime/nfp.c b/runtime/nfp.c new file mode 100644 index 00000000..04640fb3 --- /dev/null +++ b/runtime/nfp.c @@ -0,0 +1,230 @@ +// Horizon OS nfp:user IPC service implementation (nn::nfp::detail::IUserManager & IUser). +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "amiibo.h" +#include "host.h" +#include "ipc.h" +#include "kernel.h" +#include "nfp.h" + +// Simulated NFC device handle: ID 1 +static const uint8_t g_device_handle[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; + +typedef struct { + uint32_t activate_event; // writable handle + uint32_t deactivate_event; // writable handle + uint32_t avail_event; // writable handle +} NfpUserState; + +static void nfp_user_close(void *p) { + NfpUserState *st = p; + if (st) { + if (st->activate_event) kern_handle_close(st->activate_event); + if (st->deactivate_event) kern_handle_close(st->deactivate_event); + if (st->avail_event) kern_handle_close(st->avail_event); + free(st); + } + rt_log("nfp: IUser session closed"); +} + +// Handler for nn::nfp::detail::IUser +static void nfp_user_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) { + NfpUserState *st = (NfpUserState *)s->state; + + switch (rq->cmd) { + case 0: { // InitializeNfp(u64 aruid, u64 m_pid, pid, buffer<unknown, 5>) + rt_log("nfp: Initialize"); + rs->result = 0; + return; + } + case 1: { // FinalizeNfp() + rt_log("nfp: Finalize"); + amiibo_stop_detection(); + amiibo_unmount(); + rs->result = 0; + return; + } + case 2: { // ListDevicesNfp() -> (s32 total_out, buffer<DeviceHandle, 0xa>) + int32_t count = amiibo_is_device_present() ? 1 : 0; + IPC_OUT(rs, count); + if (count > 0 && rq->nb > 0 && rq->b[0].size >= sizeof(g_device_handle)) { + void *dst = ipc_guest(rq->b[0].addr, sizeof(g_device_handle)); + memcpy(dst, g_device_handle, sizeof(g_device_handle)); + } + rs->result = 0; + return; + } + case 3: { // StartDetectionNfp(DeviceHandle) + amiibo_start_detection(); + if (amiibo_has_tag() && st && st->activate_event) { + kern_event_signal(st->activate_event); + } + rs->result = 0; + return; + } + case 4: { // StopDetectionNfp(DeviceHandle) + amiibo_stop_detection(); + rs->result = 0; + return; + } + case 5: { // MountNfp(DeviceHandle, u32 ModelType, u32 MountTarget) + if (!amiibo_has_tag()) { + rs->result = 0x8073u; // Device/Tag not found + return; + } + amiibo_mount(); + rs->result = 0; + return; + } + case 6: { // UnmountNfp(DeviceHandle) + amiibo_unmount(); + if (st && st->deactivate_event) { + kern_event_signal(st->deactivate_event); + } + rs->result = 0; + return; + } + case 7: { // OpenApplicationAreaNfp(DeviceHandle, u32 access_id) + if (!amiibo_is_mounted()) { + rs->result = 0x927Fu; // not mounted + return; + } + rs->result = 0; + return; + } + case 8: { // GetApplicationAreaNfp(DeviceHandle) -> (u32 size, buffer<data, 0x6>) + if (!amiibo_is_mounted()) { + rs->result = 0x927Fu; + return; + } + uint32_t area_size = 0xD8; + if (rq->nb > 0 && rq->b[0].size > 0) { + void *dst = ipc_guest(rq->b[0].addr, rq->b[0].size); + area_size = (uint32_t)amiibo_get_application_area(dst, (uint32_t)rq->b[0].size); + } + IPC_OUT(rs, area_size); + rs->result = 0; + return; + } + case 13: { // GetTagInfoNfp(DeviceHandle) -> buffer<TagInfo, 0x1a> + if (!amiibo_has_tag() && !amiibo_is_mounted()) { + rs->result = 0x8073u; + return; + } + if (rq->nb > 0 && rq->b[0].size >= 0x58) { + void *dst = ipc_guest(rq->b[0].addr, 0x58); + amiibo_get_tag_info(dst); + } + rs->result = 0; + return; + } + case 14: { // GetRegisterInfoNfp(DeviceHandle) -> buffer<RegisterInfo, 0x1a> + if (!amiibo_is_mounted()) { + rs->result = 0x927Fu; + return; + } + if (rq->nb > 0 && rq->b[0].size >= 0x100) { + void *dst = ipc_guest(rq->b[0].addr, 0x100); + amiibo_get_register_info(dst); + } + rs->result = 0; + return; + } + case 15: { // GetCommonInfoNfp(DeviceHandle) -> buffer<CommonInfo, 0x1a> + if (!amiibo_is_mounted()) { + rs->result = 0x927Fu; + return; + } + if (rq->nb > 0 && rq->b[0].size >= 0x40) { + void *dst = ipc_guest(rq->b[0].addr, 0x40); + amiibo_get_common_info(dst); + } + rs->result = 0; + return; + } + case 16: { // GetModelInfoNfp(DeviceHandle) -> buffer<ModelInfo, 0x1a> + if (!amiibo_is_mounted()) { + rs->result = 0x927Fu; + return; + } + if (rq->nb > 0 && rq->b[0].size >= 0x40) { + void *dst = ipc_guest(rq->b[0].addr, 0x40); + amiibo_get_model_info(dst); + } + rs->result = 0; + return; + } + case 17: { // GetActivateEventHandleNfp(DeviceHandle) -> handle<copy> + if (st && st->activate_event) { + rs->copy[rs->ncopy++] = kern_event_readable(st->activate_event); + } + rs->result = 0; + return; + } + case 18: { // GetDeactivateEventHandleNfp(DeviceHandle) -> handle<copy> + if (st && st->deactivate_event) { + rs->copy[rs->ncopy++] = kern_event_readable(st->deactivate_event); + } + rs->result = 0; + return; + } + case 19: { // GetStateNfp() -> u32 state + uint32_t state = 1; // Initialized + IPC_OUT(rs, state); + rs->result = 0; + return; + } + case 20: { // GetDeviceStateNfp(DeviceHandle) -> u32 device_state + uint32_t dev_state = (uint32_t)amiibo_get_device_state(); + IPC_OUT(rs, dev_state); + rs->result = 0; + return; + } + case 21: { // GetNpadIdNfp(DeviceHandle) -> u32 npad_id + uint32_t npad_id = 0; // Handheld / Player 1 + IPC_OUT(rs, npad_id); + rs->result = 0; + return; + } + case 22: { // GetApplicationAreaSizeNfp(DeviceHandle) -> u32 size + uint32_t size = 0xD8; + IPC_OUT(rs, size); + rs->result = 0; + return; + } + case 23: { // GetAvailabilityChangeEventHandleNfp() -> handle<copy> + if (st && st->avail_event) { + rs->copy[rs->ncopy++] = kern_event_readable(st->avail_event); + } + rs->result = 0; + return; + } + default: + ipc_unimplemented(s, rq); + } +} + +// Handler for nn::nfp::detail::IUserManager (nfp:user) +void nfp_user_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs) { + switch (rq->cmd) { + case 0: { // CreateUserInterface() -> move handle to IUser + NfpUserState *st = calloc(1, sizeof(*st)); + if (!st) rt_fatal("nfp: failed to allocate user state"); + st->activate_event = kern_event_new(); + st->deactivate_event = kern_event_new(); + st->avail_event = kern_event_new(); + // Signal availability immediately so game detects reader + kern_event_signal(st->avail_event); + + uint32_t user_session = ipc_session_new("nfp:IUser", nfp_user_handler, st, nfp_user_close); + rs->move[rs->nmove++] = user_session; + rs->result = 0; + rt_log("nfp: created IUser session handle %#x", user_session); + return; + } + default: + ipc_unimplemented(s, rq); + } +} diff --git a/runtime/nfp.h b/runtime/nfp.h new file mode 100644 index 00000000..e0c5a219 --- /dev/null +++ b/runtime/nfp.h @@ -0,0 +1,14 @@ +// Nintendo Figurine Platform (nfp:user) Horizon OS IPC service emulation. +#pragma once +#include "ipc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// sm: handler for "nfp:user" (nn::nfp::detail::IUserManager) +void nfp_user_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); + +#ifdef __cplusplus +} +#endif diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 9494e259..1dbff42c 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -75,15 +75,114 @@ static void put_hdr(uint8_t *p, uint32_t op, uint32_t size) { memcpy(p, &h, sizeof h); } +// 1.4.3 initializes and finalizes command buffers about 2000 times a second while a scene loads and several times a +// frame in play, and its two largest per-frame recordings take 1-2 MiB. Freeing the recording buffer at Finalize made +// every new recording grow again from 64 KiB through up to five reallocations and copies, the last ones fresh 1-2 MiB +// blocks the heap takes from the system: new commit and zero-filled pages twice a frame. Those growths were most of the +// process's new commit requests, so when the system ran short for a moment (a Depths load at render scale 200 on 16 GB) +// a 1 MiB growth was the allocation that failed. Finalized buffers therefore wait in a small pool for the next +// recording, and a failed allocation waits for memory instead of ending the game (realloc_wait). +#define POOL_SLOTS 16 +#define POOL_MAX_BYTES ((size_t)4 << 20) // a larger buffer (a recording that inlined a big one) goes back to the heap +static PlatMutex pool_lock = PLAT_MUTEX_INIT; +static struct { uint8_t *p; size_t size; } pool[POOL_SLOTS]; +static uint32_t npool; + +// The largest pooled buffer and its size, or NULL: the largest, so the big per-frame recordings stop growing once the +// pool holds enough buffers of their size. +static uint8_t *pool_take(size_t *size) { + uint8_t *p = NULL; + plat_mutex_lock(&pool_lock); + if (npool) { + uint32_t best = 0; + for (uint32_t i = 1; i < npool; i++) + if (pool[i].size > pool[best].size) best = i; + p = pool[best].p; + *size = pool[best].size; + pool[best] = pool[--npool]; + } + plat_mutex_unlock(&pool_lock); + return p; +} + +// Keeps a finalized buffer when the pool has room or holds a smaller one (which is freed instead). +static void pool_put(uint8_t *p, size_t size) { + if (!p) return; + if (size <= POOL_MAX_BYTES) { + plat_mutex_lock(&pool_lock); + if (npool < POOL_SLOTS) { + pool[npool].p = p; + pool[npool++].size = size; + p = NULL; + } else { + uint32_t small = 0; + for (uint32_t i = 1; i < npool; i++) + if (pool[i].size < pool[small].size) small = i; + if (pool[small].size < size) { + uint8_t *drop = pool[small].p; + pool[small].p = p; + pool[small].size = size; + p = drop; + } + } + plat_mutex_unlock(&pool_lock); + } + free(p); +} + +static void pool_drain(void) { + plat_mutex_lock(&pool_lock); + while (npool) free(pool[--npool].p); + plat_mutex_unlock(&pool_lock); +} + +// realloc that waits out a short commit shortage: Windows refuses commit beyond the current limit while it grows a +// system-managed page file, so the same request succeeds a moment later, and a stall of some frames is better than +// ending the game. Frees the pooled buffers first. NULL only after about 30 s without memory; the caller ends the game. +// Never called with a lock held. +static void *realloc_wait(void *old, size_t n, const char *what) { + void *p = realloc(old, n); + if (p) return p; + pool_drain(); + static uint32_t reports; + int report = __atomic_fetch_add(&reports, 1, __ATOMIC_RELAXED) < 8; + if (report) { + double proc, used, limit; + plat_commit_info(&proc, &used, &limit); + rt_log("nvn: %s of %zu bytes failed (memory commit: this process %.0f MB, the system %.0f of %.0f MB); waiting " + "for memory", what, n, proc, used, limit); + } + uint64_t t0 = plat_now_ms(); + while (plat_now_ms() - t0 < 30000) { + plat_sleep_ms(10); + if ((p = realloc(old, n))) { + if (report) + rt_log("nvn: %s of %zu bytes succeeded after %llu ms", what, n, + (unsigned long long)(plat_now_ms() - t0)); + return p; + } + } + return NULL; +} + static void cmd_reserve(cmd_t *o, size_t need) { if (o->seg && o->cur + need + OP_RESERVE <= o->end) return; + if (!o->seg) { // the first record since Initialize: a pooled buffer when one waits + size_t size = 0; + uint8_t *p = pool_take(&size); + if (p) { + o->seg = o->cur = p; + o->end = p + size; + if (need + OP_RESERVE <= size) return; + } + } size_t used = o->seg ? (size_t)(o->cur - o->seg) : 0; size_t cap = o->seg ? (size_t)(o->end - o->seg) : 0; if (!cap) cap = 64 << 10; while (used + need + OP_RESERVE > cap) cap *= 2; - uint8_t *b = realloc(o->seg, cap); - // A block this size fails only without commit (the fatal report states it) or when seg is no block of ours: a - // command buffer object the game overwrote or copied shows a wrong magic or more used than its old size. + uint8_t *b = realloc_wait(o->seg, cap, "command recording buffer"); + // Still failing after the wait: the system has no commit left (the fatal report states it), or seg is no block of + // ours: a command buffer object the game overwrote or copied shows a wrong magic or more used than its old size. if (!b) rt_fatal("nvn: command recording buffer of %zu bytes (command buffer %p, magic %#x, buffer %p of %zu bytes, %zu " "used, %zu asked)", cap, (void *)o, o->magic, (void *)o->seg, o->seg ? (size_t)(o->end - o->seg) : 0, @@ -235,7 +334,7 @@ static uint64_t copy_touch(uint64_t handle, uint64_t now, const char *what) { // into one host block of its exact size. One allocation and one copy: growing the copy record by record cost the // game's main thread about 1 ms a frame in reallocations and small copies (1.4.3, field). static uint8_t *flatten_recording(const uint8_t *p, size_t n) { - uint8_t *out = malloc(sizeof(copy_prefix) + n); + uint8_t *out = realloc_wait(NULL, sizeof(copy_prefix) + n, "command copy"); if (!out) rt_fatal("nvn: command copy allocation"); memcpy(out + sizeof(copy_prefix), p, n); for (size_t at = 0;;) { // the records stay checked: a corrupt one stops here, not in the replay @@ -334,7 +433,7 @@ NVN_API(nvnCommandBufferInitialize) { // void nvnCommandBufferFinalize(NVNcommandBuffer *cmd): handles outlive their recorder NVN_API(nvnCommandBufferFinalize) { cmd_t *o = cmd_of(c); - free(o->seg); + pool_put(o->seg, (size_t)(o->end - o->seg)); o->seg = o->cur = o->end = NULL; o->magic = 0; cb_set_disabled(o, CB_NO_COMMAND | CB_NO_CONTROL, 0); diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index f9d50e59..bca459ac 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -197,6 +197,7 @@ NVN_API(nvnTextureInitialize) { o->magic = NVN_TEX_MAGIC; o->d = s->d; o->pool = s->pool; + o->pool_flags = s->pool ? (uint16_t)s->pool->flags : 0; o->off = s->off; o->has_layout = s->has_layout; memcpy(o->layout, s->layout, 8); @@ -323,8 +324,11 @@ NVN_API(nvnTextureReadTexelsStrided) { read_texels(c, 1); } NVN_API(nvnTextureWriteTexels) { write_texels(c, 0); } NVN_API(nvnTextureWriteTexelsStrided) { write_texels(c, 1); } // void nvnTexture{Flush,Invalidate}Texels(const NVNtexture *t, const NVNtextureView *v, const NVNcopyRegion *r): texel -// access is coherent -NVN_API(nvnTextureFlushTexels) { (void)c; } +// access is coherent; flush marks the storage as written for re-upload +NVN_API(nvnTextureFlushTexels) { + const tex_t *t = arg_p(c, 0); + if (t && t->mem && !nvn_gpu_only((uintptr_t)t->mem)) nvn_guest_written((uintptr_t)t->mem, t->L.size); +} NVN_API(nvnTextureInvalidateTexels) { (void)c; } // ---------- Samplers @@ -633,7 +637,7 @@ NVN_API(nvnWindowSetCrop) { // ---------- Present pacing // The vsync thread ticks at a fixed 60 Hz whatever the monitor does, so one game frame lasts `interval` ticks. With // the real-time frame step (frame_rate.c) game speed does not depend on the interval, so a frame is shown when it is -// ready, at most at the frame limit (TOTK_FPS_LIMIT: 30 to 360 per second or the monitor's rate): holding it to the +// ready, at most at the frame limit (TOTK_FPS_LIMIT: 30 to 240 per second or the monitor's rate): holding it to the // 2nd or 3rd tick would only add latency and 50 ms frames. #define VSYNC_HZ 60ull static uint64_t present_period(const win_t *o) { @@ -695,6 +699,7 @@ NVN_API(nvnQueuePresentTexture) { win_t *o = arg_p(c, 1); int idx = (int)arg_x(c, 2); movie_tick(c); // ov_movie.c: hand over decoded movie frames that are due; needs the core, so before the wait + res_present(c); // resolution.c: a DLSS mode change sets the game's render size again, between frames NVN_BLOCKING(); if (o->magic != NVN_WIN_MAGIC || idx < 0 || idx >= o->ntex || idx >= 4 || !o->tex[idx]) rt_fatal("nvn: QueuePresentTexture of image %d on window %p", idx, (void *)o); diff --git a/runtime/nvn_obj.h b/runtime/nvn_obj.h index 662ba149..7b81c6ec 100644 --- a/runtime/nvn_obj.h +++ b/runtime/nvn_obj.h @@ -48,6 +48,9 @@ typedef struct { } tex_builder_t; FITS(tex_builder_t, NVNtextureBuilder); typedef struct { uint32_t magic; nvn_texdesc d; uint8_t has_layout, layout[8]; + uint16_t pool_flags; // the pool's flags at Initialize (in the padding): a game may finalize and free + // the pool object while the back end still looks the texture up, and the + // driver keeps no pointer to it either nvn_texlayout L; uint8_t *mem; pool_t *pool; int64_t off; uint64_t serial; // Initialization serial: the back end uses it to find its texture in the host table (the game may copy the object by value, so object pointers cannot be stored) } tex_t; FITS(tex_t, NVNtexture); diff --git a/runtime/ov_probe.c b/runtime/ov_probe.c index 9a08a3e5..3d1a4327 100644 --- a/runtime/ov_probe.c +++ b/runtime/ov_probe.c @@ -12,6 +12,7 @@ #include <string.h> #include <time.h> +#include "amiibo.h" #include "cfg.h" #include "fs.h" #include "gfx.h" @@ -25,6 +26,7 @@ #define CHILD_ENV "TOTK_PROBE_SERVICES_CHILD" #define TRY_WAIT_SYSTEM_EVENT "_ZN2nn2os18TryWaitSystemEventEPNS0_15SystemEventTypeE" +#define DESTROY_SYSTEM_EVENT "_ZN2nn2os18DestroySystemEventEPNS0_15SystemEventTypeE" typedef struct { const char *key, *name; int fatal; uint64_t addr; int bound, calls; } Ov; static Ov ovs[] = { @@ -348,6 +350,13 @@ static int event_works(uint64_t ev, int autoclear) { return !before && after && again == !autoclear; } +// A manual-clear system event an override signalled when binding it: set, and still set on a second wait. +static int event_set(uint64_t ev) { + static uint64_t try_wait; + if (!try_wait && !(try_wait = ldr_symbol(TRY_WAIT_SYSTEM_EVENT))) rt_fatal("probe-services: no %s", TRY_WAIT_SYSTEM_EVENT); + return (uint8_t)rt_call(gc, try_wait, 1, ev) && (uint8_t)rt_call(gc, try_wait, 1, ev); +} + static void probe_applet(void) { uint8_t *report = guest_alloc(0x100); uint64_t uid[2] = {1, 2}; @@ -357,21 +366,65 @@ static void probe_applet(void) { "AddInternetConnectionStatus"); CHECK(CALL("nfp.Initialize", 0) == 0 && (uint32_t)CALL("nfp.GetState", 0) == 1, "nfp initialize"); uint8_t handles[64]; - int32_t count = 7; - CHECK((uint32_t)CALL("nfp.ListDevices", U(handles), U(&count), 4) == 0x8073 && count == 0, "nfp ListDevices"); + int32_t count = 0; + CHECK((uint32_t)CALL("nfp.ListDevices", U(handles), U(&count), 4) == 0 && count == 1, "nfp ListDevices"); uint64_t ev = U(guest_alloc(0x100)); CHECK(CALL("nfp.AttachAvailabilityChangeEvent", ev) == 0, "AttachAvailabilityChangeEvent"); - int nfp_event = event_works(ev, 0); - CHECK(nfp_event, "the nfp availability event does not work as a manual-clear system event"); + // The emulated reader is there from the start (amiibo.c), so the availability event is set when it is attached. + int nfp_event = event_set(ev); + CHECK(nfp_event, "the nfp availability event is not set for the emulated reader, or clears itself"); CHECK((uint32_t)CALL("nfp.GetDeviceState", U(handles)) == 0, "nfp device state"); - static const char *const none[] = {"nfp.Mount", "nfp.GetNpadId", "nfp.AttachActivateEvent", "nfp.AttachDeactivateEvent", - "nfp.StartDetection", "nfp.StopDetection", "nfp.GetTagInfo", "nfp.GetModelInfo"}; + static const char *const none[] = {"nfp.Mount", "nfp.GetTagInfo", "nfp.GetModelInfo"}; uint8_t out[0x100]; for (size_t i = 0; i < sizeof none / sizeof *none; i++) { - int first_is_handle = !strcmp(none[i], "nfp.Mount") || strstr(none[i], "Detection"); + int first_is_handle = !strcmp(none[i], "nfp.Mount"); uint64_t r = first_is_handle ? CALL(none[i], U(handles), 0, 0) : CALL(none[i], U(out), U(handles)); CHECK((uint32_t)r == 0x8073, "%s: %#x, not device-not-found", none[i], (uint32_t)r); } + CHECK((uint32_t)CALL("nfp.GetNpadId", U(out), U(handles)) == 0, "nfp GetNpadId"); + uint64_t activate = U(guest_alloc(0x100)), deactivate = U(guest_alloc(0x100)); + CHECK(CALL("nfp.AttachActivateEvent", activate, U(handles)) == 0, "nfp AttachActivateEvent"); + CHECK(CALL("nfp.AttachDeactivateEvent", deactivate, U(handles)) == 0, "nfp AttachDeactivateEvent"); + CHECK(event_works(activate, 0), "the nfp activate event is set without an amiibo on the reader"); + CHECK((uint32_t)CALL("nfp.StartDetection", U(handles)) == 0, "nfp StartDetection"); + CHECK((uint32_t)CALL("nfp.StopDetection", U(handles)) == 0, "nfp StopDetection"); + // The game attaches again before each detection: DestroySystemEvent on the attached event, then Attach* on the + // same SystemEventType. Both events must work again afterwards. + static uint64_t destroy; + if (!destroy && !(destroy = ldr_symbol(DESTROY_SYSTEM_EVENT))) rt_fatal("probe-services: no %s", DESTROY_SYSTEM_EVENT); + rt_call(gc, destroy, 1, activate); + rt_call(gc, destroy, 1, deactivate); + CHECK(CALL("nfp.AttachActivateEvent", activate, U(handles)) == 0, "nfp AttachActivateEvent again"); + CHECK(CALL("nfp.AttachDeactivateEvent", deactivate, U(handles)) == 0, "nfp AttachDeactivateEvent again"); + int reattached = event_works(activate, 0) && event_works(deactivate, 0); + CHECK(reattached, "the nfp events do not work after DestroySystemEvent and a second Attach"); + CHECK((uint32_t)CALL("nfp.StartDetection", U(handles)) == 0, "nfp StartDetection again"); + // An amiibo's NTAG215 header: UID with its two check bytes, static lock 0F E0, capability container F1 10 FF EE. + // The loader takes it and refuses the same file with a plain NDEF container (E1). + static const uint8_t head[16] = {0x04, 0x11, 0x22, 0x88 ^ 0x04 ^ 0x11 ^ 0x22, 0x33, 0x44, 0x55, 0x66, + 0x33 ^ 0x44 ^ 0x55 ^ 0x66, 0x48, 0x0F, 0xE0, 0xF1, 0x10, 0xFF, 0xEE}; + uint8_t dump[AMIIBO_DUMP_SIZE] = {0}; + memcpy(dump, head, sizeof head); + // Link (Archer): character 0x0100, variant 0, figure type 0, model number 0x0353, series 9, format 02 + static const uint8_t id[8] = {0x01, 0x00, 0x00, 0x00, 0x03, 0x53, 0x09, 0x02}; + memcpy(dump + 0x54, id, sizeof id); + const char *dump_path = "probe-amiibo.bin"; + int amiibo_ok = plat_file_write(dump_path, dump, sizeof dump) && amiibo_load_file(dump_path); + // TagInfo carries the 7-byte UID without its check byte; ModelInfo has the series at 3, the model number + // little-endian at 4 and the figure type at 6 + uint8_t ti[0x58], mi[0x40]; + amiibo_get_tag_info(ti); + amiibo_get_model_info(mi); + static const uint8_t want_uid[7] = {0x04, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; + static const uint8_t want_model[7] = {0x01, 0x00, 0x00, 0x09, 0x53, 0x03, 0x00}; + CHECK(amiibo_ok && !memcmp(ti, want_uid, 7) && ti[0x0A] == 7, "amiibo TagInfo UID %02x %02x %02x %02x %02x %02x %02x", + ti[0], ti[1], ti[2], ti[3], ti[4], ti[5], ti[6]); + CHECK(amiibo_ok && !memcmp(mi, want_model, 7), "amiibo ModelInfo %02x %02x %02x %02x %02x %02x %02x", mi[0], mi[1], + mi[2], mi[3], mi[4], mi[5], mi[6]); + dump[0x0C] = 0xE1; + amiibo_ok = amiibo_ok && plat_file_write(dump_path, dump, sizeof dump) && !amiibo_load_file(dump_path); + plat_fs_unlink(dump_path); + CHECK(amiibo_ok, "the amiibo loader refuses a dump with the amiibo capability container, or takes a plain NDEF one"); CHECK(CALL("nfp.Finalize", 0) == 0 && (uint32_t)CALL("nfp.GetState", 0) == 0, "nfp finalize"); CALL("err.ShowError", 0x2a8); CALL("nndiagStartup", 0); @@ -380,8 +433,9 @@ static void probe_applet(void) { CALL("err.InitializeContextWriterApi", 0); CALL("pctl.Initialize", 0); CALL("diag.InitializeApplicationAbortObserver", 0); - printf("applet/prepo/nfp/err: launch parameter none, volume balance, play reports local, no NFC device, " - "availability event (bound through the SDK's CreateSystemEvent) %s\n", nfp_event ? "ok" : "FAILED"); + printf("applet/prepo/nfp/err: launch parameter none, volume balance, play reports local, an NFC reader without " + "an amiibo, availability event (bound through the SDK's CreateSystemEvent) %s, activate and deactivate events " + "destroyed and attached again %s\n", nfp_event ? "ok" : "FAILED", reattached ? "ok" : "FAILED"); } static void probe_account(void) { @@ -811,17 +865,23 @@ static void probe_fonts(void) { (unsigned long long)size, path); } -// The host catalog (ui_text.c): every menu id has its own English and Russian text, the software keyboard keeps its -// game-language rows, and menu_text follows TOTK_MENU_LANG. +// The host catalog (ui_text.c): every menu id has its own English, Russian, Brazilian Portuguese and Simplified +// Chinese text, the software keyboard keeps its game-language rows, and menu_text follows TOTK_MENU_LANG. static void probe_menu_text(void) { - static const char *const langs[] = {"en", "ja", "fr", "de", "it", "es", "nl", "pt", "ru", "zh-Hans", "zh-Hant", "ko"}; + static const char *const langs[] = {"en", "ja", "fr", "de", "it", "es", "nl", "pt", "ru", "zh-Hans", "zh-Hant", "ko", + "pt-BR"}; for (int id = UI_MENU_FIRST; id <= UI_MENU_LAST_ID; id++) { - CHECK(ui_text_defined("en", (UiTextId)id) && ui_text_defined("ru", (UiTextId)id), "menu text id %d lacks en or ru", id); - CHECK(*ui_text_in("en", (UiTextId)id) && *ui_text_in("ru", (UiTextId)id), "menu text id %d is empty", id); + CHECK(ui_text_defined("en", (UiTextId)id) && ui_text_defined("ru", (UiTextId)id) && + ui_text_defined("pt-BR", (UiTextId)id), "menu text id %d lacks en, ru or pt-BR", id); + CHECK(*ui_text_in("en", (UiTextId)id) && *ui_text_in("ru", (UiTextId)id) && *ui_text_in("pt-BR", (UiTextId)id), + "menu text id %d is empty", id); // the description pane keeps the whole text and a note after it (option_rows.cpp desc_set) CHECK(strlen(ui_text_in("en", (UiTextId)id)) + 256 < UI_DESC_BYTES && - strlen(ui_text_in("ru", (UiTextId)id)) + 256 < UI_DESC_BYTES, "menu text id %d is too long for the " - "description pane", id); + strlen(ui_text_in("ru", (UiTextId)id)) + 256 < UI_DESC_BYTES && + strlen(ui_text_in("pt-BR", (UiTextId)id)) + 256 < UI_DESC_BYTES && + strlen(ui_text_in("zh-Hans", (UiTextId)id)) + 256 < UI_DESC_BYTES, + "menu text id %d is too long for the description pane", id); + CHECK(ui_text_defined("zh-Hans", (UiTextId)id), "menu text id %d lacks Simplified Chinese", id); } CHECK(!ui_text_defined("en", UI_TEXT_COUNT) && !*ui_text_in("ru", UI_TEXT_COUNT), "an id past the catalog gives text"); // other languages fall back to English for the menu, and keep their own keyboard texts @@ -829,22 +889,49 @@ static void probe_menu_text(void) { CHECK(!strcmp(ui_text_in("ru", UI_SWKBD_OK), "ОК") && !strcmp(ui_text_in("de", UI_SWKBD_CANCEL), "Abbrechen") && !strcmp(ui_text_in("en", UI_SWKBD_OK), "OK"), "keyboard texts changed"); // language names: the same string in every row - for (int id = UI_MENU_LANGNAME_EN; id <= UI_MENU_LANGNAME_KO; id++) + for (int id = UI_MENU_LANGNAME_EN; id <= UI_MENU_LANGNAME_ES_419; id++) for (size_t i = 1; i < sizeof langs / sizeof *langs; i++) CHECK(!strcmp(ui_text_in(langs[i], (UiTextId)id), ui_text_in("en", (UiTextId)id)), "language name %d differs in %s", id, langs[i]); CHECK(!strcmp(ui_text_in("en", UI_MENU_LANGNAME_RU), "Русский"), "Russian is not shown as Russian"); + // game language list: pt-BR only where the build ships USpt; the Switch variants are entries of their own + { + UiGameLang gl[16]; + int n121 = ui_text_game_langs(121, gl, 16), has[16] = {0}; + for (int i = 0; i < n121; i++) CHECK(strcmp(gl[i].code, "pt-BR"), "1.2.1 game language list offers pt-BR"); + int n143 = ui_text_game_langs(143, gl, 16), pt = 0; + for (int i = 0; i < n143; i++) { + pt |= !strcmp(gl[i].code, "pt-BR"); + for (int j = 0; j < i; j++) CHECK(strcmp(gl[i].code, gl[j].code), "duplicate game language %s", gl[i].code); + } + CHECK(pt && n143 == n121 + 1, "1.4.3 game language list lacks pt-BR"); + static const char *const want[] = {"en-GB", "fr-CA", "es-419"}; + for (int w = 0; w < 3; w++) + for (int i = 0; i < n143; i++) has[w] |= !strcmp(gl[i].code, want[w]); + CHECK(has[0] && has[1] && has[2], "game language list lacks en-GB, fr-CA or es-419"); + } // the overlay formats keep their conversions - CHECK(strstr(ui_text_in("ru", UI_MENU_OV_GAME_PCT), "%.0f%%") && strstr(ui_text_in("ru", UI_MENU_OV_MAX_MS), "%.1f"), + CHECK(strstr(ui_text_in("ru", UI_MENU_OV_GAME_PCT), "%.0f%%") && strstr(ui_text_in("ru", UI_MENU_OV_MAX_MS), "%.1f") && + strstr(ui_text_in("ru", UI_MENU_OV_LOW1), "%.0f") && strstr(ui_text_in("ru", UI_MENU_OV_LOW01), "%.0f"), "overlay format lost its conversion"); + CHECK(strstr(ui_text_in("pt-BR", UI_MENU_OV_GAME_PCT), "%.0f%%") && + strstr(ui_text_in("pt-BR", UI_MENU_OV_MAX_MS), "%.1f") && + strstr(ui_text_in("pt-BR", UI_MENU_OV_LOW1), "%.0f") && strstr(ui_text_in("zh-Hans", UI_MENU_OV_LOW01), "%.0f"), "pt-BR overlay format lost its conversion"); + // a Brazilian game language gets the Brazilian keyboard row, Portugal's Portuguese keeps its own + CHECK(!strcmp(ui_text_lang("pt-BR"), "pt-BR") && !strcmp(ui_text_lang("pt"), "pt") && + strcmp(ui_text_in("pt-BR", UI_SWKBD_PROMPT), ui_text_in("pt", UI_SWKBD_PROMPT)), "pt-BR keyboard row not used"); // menu_text follows the setting live cfg_set("TOTK_MENU_LANG", "ru"); CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in("ru", UI_MENU_CONTINUE), "menu_text ignores MENU_LANG=ru"); + cfg_set("TOTK_MENU_LANG", "pt-BR"); + CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in("pt-BR", UI_MENU_CONTINUE), "menu_text ignores MENU_LANG=pt-BR"); cfg_set("TOTK_MENU_LANG", "en"); CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in("en", UI_MENU_CONTINUE), "menu_text did not follow MENU_LANG=en"); + cfg_set("TOTK_MENU_LANG", "zh-Hans"); + CHECK(menu_text(UI_MENU_TITLE) == ui_text_in("zh-Hans", UI_MENU_TITLE), "menu_text ignores MENU_LANG=zh-Hans"); cfg_set("TOTK_MENU_LANG", ""); - const char *sys = plat_ui_language(); - int ru = !strncmp(sys, "ru", 2); - CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in(ru ? "ru" : "en", UI_MENU_CONTINUE), "menu_text auto ignores the system language"); + const char *sys_lang = ui_text_lang(plat_ui_language()); + const char *auto_lang = ui_text_defined(sys_lang, UI_MENU_TITLE) ? sys_lang : "en"; + CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in(auto_lang, UI_MENU_CONTINUE), "menu_text auto ignores the system language"); } static void probe_main(Cpu *c) { diff --git a/runtime/ov_system.c b/runtime/ov_system.c index 4e9b3bc0..5aeabed2 100644 --- a/runtime/ov_system.c +++ b/runtime/ov_system.c @@ -19,6 +19,7 @@ #include <string.h> #include <time.h> +#include "amiibo.h" #include "cfg.h" #include "fs.h" #include "host.h" @@ -266,26 +267,124 @@ static void ov_report_save(Cpu *c) { // Result PlayReport::Save() and Save(con } static void ov_result_ok(Cpu *c) { ret_x(c, 0); } // RequestImmediateTransmission, AddInternetConnectionStatus -// nn::nfp: no NFC reader +// nn::nfp: NFC reader & amiibo support static volatile int32_t nfp_state; static void ov_nfp_initialize(Cpu *c) { plat_xchg32(&nfp_state, 1); ret_x(c, 0); } -static void ov_nfp_finalize(Cpu *c) { plat_xchg32(&nfp_state, 0); ret_x(c, 0); } +static void ov_nfp_finalize(Cpu *c) { + plat_xchg32(&nfp_state, 0); + amiibo_stop_detection(); + amiibo_unmount(); + ret_x(c, 0); +} static void ov_nfp_state(Cpu *c) { ret_x(c, (uint64_t)nfp_state); } + // Result ListDevices(DeviceHandle *out, int *count, int max) static void ov_nfp_list(Cpu *c) { + void *out = arg_p(c, 0); int32_t *count = arg_p(c, 1); - if (count) *count = 0; - ret_x(c, NFP_R_DEVICE_NOT_FOUND); + int32_t max = (int32_t)arg_x(c, 2); + if (!amiibo_is_device_present()) { + if (count) *count = 0; + ret_x(c, NFP_R_DEVICE_NOT_FOUND); + return; + } + if (out && max > 0) { + static const uint8_t dev[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; + memcpy(out, dev, sizeof dev); + } + if (count) *count = 1; + ret_x(c, 0); } + // Result AttachAvailabilityChangeEvent(os::SystemEventType *event) static void ov_nfp_availability(Cpu *c) { uint64_t ev = arg_x(c, 0); if (!ev) rt_fatal("nn::nfp::AttachAvailabilityChangeEvent: null event"); kern_bind_system_event(c, ev, 0); + kern_signal_system_event(ev); ret_x(c, 0); } + +// DeviceState GetDeviceState(const DeviceHandle &) +static void ov_nfp_device_state(Cpu *c) { + (void)c; + ret_x(c, (uint64_t)amiibo_get_device_state()); +} + +// Result Mount(const DeviceHandle &, ModelType, MountTarget) +static void ov_nfp_mount(Cpu *c) { + (void)c; + if (!amiibo_has_tag()) { + ret_x(c, NFP_R_DEVICE_NOT_FOUND); + return; + } + amiibo_mount(); + ret_x(c, 0); +} + +// Result GetNpadId(u32 *npad_id, const DeviceHandle &) +static void ov_nfp_get_npad_id(Cpu *c) { + uint32_t *npad = arg_p(c, 0); + if (npad) *npad = 0; // Handheld / Player 1 + ret_x(c, 0); +} + +// Result AttachActivateEvent(os::SystemEventType *event, const DeviceHandle &) +static void ov_nfp_attach_activate(Cpu *c) { + uint64_t ev = arg_x(c, 0); + if (ev) { + kern_bind_system_event(c, ev, 0); + if (amiibo_has_tag()) kern_signal_system_event(ev); + } + ret_x(c, 0); +} + +// Result AttachDeactivateEvent(os::SystemEventType *event, const DeviceHandle &) +static void ov_nfp_attach_deactivate(Cpu *c) { + uint64_t ev = arg_x(c, 0); + if (ev) { + kern_bind_system_event(c, ev, 0); + } + ret_x(c, 0); +} + +// Result StartDetection(const DeviceHandle &) +static void ov_nfp_start_detection(Cpu *c) { + (void)c; + amiibo_start_detection(); + ret_x(c, 0); +} + +// Result StopDetection(const DeviceHandle &) +static void ov_nfp_stop_detection(Cpu *c) { + (void)c; + amiibo_stop_detection(); + ret_x(c, 0); +} + +// Result GetTagInfo(TagInfo *out, const DeviceHandle &) +static void ov_nfp_get_tag_info(Cpu *c) { + void *out = arg_p(c, 0); + if (!amiibo_has_tag() && !amiibo_is_mounted()) { + ret_x(c, NFP_R_DEVICE_NOT_FOUND); + return; + } + if (out) amiibo_get_tag_info(out); + ret_x(c, 0); +} + +// Result GetModelInfo(ModelInfo *out, const DeviceHandle &) +static void ov_nfp_get_model_info(Cpu *c) { + void *out = arg_p(c, 0); + if (!amiibo_is_mounted() && !amiibo_has_tag()) { + ret_x(c, NFP_R_DEVICE_NOT_FOUND); + return; + } + if (out) amiibo_get_model_info(out); + ret_x(c, 0); +} + static void ov_nfp_no_device(Cpu *c) { ret_x(c, NFP_R_DEVICE_NOT_FOUND); } -static void ov_nfp_device_state(Cpu *c) { ret_x(c, 0); } // DeviceState GetDeviceState(const DeviceHandle &): none // void err::ShowError(Result): the game's error dialog, logged static void ov_show_error(Cpu *c) { @@ -795,14 +894,14 @@ HOST_OVERRIDE("_ZN2nn3nfp8GetStateEv", ov_nfp_state); HOST_OVERRIDE("_ZN2nn3nfp11ListDevicesEPNS0_12DeviceHandleEPii", ov_nfp_list); HOST_OVERRIDE("_ZN2nn3nfp29AttachAvailabilityChangeEventEPNS_2os15SystemEventTypeE", ov_nfp_availability); HOST_OVERRIDE("_ZN2nn3nfp14GetDeviceStateERKNS0_12DeviceHandleE", ov_nfp_device_state); -HOST_OVERRIDE("_ZN2nn3nfp5MountERKNS0_12DeviceHandleENS0_9ModelTypeENS0_11MountTargetE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp9GetNpadIdEPjRKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp19AttachActivateEventEPNS_2os15SystemEventTypeERKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp21AttachDeactivateEventEPNS_2os15SystemEventTypeERKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp14StartDetectionERKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp13StopDetectionERKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp10GetTagInfoEPNS0_7TagInfoERKNS0_12DeviceHandleE", ov_nfp_no_device); -HOST_OVERRIDE("_ZN2nn3nfp12GetModelInfoEPNS0_9ModelInfoERKNS0_12DeviceHandleE", ov_nfp_no_device); +HOST_OVERRIDE("_ZN2nn3nfp5MountERKNS0_12DeviceHandleENS0_9ModelTypeENS0_11MountTargetE", ov_nfp_mount); +HOST_OVERRIDE("_ZN2nn3nfp9GetNpadIdEPjRKNS0_12DeviceHandleE", ov_nfp_get_npad_id); +HOST_OVERRIDE("_ZN2nn3nfp19AttachActivateEventEPNS_2os15SystemEventTypeERKNS0_12DeviceHandleE", ov_nfp_attach_activate); +HOST_OVERRIDE("_ZN2nn3nfp21AttachDeactivateEventEPNS_2os15SystemEventTypeERKNS0_12DeviceHandleE", ov_nfp_attach_deactivate); +HOST_OVERRIDE("_ZN2nn3nfp14StartDetectionERKNS0_12DeviceHandleE", ov_nfp_start_detection); +HOST_OVERRIDE("_ZN2nn3nfp13StopDetectionERKNS0_12DeviceHandleE", ov_nfp_stop_detection); +HOST_OVERRIDE("_ZN2nn3nfp10GetTagInfoEPNS0_7TagInfoERKNS0_12DeviceHandleE", ov_nfp_get_tag_info); +HOST_OVERRIDE("_ZN2nn3nfp12GetModelInfoEPNS0_9ModelInfoERKNS0_12DeviceHandleE", ov_nfp_get_model_info); HOST_OVERRIDE("_ZN2nn3err9ShowErrorENS_6ResultE", ov_show_error); HOST_OVERRIDE("nndiagStartup", ov_diag_startup); diff --git a/runtime/perf.c b/runtime/perf.c index 3db4b6ed..ef04ede5 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -183,6 +183,9 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) { // process memory, and which side holds the frame back. pace: the frame waited for pacing (vsync, the frame limiter) at // least 15% of the window; gpu: the GPU was busy for 80% of the frame interval or more (without timestamps: threads // waited for fences for half the interval or more); cpu otherwise. +// With calibrated timestamps a third line splits the GPU's idle time between timed parts: the part was submitted only +// after the previous one had ended (the GPU waited for the CPU's replay or the game), or it was already queued (untimed +// work such as uploads and presents ran there, or a wait held it). static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, uint64_t n) { PerfGpu g; memset(&g, 0, sizeof g); @@ -205,6 +208,11 @@ static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, rt_log("perf: frame %llu: %s; fence wait per frame: render thread %.2f ms, other threads %.2f ms; %s; ram working set " "%.0f MiB, private %.0f MiB; bound: %s", (unsigned long long)frame, gs, rw, ow, vs, ws, priv, bound); + if (g.have_gpu && g.have_idle) + rt_log("perf: frame %llu: gpu idle per frame %.2f ms between timed parts: %.2f ms waiting for the next submission, " + "%.2f ms with it queued (untimed work or waits); %.1f gaps over 0.5 ms, longest %.2f ms; submit to start avg %.2f ms", + (unsigned long long)frame, g.idle_ns / 1e6 / n, g.starved_ns / 1e6 / n, (g.idle_ns - g.starved_ns) / 1e6 / n, + (double)g.idle_gaps / n, g.max_gap_ns / 1e6, g.gpu_parts ? g.queued_ns / 1e6 / g.gpu_parts : 0.0); } int rt_ab(void) { diff --git a/runtime/perfmon.c b/runtime/perfmon.c index 3a5e2f28..f2f01beb 100644 --- a/runtime/perfmon.c +++ b/runtime/perfmon.c @@ -32,6 +32,14 @@ static float g_ring[PERF_HISTORY]; static uint64_t g_ring_at[PERF_HISTORY]; // when each frame ended static int g_ring_n, g_ring_pos; +// The lows: a histogram of the frame times (perf.c's 0.25 ms bins) over the last LOW_SLICES seconds, kept as one slice +// per second so the oldest second drops out whole. Per frame: one increment. The percentiles are read twice a second. +#define LOW_BINS 1024 +#define LOW_SLICES 10 +static uint16_t g_low_hist[LOW_SLICES][LOW_BINS]; +static int g_low_cur; +static uint64_t g_low_t0; + // The per-frame counters, summed since t0 (the overlay's lines are averages over half a second). static struct { uint64_t t0, frames, gpu0; @@ -47,6 +55,14 @@ void perfmon_frame(uint64_t now, uint64_t interval_ns, const uint64_t *wait_ns, g_ring_at[g_ring_pos] = now; g_ring_pos = (g_ring_pos + 1) % PERF_HISTORY; if (g_ring_n < PERF_HISTORY) g_ring_n++; + if (now - g_low_t0 >= 1000000000ull) { // the next second: its slice starts empty + g_low_cur = (g_low_cur + 1) % LOW_SLICES; + memset(g_low_hist[g_low_cur], 0, sizeof g_low_hist[0]); + g_low_t0 = now; + } + uint64_t bin = interval_ns / 250000ull; + uint16_t *h = &g_low_hist[g_low_cur][bin < LOW_BINS ? bin : LOW_BINS - 1]; + if (*h < 65535) (*h)++; } g_win.frames++; g_win.replay += replay_ns / 1e6; @@ -76,9 +92,27 @@ static void frames_summary(PerfStats *s, uint64_t now) { // (lock held) } } +// The fps of the slowest 1% and 0.1% of the frames in the window (the bin's upper edge, as perf.c's summary does); -1 +// when the window holds too few frames. (lock held) +static void lows_summary(PerfStats *s) { + uint32_t total = 0; + for (int k = 0; k < LOW_SLICES; k++) + for (int b = 0; b < LOW_BINS; b++) total += g_low_hist[k][b]; + s->low1_fps = s->low01_fps = -1; + if (total < 100) return; + uint32_t cut1 = (total + 99) / 100, cut01 = (total + 999) / 1000, acc = 0; + for (int b = LOW_BINS - 1; b >= 0 && s->low1_fps < 0; b--) { + for (int k = 0; k < LOW_SLICES; k++) acc += g_low_hist[k][b]; + float fps = 1000.0f / ((b + 1) * 0.25f); + if (s->low01_fps < 0 && acc >= cut01) s->low01_fps = fps; + if (acc >= cut1) s->low1_fps = fps; + } +} + // Every half second: the window's sums as per-frame averages into g_stats, and a new window. (lock held) static void counters_summary(uint64_t now) { if (now - g_win.t0 < 500000000ull) return; + lows_summary(&g_stats); uint64_t gpu = perf_gpu_busy_total(); if (g_win.t0 && g_win.frames) { double n = (double)g_win.frames; @@ -342,6 +376,8 @@ void perfmon_mode(int mode) { } if (!mode) { // the next start begins a new graph and a new counter window g_ring_n = g_ring_pos = 0; + memset(g_low_hist, 0, sizeof g_low_hist); + g_low_t0 = 0; memset(&g_win, 0, sizeof g_win); } plat_mutex_unlock(&g_lock); diff --git a/runtime/perfmon.h b/runtime/perfmon.h index c5ea1d3a..7c2b99bb 100644 --- a/runtime/perfmon.h +++ b/runtime/perfmon.h @@ -17,6 +17,8 @@ extern "C" { typedef struct { // the game's frames (presents), over the last second float fps, frame_ms_avg, frame_ms_max; + // fps of the slowest 1% and 0.1% of the frames over the last ten seconds (-1: too few frames yet) + float low1_fps, low01_fps; float frame_ms[PERF_HISTORY]; // oldest first // CPU: the whole machine and this process, percent of all logical processors char cpu_name[64]; diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 23a9f7fd..f32ddd3d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -9,6 +9,7 @@ #include <linux/fs.h> #include <linux/futex.h> #include <linux/userfaultfd.h> +#include <poll.h> #include <pthread.h> #include <sched.h> #include <signal.h> @@ -21,9 +22,11 @@ #include <sys/prctl.h> #include <sys/random.h> #include <sys/resource.h> +#include <sys/socket.h> #include <sys/stat.h> #include <sys/syscall.h> #include <sys/uio.h> +#include <sys/un.h> #include <sys/utsname.h> #include <sys/wait.h> #include <time.h> @@ -915,8 +918,9 @@ uint64_t plat_mem_written(uint64_t addr, uint64_t n, uint64_t *pages, uint64_t m } // One handler for the synchronous faults. A SIGSEGV whose page-fault error code has the write bit set goes to the -// write-fault hook first (memory.c thaws a frozen region); a fault in this executable's code then goes to the crash -// hook (main.c); anything else gets the default action. Runs on the thread's signal stack. +// write-fault hook first (memory.c thaws a frozen region); any other fault, in this executable or in a library (Mesa, +// SDL), then goes to the crash hook (main.c), which reports it and ends the process. Without a crash hook the fault +// gets the default action. Runs on the thread's signal stack. static int (*fault_hook)(uint64_t addr); static void (*crash_fn)(const PlatCrash *c); @@ -926,10 +930,17 @@ static void fault_handler(int sig, siginfo_t *si, void *uc) { if (sig == SIGSEGV && fault_hook && (err & 2) && fault_hook((uint64_t)(uintptr_t)si->si_addr)) return; uintptr_t rip = (uintptr_t)u->uc_mcontext.gregs[REG_RIP]; Dl_info at, self; - if (crash_fn && dladdr((void *)rip, &at) && dladdr((void *)plat_crash_hook, &self) && at.dli_fbase == self.dli_fbase) { + if (crash_fn) { + int found = dladdr((void *)rip, &at) && at.dli_fname; PlatCrash c = {sig == SIGSEGV ? "access violation" : sig == SIGBUS ? "bus error" : sig == SIGILL ? "illegal instruction" : "arithmetic exception", - (uint32_t)sig, rip, rip - (uintptr_t)self.dli_fbase, -1, 0}; + (uint32_t)sig, rip, found ? rip - (uintptr_t)at.dli_fbase : rip, -1, 0}; + if (!found) { + c.module = ""; + } else if (!dladdr((void *)plat_crash_hook, &self) || at.dli_fbase != self.dli_fbase) { + const char *n = strrchr(at.dli_fname, '/'); + c.module = n ? n + 1 : at.dli_fname; + } if (sig == SIGSEGV || sig == SIGBUS) c.access = (err & 16) ? 8 : (err & 2) ? 1 : 0, c.addr = (uint64_t)(uintptr_t)si->si_addr; crash_fn(&c); @@ -1037,6 +1048,8 @@ const void *plat_file_map(PlatFile *f) { return v == MAP_FAILED ? NULL : v; } +int plat_fs_os_error(void) { return errno; } + static int fs_err(int e) { switch (e) { case ENOENT: case ENOTDIR: return PLAT_FS_NOT_FOUND; @@ -1180,8 +1193,10 @@ size_t plat_getenv(const char *name, char *out, size_t cap) { // Cached per thread: plat_rmutex_lock asks on every acquire, hundreds of thousands of times a second in play, and // gettid is a full syscall on Linux (Windows reads GetCurrentThreadId from the TEB). A thread's id never changes, and -// the runtime starts child processes with posix_spawn only, so no forked copy inherits a stale id. -uint32_t plat_thread_id(void) { +// the runtime starts child processes with posix_spawn only, so no forked copy inherits a stale id. noinline: a fiber +// may resume on another thread (platform.h), and a caller that inlined this could keep the address of tid it computed +// before a plat_fiber_switch; as a call it reads the slot of the thread it runs on. +__attribute__((noinline)) uint32_t plat_thread_id(void) { static __thread uint32_t tid; if (!tid) tid = (uint32_t)syscall(SYS_gettid); return tid; @@ -1247,6 +1262,8 @@ int plat_os_version(char *out, size_t cap) { return snprintf(out, cap, "%s (Linux %s)", name, u.release) < (int)cap; } +int plat_is_wine(void) { return 0; } + int plat_stderr_path(char *out, size_t cap) { struct stat st; if (fstat(2, &st) || !S_ISREG(st.st_mode)) return 0; @@ -1648,3 +1665,169 @@ long plat_child_read(PlatChild *c, void *buf, size_t cap) { while (k < 0 && errno == EINTR); return (long)k; } + +// ---- local IPC (platform.h): Discord's Rich Presence Unix domain socket +struct PlatIpc { int fd; }; + +PlatIpc *plat_ipc_connect(const char *name) { + const char *dirs[] = {getenv("DISCORD_IPC_PATH"), getenv("XDG_RUNTIME_DIR"), getenv("TMPDIR"), "/tmp"}; + for (size_t d = 0; d < sizeof dirs / sizeof *dirs; d++) { + if (!dirs[d] || !*dirs[d]) continue; + char path[sizeof ((struct sockaddr_un *)0)->sun_path]; + if ((size_t)snprintf(path, sizeof path, "%s/%s", dirs[d], name) >= sizeof path) continue; + int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + if (fd < 0) return NULL; + struct sockaddr_un sa = {0}; + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, path); + if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == 0) { + PlatIpc *p = malloc(sizeof *p); + if (!p) { + close(fd); + return NULL; + } + p->fd = fd; + return p; + } + close(fd); // nothing listens at this path: the next directory may have it + } + return NULL; +} + +int plat_ipc_read(PlatIpc *p, void *buf, size_t n, uint32_t ms) { + struct pollfd pf = {.fd = p->fd, .events = POLLIN}; + int r = poll(&pf, 1, (int)ms); + if (r <= 0) return r < 0 && errno != EINTR ? -1 : 0; + ssize_t got = recv(p->fd, buf, n, 0); + return got > 0 ? (int)got : -1; // 0 bytes: the peer closed the socket +} + +int plat_ipc_write(PlatIpc *p, const void *buf, size_t n) { + const uint8_t *at = buf; + while (n) { + ssize_t put = send(p->fd, at, n, MSG_NOSIGNAL); // a closed peer is an error, not a SIG_PIPE + if (put <= 0) { + if (put < 0 && errno == EINTR) continue; + return 0; + } + at += put, n -= put; + } + return 1; +} + +void plat_ipc_close(PlatIpc *p) { + if (!p) return; + close(p->fd); + free(p); +} + + +// ---- restart +// TOTK_RESTART_WAIT names the process that called plat_restart, as its id and start time (field 22 of /proc/<pid>/stat, +// clock ticks after boot): a process that has only reused the id is not waited for. Waiting keeps the new process off +// the old one's files (the log it moves at start, the saves) until the old one is gone. The new process gets no +// descriptor past stderr (glibc 2.34's closefrom file action), and the default signal mask and dispositions, whatever +// the calling thread had. +#define RESTART_WAIT "TOTK_RESTART_WAIT" + +// The start time of process pid (field 22 of its /proc stat line), 0 when it is gone. +static unsigned long long process_start(int pid) { + char path[64], buf[1024]; + snprintf(path, sizeof path, "/proc/%d/stat", pid); + FILE *f = fopen(path, "r"); + if (!f) return 0; + size_t n = fread(buf, 1, sizeof buf - 1, f); + fclose(f); + buf[n] = 0; + char *p = strrchr(buf, ')'); // the command name may hold spaces + unsigned long long start = 0; + if (!p || sscanf(p + 2, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %llu", + &start) != 1) + return 0; + return start; +} + +// glibc 2.34 added posix_spawn_file_actions_addclosefrom_np, and the release links against the Steam Runtime's 2.31: a +// weak reference, used when the running glibc has it. Without it the child inherits the open descriptors. +extern int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *fa, int from) __attribute__((weak)); + +static void spawn_close_from(posix_spawn_file_actions_t *fa, int from) { + if (posix_spawn_file_actions_addclosefrom_np) posix_spawn_file_actions_addclosefrom_np(fa, from); +} + +int plat_restart(void) { + // the arguments, NUL-separated, from /proc/self/cmdline + static char args[16384]; + char *argv[256]; + int argc = 0; + FILE *f = fopen("/proc/self/cmdline", "rb"); + size_t n = f ? fread(args, 1, sizeof args - 1, f) : 0; + if (f) fclose(f); + args[n] = 0; + for (size_t i = 0; i < n && argc < 255; i += strlen(args + i) + 1) argv[argc++] = args + i; + argv[argc] = NULL; + char exe[PATH_MAX], wait[64]; + if (!argc || !plat_exe_path(exe, sizeof exe)) return 0; + snprintf(wait, sizeof wait, "%d.%llu", (int)getpid(), process_start((int)getpid())); + posix_spawn_file_actions_t fa; + posix_spawn_file_actions_init(&fa); + spawn_close_from(&fa, 3); + posix_spawnattr_t at; + posix_spawnattr_init(&at); + sigset_t none, all; + sigemptyset(&none); + sigfillset(&all); + posix_spawnattr_setsigmask(&at, &none); + posix_spawnattr_setsigdefault(&at, &all); + posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF); + setenv(RESTART_WAIT, wait, 1); + pid_t pid; + int e = posix_spawn(&pid, exe, &fa, &at, argv, environ); + unsetenv(RESTART_WAIT); + posix_spawnattr_destroy(&at); + posix_spawn_file_actions_destroy(&fa); + return e == 0; +} + +int plat_spawn(const char *const *argv, const char *cwd) { + posix_spawn_file_actions_t fa; + posix_spawn_file_actions_init(&fa); + spawn_close_from(&fa, 3); + posix_spawn_file_actions_addchdir_np(&fa, cwd); + posix_spawnattr_t at; + posix_spawnattr_init(&at); + sigset_t none, all; + sigemptyset(&none); + sigfillset(&all); + posix_spawnattr_setsigmask(&at, &none); + posix_spawnattr_setsigdefault(&at, &all); + posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSID); + pid_t pid; + int e = posix_spawn(&pid, argv[0], &fa, &at, (char *const *)argv, environ); + posix_spawnattr_destroy(&at); + posix_spawn_file_actions_destroy(&fa); + return e == 0; +} + +int plat_restart_wait(uint32_t timeout_ms) { + const char *v = getenv(RESTART_WAIT); + if (!v) return 0; + int pid = 0; + unsigned long long start = 0; + int ok = sscanf(v, "%d.%llu", &pid, &start) == 2 && pid > 0 && start; + unsetenv(RESTART_WAIT); // the processes this one starts do not wait again + if (!ok) return 0; + // The descriptor is taken first: a start time that still matches then proves it is that process, not a new one with + // the id. It reads as ready once the process has exited, reaped or not. + int fd = (int)syscall(SYS_pidfd_open, pid, 0); + if (fd < 0) return 0; // already gone + int r = 0; + if (process_start(pid) == start) { + struct pollfd p = {fd, POLLIN, 0}; + do r = poll(&p, 1, (int)timeout_ms); + while (r < 0 && errno == EINTR); + r = r > 0 ? 1 : -1; + } + close(fd); + return r; +} diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index df232d22..af20ec68 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -61,6 +61,23 @@ void plat_fiber_unself(void); // the calling thread stops being a fi PlatFiber *plat_fiber_new(size_t stack, void (*fn)(void *arg), void *arg); void plat_fiber_switch(PlatFiber *to); // from the fiber the calling thread runs void plat_fiber_free(PlatFiber *f); // a fiber nobody runs + +// A jump back to a point saved earlier on the same thread, without unwinding the frames in between (no SEH, no C++ +// cleanups): the kernel's ExitThread leaves the lifted frames this way. plat_jmp_save returns 0 when it saves and 1 +// when plat_jmp_go comes back to it; the function that called it must still be running. It keeps the callee-saved +// registers and the floating-point control state. On Windows this is not the C runtime's setjmp: a non-unwinding +// setjmp leaves the buffer's frame 0, and the C runtime's longjmp ends the process with 0xc0000409 (fast fail 13) on +// such a buffer when Control Flow Guard is on for the process, which Exploit protection can force on any program. +#ifdef _WIN32 +typedef struct { uint64_t gpr[10]; uint32_t mxcsr, fpcw; uint64_t pad; uint8_t xmm[160]; } PlatJmp; +int plat_jmp_save(PlatJmp *j) __attribute__((returns_twice, nothrow)); +_Noreturn void plat_jmp_go(PlatJmp *j); +#else +#include <setjmp.h> +typedef struct { jmp_buf b; } PlatJmp; +#define plat_jmp_save(j) setjmp((j)->b) +#define plat_jmp_go(j) longjmp((j)->b, 1) +#endif // Scheduling priority, Windows' seven levels: of the calling thread, or of t. On Linux a thread lowered without the // right to raise its priority again (CAP_SYS_NICE, or an RLIMIT_NICE of 20) could never return to normal, and the // runtime lowers threads that later take shared locks at normal priority; without that right every change is skipped @@ -201,19 +218,25 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)); // replaces this process with it (Linux). Environment changes made before the call are passed on. _Noreturn void plat_reexec(void); -// A crash in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack -// overflow) goes to fn, which reports it and must end the process. Faults in other modules (drivers) are left to the -// OS. The write-fault hook above runs first. On Windows abort() (a failed assert, std::terminate) and an invalid -// parameter to a C runtime function go there too, with the host frames in this executable: the C runtime ends the -// process for both with 0xc0000409 and no exception. +// A crash goes to fn, which reports it and must end the process. The write-fault hook above runs first. +// - A fault in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack +// overflow) is reported as it happens. +// - A fault in another module (a GPU driver, a system library) is reported only when nothing handles it: on Windows +// from the unhandled-exception filter, with the return addresses in this executable on the faulting thread's stack, +// so the report shows which host call ran into the driver; on Linux from the signal handler, without frames. Drivers +// that raise and catch their own exceptions never reach fn. +// - On Windows abort() (a failed assert, std::terminate) and an invalid parameter to a C runtime function go there +// too, with the host frames in this executable: the C runtime ends the process for both with 0xc0000409 and no +// exception. typedef struct { const char *what; // "access violation", "illegal instruction", ... uint32_t code; // the Windows exception code or the signal number - uintptr_t rip, offset; // the faulting instruction, and its offset in the executable + uintptr_t rip, offset; // the faulting instruction, and its offset in its module int access; // the data access that faulted: 0 read, 1 write, 8 execute; -1 when not an access uint64_t addr; // its address when access >= 0 - int nframes; // abort and invalid parameter: return addresses in this executable, innermost first - uintptr_t frames[16]; // (rip is frames[0]); 0 for a fault, whose rip is exact + int nframes; // return addresses in this executable, innermost first: for abort and invalid parameter + uintptr_t frames[16]; // (rip is frames[0]) and for a fault in another module; 0 for a fault in this one + const char *module; // the module holding rip: NULL for this executable, "" for none (a jump to address 0) } PlatCrash; void plat_crash_hook(void (*fn)(const PlatCrash *c)); int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory @@ -299,8 +322,9 @@ enum { PLAT_FS_OK, PLAT_FS_NOT_FOUND, PLAT_FS_EXISTS, PLAT_FS_NOT_EMPTY, PLAT_FS enum { PLAT_FS_MISSING = -1, PLAT_FS_FILE, PLAT_FS_DIR, PLAT_FS_LINK }; // plat_fs_kind; LINK: a symbolic link or // reparse point, never followed int plat_fs_kind(const char *path); +int plat_fs_os_error(void); // the OS error code (GetLastError, errno) of this thread's last failed plat_fs_* call int plat_fs_mkdir(const char *path); // PLAT_FS_EXISTS when something is there -int plat_fs_rmdir(const char *path); // an empty directory, or a link to one +int plat_fs_rmdir(const char *path); // an empty directory (a read-only one too), or a link to one int plat_fs_unlink(const char *path); // a file or a link; a read-only one too // Renames a file or directory; never replaces (PLAT_FS_EXISTS). durable: the change is on the disk at return. int plat_fs_rename(const char *from, const char *to, int durable); @@ -350,6 +374,7 @@ uint32_t plat_thread_id(void); // id of the calling t int plat_font_path(const char *lang, char *out, size_t cap); // The operating system and its build ("Windows 10.0.28000") into out; 0 when unknown. int plat_os_version(char *out, size_t cap); +int plat_is_wine(void); // 1 when the Windows build runs under Wine; 0 elsewhere // The file the process's stderr (the log) goes to, as a UTF-8 path; 0 when stderr is not a file (a console, a pipe). int plat_stderr_path(char *out, size_t cap); @@ -393,3 +418,25 @@ PlatChild *plat_child_start(const char *exe, const char *arg, uint32_t *err); void plat_child_stop(PlatChild *c); // kills the child and frees c int plat_child_write(PlatChild *c, const void *data, size_t n); // 1 when all n bytes were written long plat_child_read(PlatChild *c, void *buf, size_t cap); // bytes read (at least 1); 0 or negative: pipe broke + +// ---- local IPC: a stream connection to another program of this machine, one listen location per name (Discord's +// Rich Presence, discord_rpc.c). On Windows the named pipe \\.\pipe\<name>; on Linux the Unix domain socket +// <dir>/<name> with <dir> the first of $DISCORD_IPC_PATH, $XDG_RUNTIME_DIR, $TMPDIR and /tmp that has one. +typedef struct PlatIpc PlatIpc; +PlatIpc *plat_ipc_connect(const char *name); // NULL when nothing listens under that name +// Up to n bytes, waiting at most ms for the first of them: bytes read, 0 none in time, -1 the connection broke. +// The rest of what the peer wrote may still be in its buffer: a caller that needs a whole frame loops. +int plat_ipc_read(PlatIpc *p, void *buf, size_t n, uint32_t ms); +int plat_ipc_write(PlatIpc *p, const void *buf, size_t n); // 1 when all n bytes were written, 0 the connection broke +void plat_ipc_close(PlatIpc *p); + +// ---- restart: this program again, in a new process that starts once this one is gone +// Starts this executable again with the same command line, environment and working folder, and no handle of this +// process. 1 when it started; the caller then exits. +int plat_restart(void); +// Starts argv[0] with the arguments argv[1..] (NULL-terminated, UTF-8) in folder cwd, detached from this process: no +// console, no handle of this one. 1 when it started (the setup window that rebuilds the game, runtime/cheats.c). +int plat_spawn(const char *const *argv, const char *cwd); +// In a process plat_restart started: waits, at most timeout_ms, until the process that started it has exited. 1 after +// the wait, -1 on timeout, 0 when there is nothing to wait for (not started by plat_restart, or that process is gone). +int plat_restart_wait(uint32_t timeout_ms); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index e785095f..c4aca224 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -199,6 +199,69 @@ void plat_fiber_free(PlatFiber *f) { free(f); } +// plat_jmp_save / plat_jmp_go: PlatJmp holds rbx, rbp, rdi, rsi, r12-r15, the caller's rsp and the return address at +// 0..72, MXCSR at 80, the x87 control word at 84 and xmm6-xmm15 from 96, the Win64 callee-saved state. Both are leaf +// code that does not move rsp before it leaves, so they need no unwind data. +_Static_assert(sizeof(PlatJmp) == 256 && __builtin_offsetof(PlatJmp, xmm) == 96, "plat_jmp_save layout"); +__asm__(".text\n" + ".globl plat_jmp_save\n" + ".def plat_jmp_save; .scl 2; .type 32; .endef\n" + ".p2align 4\n" + "plat_jmp_save:\n" + " movq %rbx, 0(%rcx)\n" + " movq %rbp, 8(%rcx)\n" + " movq %rdi, 16(%rcx)\n" + " movq %rsi, 24(%rcx)\n" + " movq %r12, 32(%rcx)\n" + " movq %r13, 40(%rcx)\n" + " movq %r14, 48(%rcx)\n" + " movq %r15, 56(%rcx)\n" + " leaq 8(%rsp), %rdx\n" // the caller's rsp after this call returns + " movq %rdx, 64(%rcx)\n" + " movq (%rsp), %rdx\n" + " movq %rdx, 72(%rcx)\n" + " stmxcsr 80(%rcx)\n" + " fnstcw 84(%rcx)\n" + " movdqu %xmm6, 96(%rcx)\n" + " movdqu %xmm7, 112(%rcx)\n" + " movdqu %xmm8, 128(%rcx)\n" + " movdqu %xmm9, 144(%rcx)\n" + " movdqu %xmm10, 160(%rcx)\n" + " movdqu %xmm11, 176(%rcx)\n" + " movdqu %xmm12, 192(%rcx)\n" + " movdqu %xmm13, 208(%rcx)\n" + " movdqu %xmm14, 224(%rcx)\n" + " movdqu %xmm15, 240(%rcx)\n" + " xorl %eax, %eax\n" + " ret\n" + ".globl plat_jmp_go\n" + ".def plat_jmp_go; .scl 2; .type 32; .endef\n" + ".p2align 4\n" + "plat_jmp_go:\n" + " movq 0(%rcx), %rbx\n" + " movq 8(%rcx), %rbp\n" + " movq 16(%rcx), %rdi\n" + " movq 24(%rcx), %rsi\n" + " movq 32(%rcx), %r12\n" + " movq 40(%rcx), %r13\n" + " movq 48(%rcx), %r14\n" + " movq 56(%rcx), %r15\n" + " ldmxcsr 80(%rcx)\n" + " fldcw 84(%rcx)\n" + " movdqu 96(%rcx), %xmm6\n" + " movdqu 112(%rcx), %xmm7\n" + " movdqu 128(%rcx), %xmm8\n" + " movdqu 144(%rcx), %xmm9\n" + " movdqu 160(%rcx), %xmm10\n" + " movdqu 176(%rcx), %xmm11\n" + " movdqu 192(%rcx), %xmm12\n" + " movdqu 208(%rcx), %xmm13\n" + " movdqu 224(%rcx), %xmm14\n" + " movdqu 240(%rcx), %xmm15\n" + " movq 64(%rcx), %rsp\n" + " movl $1, %eax\n" + " jmpq *72(%rcx)\n"); + int plat_thread_spawn(PlatThreadFn fn, void *arg, const char *name, size_t stack) { PlatThread *t = plat_thread_new_ex(fn, arg, name, stack); if (t) plat_thread_detach(t); @@ -602,6 +665,76 @@ __attribute__((noinline)) static void crash_from_runtime(const char *what, uint3 crash_fn(&c); } +// The name a DLL records for itself in its export directory ("amdvlk64.dll"). Read from its mapped headers, not through +// the loader, whose lock another thread may hold when the process crashes. +static const char *image_name(const char *base) { + const IMAGE_NT_HEADERS64 *nt = (const void *)(base + ((const IMAGE_DOS_HEADER *)base)->e_lfanew); + const IMAGE_DATA_DIRECTORY *d = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; + if (!d->VirtualAddress || !d->Size) return "unnamed module"; + return base + ((const IMAGE_EXPORT_DIRECTORY *)(base + d->VirtualAddress))->Name; +} + +// The return addresses in [lo, lo + size) on the crashed thread's stack, innermost first: unwinds the faulting context +// with the x64 unwind tables; a function without a table entry (a leaf, or address 0 after a call through a null +// pointer) has its return address on top of the stack. The walk stops at return address 0 (the thread's first frame) +// and when the stack pointer leaves the thread's stack, so a damaged stack ends it instead of faulting. +static int frames_in(const CONTEXT *at, uintptr_t lo, uintptr_t size, uintptr_t *out, int max) { + CONTEXT c = *at; + ULONG_PTR low, high; + GetCurrentThreadStackLimits(&low, &high); + int n = 0; + for (int i = 0; i < 256 && n < max && (c.Rip || !i) && c.Rsp >= low && c.Rsp + 8 <= high; i++) { + DWORD64 img; + RUNTIME_FUNCTION *fe = RtlLookupFunctionEntry(c.Rip, &img, NULL); + if (fe) { + void *data; + DWORD64 frame; + RtlVirtualUnwind(UNW_FLAG_NHANDLER, img, c.Rip, fe, &c, &data, &frame, NULL); + } else { + c.Rip = *(const DWORD64 *)c.Rsp; + c.Rsp += 8; + } + if (c.Rip - lo < size) out[n++] = (uintptr_t)c.Rip; + } + return n; +} + +// The last-chance filter: an exception no handler took, in any module. crash_veh reports faults in this executable as +// they happen; this one catches the rest, which used to end the process with no line in the log: a fault in a GPU +// driver's own thread or in a driver call (vkQueueSubmit, vkAllocateMemory), a C++ exception a library let escape, a +// call through a null pointer (rip 0, outside every module). Windows calls it only after every handler declined, so +// the exceptions drivers raise and catch themselves never get here; it costs nothing until a crash. +static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep) { + const EXCEPTION_RECORD *er = ep->ExceptionRecord; + DWORD code = er->ExceptionCode; + uintptr_t rip = (uintptr_t)ep->ContextRecord->Rip; + PlatCrash c = {code == EXCEPTION_ACCESS_VIOLATION ? "access violation" + : code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction" + : code == EXCEPTION_INT_DIVIDE_BY_ZERO ? "integer division by zero" + : code == EXCEPTION_PRIV_INSTRUCTION ? "privileged instruction" + : code == EXCEPTION_STACK_OVERFLOW ? "stack overflow" + : code == EXCEPTION_IN_PAGE_ERROR ? "in-page error" + : code == EXCEPTION_BREAKPOINT ? "breakpoint" + : code == 0xe06d7363u ? "C++ exception" + : code == 0xc0000374u ? "heap corruption" // STATUS_HEAP_CORRUPTION + : "unhandled exception", + (uint32_t)code, rip, 0, -1, 0}; + if (code == EXCEPTION_ACCESS_VIOLATION && er->NumberParameters >= 2) + c.access = (int)er->ExceptionInformation[0], c.addr = er->ExceptionInformation[1]; + void *base = NULL; + RtlPcToFileHeader((void *)rip, &base); + MODULEINFO mi; + uintptr_t exe = (uintptr_t)GetModuleHandleW(NULL); + c.offset = rip - (uintptr_t)base; + if (!base) c.module = ""; + else if ((uintptr_t)base != exe) c.module = image_name(base); + if (c.module && GetModuleInformation(GetCurrentProcess(), (HMODULE)exe, &mi, sizeof mi)) + c.nframes = frames_in(ep->ContextRecord, exe, mi.SizeOfImage, c.frames, + (int)(sizeof c.frames / sizeof *c.frames)); + crash_fn(&c); + return EXCEPTION_CONTINUE_SEARCH; +} + static void abort_signal(int sig) { crash_from_runtime("abort", (uint32_t)sig); } static void invalid_parameter(const wchar_t *expr, const wchar_t *func, const wchar_t *file, unsigned line, @@ -613,6 +746,7 @@ static void invalid_parameter(const wchar_t *expr, const wchar_t *func, const wc void plat_crash_hook(void (*fn)(const PlatCrash *c)) { crash_fn = fn; AddVectoredExceptionHandler(0, crash_veh); + SetUnhandledExceptionFilter(crash_filter); // replaces the C runtime's filter; crash_fn never returns signal(SIGABRT, abort_signal); _set_invalid_parameter_handler(invalid_parameter); } @@ -803,7 +937,11 @@ const void *plat_file_map(PlatFile *f) { return v; } +static __thread DWORD fs_last_error; +int plat_fs_os_error(void) { return (int)fs_last_error; } + static int fs_err(DWORD e) { + fs_last_error = e; switch (e) { case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: case ERROR_DIRECTORY: return PLAT_FS_NOT_FOUND; case ERROR_FILE_EXISTS: case ERROR_ALREADY_EXISTS: return PLAT_FS_EXISTS; @@ -813,12 +951,28 @@ static int fs_err(DWORD e) { } } +// A symbolic link or junction is a link: removed as a link, never followed. Any other reparse point (OneDrive's cloud +// placeholders, deduplicated files) is an ordinary file or directory whose content the file system fetches on access. +static int fs_kind_of(const WIN32_FIND_DATAW *fd) { + DWORD a = fd->dwFileAttributes; + if (a & FILE_ATTRIBUTE_REPARSE_POINT && (fd->dwReserved0 == IO_REPARSE_TAG_SYMLINK || fd->dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT)) + return PLAT_FS_LINK; + return a & FILE_ATTRIBUTE_DIRECTORY ? PLAT_FS_DIR : PLAT_FS_FILE; +} + int plat_fs_kind(const char *path) { wchar_t w[WPATH]; if (!widen(path, w, WPATH)) return PLAT_FS_MISSING; DWORD a = GetFileAttributesW(w); if (a == INVALID_FILE_ATTRIBUTES) return PLAT_FS_MISSING; - return a & FILE_ATTRIBUTE_REPARSE_POINT ? PLAT_FS_LINK : a & FILE_ATTRIBUTE_DIRECTORY ? PLAT_FS_DIR : PLAT_FS_FILE; + if (!(a & FILE_ATTRIBUTE_REPARSE_POINT)) return a & FILE_ATTRIBUTE_DIRECTORY ? PLAT_FS_DIR : PLAT_FS_FILE; + // the reparse tag comes with the find data only; FindFirstFileW is asked only here, since it fails on a path + // with a trailing separator and reads wildcards + WIN32_FIND_DATAW fd; + HANDLE h = FindFirstFileW(w, &fd); + if (h == INVALID_HANDLE_VALUE) return PLAT_FS_LINK; + FindClose(h); + return fs_kind_of(&fd); } int plat_fs_mkdir(const char *path) { @@ -830,7 +984,15 @@ int plat_fs_mkdir(const char *path) { int plat_fs_rmdir(const char *path) { wchar_t w[WPATH]; if (!widen(path, w, WPATH)) return PLAT_FS_ERROR; - return RemoveDirectoryW(w) ? PLAT_FS_OK : fs_err(GetLastError()); + if (RemoveDirectoryW(w)) return PLAT_FS_OK; + DWORD e = GetLastError(), a = GetFileAttributesW(w); + if (e == ERROR_ACCESS_DENIED && a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_READONLY)) { + // a read-only directory (copied over from another emulator's save folder) refuses RemoveDirectoryW + SetFileAttributesW(w, a & ~(DWORD)FILE_ATTRIBUTE_READONLY); + if (RemoveDirectoryW(w)) return PLAT_FS_OK; + e = GetLastError(); + } + return fs_err(e); } int plat_fs_unlink(const char *path) { @@ -975,9 +1137,7 @@ int plat_fs_list(const char *dir, int (*cb)(const char *name, int kind, uint64_t if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..")) continue; char name[1024]; if (!WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, name, sizeof name, NULL, NULL)) continue; - DWORD a = fd.dwFileAttributes; - int kind = a & FILE_ATTRIBUTE_REPARSE_POINT ? PLAT_FS_LINK : a & FILE_ATTRIBUTE_DIRECTORY ? PLAT_FS_DIR - : PLAT_FS_FILE; + int kind = fs_kind_of(&fd); if (!cb(name, kind, (uint64_t)fd.nFileSizeHigh << 32 | fd.nFileSizeLow, ctx)) break; } while (FindNextFileW(h, &fd)); FindClose(h); @@ -1128,6 +1288,11 @@ int plat_os_version(char *out, size_t cap) { return 1; } +int plat_is_wine(void) { + HMODULE nt = GetModuleHandleW(L"ntdll.dll"); + return nt && GetProcAddress(nt, "wine_get_version") != NULL; +} + int plat_stderr_path(char *out, size_t cap) { HANDLE h = GetStdHandle(STD_ERROR_HANDLE); wchar_t w[WPATH]; @@ -1458,3 +1623,139 @@ long plat_child_read(PlatChild *c, void *buf, size_t cap) { if (!ReadFile(c->out, buf, (DWORD)cap, &got, NULL)) return -1; return (long)got; } + +// ---- local IPC (platform.h): Discord's Rich Presence named pipe +struct PlatIpc { HANDLE h; }; + +PlatIpc *plat_ipc_connect(const char *name) { + wchar_t wname[128], path[152]; + if (!widen(name, wname, 128)) return NULL; + swprintf(path, 152, L"\\\\.\\pipe\\%ls", wname); + HANDLE h = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + if (h == INVALID_HANDLE_VALUE) return NULL; // no listener, or the pipe is busy: the caller retries later + PlatIpc *p = malloc(sizeof *p); + if (!p) { + CloseHandle(h); + return NULL; + } + p->h = h; + return p; +} + +int plat_ipc_read(PlatIpc *p, void *buf, size_t n, uint32_t ms) { + uint64_t deadline = plat_now_ms() + ms; + for (;;) { + DWORD have = 0; + if (!PeekNamedPipe(p->h, NULL, 0, NULL, &have, NULL)) return -1; // the writer closed the pipe + if (have) { + DWORD got = 0; + if (!ReadFile(p->h, buf, have < n ? have : (DWORD)n, &got, NULL) || !got) return -1; + return (int)got; + } + if (plat_now_ms() >= deadline) return 0; + plat_sleep_ms(5); + } +} + +int plat_ipc_write(PlatIpc *p, const void *buf, size_t n) { + const uint8_t *at = buf; + while (n) { + DWORD put = 0; + if (!WriteFile(p->h, at, n > (1u << 30) ? 1u << 30 : (DWORD)n, &put, NULL) || !put) return 0; + at += put, n -= put; + } + return 1; +} + +void plat_ipc_close(PlatIpc *p) { + if (!p) return; + CloseHandle(p->h); + free(p); +} + + +// ---- restart +// TOTK_RESTART_WAIT names the process that called plat_restart, as its id and creation time: a process that has only +// reused the id is not waited for. Waiting keeps the new process off the old one's files (the log it moves at start, +// the saves) until the old one is gone; no handle is inherited, so none of them stays open in the new one either. +#define RESTART_WAIT "TOTK_RESTART_WAIT" + +static uint64_t process_created(HANDLE h) { + FILETIME c, e, k, u; + return GetProcessTimes(h, &c, &e, &k, &u) ? ft64(c) : 0; +} + +int plat_restart(void) { + char wait[48]; + snprintf(wait, sizeof wait, "%lu.%llu", GetCurrentProcessId(), (unsigned long long)process_created(GetCurrentProcess())); + plat_setenv(RESTART_WAIT, wait); + STARTUPINFOW si = {.cb = sizeof si}; + PROCESS_INFORMATION pi; + wchar_t *cmd = _wcsdup(GetCommandLineW()); // CreateProcessW may write its command line + BOOL ok = cmd && CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); + free(cmd); + plat_setenv(RESTART_WAIT, NULL); + if (!ok) return 0; + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + return 1; +} + +// One argument of a command line as CommandLineToArgvW reads it back: in quotes, a backslash run doubled before a quote +// and at the end, a quote escaped. +static size_t quote_arg(const wchar_t *a, wchar_t *out, size_t cap, size_t at) { +#define PUT(c) do { if (at + 1 < cap) out[at] = (c); at++; } while (0) + PUT(L'"'); + for (const wchar_t *p = a;; p++) { + size_t slashes = 0; + while (*p == L'\\') p++, slashes++; + size_t times = !*p || *p == L'"' ? 2 : 1; + for (size_t k = 0; k < slashes * times; k++) PUT(L'\\'); + if (!*p) break; + if (*p == L'"') PUT(L'\\'); + PUT(*p); + } + PUT(L'"'); + PUT(L' '); +#undef PUT + return at; +} + +int plat_spawn(const char *const *argv, const char *cwd) { + enum { CMD = 32768 }; + wchar_t *cmd = calloc(CMD, sizeof *cmd), *arg = malloc(WPATH * sizeof *arg), *dir = malloc(WPATH * sizeof *dir); + int ok = cmd && arg && dir && argv[0] && widen(cwd, dir, WPATH); + size_t at = 0; + for (int i = 0; ok && argv[i]; i++) { + ok = widen(argv[i], arg, WPATH); + if (ok) at = quote_arg(arg, cmd, CMD, at); + } + ok = ok && at < CMD && at > 0; + if (ok) cmd[at - 1] = 0; // the last separator + STARTUPINFOW si = {.cb = sizeof si}; + PROCESS_INFORMATION pi; + ok = ok && CreateProcessW(NULL, cmd, NULL, NULL, FALSE, DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, NULL, dir, &si, + &pi); + if (ok) { + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + } + free(cmd); + free(arg); + free(dir); + return ok; +} + +int plat_restart_wait(uint32_t timeout_ms) { + char v[48]; + if (!plat_getenv(RESTART_WAIT, v, sizeof v)) return 0; + plat_setenv(RESTART_WAIT, NULL); // the processes this one starts do not wait again + unsigned long pid = 0; + unsigned long long created = 0; + if (sscanf(v, "%lu.%llu", &pid, &created) != 2) return 0; + HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + if (!h) return 0; // already gone + int r = process_created(h) != created ? 0 : WaitForSingleObject(h, timeout_ms) == WAIT_OBJECT_0 ? 1 : -1; + CloseHandle(h); + return r; +} diff --git a/runtime/resolution.c b/runtime/resolution.c index 1d8e2355..3e70fefa 100644 --- a/runtime/resolution.c +++ b/runtime/resolution.c @@ -27,6 +27,9 @@ // rewritten table (res_heapdef_build) in place of the original; memory.c adds the amount to the application pool, so // no other heap shrinks. Without the rewritten table (a RomFS that lacks it, a format this code does not know) the // patch sets stay off and the game keeps its own sizes. +#ifndef _WIN32 +#include <dirent.h> +#endif #include <math.h> #include <stdint.h> #include <stdio.h> @@ -36,6 +39,7 @@ #include "cfg.h" #include "host.h" +#include "platform/platform.h" #define BASE_W 1920 #define BASE_H 1080 @@ -43,6 +47,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,8 +55,71 @@ 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 + double dlss_k; // TOTK_DLSS quality/balanced/performance/ultra_performance: the scene's share of the output, else 0 + double base_k; // the scene's share without a DLSS mode (render_scale, or the game's 5/6) + unsigned hw, hh; // the largest scene a DLSS mode change can ask for: the heap is sized for it + int live; // 1.4.3 with DLSS able to run: the DLSS mode can change while the game runs } R; +// The scene size is decided before the device exists, so NGX cannot be asked yet. NGX loads DLSS from nvngx_dlss.dll +// next to totk.exe on Windows, libnvidia-ngx-dlss.so.<version> next to totk on Linux (vk_dlaa.c vk_dlaa_init): +// without that file DLSS never runs, and a DLSS mode would leave the game's plain scaling at 50..67 % of the output. +static int dlss_dll(void) { +#ifdef _WIN32 + char exe[1024], path[1024 + 32]; + if (!plat_exe_path(exe, sizeof exe)) return 1; + char *last = plat_last_sep(exe); + if (!last) return 1; + snprintf(path, sizeof path, "%.*s/nvngx_dlss.dll", (int)(last - exe), exe); + return plat_file_exists(path); +#else + char exe[1024]; + if (!plat_exe_path(exe, sizeof exe)) return 1; + char *last = plat_last_sep(exe); + if (!last) return 1; + *last = 0; + DIR *d = opendir(exe); + if (!d) return 1; + int found = 0; + for (struct dirent *e; !found && (e = readdir(d));) + found = !strncmp(e->d_name, "libnvidia-ngx-dlss.so.", 22); + closedir(d); + return found; +#endif +} + +// The scene size for a share k of the output, within SCENE_MAX per side. +static void scene_for(double k, unsigned *w, unsigned *h) { + double sw = R.ow * k, sh = R.oh * k; + double fit = fmin(1.0, fmin(SCENE_MAX / sw, SCENE_MAX / sh)); + *w = (unsigned)(sw * fit + 0.5); + *h = (unsigned)(sh * fit + 0.5); +} + +// The scene's share of the output for a TOTK_DLSS mode; 0 for dlaa (the scene keeps base_k), while DLSS/DLAA is off +// (the mode the menu still holds would shrink the scene for the plain upscaler) and where DLSS cannot run (dlss_dll). +static double dlss_share(const char *m) { + static const struct { const char *name; double k; } dlss[] = { + {"quality", 2.0 / 3.0}, {"balanced", 0.58}, {"performance", 0.5}, {"ultra_performance", 1.0 / 3.0}}; + static int dll = -1; + if (!cfg_bool("TOTK_DLAA")) return 0; + for (int i = 0; i < (int)(sizeof dlss / sizeof dlss[0]); i++) + if (!strcmp(m, dlss[i].name)) { + if (dll < 0 && !(dll = dlss_dll())) { +#ifdef _WIN32 + rt_log("resolution: DLSS %s needs nvngx_dlss.dll next to totk.exe, which is missing: the scene follows " + "render_scale", m); +#else + rt_log("resolution: DLSS %s needs libnvidia-ngx-dlss.so.<version> next to totk, which is missing: the " + "scene follows render_scale", m); +#endif + } + return dll ? dlss[i].k : 0; + } + return 0; +} + static void decide(void) { if (R.decided) return; R.decided = 1; @@ -78,24 +146,45 @@ static void decide(void) { rt_log("resolution: TOTK_RENDER_SCALE=%s is not game or a percentage 25..400; game", s); } } - double sw = R.ow * k, sh = R.oh * k; - double fit = fmin(1.0, fmin(SCENE_MAX / sw, SCENE_MAX / sh)); - if (fit < 1.0) R.scene_set = 1; - R.sw = (unsigned)(sw * fit + 0.5); - R.sh = (unsigned)(sh * fit + 0.5); + // A DLSS mode other than dlaa sets the scene size instead. On 1.4.3 with DLSS able to run (DLAA on, the DLL there) + // the mode can change while the game runs (res_dlss_mode): the scene patch is then always on and the heap is sized + // for the largest scene the modes ask for. Everyone else keeps the game's own scene size and its dynamic resolution + // unless render_scale or a DLSS mode asks for another, and a heap sized for that scene alone. + R.base_k = k; + R.live = RT_V143 && cfg_bool("TOTK_DLAA") && dlss_dll(); + const char *m = cfg_value("TOTK_DLSS"); + R.dlss_k = dlss_share(m); + if (R.dlss_k) rt_log("resolution: DLSS %s: the scene at %.0f %% of the output (render_scale ignored)", m, R.dlss_k * 100); + double sk = R.dlss_k ? R.dlss_k : k; + if (R.live || R.dlss_k || R.ow * sk > SCENE_MAX || R.oh * sk > SCENE_MAX) R.scene_set = 1; + scene_for(sk, &R.sw, &R.sh); + if (R.live) scene_for(fmax(k, 2.0 / 3.0), &R.hw, &R.hh); + else R.hw = R.sw, R.hh = R.sh; + 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; + fmax(0.0, (double)R.hw * R.hh / (SCENE_W * SCENE_H) - 1.0); + 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 +279,50 @@ 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; } +// TOTK_SHADOW_RANGE (percent, 50 to 400; 100 = the game's): the sun's shadow cascades end at 20, 40, 80 and 160 m. +// Other values multiply those distances through the cascade patch set (recomp/patches-v143.txt), so the sharp first +// cascade reaches further and the visible steps between cascades move away from the player, at the cost of shadow +// texels per metre (TOTK_SHADOW_SCALE pays that back). +static void res_shadow_range(void) { + int pct = cfg_int("TOTK_SHADOW_RANGE"), set = rt_patch_set("cascade"); + if (pct == 100) return; + if (set < 0) { + rt_log("resolution: the generated C has no cascade patch set; shadow_range stays 100"); + return; + } + float k = (float)(pct < 50 ? 50 : pct > 400 ? 400 : pct) / 100.0f, d[4] = {20 * k, 40 * k, 80 * k, 160 * k}; + memcpy((uint8_t *)rt_patch_page() + 64, d, sizeof d); + rt_patch_switch(set, 1); + rt_log("resolution: shadow cascades end at %g, %g, %g and %g m", d[0], d[1], d[2], d[3]); +} + void res_init(void) { + res_shadow_range(); 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"); @@ -220,6 +347,7 @@ void res_init(void) { rt_patch_switch(output, R.ow != BASE_W || R.oh != BASE_H); rt_patch_switch(scene, scene_on); R.on = 1; + if (R.live) g_fnprof = 1; // until the game sets its render size: res_fn_hit, or res_present gives up // Another shape than 16:9 (more than 1% off): the camera takes the output's ratio (recomp/patches-v121.txt and // patches-v143.txt, set aspect), so a 21:9 output shows more of the world at the sides instead of a stretched 16:9 view. The HUD's layouts // for that shape come from romfs-patches-<w>x<h> (romfs.c overlay_init, tools/make_romfs_patches.py --aspect). @@ -238,10 +366,18 @@ 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()); } +// DLSS upscaling: textures are sampled for the output's resolution, not the scene's (NVIDIA's DLSS guide: a mip bias +// of log2(render / output)), so their detail survives the scaling. 0 without a DLSS mode. +float res_dlss_lod_bias(void) { + decide(); + return R.dlss_k ? (float)log2(R.dlss_k) : 0.0f; +} + // 16:9 within 1% (1366x768 counts): the shape the game's own layouts and camera are made for. int res_is_16x9(unsigned w, unsigned h) { double rel = ((double)w / h) / ((double)BASE_W / BASE_H); @@ -282,6 +418,62 @@ void res_wanted(unsigned out[2]) { out[0] = R.ow, out[1] = R.oh; } +// A TOTK_DLSS change while the game runs (vk_dlaa.c, at a present): the new scene size goes to the patch page, where +// the game reads it when it sets its render size (main+0x585e48). 1.4.3 does that once, at start: its renderer setup +// (main+0x7000e67028) calls main+0x585bd0(renderer, force), which picks the console mode's sizes and flags the render +// targets for re-creation. The setup call is caught through the function-entry hook (res_fn_hit, on only until then), +// and the next nvnQueuePresentTexture on the game's thread calls it again with force set (res_present), between frames +// as at start. The heap was sized for the largest scene a mode asks for. 1 when the size changed. +#define SET_RENDER_SIZE 0x7000585bd0ull +static uint64_t renderer; // x0 of the game's own call of SET_RENDER_SIZE +static volatile int size_pending; // the page holds a size the game has not set yet + +void res_fn_hit(uint64_t addr, uint64_t x0) { + if (addr != SET_RENDER_SIZE) return; + renderer = x0; + g_fnprof = 0; + rt_log("resolution: renderer at %#llx; the scene size can change while the game runs", (unsigned long long)x0); +} + +void res_present(Cpu *c) { + // The hook runs on every guest function entry while it waits: should the game set its render size before + // res_init or never, it stops after 1800 presents (half a minute at 60 fps) and modes change at the next start. + static unsigned waited; + if (g_fnprof && !renderer && ++waited == 1800) { + g_fnprof = 0; + rt_log("resolution: the game's render size setup was not seen; DLSS modes change at the next start"); + } + if (!size_pending || !renderer) return; + size_pending = 0; + Cpu keep = *c; // the guest's registers as at the present call: the setup runs as if it never happened + rt_call(c, SET_RENDER_SIZE, 2, (uint64_t)renderer, (uint64_t)1); + memcpy(c->x, keep.x, sizeof c->x); + memcpy(c->v, keep.v, sizeof c->v); + c->nzcv = keep.nzcv, c->fpsr = keep.fpsr; + rt_log("resolution: the game set the scene size %ux%u", R.sw, R.sh); +} + +int res_dlss_mode(const char *m) { + decide(); + double k = dlss_share(m); + if (k == R.dlss_k || !R.on) return 0; + if (!RT_V143 || !renderer) { + rt_log("resolution: DLSS %s takes effect at the next start (the game set its render size already)", m); + return 0; + } + R.dlss_k = k; + unsigned w, h; + scene_for(k ? k : R.base_k, &w, &h); + if (w == R.sw && h == R.sh) return 0; + double s = RT_V143 ? (double)R.ow / BASE_W : 1.0; + float sc[2] = {(float)(w / s), (float)(h / s)}; + memcpy((uint8_t *)rt_patch_page() + 16, sc, sizeof sc); + R.sw = w, R.sh = h; + size_pending = 1; + rt_log("resolution: DLSS %s: scene %ux%u", m, w, h); + return 1; +} + void res_sizes(unsigned out[2], unsigned scene[2]) { decide(); out[0] = R.on ? R.ow : BASE_W, out[1] = R.on ? R.oh : BASE_H; diff --git a/runtime/restbl.c b/runtime/restbl.c new file mode 100644 index 00000000..7436ddd1 --- /dev/null +++ b/runtime/restbl.c @@ -0,0 +1,236 @@ +// The resource size table (restbl.h). Layout of a decompressed RESTBL, little-endian: +// +// "RESTBL", u32 version (1), u32 name length (160), u32 hashed count, u32 named count +// hashed entries {u32 crc32 of the key, u32 size}, sorted by the hash +// named entries {char key[name length], u32 size}: the keys whose hash collides with another key's +// +// A key is a RomFS path without .zs or .mc. The estimates come from the 1.4.3 table against its own files: most types +// need the decompressed size plus at most 8 KiB (headers, alignment); a few need a multiple of it (bfres 1.5, bkres +// 2.05, bgyml 3.85, asb 1.4, bstar 1.2, bphsc 1.1). A type the table does not list takes 1.5 times its size plus 8 KiB. +#include <stdlib.h> +#include <string.h> + +#include "restbl.h" + +typedef struct { uint32_t hash, size; } Hashed; + +struct Restbl { + uint32_t version, name_len; + Hashed *hashed; + size_t nhashed, hashed_cap; + uint8_t *named; // named entries as in the file: key (name_len bytes, NUL padded) then u32 size + size_t nnamed; +}; + +static uint32_t rd32(const uint8_t *p) { return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; } +static void wr32(uint8_t *p, uint32_t v) { + for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> 8 * i); +} + +static uint32_t crc32_of(const char *s) { + uint32_t c = ~0u; + for (; *s; s++) { + c ^= (uint8_t)*s; + for (int k = 0; k < 8; k++) c = c >> 1 ^ (0xEDB88320u & (0u - (c & 1))); + } + return ~c; +} + +Restbl *restbl_parse(const uint8_t *data, size_t n) { + if (n < 22 || memcmp(data, "RESTBL", 6)) return NULL; + uint32_t version = rd32(data + 6), name_len = rd32(data + 10), nh = rd32(data + 14), nn = rd32(data + 18); + if (!name_len || name_len > 4096 || (uint64_t)nh * 8 + (uint64_t)nn * (name_len + 4) != n - 22) return NULL; + Restbl *t = calloc(1, sizeof *t); + if (!t) return NULL; + t->version = version, t->name_len = name_len; + t->hashed = malloc(((size_t)nh + 1) * sizeof *t->hashed); + t->named = malloc((size_t)nn * (name_len + 4) + 1); + if (!t->hashed || !t->named) { + restbl_free(t); + return NULL; + } + for (uint32_t i = 0; i < nh; i++) t->hashed[i] = (Hashed){rd32(data + 22 + i * 8), rd32(data + 26 + i * 8)}; + t->nhashed = t->hashed_cap = nh; + if (!t->hashed_cap) t->hashed_cap = 1; + memcpy(t->named, data + 22 + (size_t)nh * 8, (size_t)nn * (name_len + 4)); + t->nnamed = nn; + return t; +} + +void restbl_free(Restbl *t) { + if (!t) return; + free(t->hashed); + free(t->named); + free(t); +} + +const char *restbl_key(const char *path, char *buf, size_t n) { + size_t len = strlen(path); + if (len >= 3 && (!strcmp(path + len - 3, ".zs") || !strcmp(path + len - 3, ".mc"))) len -= 3; + if (len >= n) len = n - 1; + memcpy(buf, path, len); + buf[len] = 0; + return buf; +} + +// The named entry of key, or NULL. +static uint8_t *named_of(const Restbl *t, const char *key) { + size_t len = strlen(key); + if (len >= t->name_len) return NULL; + for (size_t i = 0; i < t->nnamed; i++) { + uint8_t *e = t->named + i * (t->name_len + 4); + if (!memcmp(e, key, len + 1)) return e; + } + return NULL; +} + +// The position of hash in the hashed entries, or where it would go. +static size_t hashed_at(const Restbl *t, uint32_t hash) { + size_t lo = 0, hi = t->nhashed; + while (lo < hi) { + size_t m = lo + (hi - lo) / 2; + if (t->hashed[m].hash < hash) lo = m + 1; + else hi = m; + } + return lo; +} + +int restbl_get(const Restbl *t, const char *key, uint32_t *size) { + const uint8_t *e = named_of(t, key); + if (e) { + *size = rd32(e + t->name_len); + return 1; + } + uint32_t h = crc32_of(key); + size_t i = hashed_at(t, h); + if (i < t->nhashed && t->hashed[i].hash == h) { + *size = t->hashed[i].size; + return 1; + } + return 0; +} + +// The hashed entry of hash raised to size (added when missing); 1 when the table changed. +static int raise_hash(Restbl *t, uint32_t h, uint32_t size) { + size_t i = hashed_at(t, h); + if (i < t->nhashed && t->hashed[i].hash == h) { + if (t->hashed[i].size >= size) return 0; + t->hashed[i].size = size; + return 1; + } + if (t->nhashed == t->hashed_cap) { + size_t cap = t->hashed_cap * 2; + Hashed *grown = realloc(t->hashed, cap * sizeof *grown); + if (!grown) return 0; + t->hashed = grown, t->hashed_cap = cap; + } + memmove(t->hashed + i + 1, t->hashed + i, (t->nhashed - i) * sizeof *t->hashed); + t->hashed[i] = (Hashed){h, size}; + t->nhashed++; + return 1; +} + +int restbl_raise(Restbl *t, const char *key, uint32_t size) { + uint8_t *e = named_of(t, key); + if (!e) return raise_hash(t, crc32_of(key), size); + if (rd32(e + t->name_len) >= size) return 0; + wr32(e + t->name_len, size); + return 1; +} + +int restbl_merge(Restbl *t, const Restbl *base, const Restbl *shipped) { + int raised = 0; + for (size_t i = 0; i < shipped->nhashed; i++) { + Hashed s = shipped->hashed[i]; + size_t k = hashed_at(base, s.hash); + if (k < base->nhashed && base->hashed[k].hash == s.hash && base->hashed[k].size >= s.size) continue; + raised += raise_hash(t, s.hash, s.size); + } + if (shipped->name_len != t->name_len) return raised; + for (size_t i = 0; i < shipped->nnamed; i++) { + const uint8_t *e = shipped->named + i * (shipped->name_len + 4); + char key[4097]; + memcpy(key, e, shipped->name_len); + key[shipped->name_len] = 0; + uint32_t size = rd32(e + shipped->name_len), have; + if (restbl_get(base, key, &have) && have >= size) continue; + uint8_t *mine = named_of(t, key); + if (mine) { + if (rd32(mine + t->name_len) < size) wr32(mine + t->name_len, size), raised++; + continue; + } + uint8_t *grown = realloc(t->named, (t->nnamed + 1) * (t->name_len + 4)); + if (!grown) return raised; + t->named = grown; + memcpy(t->named + t->nnamed * (t->name_len + 4), e, t->name_len + 4); + t->nnamed++; + raised++; + } + return raised; +} + +int restbl_take(Restbl *t, const Restbl *from, const char *const *keys, size_t n) { + int raised = 0; + for (size_t i = 0; i < n; i++) { + uint32_t size; + if (restbl_get(from, keys[i], &size)) raised += restbl_raise(t, keys[i], size); + } + return raised; +} + +uint8_t *restbl_write(const Restbl *t, size_t *n) { + size_t size = 22 + t->nhashed * 8 + t->nnamed * (t->name_len + 4); + uint8_t *out = malloc(size); + if (!out) return NULL; + memcpy(out, "RESTBL", 6); + wr32(out + 6, t->version); + wr32(out + 10, t->name_len); + wr32(out + 14, (uint32_t)t->nhashed); + wr32(out + 18, (uint32_t)t->nnamed); + for (size_t i = 0; i < t->nhashed; i++) { + wr32(out + 22 + i * 8, t->hashed[i].hash); + wr32(out + 26 + i * 8, t->hashed[i].size); + } + memcpy(out + 22 + t->nhashed * 8, t->named, t->nnamed * (t->name_len + 4)); + *n = size; + return out; +} + +// ---- estimates + +// The factor a type's entry needs over its decompressed size (1000 = 1.0); 0 for a type the table never lists. +static unsigned type_factor(const char *key) { + static const struct { const char *ext; unsigned f; } types[] = { + {"bfres", 1500}, {"bkres", 2050}, {"bgyml", 3850}, {"asb", 1400}, {"bstar", 1200}, {"bphsc", 1100}, + {"byml", 1000}, {"txtg", 1000}, {"pack", 1000}, {"bntx", 1000}, {"bphsh", 1000}, {"vsts", 1000}, + {"ainb", 1000}, {"bphnm", 1000}, {"bars", 1000}, {"chunk", 1000}, {"bfevfl", 1000}, {"quad", 1000}, + {"baev", 1000}, {"bcul", 1000}, {"crbin", 1000}, {"wbr", 1000}, {"bhtmp", 1000}, {"dpi", 1000}, + {"cai", 1000}, {"png", 1000}, {"cutinfo", 1000}, {"blwp", 1000}, {"sarc", 1000}, {"tscb", 1000}, + {"bfsha", 1000}, {"jpg", 1000}, {"beco", 1000}, {"bfarc", 1000}, {"blarc", 1000}, {"belnk", 1000}, + {"genvb", 1000}, {"bnsh", 1000}, {"bslnk", 1000}, {"baatarc", 1000}, {"bagst", 1000}, + {"bwav", 0}, {"webm", 0}, + }; + const char *leaf = strrchr(key, '/') ? strrchr(key, '/') + 1 : key, *dot = strrchr(leaf, '.'); + if (!dot) return 1500; + for (size_t i = 0; i < sizeof types / sizeof *types; i++) + if (!strcmp(dot + 1, types[i].ext)) return types[i].f; + return 1500; +} + +static uint32_t clamp32(uint64_t v) { return v > 0xFFFFFFF0u ? 0xFFFFFFF0u : (uint32_t)v; } + +uint32_t restbl_estimate(const char *key, uint64_t size) { + unsigned f = type_factor(key); + if (!f) return 0; + uint64_t e = (size * f + 999) / 1000 + 8192; + return clamp32((e + 4095) & ~(uint64_t)4095); +} + +uint32_t restbl_grow(const char *key, uint32_t entry, uint64_t old, uint64_t new_size, int scaled) { + if (new_size <= old) return entry; + if (scaled) return clamp32(old ? ((uint64_t)entry * new_size + old - 1) / old : restbl_estimate(key, new_size * 8)); + unsigned f = type_factor(key); + if (!f) return entry; + uint64_t e = entry + ((new_size - old) * f + 999) / 1000; + return clamp32((e + 31) & ~(uint64_t)31); +} diff --git a/runtime/restbl.h b/runtime/restbl.h new file mode 100644 index 00000000..430480f5 --- /dev/null +++ b/runtime/restbl.h @@ -0,0 +1,33 @@ +// The game's resource size table (restbl.c): System/Resource/ResourceSizeTable.Product.<n>.Nin_NX_NVN.rsizetable.zs, a +// zstd-compressed RESTBL. The game sizes the buffer it loads a file into from the file's entry, so a mod file larger +// than the original needs a larger entry, and a file a mod adds needs one of its own. romfs.c merges the tables the +// mods ship and raises the entries of the files they serve (romfs.c restbl_overlay). +#pragma once +#include <stddef.h> +#include <stdint.h> + +typedef struct Restbl Restbl; + +// The table of a decompressed RESTBL (copied); NULL when it is not one. +Restbl *restbl_parse(const uint8_t *data, size_t n); +void restbl_free(Restbl *t); +// The table's key of a RomFS path: the path without a trailing .zs or .mc, into buf. +const char *restbl_key(const char *path, char *buf, size_t n); +// 1 with *size when key has an entry. +int restbl_get(const Restbl *t, const char *key, uint32_t *size); +// The entry of key raised to size (added when missing); 1 when the table changed. +int restbl_raise(Restbl *t, const char *key, uint32_t size); +// Every entry of shipped larger than base's (or missing from base) raised in t; the number of entries raised. +int restbl_merge(Restbl *t, const Restbl *base, const Restbl *shipped); +// Each entry of from whose key one of keys[0..n) names, raised in t; the number raised. +int restbl_take(Restbl *t, const Restbl *from, const char *const *keys, size_t n); +// The table as a decompressed RESTBL (malloc'd); NULL when out of memory. +uint8_t *restbl_write(const Restbl *t, size_t *n); + +// The entry a file of the key's type and decompressed size needs when the game has none for it; 0 for a type the +// table never lists (streamed sound and video). +uint32_t restbl_estimate(const char *key, uint64_t size); +// The entry of a file the game lists with `entry` for its own copy of old bytes, now new bytes: raised by the growth +// times the type's factor, never lowered. scaled: the sizes are compressed ones (a mesh codec .mc file), whose entry +// grows in proportion. +uint32_t restbl_grow(const char *key, uint32_t entry, uint64_t old, uint64_t new_size, int scaled); diff --git a/runtime/romfs.c b/runtime/romfs.c index 64129b0d..1f214f91 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -17,18 +17,33 @@ // TOTK_ROMFS_MODS; unset or "0" = none. A missing path, a size or hash mismatch or a changed original stops the start. // An overlay file is opened when the game opens it; its identity (volume, file id, size, last write) is kept from the // start check, and an open that finds another file there stops the game. +// +// Player mods (mods.c): the files under each enabled mod's RomFS root are overlays too, checked by identity only (no +// hash, no manifest). A path several overlays give is served by one: a player mod higher in the order beats a lower +// one, any player mod beats the TOTK_ROMFS_MODS manifest, which beats romfs-patches (overlay_contest). A mod path that +// matches a RomFS path only without case is served under the RomFS spelling. +// +// The resource size table: with a player mod enabled, the game's table is rebuilt at start with the tables the mods +// ship merged in and the entries of the mods' files raised to what they need (restbl_overlay), and served from +// <profile>/mods/ in place of the game's or a mod's table. #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <strings.h> #include "cfg.h" #include "fs.h" #include "host.h" +#include "input_map.h" +#include "mods.h" #include "romfs_config.h" #include "romfs_path.h" #include "platform/platform.h" +#include "restbl.h" #include "sha256.h" +#include <zstd.h> + #define NH 65536 // RomFS handle slots #define READ_CHUNK (64u << 20) // largest single read #define HASH_CHUNK (8u << 20) @@ -38,7 +53,9 @@ typedef struct { uint64_t offset, size; uint32_t name, len; } Entry; _Static_assert(sizeof(Entry) == 24, "index entry layout"); typedef struct { uint32_t volume, pad; uint64_t file_id, size, write_time, create_time; } Identity; // no padding -typedef struct { uint64_t index, size; char *path, *name; Identity id; } Overlay; +// prio: which overlay serves a path two of them give (the higher); romfs-patches 0, the TOTK_ROMFS_MODS manifest 1, a +// player mod 2 + its distance from the bottom of the order. mod: its index in mods_at + 1, 0 for any other overlay. +typedef struct { uint64_t index, size; char *path, *name; Identity id; int prio, mod; } Overlay; static uint8_t *index_bytes; static size_t index_size; @@ -56,6 +73,7 @@ static Overlay *overlays; // sorted by RomFS index after overlay_init static size_t noverlay, overlay_cap; static char **added; // RomFS paths of the added files, sorted; added[k] is index file_count + k static size_t nadded; +static int patches_loaded; // a romfs-patches manifest gave a file (romfs_patches_loaded) static void need(int c, const char *m) { if (!c) rt_fatal("RomFS: %s", m); @@ -204,8 +222,8 @@ static uint32_t overlay_slot(uint64_t i) { // ---- overlays // Loads and checks one overlay folder. A failed check stops the game, except with optional set (the Xbox prompts // folder): the folder is dropped, the reason logged, and 0 returned. Returns 1 when the folder is loaded or has no -// manifest. -static int overlay_load(const char *dir, int optional) { +// manifest. prio: the Overlay field (0 for the port's own folders, 1 for the TOTK_ROMFS_MODS manifest). +static int overlay_load(const char *dir, int optional, int prio) { #define BAD(...) do { if (!optional) rt_fatal(__VA_ARGS__); rt_log(__VA_ARGS__); goto bad; } while (0) char path[2100]; need(snprintf(path, sizeof path, "%s/manifest.tsv", dir) < (int)sizeof path, "overlay path"); @@ -262,7 +280,8 @@ static int overlay_load(const char *dir, int optional) { } char *copy = strdup(file), *rel = strdup(name); need(copy && rel, "overlay path copy"); - overlays[noverlay++] = (Overlay){i, size, copy, rel, id}; + overlays[noverlay++] = (Overlay){i, size, copy, rel, id, prio, 0}; + if (!prio) patches_loaded = 1; if (noverlay - first <= 16) rt_log("RomFS overlay: %s (%llu bytes) %s", name, size, add ? "is added" : "replaces the original"); } @@ -283,6 +302,207 @@ bad: #undef BAD } +// ---- player mods (mods.c): every file under an enabled mod's RomFS root, by identity (no hash: a texture pack is +// gigabytes and the player changes it at will; romfs_open still stops on a file that changed after the start) +static uint32_t *folded; // RomFS indices sorted by name ignoring ASCII case; built on the first mod path that misses + +static int folded_order(const void *a, const void *b) { + return strcasecmp(entry_name(entry(*(const uint32_t *)a)), entry_name(entry(*(const uint32_t *)b))); +} + +// The RomFS index of name, or of the RomFS path equal to it but for case (a mod made on Windows, where an emulator's +// folder lookup ignores case); UINT64_MAX for a path the RomFS lacks. +static uint64_t index_any_case(const char *name) { + uint64_t i = lower(name); + if (i < file_count && !strcmp(entry_name(entry(i)), name)) return i; + if (!folded) { + folded = malloc(file_count * sizeof *folded); + need(folded != NULL, "case-folded index"); + for (uint64_t k = 0; k < file_count; k++) folded[k] = (uint32_t)k; + qsort(folded, file_count, sizeof *folded, folded_order); + } + uint64_t lo = 0, hi = file_count; + while (lo < hi) { + uint64_t m = lo + (hi - lo) / 2; + if (strcasecmp(entry_name(entry(folded[m])), name) < 0) lo = m + 1; + else hi = m; + } + return lo < file_count && !strcasecmp(entry_name(entry(folded[lo])), name) ? folded[lo] : UINT64_MAX; +} + +static void overlay_push(Overlay o) { + if (noverlay == overlay_cap) { + overlay_cap = overlay_cap ? overlay_cap * 2 : 16; + overlays = realloc(overlays, overlay_cap * sizeof *overlays); + need(overlays != NULL, "overlay table"); + } + overlays[noverlay++] = o; +} + +// 1 when path names a file of one game version only (Product.<n> in a name: the resource size table, the actor +// database and other version-bound files); the RomFS lacking it means it is another version's. +static int other_version(const char *path) { + for (const char *p = strstr(path, ".Product."); p; p = strstr(p + 1, ".Product.")) { + const char *d = p + strlen(".Product."); + if (*d >= '0' && *d <= '9') return 1; + } + return 0; +} + +// ---- the resource size table's inputs: the tables the overlays carry, before overlay_contest keeps one of them +static char restbl_rel[256]; // the RomFS path of the game's table, "" when it has none +typedef struct { + char *file; + int mod, other; // mod: ModInfo index + 1, 0 for the port's folders; other: made for another game version +} Shipped; +static Shipped *shipped; +static size_t nshipped; + +static int ends_with(const char *s, const char *tail) { + size_t n = strlen(s), k = strlen(tail); + return n >= k && !strcmp(s + n - k, tail); +} + +// 1 when rel is a resource size table of any game version. +static int is_restbl(const char *rel) { + return !strncmp(rel, "System/Resource/ResourceSizeTable.", 34) && ends_with(rel, ".rsizetable.zs"); +} + +static void shipped_add(const char *file, int mod, int other) { + Shipped *grown = realloc(shipped, (nshipped + 1) * sizeof *shipped); + char *copy = strdup(file); + need(grown && copy, "resource size table list"); + shipped = grown; + shipped[nshipped++] = (Shipped){copy, mod, other}; +} + +// The game's table: the one System/Resource/ResourceSizeTable.*.rsizetable.zs of the RomFS. +static void restbl_find(void) { + for (uint64_t i = lower("System/Resource/ResourceSizeTable."); i < file_count; i++) { + const char *n = entry_name(entry(i)); + if (strncmp(n, "System/Resource/ResourceSizeTable.", 34)) break; + if (is_restbl(n) && strlen(n) < sizeof restbl_rel) { + snprintf(restbl_rel, sizeof restbl_rel, "%s", n); + return; + } + } +} + +// The tables the overlays carry for this game version, every one of them (overlay_contest keeps one). +static void restbl_collect(void) { + for (size_t k = 0; k < noverlay; k++) + if (restbl_rel[0] && !strcmp(overlays[k].name, restbl_rel)) shipped_add(overlays[k].path, overlays[k].mod, 0); +} + +typedef struct { int k, prio; const char *dir, *rel; } ModWalk; + +static void mod_walk(int k, int prio, const char *dir, const char *rel); + +// One entry of a mod's RomFS folder (mod_walk). +static int mod_walk_cb(const char *leaf, int kind, uint64_t size, void *ctx) { + (void)size; + const ModWalk *w = ctx; + ModInfo *m = mods_at(w->k); + char file[4200], path[4200]; + if (snprintf(file, sizeof file, "%s/%s", w->dir, leaf) >= (int)sizeof file || + snprintf(path, sizeof path, "%s%s%s", w->rel, *w->rel ? "/" : "", leaf) >= (int)sizeof path) { + rt_log("mods: %s: path too long under %s; left out", m->name, w->dir); + return 1; + } + if (kind == PLAT_FS_DIR) { + mod_walk(w->k, w->prio, file, path); + return 1; + } + if (kind != PLAT_FS_FILE) return 1; + if (!romfs_path_valid_relative(path)) { + rt_log("mods: %s: %s is not a RomFS path; left out", m->name, path); + return 1; + } + uint64_t i = index_any_case(path); + if (i == UINT64_MAX && other_version(path)) { + if (is_restbl(path)) shipped_add(file, w->k + 1, 1); // its entries for this mod's files still apply + m->other_version++; + return 1; + } + PlatFile *f = open_read(file); + if (!f) { + rt_log("mods: %s: cannot open %s; left out", m->name, file); + return 1; + } + Identity id = identity_of(f); + plat_file_close(f); + char *copy = strdup(file), *rp = strdup(i == UINT64_MAX ? path : entry_name(entry(i))); + need(copy && rp, "overlay path copy"); + overlay_push((Overlay){i == UINT64_MAX ? ADDED : i, id.size, copy, rp, id, w->prio, w->k + 1}); + return 1; +} + +// The files under dir (a mod's RomFS root; rel: the RomFS path of dir, "" at the root) as overlays of mod k. +static void mod_walk(int k, int prio, const char *dir, const char *rel) { + ModWalk w = {k, prio, dir, rel}; + plat_fs_list(dir, mod_walk_cb, &w); +} + +// What overlays compete on: the RomFS index, an added file's path; the higher prio first. +static int contest_order(const void *a, const void *b) { + const Overlay *x = a, *y = b; + if (x->index != y->index) return (x->index > y->index) - (x->index < y->index); + int c = x->index == ADDED ? strcmp(x->name, y->name) : 0; + return c ? c : y->prio - x->prio; +} + +static const char *owner_name(const Overlay *o) { + return o->mod ? mods_at(o->mod - 1)->name : o->prio ? "romfs_mods" : "the port's patches"; +} + +// One overlay per path: the highest prio serves it; the others are dropped and counted on their mod. +static void overlay_contest(void) { + if (!noverlay) return; + qsort(overlays, noverlay, sizeof *overlays, contest_order); + size_t kept = 0; + for (size_t k = 0; k < noverlay; k++) { + Overlay *o = &overlays[k], *w = kept ? &overlays[kept - 1] : NULL; + if (w && w->index == o->index && (o->index != ADDED || !strcmp(w->name, o->name))) { + if (o->mod) { + ModInfo *m = mods_at(o->mod - 1); + m->overridden++; + if (!m->overridden_by[0]) snprintf(m->overridden_by, sizeof m->overridden_by, "%s", owner_name(w)); + } else if (w->mod) { + rt_log("RomFS overlay: %s from mod %s replaces the one of %s", o->name, owner_name(w), owner_name(o)); + } + free(o->path); + free(o->name); + continue; + } + if (o->mod) { + if (o->index == ADDED) mods_at(o->mod - 1)->added++; + else mods_at(o->mod - 1)->replaced++; + } + overlays[kept++] = *o; + } + noverlay = kept; +} + +static void mods_overlay_init(void) { + mods_scan(); + int n = mods_count(); + for (int k = 0; k < n; k++) { + const ModInfo *m = mods_at(k); + // a mod with ExeFS patches waits for the rebuild that applies them: half of a mod is worse than none + if (m->enabled && (m->kind & MOD_ROMFS) && m->problem == MOD_OK) mod_walk(k, 2 + (n - k), m->romfs, ""); + } +} + +static void mods_overlay_log(void) { + for (int k = 0; k < mods_count(); k++) { + const ModInfo *m = mods_at(k); + if (!m->enabled || !(m->kind & MOD_ROMFS) || m->problem != MOD_OK) continue; + rt_log("mods: %s: %u files served (%u replaced, %u added), %u overridden%s%s, %u for another game version", + m->name, m->replaced + m->added, m->replaced, m->added, m->overridden, m->overridden ? " by " : "", + m->overridden_by, m->other_version); + } +} + static int overlay_order(const void *a, const void *b) { uint64_t x = ((const Overlay *)a)->index, y = ((const Overlay *)b)->index; return (x > y) - (x < y); @@ -292,7 +512,7 @@ static int added_order(const void *a, const void *b) { return strcmp((*(Overlay *const *)a)->name, (*(Overlay *const *)b)->name); } -// Number the added files in path order after the RomFS's own (file_count + k); two overlays adding one path stop. +// Number the added files in path order after the RomFS's own (file_count + k); overlay_contest left one per path. static void added_init(void) { Overlay **list = NULL; for (size_t k = 0; k < noverlay; k++) @@ -306,8 +526,6 @@ static void added_init(void) { added = malloc(nadded * sizeof *added); need(added != NULL, "added files"); for (size_t k = 0; k < nadded; k++) { - if (k && !strcmp(list[k]->name, list[k - 1]->name)) - rt_fatal("RomFS overlay: %s and %s both add %s", list[k - 1]->path, list[k]->path, list[k]->name); list[k]->index = file_count + k; added[k] = list[k]->name; } @@ -315,40 +533,46 @@ static void added_init(void) { rt_log("RomFS overlay: %zu files added to the RomFS", nadded); } -// The HeapDef table a bigger game resolution needs (resolution.c): its texture heap and that heap's parent grow. The -// base is an overlay that replaces the table (a mod's heap sizes stay), else the RomFS original; the grown table is -// written to <profile>/resolution/ and served in the base's place. -#define HEAPDEF "System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs" -static void heapdef_overlay(void) { - if (!res_heap_extra_kib()) return; - uint64_t i = lower(HEAPDEF); - if (i >= file_count || strcmp(entry_name(entry(i)), HEAPDEF)) { - rt_log("RomFS: %s missing; the game keeps its own resolution", HEAPDEF); - return; +// RomFS file `rel` as the game would read it: from an overlay that replaces it (*base), else from the image. malloc'd; +// NULL when it is missing, unreadable or 16 MiB or more (logged with `keeps`, what the game does without it). +static void *read_served(const char *rel, Overlay **base, size_t *n, const char *keeps) { + uint64_t i = lower(rel); + *base = NULL; + if (i >= file_count || strcmp(entry_name(entry(i)), rel)) { + rt_log("RomFS: %s missing; %s", rel, keeps); + return NULL; } - Overlay *base = NULL; for (size_t k = 0; k < noverlay; k++) - if (overlays[k].index == i) base = &overlays[k]; - PlatFile *f = base ? open_read(base->path) : image; - uint64_t off = base ? 0 : entry(i).offset, n = base ? base->size : entry(i).size; - void *in = n && n < (16u << 20) && f ? malloc((size_t)n) : NULL; - int ok = in && read_at(f, in, (uint32_t)n, off) == (int64_t)n; - if (base && f) plat_file_close(f); - void *out = NULL; - size_t out_n = 0; - ok = ok && res_heapdef_build(in, (size_t)n, &out, &out_n); - free(in); - if (!ok) return; + if (overlays[k].index == i) *base = &overlays[k]; + PlatFile *f = *base ? open_read((*base)->path) : image; + uint64_t off = *base ? 0 : entry(i).offset, size = *base ? (*base)->size : entry(i).size; + void *in = size && size < (16u << 20) && f ? malloc((size_t)size) : NULL; + int ok = in && read_at(f, in, (uint32_t)size, off) == (int64_t)size; + if (*base && f) plat_file_close(f); + if (!ok) { + free(in); + rt_log("RomFS: cannot read %s; %s", rel, keeps); + return NULL; + } + *n = (size_t)size; + return in; +} + +// Serves out[0..out_n) (malloc'd, freed here) in place of RomFS file `rel` and of an overlay that replaces it (base): +// written to <profile>/<sub>/<file name of rel>. `what` names the change in the log. 0 when it cannot be written. +static int serve_generated(const char *rel, Overlay *base, void *out, size_t out_n, const char *sub, const char *what, + const char *keeps) { char dir[2100], file[2200]; - need(snprintf(dir, sizeof dir, "%s/resolution", rt_config()->profile) < (int)sizeof dir, "resolution path"); - need(snprintf(file, sizeof file, "%s/Product.Nin_NX_NVN.HeapDef.byml.zs", dir) < (int)sizeof file, "resolution path"); + const char *leaf = strrchr(rel, '/') ? strrchr(rel, '/') + 1 : rel; + need(snprintf(dir, sizeof dir, "%s/%s", rt_config()->profile, sub) < (int)sizeof dir, "generated overlay path"); + need(snprintf(file, sizeof file, "%s/%s", dir, leaf) < (int)sizeof file, "generated overlay path"); plat_file_mkdir_p(dir); - ok = plat_file_write(file, out, out_n); + int ok = plat_file_write(file, out, out_n); free(out); PlatFile *r = ok ? open_read(file) : NULL; if (!r) { - rt_log("RomFS: cannot write %s; the game keeps its own resolution", file); - return; + rt_log("RomFS: cannot write %s; %s", file, keeps); + return 0; } Identity id = identity_of(r); plat_file_close(r); @@ -365,31 +589,217 @@ static void heapdef_overlay(void) { overlays = realloc(overlays, overlay_cap * sizeof *overlays); need(overlays != NULL, "overlay table"); } - char *rel = strdup(HEAPDEF); - need(rel != NULL, "overlay path copy"); - overlays[noverlay++] = (Overlay){i, out_n, copy, rel, id}; + char *name = strdup(rel); + need(name != NULL, "overlay path copy"); + overlays[noverlay++] = (Overlay){lower(rel), out_n, copy, name, id}; } - rt_log("RomFS overlay: %s (%zu bytes, grown heaps) replaces %s", file, out_n, base ? "the mod's table" : "the original"); - res_heapdef_served(); + rt_log("RomFS overlay: %s (%zu bytes, %s) replaces %s", file, out_n, what, base ? "the mod's file" : "the original"); + return 1; } -static int xbox_prompts_ready; // romfs-patches-xbox/manifest.tsv was next to romfs-index.bin at start -static int xbox_prompts_active; // and overlay_init loaded it +// ---- the resource size table (restbl.c) -int romfs_xbox_prompts_ready(void) { return xbox_prompts_ready; } -int romfs_xbox_prompts_active(void) { return xbox_prompts_active; } +// The decompressed size of a file's bytes as the game unpacks them: the content size its zstd frame records (.zs; +// the game's files and zstd's own tools write it), four times the bytes for a frame without one, else its size. +static uint64_t content_size(PlatFile *f, uint64_t off, uint64_t size, const char *rel) { + if (!ends_with(rel, ".zs") || !f) return size; + uint8_t head[18]; + uint32_t n = size < sizeof head ? (uint32_t)size : (uint32_t)sizeof head; + if (read_at(f, head, n, off) != n) return size; + unsigned long long c = ZSTD_getFrameContentSize(head, n); + return c == ZSTD_CONTENTSIZE_ERROR ? size : c == ZSTD_CONTENTSIZE_UNKNOWN ? size * 4 : c; +} + +// A table from its zstd bytes; NULL when they are not one. +static Restbl *restbl_unpack(const void *z, size_t n) { + unsigned long long raw = ZSTD_getFrameContentSize(z, n); + if (raw == ZSTD_CONTENTSIZE_UNKNOWN || raw == ZSTD_CONTENTSIZE_ERROR || raw > (64u << 20)) return NULL; + uint8_t *d = malloc((size_t)raw ? (size_t)raw : 1); + size_t got = d ? ZSTD_decompress(d, (size_t)raw, z, n) : 0; + Restbl *t = d && !ZSTD_isError(got) && got == raw ? restbl_parse(d, (size_t)raw) : NULL; + free(d); + return t; +} + +// With a player mod (or the TOTK_ROMFS_MODS manifest) serving files: the game's table with every shipped table of this +// version merged in (an entry larger than the game's wins), the entries a mod's table for another version gives for +// that mod's files, and each served mod file's entry raised to what its size needs (restbl_grow for a file the game +// has, restbl_estimate for an added one). Never lowers the game's entries. +static void restbl_overlay(void) { + int player = 0; + for (size_t k = 0; k < noverlay; k++) player |= overlays[k].mod || overlays[k].prio == 1; + if (!player || !restbl_rel[0]) return; + static const char keeps[] = "mod files that grew may fail to load"; + double t0 = now_s(); + uint64_t gi = lower(restbl_rel); + Entry ge = entry(gi); + void *gz = ge.size < (64u << 20) ? malloc((size_t)ge.size) : NULL; + Restbl *base = NULL, *out = NULL; + if (gz && read_at(image, gz, (uint32_t)ge.size, ge.offset) == (int64_t)ge.size) { + base = restbl_unpack(gz, (size_t)ge.size); + out = restbl_unpack(gz, (size_t)ge.size); + } + free(gz); + if (!base || !out) { + rt_log("RomFS: cannot read the resource size table %s; %s", restbl_rel, keeps); + restbl_free(base); + restbl_free(out); + return; + } + int tables = 0, merged = 0, raised = 0, estimated = 0; + for (size_t s = 0; s < nshipped; s++) { + uint64_t n = 0; + void *z = plat_file_read(shipped[s].file, &n); + Restbl *t = z ? restbl_unpack(z, (size_t)n) : NULL; + free(z); + if (!t) { + rt_log("RomFS: %s is not a resource size table; left out", shipped[s].file); + continue; + } + tables++; + if (!shipped[s].other) { + merged += restbl_merge(out, base, t); + } else { // another version's table: only the entries of the files its mod serves + for (size_t k = 0; k < noverlay; k++) { + char key[4200]; + const char *kp = restbl_key(overlays[k].name, key, sizeof key); + if (overlays[k].mod == shipped[s].mod) merged += restbl_take(out, t, &kp, 1); + } + } + restbl_free(t); + } + Overlay *served = NULL; + for (size_t k = 0; k < noverlay; k++) { + Overlay *o = &overlays[k]; + if (o->index == gi) served = o; + if ((!o->mod && o->prio != 1) || o->index == gi) continue; + char key[4200]; + restbl_key(o->name, key, sizeof key); + int mc = ends_with(o->name, ".mc"); + PlatFile *f = open_read(o->path); + uint64_t now = mc ? o->size : content_size(f, 0, o->size, o->name); + if (f) plat_file_close(f); + uint32_t entry_size, want; + if (o->index != ADDED) { + if (!restbl_get(base, key, &entry_size)) continue; // the game sizes it without the table (streamed) + Entry e = entry(o->index); + uint64_t was = mc ? e.size : content_size(image, e.offset, e.size, o->name); + want = restbl_grow(key, entry_size, was, now, mc); + } else { + want = restbl_estimate(key, mc ? now * 8 : now); // a mesh codec file unpacks to several times its size + estimated += want != 0; + } + raised += want && restbl_raise(out, key, want); + } + restbl_free(base); + if (!merged && !raised && !served) { + restbl_free(out); + rt_log("RomFS: resource size table: the mods' files fit the game's entries (%.0f ms)", (now_s() - t0) * 1000); + return; + } + size_t rn = 0, zn = 0; + uint8_t *raw = restbl_write(out, &rn); + restbl_free(out); + size_t cap = raw ? ZSTD_compressBound(rn) : 0; + void *z = raw ? malloc(cap) : NULL; + if (z) zn = ZSTD_compress(z, cap, raw, rn, 3); + free(raw); + if (!z || ZSTD_isError(zn)) { + free(z); + rt_log("RomFS: cannot pack the merged resource size table; %s", keeps); + return; + } + if (serve_generated(restbl_rel, served, z, zn, "mods", "merged resource sizes", keeps)) + rt_log("RomFS: resource size table: %d tables merged (%d entries), %d entries raised for mod files (%d " + "added files estimated), %.0f ms", tables, merged, raised, estimated, (now_s() - t0) * 1000); +} + +// The HeapDef table a bigger game resolution needs (resolution.c): its texture heap and that heap's parent grow. The +// base is an overlay that replaces the table (a mod's heap sizes stay), else the RomFS original; the grown table is +// written to <profile>/resolution/ and served in the base's place. +#define HEAPDEF "System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs" +static void heapdef_overlay(void) { + if (!res_heap_extra_kib()) return; + static const char keeps[] = "the game keeps its own resolution"; + Overlay *base; + size_t n; + void *in = read_served(HEAPDEF, &base, &n, keeps), *out = NULL; + size_t out_n = 0; + int ok = in && res_heapdef_build(in, n, &out, &out_n); + free(in); + if (ok && serve_generated(HEAPDEF, base, out, out_n, "resolution", "grown heaps", keeps)) res_heapdef_served(); +} + +// The player camera's pack with its stick speeds multiplied by TOTK_STICK_SENS (camera_speed.c). The base is an +// overlay that replaces the pack (a mod's camera stays), else the RomFS original; the dictionary is the RomFS's own. +#define CAMPACK "Pack/Actor/PlayerCamera.pack.zs" +static void camera_overlay(void) { + if (cam_speed_wanted() == 1.0f) return; + static const char keeps[] = "the camera keeps its own speed"; + Overlay *base, *dic_base; + size_t n, dn; + void *in = read_served(CAMPACK, &base, &n, keeps), *out = NULL; + void *dic = in ? read_served("Pack/ZsDic.pack.zs", &dic_base, &dn, keeps) : NULL; + size_t out_n = 0; + int ok = in && dic && cam_pack_build(in, n, dic, dn, &out, &out_n); + free(in); + free(dic); + if (ok && serve_generated(CAMPACK, base, out, out_n, "camera", "camera speed", keeps)) cam_pack_served(); +} + +// The button prompts' folders (input_prompts_folder) and whether each had a manifest next to romfs-index.bin at start; +// the one the settings asked for at start, the one overlay_init loaded (NULL: the game's own prompts) and why the first +// did not load (ROMFS_PROMPTS_*). The menu compares them with the settings in effect (ui/menu.cpp prompts_note). +static struct { const char *name; int ready; } prompts_folders[2]; +static const char *prompts_wanted, *prompts_loaded; +static int prompts_off; + +const char *romfs_prompts(void) { return prompts_loaded; } + +int romfs_prompts_ready(const char *folder) { + for (int i = 0; i < 2; i++) + if (folder && prompts_folders[i].name && !strcmp(prompts_folders[i].name, folder)) return prompts_folders[i].ready; + return 0; +} + +int romfs_prompts_off(const char *folder) { + return folder && prompts_wanted && !strcmp(folder, prompts_wanted) ? prompts_off : ROMFS_PROMPTS_OK; +} + +// The first RomFS file that the overlays [a, b) and [c, d) both replace, or NULL. +static const char *overlay_clash(size_t a, size_t b, size_t c, size_t d) { + for (size_t i = a; i < b; i++) + for (size_t j = c; j < d; j++) + if (overlays[i].index != ADDED && overlays[i].index == overlays[j].index && + strcmp(overlays[i].name, restbl_rel)) // restbl_overlay merges both tables + return overlays[i].name; + return NULL; +} + +// Drops the overlays [a, b), the ones after them moving down. +static void overlay_drop(size_t a, size_t b) { + for (size_t k = a; k < b; k++) { + free((void *)overlays[k].path); + free((void *)overlays[k].name); + } + memmove(overlays + a, overlays + b, (noverlay - b) * sizeof *overlays); + noverlay -= b - a; +} static void overlay_init(void) { // TOTK_ROMFS_PATCHES unset: romfs-patches next to romfs-index.bin if it has a manifest (tools/make_romfs_patches.py). // A game resolution that is not 16:9 takes romfs-patches-<w>x<h> instead when it is there: the same overlay with // the UI laid out for that shape (make_romfs_patches.py --aspect), when the game version has the camera aspect patch // the layout goes with (res_wants_wide_overlay). - // With pad_layout = xbox_prompts, romfs-patches-xbox in its place: the same patches with the A/B and X/Y prompts - // swapped (make_romfs_patches.py --xbox-prompts). The game reads the archives once at boot, so the prompts stay - // until restart even though the layout itself changes live. Each folder replaces romfs-patches as a whole and the - // setup builds no wide folder with Xbox prompts, so a wide game resolution keeps the Switch prompts. - char def[2100], xbox[2100]; - const char *patches = cfg_get("TOTK_ROMFS_PATCHES"); + // With prompts = xbox, the Xbox prompts' folder for the pad layout in its place (input_prompts_folder): the same + // patches with Xbox button icons (make_romfs_patches.py --prompts). The game reads the archives once at boot, so + // the prompts stay until restart even though the layout itself changes live. Each folder replaces romfs-patches as + // a whole and the setup builds no folder of another shape with Xbox prompts, so a game resolution that is not 16:9 + // keeps the Switch prompts. So does a RomFS mod (the TOTK_ROMFS_MODS manifest or a player mod) that replaces one of + // the prompts' fonts: romfs-patches loads in their place, so the mod's file is not served beside prompts files made + // for the game's own. A mod's resource size table is no clash: restbl_overlay merges it with the prompts' one. + char def[2100], path[2100]; + const char *patches = cfg_get("TOTK_ROMFS_PATCHES"), *mods = cfg_get("TOTK_ROMFS_MODS"); int wide = 0; unsigned out[2]; res_wanted(out); @@ -403,33 +813,64 @@ static void overlay_init(void) { "(tools/make_romfs_patches.py --aspect %ux%u)", out[0], out[1], out[0], out[1]); } } - xbox_prompts_ready = - snprintf(xbox, sizeof xbox, "%s/romfs-patches-xbox/manifest.tsv", index_dir) < (int)sizeof xbox && - plat_file_exists(xbox); - int want_xbox = !strcasecmp(cfg_value("TOTK_PAD_LAYOUT"), "xbox_prompts"); - if (!patches && want_xbox && xbox_prompts_ready) { - xbox[strlen(xbox) - strlen("/manifest.tsv")] = 0; - xbox_prompts_active = overlay_load(xbox, 1); - if (xbox_prompts_active) rt_log("RomFS overlay: Xbox button prompts (%s)", xbox); - else rt_log("RomFS overlay: %s failed its check (run the setup again); romfs-patches loads instead", xbox); - } else if (want_xbox) { - rt_log("RomFS overlay: pad_layout = xbox_prompts, but %s; Switch prompts stay", - wide ? "the game resolution takes the wide UI layout" - : patches ? "TOTK_ROMFS_PATCHES is set" - : "romfs-patches-xbox has no manifest.tsv (run the setup again)"); + static const char *const layouts[2] = {"nintendo", "xbox"}; // one of each folder + for (int i = 0; i < 2; i++) { + prompts_folders[i].name = input_prompts_folder(PROMPTS_XBOX, layouts[i]); + prompts_folders[i].ready = + snprintf(path, sizeof path, "%s/%s/manifest.tsv", index_dir, prompts_folders[i].name) < (int)sizeof path && + plat_file_exists(path); } - if (!patches && !xbox_prompts_active && snprintf(def, sizeof def, "%s/romfs-patches/manifest.tsv", index_dir) < (int)sizeof def && - plat_file_exists(def)) { - def[strlen(def) - strlen("/manifest.tsv")] = 0; - patches = def; + const char *layout = cfg_value("TOTK_PAD_LAYOUT"); + prompts_wanted = input_prompts_folder(input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout), layout); + prompts_off = !prompts_wanted ? ROMFS_PROMPTS_OK + : wide ? ROMFS_PROMPTS_SHAPE + : patches ? ROMFS_PROMPTS_PATCHES + : !romfs_prompts_ready(prompts_wanted) ? ROMFS_PROMPTS_MISSING + : ROMFS_PROMPTS_OK; + restbl_find(); + size_t first = noverlay; + if (prompts_wanted && prompts_off == ROMFS_PROMPTS_OK) { + snprintf(path, sizeof path, "%s/%s", index_dir, prompts_wanted); // fits: its manifest path did + if (overlay_load(path, 1, 0)) { + prompts_loaded = prompts_wanted; + rt_log("RomFS overlay: Xbox button prompts (%s)", path); + } else { + prompts_off = ROMFS_PROMPTS_FAILED; + rt_log("RomFS overlay: %s failed its check (run the setup again); romfs-patches loads instead", path); + } + } else if (prompts_wanted) { + rt_log("RomFS overlay: prompts = xbox, but %s; Switch prompts stay", + prompts_off == ROMFS_PROMPTS_SHAPE ? "the game resolution takes its own UI layout" + : prompts_off == ROMFS_PROMPTS_PATCHES ? "TOTK_ROMFS_PATCHES is set" + : "their folder has no manifest.tsv (run the setup again)"); } - const char *dirs[2] = {patches, cfg_get("TOTK_ROMFS_MODS")}; - for (int i = 0; i < 2; i++) - if (dirs[i] && *dirs[i] && strcmp(dirs[i], "0")) overlay_load(dirs[i], 0); + size_t prompts_end = noverlay; + char plain[2100]; // romfs-patches, when it has a manifest + int plain_ok = snprintf(plain, sizeof plain, "%s/romfs-patches/manifest.tsv", index_dir) < (int)sizeof plain && + plat_file_exists(plain); + if (plain_ok) plain[strlen(plain) - strlen("/manifest.tsv")] = 0; + if (!patches && !prompts_loaded && plain_ok) patches = plain; + if (patches && *patches && strcmp(patches, "0")) overlay_load(patches, 0, 0); + size_t mods_first = noverlay; + if (mods && *mods && strcmp(mods, "0")) overlay_load(mods, 0, 1); + mods_overlay_init(); + const char *clash = prompts_loaded ? overlay_clash(first, prompts_end, mods_first, noverlay) : NULL; + if (clash) { + rt_log("RomFS overlay: a RomFS mod replaces %s, which the Xbox prompts change too; Switch prompts stay", clash); + overlay_drop(first, prompts_end); + prompts_loaded = NULL; + prompts_off = ROMFS_PROMPTS_MODS; + if (plain_ok) overlay_load(plain, 0, 0); + } + restbl_collect(); + overlay_contest(); + mods_overlay_log(); + restbl_overlay(); heapdef_overlay(); + camera_overlay(); added_init(); if (noverlay) qsort(overlays, noverlay, sizeof *overlays, overlay_order); - for (size_t k = 1; k < noverlay; k++) + for (size_t k = 1; k < noverlay; k++) // overlay_contest left one per file; the generated ones replace in place if (overlays[k].index == overlays[k - 1].index) rt_fatal("RomFS overlay: %s and %s replace the same file %s", overlays[k - 1].path, overlays[k].path, overlays[k].name); @@ -551,6 +992,8 @@ const char *romfs_image_path(void) { return image_file; } size_t romfs_overlay_count(void) { return noverlay; } +int romfs_patches_loaded(void) { return patches_loaded; } + const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_added) { if (k >= noverlay) return NULL; *file = overlays[k].path; diff --git a/runtime/shaders/contact_shadow.comp b/runtime/shaders/contact_shadow.comp new file mode 100644 index 00000000..78828bb4 --- /dev/null +++ b/runtime/shaders/contact_shadow.comp @@ -0,0 +1,67 @@ +#version 450 +// Screen-space contact shadows (runtime/vk_contact.c). Each pixel of the game's lit scene colour marches a short way +// toward the sun through the linear depth target; when the ray passes behind something close in front of it, the pixel +// is in a contact shadow the shadow maps are too coarse to show, and its colour is darkened. +layout(local_size_x = 8, local_size_y = 8) in; + +layout(binding = 0) uniform sampler2D lin_depth; // the game's linear depth: view distance / far +layout(binding = 1, r11f_g11f_b10f) uniform image2D scene; // the lit scene colour, darkened in place + +layout(push_constant) uniform Params { + vec4 proj; // projection x scale, y scale, far plane, strength (0..1) + vec4 light; // view-space direction toward the sun (xyz), march length in metres at 10 m (w) + vec4 size; // width, height, 1/width, 1/height + vec4 opt; // x: debug (1: write the shadow factor as grey) +} pc; + +vec3 view_pos(vec2 uv, float z) { // z: distance in front of the camera + vec2 ndc = uv * 2.0 - 1.0; + return vec3(ndc.x * z / pc.proj.x, -ndc.y * z / pc.proj.y, -z); +} + +vec2 to_uv(vec3 p) { + float z = -p.z; + return vec2(0.5 + 0.5 * p.x * pc.proj.x / z, 0.5 - 0.5 * p.y * pc.proj.y / z); +} + +float ign(vec2 px) { // interleaved gradient noise: a per-pixel start offset that hides the step pattern + return fract(52.9829189 * fract(dot(px, vec2(0.06711056, 0.00583715)))); +} + +void main() { + ivec2 px = ivec2(gl_GlobalInvocationID.xy); + if (px.x >= int(pc.size.x) || px.y >= int(pc.size.y)) return; + float d = texelFetch(lin_depth, px, 0).x; + if (d <= 0.0 || d >= 0.999) return; // nothing drawn, or the sky + float z = d * pc.proj.z; + if (z > 80.0) return; + vec2 uv = (vec2(px) + 0.5) * pc.size.zw; + vec3 p = view_pos(uv, z); + // the march grows with distance, as one pixel covers more ground; thickness keeps thin blades from hiding the sky + float len = pc.light.w * clamp(z / 10.0, 0.4, 3.0); + float thick = 0.15 + z * 0.01; + const int STEPS = 16; + float step_len = len / float(STEPS), t = step_len * (0.25 + ign(vec2(px))); + float occ = 0.0; + for (int i = 0; i < STEPS; i++, t += step_len) { + vec3 q = p + pc.light.xyz * t; + if (q.z >= -0.05) break; + vec2 quv = to_uv(q); + if (any(lessThan(quv, vec2(0.0))) || any(greaterThan(quv, vec2(1.0)))) break; + float sz = texelFetch(lin_depth, ivec2(quv * pc.size.xy), 0).x * pc.proj.z; + float delta = -q.z - sz; // how far the scene at that pixel is in front of the ray point + if (delta > 0.02 + z * 0.002 && delta < thick) { + occ = 1.0 - float(i) / float(STEPS); // a hit close to the pixel shadows more than one at the end + break; + } + } + float fade = 1.0 - smoothstep(40.0, 80.0, z); + float k = 1.0 - pc.proj.w * occ * fade; + if (pc.opt.x > 0.5) { + imageStore(scene, px, vec4(k, k, k, 1.0)); + return; + } + if (k >= 0.999) return; + vec4 c = imageLoad(scene, px); + imageStore(scene, px, vec4(c.rgb * k, c.a)); +} diff --git a/runtime/shaders/dlaa_mv.comp b/runtime/shaders/dlaa_mv.comp new file mode 100644 index 00000000..dcd5e810 --- /dev/null +++ b/runtime/shaders/dlaa_mv.comp @@ -0,0 +1,63 @@ +#version 450 +// Camera motion vectors for DLAA (vk_dlaa.c mv_pass): each pixel's depth is taken back to the world through this +// frame's view-projection and forward through last frame's; the vector is last frame's window position minus this +// frame's, in render pixels, without the jitter (DLSS's default convention). Objects that move on their own get only +// the camera's motion. +// The camera block is TotK's scene uniform block (bound as c4 in the scene's vertex shaders): vec4 rows 3-6 are the +// view-projection, rows of a row-major 4x4 with OpenGL clip depth (-w..w). The replay's viewports have negative +// heights, so window y grows downwards from ndc y = +1, and depth is z_ndc * 0.5 + 0.5. +// With pc.bias > 0 the pass also writes DLSS's bias mask (vk_dlaa.c, "characters"): G-buffer target 0 carries an object +// class in green, above 64/255 for characters, creatures and what they hold (Link 253, others 125, held items 160; +// terrain 0, foliage 32). A pixel gets pc.bias when a character covers it now, or covered the place its surface came +// from last frame (the previous class mask, read through the motion vector, 3x3): there the history holds a moving +// character that the camera-only vector does not follow, which is the trail. chars_out keeps this frame's mask for the +// next; chars_prev is last frame's (pc.prev_ok 0: none). +layout(local_size_x = 8, local_size_y = 8) in; +layout(binding = 0) uniform sampler2D depth_tex; +layout(binding = 1, rg16f) uniform writeonly image2D mv_out; +layout(binding = 2, std140) uniform Cur { vec4 r[16]; } cur; +layout(binding = 3, std140) uniform Prev { vec4 r[16]; } prev; +layout(binding = 4) uniform sampler2D ids_tex; +layout(binding = 5, r8) uniform writeonly image2D mask_out; +layout(binding = 6, r8) uniform readonly image2D chars_prev; +layout(binding = 7, r8) uniform writeonly image2D chars_out; +layout(push_constant) uniform P { + vec2 size; // render size in pixels + vec2 jitter; // this frame's jitter, pixels, as the scene was drawn (window x right, y down) + float bias; // the mask value for character pixels; 0: no mask (bindings 4-7 unused) + float prev_ok; // 1: chars_prev holds last frame's class mask at this size +} pc; + +mat4 vp(vec4 a, vec4 b, vec4 c, vec4 d) { return transpose(mat4(a, b, c, d)); } + +void main() { + ivec2 p = ivec2(gl_GlobalInvocationID.xy); + if (p.x >= int(pc.size.x) || p.y >= int(pc.size.y)) return; + float d = texelFetch(depth_tex, p, 0).r; + vec2 win = vec2(p) + 0.5 - pc.jitter; // where this pixel's surface sits without the jitter + vec4 ndc = vec4(win.x / pc.size.x * 2.0 - 1.0, 1.0 - win.y / pc.size.y * 2.0, d * 2.0 - 1.0, 1.0); + mat4 c = vp(cur.r[3], cur.r[4], cur.r[5], cur.r[6]); + mat4 q = vp(prev.r[3], prev.r[4], prev.r[5], prev.r[6]); + vec4 world = inverse(c) * ndc; + vec2 mv = vec2(0.0); + if (abs(world.w) > 1e-12) { + vec4 clip = q * (world / world.w); + if (clip.w > 1e-6) { + vec2 pn = clip.xy / clip.w; + vec2 pwin = vec2((pn.x * 0.5 + 0.5) * pc.size.x, (0.5 - pn.y * 0.5) * pc.size.y); + mv = pwin - win; + } + } + imageStore(mv_out, p, vec4(mv, 0.0, 0.0)); + if (pc.bias <= 0.0) return; + float ch = texelFetch(ids_tex, p, 0).g > 64.0 / 255.0 ? 1.0 : 0.0; + imageStore(chars_out, p, vec4(ch)); + float was = 0.0; + if (pc.prev_ok > 0.5) { + ivec2 q = ivec2(floor(win + mv)), hi = ivec2(pc.size) - 1; + for (int dy = -1; dy <= 1; dy++) + for (int dx = -1; dx <= 1; dx++) + was = max(was, imageLoad(chars_prev, clamp(q + ivec2(dx, dy), ivec2(0), hi)).r); + } + imageStore(mask_out, p, vec4(max(ch, was) * pc.bias)); +} diff --git a/runtime/shaders/hdr_encode.comp b/runtime/shaders/hdr_encode.comp new file mode 100644 index 00000000..c6fb04da --- /dev/null +++ b/runtime/shaders/hdr_encode.comp @@ -0,0 +1,34 @@ +#version 450 +// HDR10 encode on the present (runtime/vk_hdr.c vk_hdr_encode): the game's picture, crop rectangle at offset, linear +// Rec. 709 with the HUD's white at 1.0 (an sRGB view decodes the 8-bit window texture), into an RGBA16F image of PQ +// code values in Rec. 2020 that FSR 1 or the blit then scale into the HDR10 swapchain. p.x: nits of 1.0, p.y: the +// display's peak in nits, p.z: 1 when the source holds sRGB-encoded values in a UNORM format. +// PQ: SMPTE ST 2084; the Rec. 709 to Rec. 2020 matrix: ITU-R BT.2087. +layout(local_size_x = 8, local_size_y = 8) in; +layout(binding = 0) uniform sampler2D src; +layout(binding = 1, rgba16f) uniform writeonly image2D dst; +layout(push_constant) uniform P { + ivec2 offset; + ivec2 size; + vec4 p; +} pc; + +vec3 srgb_decode(vec3 c) { + return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), greaterThan(c, vec3(0.04045))); +} + +vec3 pq(vec3 nits) { + const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875; + vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1)); + return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2)); +} + +void main() { + ivec2 at = ivec2(gl_GlobalInvocationID.xy); + if (any(greaterThanEqual(at, pc.size))) return; + vec3 c = max(texelFetch(src, at + pc.offset, 0).rgb, vec3(0.0)); + if (pc.p.z > 0.5) c = srgb_decode(clamp(c, 0.0, 1.0)); + const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956); + vec3 nits = min(max(to2020 * c, vec3(0.0)) * pc.p.x, vec3(pc.p.y)); + imageStore(dst, at, vec4(pq(nits), 1.0)); +} diff --git a/runtime/shaders/hdr_fullscreen.vert b/runtime/shaders/hdr_fullscreen.vert new file mode 100644 index 00000000..ff645324 --- /dev/null +++ b/runtime/shaders/hdr_fullscreen.vert @@ -0,0 +1,8 @@ +#version 450 +// One triangle over the whole target, for hdr_ui.frag (runtime/vk_hdr.c vk_hdr_ui_compose). +layout(location = 0) out vec2 uv; + +void main() { + uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/runtime/shaders/hdr_tonemap.frag b/runtime/shaders/hdr_tonemap.frag new file mode 100644 index 00000000..4de1f2d6 --- /dev/null +++ b/runtime/shaders/hdr_tonemap.frag @@ -0,0 +1,74 @@ +#version 450 +// The game's tonemap pass for HDR output (runtime/vk_hdr.c vk_hdr_shader_swap puts it in place of the translated +// shader when TOTK_HDR=1). Same inputs as the game's: fp_t_tcb_8 (bloom, clamped to 10000 as the game does) plus +// fp_t_tcb_A (the scene), weights in c1[0] and the saturation grade in c3[0]. The game maps luminance and each channel +// with 1 - e^-x, blends the two by the luminance curve squared, clamps to 0..1 and grades saturation. Here the curve is +// the game's up to PIVOT and a straight line with its slope above, so mid-greys look as in SDR and highlights keep +// going; the grade works on the colour divided by its largest channel above 1; a roll-off on the largest channel stops +// at the display's peak. The output is relative to the HUD's white (1.0), which the present maps to TOTK_HDR_UI_NITS. +// Hue correction (hdr.z, TOTK_HDR_HUE): the game's SDR clamp turns bright saturated colours towards white (a torch flame +// clips red, then green, and reads yellow-white, the colour its emitted light was tuned to); the extended curve keeps +// their ratios, so they stay saturated. From the picture's white upwards the colour moves towards the game's own SDR +// result at the HDR luminance, by up to hdr.z (RenoDX's TotK mod has the same control). +// Approach after RenoDX's TotK mod by souperman9 and Musa (github.com/clshortfuse/renodx, src/games/totk, MIT): its +// "extended vanilla" curve. +// Bindings are those of the game's translated shader: vk_hdr.c rewrites them from the original's names. +layout(location = 0) in vec4 uv; +layout(location = 0) out vec4 color; + +layout(set = 0, binding = 0, std140) uniform SupportBuffer { + vec4 pad[90]; + vec4 hdr; // byte 1440 (vk_exec_draw.c support_fill): game white / HUD white, peak / game white, hue correction +} support_buffer; +layout(set = 0, binding = 1) uniform sampler2D fp_t_tcb_8; +layout(set = 0, binding = 2) uniform sampler2D fp_t_tcb_A; +layout(set = 0, binding = 3, std140) uniform C1 { vec4 v[4096]; } fp_c1; +layout(set = 0, binding = 4, std140) uniform C3 { vec4 v[4096]; } fp_c3; + +const float PIVOT = 0.18; + +float curve(float x) { // the game's 1 - e^-x below PIVOT, its tangent there above + float y0 = 1.0 - exp(-PIVOT); + return x <= PIVOT ? 1.0 - exp(-x) : y0 + exp(-PIVOT) * (x - PIVOT); +} + +vec3 curve3(vec3 x) { return vec3(curve(x.r), curve(x.g), curve(x.b)); } + +float maxc(vec3 c) { return max(c.r, max(c.g, c.b)); } + +void main() { + vec3 x = min(texture(fp_t_tcb_8, uv.xy).rgb, vec3(10000.0)) + texture(fp_t_tcb_A, uv.xy).rgb; + vec4 w = fp_c1.v[0], g = fp_c3.v[0]; + float game = support_buffer.hdr.x > 0.0 ? support_buffer.hdr.x : 1.0; + float peak = support_buffer.hdr.y > 1.0 ? support_buffer.hdr.y : 1.0; + + float lum = max(x.r * 0.2989 + x.g * w.x + x.b * w.y, 0.0); + float sdr = 1.0 - exp(-lum); + vec3 by_lum = lum > 0.0 ? x * (curve(lum) / lum) : x; + vec3 c = max(mix(by_lum, curve3(x), sdr * sdr), vec3(0.0)); + + // the game's saturation grade, on the colour scaled into 0..1 by its largest channel + float s = max(maxc(c), 1.0); + vec3 n = c / s; + float m = maxc(n); + float d = n.r * 0.6666667 + (n.g + n.b) * w.z - 1.0; + float grade = g.y * (1.0 - d * d) + g.x; + c = s * ((n - m) * grade + m); + + // the game's own SDR result: the same blend clamped to 0..1, graded + vec3 sdr_c = clamp(mix(lum > 0.0 ? x * (sdr / lum) : x, vec3(1.0) - exp(-x), sdr * sdr), 0.0, 1.0); + float sm = maxc(sdr_c), sd = sdr_c.r * 0.6666667 + (sdr_c.g + sdr_c.b) * w.z - 1.0; + sdr_c = (sdr_c - sm) * (g.y * (1.0 - sd * sd) + g.x) + sm; + float hue = clamp(support_buffer.hdr.z, 0.0, 1.0); + const vec3 Y = vec3(0.2126, 0.7152, 0.0722); // Rec. 709 luminance + float yc = dot(c, Y), ys = dot(sdr_c, Y); + if (hue > 0.0 && ys > 1e-5) c = mix(c, max(sdr_c, vec3(0.0)) * (yc / ys), hue * smoothstep(0.5, 1.5, maxc(c))); + + // roll-off of the largest channel from half the peak to the peak + float top = maxc(c), knee = 0.5 * peak; + if (top > knee) { + float r = peak - knee; + c *= (knee + r * (1.0 - exp(-(top - knee) / r))) / top; + } + color = vec4(max(c, vec3(0.0)) * game, 0.0); +} diff --git a/runtime/shaders/hdr_ui.frag b/runtime/shaders/hdr_ui.frag new file mode 100644 index 00000000..30299d03 --- /dev/null +++ b/runtime/shaders/hdr_ui.frag @@ -0,0 +1,27 @@ +#version 450 +// The in-game menu on an HDR10 swapchain (runtime/vk_hdr.c vk_hdr_ui_compose): ImGui drew it into an RGBA8 image, +// premultiplied sRGB colour over a clear 0; this turns each pixel back into its colour, puts its white at p.x nits and +// writes the PQ code with its coverage, which the pipeline blends over the picture (in PQ code values, as the menu +// blends in sRGB code values on an SDR swapchain). PQ: SMPTE ST 2084; Rec. 709 to Rec. 2020: ITU-R BT.2087. +layout(location = 0) in vec2 uv; +layout(location = 0) out vec4 color; +layout(binding = 0) uniform sampler2D ui; +layout(push_constant) uniform P { vec4 p; } pc; + +vec3 srgb_decode(vec3 c) { + return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), greaterThan(c, vec3(0.04045))); +} + +vec3 pq(vec3 nits) { + const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875; + vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1)); + return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2)); +} + +void main() { + vec4 u = texture(ui, uv); + if (u.a <= 0.0) discard; + vec3 c = srgb_decode(clamp(u.rgb / u.a, 0.0, 1.0)); + const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956); + color = vec4(pq(max(to2020 * c, vec3(0.0)) * pc.p.x), u.a); +} diff --git a/runtime/simd.h b/runtime/simd.h index 535cb106..d8fb45c8 100644 --- a/runtime/simd.h +++ b/runtime/simd.h @@ -7,7 +7,7 @@ // tables. Not modelled: FPSR cumulative exception bits and QC, FPCR trap enables, FP16 arithmetic (ARMv8.2). // FPCR.AHP is honoured by the half-precision conversions. Host assumptions: IEEE binary32/64, MXCSR in // round-to-nearest outside the slow paths, with FTZ|DAZ equal to FPCR.FZ (cpu.h rt_fpcr_sync keeps it so); fma must be -// correctly rounded (build x86 with -mfma). +// correctly rounded: -march=x86-64-v3 has the FMA instruction, and the v2 build links runtime/fma.c for it. #pragma once #include <fenv.h> #include <immintrin.h> diff --git a/runtime/simd_fp.h b/runtime/simd_fp.h index 7020c254..5593bb5b 100644 --- a/runtime/simd_fp.h +++ b/runtime/simd_fp.h @@ -59,16 +59,60 @@ SN T FN(nan3)(Cpu *c, T a, T b, T d) { return FN(fv)(DNAN); } +// ---- FMA flavours. FMA_S / FMA_V: fast paths, host MXCSR is round-to-nearest there (simd_directed() is checked first). +// FMA_X: scalar FMA for the slow paths, any host rounding mode. +// Without hardware FMA (Ivy Bridge, no -mfma) clang turns __builtin_elementwise_fma into a call to the C runtime's +// fmaf/fma for every lane, which is very slow and keeps the thread outside generated code. For float the product of +// two floats is exact in a double (24 x 24 = 48 bits), so the fused op is done in double. A plain +// (float)(x * y + z) rounds twice and can differ by one ulp; rounding the double sum to odd first (Boldo-Melquiond) +// makes the final float rounding exact: TwoSum gives the error e of s = fl(p + z), and when e != 0 and s has an even +// last bit, s is moved one ulp toward the exact value (the odd neighbour). Inf/NaN lanes are left alone (e is NaN +// there, so the ordered compares below are false). Double has no wider type: it keeps the C runtime's fma. +#if defined(__FMA__) || defined(__aarch64__) || NB == 64 +#define FMA_S(x, y, z) __builtin_elementwise_fma(x, y, z) +#define FMA_V(x, y, z) __builtin_elementwise_fma(x, y, z) +#define FMA_X(x, y, z) __builtin_elementwise_fma(x, y, z) +#else +typedef double simd_vf64x4 __attribute__((vector_size(32))); +typedef uint64_t simd_vu64x4 __attribute__((vector_size(32))); +SI float fma_rn_s(float x, float y, float z) { + double p = (double)x * (double)y, zd = (double)z, s = p + zd; + double bb = s - p, e = (p - (s - bb)) + (zd - bb); // TwoSum: s + e == p + zd exactly + uint64_t u; + memcpy(&u, &s, sizeof u); + if (((e < 0.0) | (e > 0.0)) && !(u & 1)) u += (e > 0.0) == (s > 0.0) ? 1 : (uint64_t)-1; + memcpy(&s, &u, sizeof s); + return (float)s; +} +SI vf32 fma_rn_v(vf32 x, vf32 y, vf32 z) { + simd_vf64x4 p = CVT(x, simd_vf64x4) * CVT(y, simd_vf64x4), zd = CVT(z, simd_vf64x4), s = p + zd; + simd_vf64x4 bb = s - p, e = (p - (s - bb)) + (zd - bb); + simd_vu64x4 u = (simd_vu64x4)s; + simd_vu64x4 fix = (simd_vu64x4)((e < 0.0) | (e > 0.0)) & (simd_vu64x4)((u & 1) == 0); + simd_vu64x4 up = (simd_vu64x4)((e > 0.0) == (s > 0.0)); + u += fix & ((up & 1) | ~up); // +1 toward the exact value, i.e. +1 if e and s share a sign, else -1 + return CVT((simd_vf64x4)u, vf32); +} +// Any rounding mode: directed modes are exact with a plain double (same-direction double rounding is harmless). +SI float fma_any_s(float x, float y, float z) { + if (__builtin_expect(_mm_getcsr() & 0x6000u, 0)) return (float)((double)x * (double)y + (double)z); + return fma_rn_s(x, y, z); +} +#define FMA_S(x, y, z) fma_rn_s(x, y, z) +#define FMA_V(x, y, z) fma_rn_v(x, y, z) +#define FMA_X(x, y, z) fma_any_s(x, y, z) +#endif + // The arithmetic itself, for scalars (KK = K) and vectors (KK = VK). MADD: a + b*d (a = addend). // RECPS/RSQRTS get their first operand already negated (FPNeg happens before NaN processing). -#define FCALC(op, a, b, d, KK) \ +#define FCALC(op, a, b, d, KK, FMA) \ ((op) == FOP_ADD ? (a) + (b) : (op) == FOP_SUB ? (a) - (b) \ : (op) == FOP_MUL || (op) == FOP_MULX ? (a) * (b) : (op) == FOP_NMUL ? -((a) * (b)) \ : (op) == FOP_DIV ? (a) / (b) : (op) == FOP_ABD ? __builtin_elementwise_abs((a) - (b)) \ : (op) == FOP_SQRT ? __builtin_elementwise_sqrt(a) \ - : (op) == FOP_MADD ? __builtin_elementwise_fma(b, d, a) \ - : (op) == FOP_RECPS ? __builtin_elementwise_fma(a, b, KK(2)) \ - : (op) == FOP_RSQRTS ? __builtin_elementwise_fma(a, b, KK(3)) * KK(0.5) : (a)) + : (op) == FOP_MADD ? FMA(b, d, a) \ + : (op) == FOP_RECPS ? FMA(a, b, KK(2)) \ + : (op) == FOP_RSQRTS ? FMA(a, b, KK(3)) * KK(0.5) : (a)) // Lanes whose fast result needs fixing: NaN results (NaN operand or invalid op), RSQRTS overflow. #define FBAD(op, r) ((op) == FOP_RSQRTS ? ((r) != (r)) | (__builtin_elementwise_abs(r) == K(INFINITY)) : (r) != (r)) @@ -76,9 +120,9 @@ SN T FN(nan3)(Cpu *c, T a, T b, T d) { // (3 + a*b) / 2 with one rounding (a already negated): halve a normal operand first, so a product beyond // the format's range (which the fast path rounds to inf, or to the largest normal when rounding down) is exact. SN T FN(rsqrts)(T a, T b) { - if (__builtin_elementwise_abs(a) >= FN(fv)(2 * MINN)) return __builtin_elementwise_fma(a * K(0.5), b, K(1.5)); - if (__builtin_elementwise_abs(b) >= FN(fv)(2 * MINN)) return __builtin_elementwise_fma(a, b * K(0.5), K(1.5)); - return __builtin_elementwise_fma(a, b, K(3)) * K(0.5); + if (__builtin_elementwise_abs(a) >= FN(fv)(2 * MINN)) return FMA_X(a * K(0.5), b, K(1.5)); + if (__builtin_elementwise_abs(b) >= FN(fv)(2 * MINN)) return FMA_X(a, b * K(0.5), K(1.5)); + return FMA_X(a, b, K(3)) * K(0.5); } // ARM result for a lane flagged by FBAD (operands already flushed). @@ -136,13 +180,13 @@ SN T FN(slow)(Cpu *c, int op, T a, T b, T d) { if (FMINMAX(op)) return FN(minmax)(c, op, a, b); unsigned old = simd_rm_enter(fpcr); volatile T va = a, vb = b, vd = d; - volatile T vr = op == FOP_RSQRTS ? FN(rsqrts)(va, vb) : FCALC(op, va, vb, vd, K); + volatile T vr = op == FOP_RSQRTS ? FN(rsqrts)(va, vb) : FCALC(op, va, vb, vd, K, FMA_X); T r = vr; if (FBAD(op, r)) vr = FN(fix)(c, op, a, b, d), r = vr; if ((fpcr & FPCR_FZ) && (FN(fb)(r) & ~SGN) <= MINN && !FN(iszero)(FN(fb)(r))) { if ((FN(fb)(r) & ~SGN) == MINN) { simd_rm_rz(); - volatile T vz = FCALC(op, va, vb, vd, K); + volatile T vz = FCALC(op, va, vb, vd, K, FMA_X); if ((FN(fb)(vz) & ~SGN) < MINN) r = FN(fv)(FN(fb)(r) & SGN); } else { r = FN(fv)(FN(fb)(r) & SGN); @@ -172,7 +216,7 @@ SI T FN(op)(Cpu *c, int op, T a, T b, T d) { if (c->fpcr & FPCR_FZ) a = FN(flush)(a), b = FN(flush)(b); return FN(minmax)(c, op, a, b); } - T r = FCALC(op, a, b, d, K); + T r = FCALC(op, a, b, d, K, FMA_S); if (__builtin_expect(op == FOP_RSQRTS ? FREDO(op, r) : !FOK(r), 0)) r = FN(redo)(c, op, a, b, d, r); return r; } @@ -207,7 +251,7 @@ SI VT FN(vop)(Cpu *c, int op, VT a, VT b, VT d) { r = (VT)FN(vsel)(a < b, x, FN(vsel)(a == b, x | y, y)); return r; } - r = FCALC(op, a, b, d, VK); + r = FCALC(op, a, b, d, VK, FMA_V); if (__builtin_expect(op == FOP_RSQRTS ? __builtin_reduce_or(FREDO(op, r)) != 0 : __builtin_reduce_and(FOK(r)) == 0, 0)) return FN(vslow)(c, op, a, b, d); return r; @@ -363,3 +407,6 @@ SI T FN(recpx)(Cpu *c, T a) { #undef MINN #undef VK #undef K +#undef FMA_S +#undef FMA_V +#undef FMA_X diff --git a/runtime/ui/amiibo_page.cpp b/runtime/ui/amiibo_page.cpp new file mode 100644 index 00000000..2f2ed996 --- /dev/null +++ b/runtime/ui/amiibo_page.cpp @@ -0,0 +1,305 @@ +// Amiibo / NFC menu page (Dear ImGui): browser and scan controls. +#include <algorithm> +#include <cctype> +#include <cfloat> +#include <filesystem> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <string> +#include <string_view> +#include <vector> + +#include "imgui.h" +#include "ui/amiibo_page.h" +#include "ui/option_rows.h" + +extern "C" { +#include "amiibo.h" +#include "cfg.h" +#include "folder_pick.h" +#include "platform/platform.h" +#include "ui_text.h" +} + +namespace fs = std::filesystem; + +struct FileEntry { + std::string name; + bool is_dir; +}; + +static const char kDefaultAmiiboDir[] = "./amiibo"; +static std::string g_current_dir = kDefaultAmiiboDir; +static std::vector<FileEntry> g_entries; +static int g_selected_entry = -1; +// The last action: a catalog id and the file or folder it names, formatted when drawn (the menu language can change) +static UiTextId g_status_id = UI_MENU_AMIIBO_READY; +static std::string g_status_arg; + +static void set_status(UiTextId id, const std::string &arg = std::string()) { + g_status_id = id; + g_status_arg = arg; +} + +// Loads a dump and puts it on the reader; the status names the file. +static void tap(const std::string &dir, const std::string &name) { + std::string full_path = (fs::path(dir) / name).string(); + set_status(amiibo_scan_and_tap(full_path.c_str()) ? UI_MENU_AMIIBO_TAPPED : UI_MENU_AMIIBO_LOAD_FAILED, name); +} + +static bool iequals(std::string_view a, std::string_view b) { + if (a.size() != b.size()) return false; + for (size_t i = 0; i < a.size(); ++i) { + if (std::tolower(static_cast<unsigned char>(a[i])) != + std::tolower(static_cast<unsigned char>(b[i]))) { + return false; + } + } + return true; +} + +static void set_current_dir(const std::string &path) { + g_current_dir = path; + cfg_set("TOTK_AMIIBO_DIR", g_current_dir.c_str()); +} + +// The host's folder dialog (folder_pick.h) answers on its own thread; the page takes the answer when it draws. +static PlatMutex g_pick_lock = PLAT_MUTEX_INIT; +static bool g_pick_open; +static bool g_pick_ready; +static std::string g_pick_path; + +static void on_folder_picked(const char *path, void *ctx) { + (void)ctx; + plat_mutex_lock(&g_pick_lock); + g_pick_ready = path != NULL; + if (path) g_pick_path = path; + g_pick_open = false; + plat_mutex_unlock(&g_pick_lock); +} + +static void refresh_dir(void) { + g_entries.clear(); + g_selected_entry = -1; + + std::error_code ec; + fs::path dir_path(g_current_dir); + + // A missing folder lists nothing and says so; it is not replaced by another one, and opening the tab writes no + // setting (the folder is saved only when the player sets one). + if (!fs::exists(dir_path, ec) || !fs::is_directory(dir_path, ec)) { + set_status(UI_MENU_AMIIBO_NO_DIR, g_current_dir); + return; + } + if (g_status_id == UI_MENU_AMIIBO_NO_DIR) set_status(UI_MENU_AMIIBO_READY); + + // Add parent navigation if not at root + if (dir_path.has_parent_path() && dir_path != dir_path.parent_path()) { + g_entries.push_back({"..", true}); + } + + for (const auto &entry : fs::directory_iterator(dir_path, fs::directory_options::skip_permission_denied, ec)) { + if (ec) break; + std::error_code status_ec; + bool is_dir = entry.is_directory(status_ec); + if (status_ec) continue; + + std::string filename = entry.path().filename().string(); + if (filename.empty() || filename == "." || filename == "..") continue; + + if (!is_dir) { + std::string ext = entry.path().extension().string(); + if (!iequals(ext, ".bin") && !iequals(ext, ".nfc")) { + continue; + } + } + + g_entries.push_back({std::move(filename), is_dir}); + } + + std::sort(g_entries.begin(), g_entries.end(), [](const FileEntry &a, const FileEntry &b) { + if (a.name == "..") return true; + if (b.name == "..") return false; + if (a.is_dir != b.is_dir) return a.is_dir; + return a.name < b.name; + }); +} + +void tab_amiibo(float scale) { + (void)scale; // the rows take ImGui's own sizes, already scaled with the font + static bool s_inited = false; + if (!s_inited) { + const char *cfg_dir = cfg_get("TOTK_AMIIBO_DIR"); + if (cfg_dir && *cfg_dir) { + g_current_dir = cfg_dir; + } else { + g_current_dir = kDefaultAmiiboDir; + } + refresh_dir(); + s_inited = true; + } + + plat_mutex_lock(&g_pick_lock); + bool picking = g_pick_open; + std::string picked; + if (g_pick_ready) picked.swap(g_pick_path); + g_pick_ready = false; + plat_mutex_unlock(&g_pick_lock); + if (!picked.empty()) { + set_current_dir(picked); + refresh_dir(); + } + + // Two panes: Left (Directory Browser & Tag List), Right (Amiibo Tag Details & Controls) + const float content_h = ImGui::GetContentRegionAvail().y - opt_bottom(); // Continue and Quit stay visible + const float left_w = ImGui::GetContentRegionAvail().x * 0.58f; + const float right_w = ImGui::GetContentRegionAvail().x - left_w - ImGui::GetStyle().ItemSpacing.x; + + // ---- Left Pane: Explorer & List ---- + ImGui::BeginChild("##amiibo_explorer", ImVec2(left_w, content_h), true); + ImGui::TextColored(ImVec4(0.93f, 0.76f, 0.33f, 1.0f), "%s", menu_text(UI_MENU_AMIIBO_DIR)); + + ImGui::TextWrapped("%s", g_current_dir.c_str()); + + ImGui::Spacing(); + ImGui::BeginDisabled(picking); + if (ImGui::Button(menu_text(UI_MENU_AMIIBO_OPEN))) { + plat_mutex_lock(&g_pick_lock); + g_pick_open = true; + plat_mutex_unlock(&g_pick_lock); + folder_pick(g_current_dir.c_str(), on_folder_picked, NULL); + } + ImGui::EndDisabled(); + ImGui::SameLine(); + if (ImGui::Button(menu_text(UI_MENU_AMIIBO_DEFAULT_DIR))) { + set_current_dir(kDefaultAmiiboDir); + refresh_dir(); + } + ImGui::SameLine(); + if (ImGui::Button(menu_text(UI_MENU_AMIIBO_REFRESH))) { + refresh_dir(); + } + + ImGui::Separator(); + ImGui::TextDisabled("%s", menu_text(UI_MENU_AMIIBO_PICK)); + + ImGui::BeginChild("##file_list", ImVec2(0, 0), true); + if (g_status_id == UI_MENU_AMIIBO_NO_DIR) { + char missing[512]; + snprintf(missing, sizeof missing, menu_text(UI_MENU_AMIIBO_NO_DIR), g_current_dir.c_str()); + ImGui::TextWrapped("%s", missing); + } + for (size_t i = 0; i < g_entries.size(); i++) { + const auto &e = g_entries[i]; + std::string label = e.is_dir ? e.name + "/" : e.name; + bool selected = ((int)i == g_selected_entry); + + if (ImGui::Selectable(label.c_str(), selected, ImGuiSelectableFlags_AllowDoubleClick)) { + g_selected_entry = (int)i; + if (e.is_dir) { + if (ImGui::IsMouseDoubleClicked(0) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceDown)) { + if (e.name == "..") { + fs::path cur(g_current_dir); + if (cur.has_parent_path() && cur != cur.parent_path()) { + set_current_dir(cur.parent_path().string()); + } else { + set_current_dir("."); + } + } else { + set_current_dir((fs::path(g_current_dir) / e.name).string()); + } + refresh_dir(); + break; + } + } else { + if (ImGui::IsMouseDoubleClicked(0)) tap(g_current_dir, e.name); + } + } + } + ImGui::EndChild(); + ImGui::EndChild(); + + ImGui::SameLine(); + + // ---- Right Pane: Controls & Status ---- + ImGui::BeginChild("##amiibo_controls", ImVec2(right_w, content_h), true); + ImGui::TextColored(ImVec4(0.93f, 0.76f, 0.33f, 1.0f), "%s", menu_text(UI_MENU_AMIIBO_READER)); + ImGui::Separator(); + + AmiiboDeviceState state = amiibo_get_reader_state(); + UiTextId state_id = UI_MENU_AMIIBO_STATE_IDLE; + ImVec4 state_col = ImVec4(0.8f, 0.8f, 0.8f, 1.0f); + switch (state) { + case AMIIBO_DEV_STATE_INITIALIZED: state_id = UI_MENU_AMIIBO_STATE_IDLE; break; + case AMIIBO_DEV_STATE_SEARCHING: state_id = UI_MENU_AMIIBO_STATE_SEARCHING; state_col = ImVec4(1.0f, 0.8f, 0.2f, 1.0f); break; + case AMIIBO_DEV_STATE_TAG_FOUND: state_id = UI_MENU_AMIIBO_STATE_FOUND; state_col = ImVec4(0.3f, 0.9f, 0.3f, 1.0f); break; + case AMIIBO_DEV_STATE_TAG_MOUNTED: state_id = UI_MENU_AMIIBO_STATE_MOUNTED; state_col = ImVec4(0.2f, 0.9f, 1.0f, 1.0f); break; + case AMIIBO_DEV_STATE_UNAVAILABLE: state_id = UI_MENU_AMIIBO_STATE_OFF; state_col = ImVec4(0.9f, 0.3f, 0.3f, 1.0f); break; + default: break; + } + ImGui::TextUnformatted(menu_text(UI_MENU_AMIIBO_STATE)); + ImGui::SameLine(); + ImGui::TextColored(state_col, "%s", menu_text(state_id)); + + ImGui::Spacing(); + ImGui::Separator(); + ImGui::TextUnformatted(menu_text(UI_MENU_AMIIBO_FILE)); + if (g_selected_entry >= 0 && g_selected_entry < (int)g_entries.size() && !g_entries[g_selected_entry].is_dir) { + ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.4f, 1.0f), "%s", g_entries[g_selected_entry].name.c_str()); + + if (ImGui::Button(menu_text(UI_MENU_AMIIBO_TAP), ImVec2(ImGui::GetContentRegionAvail().x, 0))) + tap(g_current_dir, g_entries[g_selected_entry].name); + } else { + ImGui::TextDisabled("%s", menu_text(UI_MENU_AMIIBO_NO_FILE)); + ImGui::BeginDisabled(); + ImGui::Button(menu_text(UI_MENU_AMIIBO_TAP), ImVec2(ImGui::GetContentRegionAvail().x, 0)); + ImGui::EndDisabled(); + } + + ImGui::Spacing(); + if (ImGui::Button(menu_text(UI_MENU_AMIIBO_REMOVE), ImVec2(ImGui::GetContentRegionAvail().x, 0))) { + amiibo_remove_tag(); + set_status(UI_MENU_AMIIBO_REMOVED); + } + + ImGui::Spacing(); + ImGui::Separator(); + bool auto_remove = amiibo_get_auto_remove(); + if (ImGui::Checkbox(menu_text(UI_MENU_AMIIBO_AUTO_REMOVE), &auto_remove)) { + amiibo_set_auto_remove(auto_remove); + } + if (auto_remove) { + float delay = amiibo_get_auto_remove_delay_s(); + ImGui::TextUnformatted(menu_text(UI_MENU_AMIIBO_HOLD)); // above the slider: beside it the text is cut off + ImGui::SetNextItemWidth(-FLT_MIN); + if (ImGui::SliderFloat("##amiibo_hold", &delay, 1.0f, 6.0f, menu_text(UI_MENU_AMIIBO_SECONDS))) { + amiibo_set_auto_remove_delay_s(delay); + } + } + + ImGui::Spacing(); + ImGui::Separator(); + ImGui::TextUnformatted(menu_text(UI_MENU_AMIIBO_TAG)); + if (amiibo_has_tag()) { + const AmiiboInfo *info = amiibo_get_current_info(); + char character[8], uid[24]; + snprintf(character, sizeof character, "%02x%02x%02x", info->character_id[0], info->character_id[1], + info->character_id[2]); + snprintf(uid, sizeof uid, "%02x:%02x:%02x:%02x:%02x:%02x:%02x", info->uuid[0], info->uuid[1], info->uuid[2], + info->uuid[3], info->uuid[4], info->uuid[5], info->uuid[6]); + ImGui::Text(menu_text(UI_MENU_AMIIBO_TAG_FILE), info->filename); + ImGui::Text(menu_text(UI_MENU_AMIIBO_TAG_ID), character, info->series_id, info->numbering_id, info->nfp_type); + ImGui::Text(menu_text(UI_MENU_AMIIBO_TAG_UID), uid); + } else { + ImGui::TextDisabled("%s", menu_text(UI_MENU_AMIIBO_NO_TAG)); + } + + ImGui::Spacing(); + ImGui::Separator(); + char status[512]; + snprintf(status, sizeof status, menu_text(g_status_id), g_status_arg.c_str()); + ImGui::TextDisabled(menu_text(UI_MENU_AMIIBO_STATUS), status); + + ImGui::EndChild(); +} diff --git a/runtime/ui/amiibo_page.h b/runtime/ui/amiibo_page.h new file mode 100644 index 00000000..167ee48c --- /dev/null +++ b/runtime/ui/amiibo_page.h @@ -0,0 +1,4 @@ +// Amiibo / NFC menu page (ui/amiibo_page.cpp). +#pragma once + +void tab_amiibo(float scale); diff --git a/runtime/ui/cheats_tab.cpp b/runtime/ui/cheats_tab.cpp new file mode 100644 index 00000000..236465df --- /dev/null +++ b/runtime/ui/cheats_tab.cpp @@ -0,0 +1,66 @@ +// The Cheats tab of the in-game menu: the cheats of the mods' cheat files (cheats.c), a heading per mod in the Mods +// tab's order, then a row per cheat in file order and a line per group label of the file; a cheat that cannot be +// switched here is greyed out with the reason. While code cheats wait for a rebuild, a row on top restarts the game +// into it. +#include <stdio.h> +#include <string.h> + +#include "imgui.h" +#include "ui/option_rows.h" + +extern "C" { +#include "cheats.h" +#include "ui_text.h" +} + +// Returns true when "Rebuild the game now" was chosen: the menu then asks to restart (ui/menu.cpp restart_confirm). +extern "C" bool tab_cheats(float scale) { + opt_begin("##cheats", scale); + static char line[512], desc[768], buttons[192]; + int n = cheats_count(); + if (!n) { + snprintf(line, sizeof line, menu_text(UI_MENU_CHEATS_NONE), cheats_file()[0] ? cheats_file() : "<build id>.txt"); + opt_text_str(line); + opt_end(); + return false; + } + bool rebuild = + cheats_rebuild_pending() && opt_button("##restart", UI_MENU_CHEATS_REBUILD, UI_MENU_CHEATS_REBUILD_DESC); + for (int i = 0; i < n; i++) { + const CheatInfo *c = cheats_info(i); + if (!i || strcmp(c->mod, cheats_info(i - 1)->mod)) { + int listed = 0; + for (int j = i; j < n && !strcmp(cheats_info(j)->mod, c->mod); j++) + listed += cheats_info(j)->kind != CHEAT_HEADING; + snprintf(line, sizeof line, menu_text(UI_MENU_CHEATS_MOD), c->mod, listed); + opt_group_text(line); + } + UiTextId text = UI_MENU_CHEAT_CODE_DESC; + switch (c->kind) { + case CHEAT_HEADING: + if (!strstr(c->name, "TID=")) opt_text_str(c->name); // the file's title line only names the game + continue; + case CHEAT_CODE: break; + case CHEAT_KEY: text = UI_MENU_CHEAT_KEY_DESC; break; + case CHEAT_DATA: text = UI_MENU_CHEAT_DATA_DESC; break; + case CHEAT_MASTER: text = UI_MENU_CHEAT_MASTER_DESC, opt_disable_next(UI_MENU_CHEAT_MASTER); break; + case CHEAT_RESTORE: text = UI_MENU_CHEAT_RESTORE_DESC, opt_disable_next(UI_MENU_CHEAT_RESTORE); break; + case CHEAT_REBUILD: text = UI_MENU_CHEAT_REBUILD_DESC, opt_disable_next(UI_MENU_CHEAT_REBUILD); break; + case CHEAT_UNSUPPORTED: + text = UI_MENU_CHEAT_UNSUPPORTED_DESC, opt_disable_next(UI_MENU_CHEAT_UNSUPPORTED); + break; + } + if (c->kind == CHEAT_KEY) + snprintf(desc, sizeof desc, menu_text(text), cheats_buttons(c->buttons, buttons, sizeof buttons)); + else if (c->kind == CHEAT_UNSUPPORTED) + snprintf(desc, sizeof desc, "%s %s", menu_text(text), c->note); + else + snprintf(desc, sizeof desc, "%s", menu_text(text)); + if (opt_toggle_text(c->key, c->name, desc, cheats_on(i) != 0)) { + cheats_set(i, !cheats_on(i)); + opt_save_soon(); + } + } + opt_end(); + return rebuild; +} diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 891a44fb..ed455550 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -30,13 +30,18 @@ static int g_font_bad; #pragma clang diagnostic ignored "-Wunused-function" #include "imstb_truetype.h" #pragma clang diagnostic pop +#include "ui/amiibo_page.h" #include "ui/binds_page.h" #include "ui/option_rows.h" extern "C" { +#include "amiibo.h" +#include "cheats.h" #include "cfg.h" +#include "fs.h" #include "input.h" #include "input_map.h" +#include "mods.h" #include "perfmon.h" #include "platform/platform.h" #include "sysinfo.h" @@ -49,10 +54,9 @@ void vk_queue_unlock(void); void res_sizes(unsigned out[2], unsigned scene[2]); double frame_step_limit_hz(void); extern const int rt_game_version; -// fs.h (romfs.c): romfs-patches-xbox was next to romfs-index.bin at start, and was loaded. -int romfs_xbox_prompts_ready(void); -int romfs_xbox_prompts_active(void); extern const char rt_port_version[]; +void cheats_frame(uint64_t frame); // cheats.c +bool tab_cheats(float scale); // ui/cheats_tab.cpp; true: restart into the rebuild } // Roboto Medium (Apache-2.0, ui/fonts/LICENSE-Roboto.txt) with Latin and Cyrillic glyphs. @@ -266,7 +270,9 @@ static int vk_up(const MenuTarget *t) { // Roboto Medium (Latin, Cyrillic, dashes and quotes) at 26 px per 1080 lines, with the glyphs of the Japanese, Korean // and Chinese language names merged in from a system font when there is one (each language is named in its own script). -// Made again when the window height changes; the old texture goes after the queue is idle (CreateFontsTexture). +// A catalog row also holds menu text (Simplified Chinese), so every string of the row goes into the builder: the merged +// font must cover the whole menu of that language, not only its name. Made again when the window height changes; the old +// texture goes after the queue is idle (CreateFontsTexture). static const ImWchar kRanges[] = {0x0020, 0x00FF, 0x0400, 0x052F, 0x2010, 0x2027, 0}; static const struct { const char *lang; UiTextId name; } kCjk[] = { {"ja", UI_MENU_LANGNAME_JA}, {"ko", UI_MENU_LANGNAME_KO}, {"zh-Hans", UI_MENU_LANGNAME_ZH_HANS}, @@ -310,6 +316,11 @@ extern "C" int menu_font_check(void) { return ok; } +// The glyphs of every string of a catalog row (ui_text_visit), for the font that draws that language. +static void add_row_glyphs(void *ctx, const char *text) { + static_cast<ImFontGlyphRangesBuilder *>(ctx)->AddText(text); +} + static int fonts_up(float scale) { enum { N = sizeof kCjk / sizeof *kCjk }; ImFontAtlas *fa = ImGui::GetIO().Fonts; @@ -334,6 +345,13 @@ static int fonts_up(float scale) { merged++; ImFontGlyphRangesBuilder b; b.AddText(ui_text_in("en", kCjk[i].name)); + ui_text_visit(kCjk[i].lang, add_row_glyphs, &b); + // Text the menu shows that no catalog row holds: a mod's cheat names and its folder name, + // both read from disk at runtime. Their glyphs would otherwise fall back to the missing-glyph box. + for (int k = 0; k < cheats_count(); k++) + if (const CheatInfo *ci = cheats_info(k); ci && ci->name) b.AddText(ci->name); + for (int k = 0; k < mods_count(); k++) + if (const ModInfo *mi = mods_at(k); mi) b.AddText(mi->name); b.BuildRanges(&ranges[i]); ImFontConfig mc; mc.MergeMode = true; @@ -362,7 +380,7 @@ extern "C" void menu_vk_release(void) { // TOTK_MENU_TEST_SET=<frame>:<key>=<value>[,<key>=<value>...][;<frame>:...]: cfg_set of each key (TOTK_ prefix // optional) at that presented frame; changes live settings with no input, menu open or not. totk.ini is written only by // the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note). -enum { TAB_SCREEN, TAB_GAME, TAB_SOUND, TAB_CONTROLS, TAB_MENU, TAB_ADVANCED, TAB_COUNT }; +enum { TAB_SCREEN, TAB_GAME, TAB_SOUND, TAB_CONTROLS, TAB_AMIIBO, TAB_MENU, TAB_ADVANCED, TAB_MODS, TAB_CHEATS, TAB_COUNT }; enum { TEST_PAGE_FRAMES = 30, TEST_SETS = 16 }; struct TestSet { uint64_t frame; char key[64], value[64]; }; static struct { @@ -473,13 +491,15 @@ static void row_window_size(void) { } // The game resolution (resolution.c): 16:9 sizes from its smallest, 1280x720 (for weak GPUs), up, the common 21:9 and -// 32:9 monitor sizes and the 16:10 screens of the Steam Deck, handhelds and laptops (the camera and the HUD follow +// 32:9 monitor sizes, the 16:10 screens of the Steam Deck, handhelds and laptops and the 3:2 screens of laptops and +// tablets (Surface, Framework; the camera and the HUD follow // another shape, resolution.c res_init; the installer lays the HUD out for each, installer/steps.py ASPECT_SIZES), and // the current value when it is another size. static void row_output_res(void) { static const char *const vals[] = {"1280x720", "1600x900", "1920x1080", "2560x1440", "3200x1800", "3840x2160", "2560x1080", "3440x1440", "3840x1600", "5120x2160", "3840x1080", "5120x1440", - "1280x800", "1680x1050", "1920x1200", "2560x1600"}; + "1280x800", "1680x1050", "1920x1200", "2560x1600", + "1920x1280", "2160x1440", "2256x1504", "2880x1920", "3000x2000"}; enum { N = sizeof vals / sizeof *vals }; char shown[N][32]; const char *names[N]; @@ -505,6 +525,27 @@ 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", "256", "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_shadow_scale(void) { + static const char *const vals[] = {"1", "2", "3", "4"}; + static const char *const names[] = {"960 (1x)", "1920 (2x)", "2880 (3x)", "3840 (4x)"}; + opt_choice_list("TOTK_SHADOW_SCALE", UI_MENU_SHADOW_SCALE, vals, names, 4, UI_MENU_SHADOW_SCALE_DESC); +} + +static void row_shadow_range(void) { + static const char *const vals[] = {"100", "150", "200", "300"}; + static const char *const names[] = {"160 m (1x)", "240 m (1.5x)", "320 m (2x)", "480 m (3x)"}; + opt_choice_list("TOTK_SHADOW_RANGE", UI_MENU_SHADOW_RANGE, vals, names, 4, UI_MENU_SHADOW_RANGE_DESC); +} + static void row_gpu(void) { const char *vals[MAX_GPUS + 1], *names[MAX_GPUS + 1]; char idx[MAX_GPUS][4]; @@ -518,27 +559,25 @@ static void row_gpu(void) { opt_choice_list("TOTK_GPU_INDEX", UI_MENU_GPU, vals, names, g_gpu_n + 1, UI_MENU_GPU_DESC); } -// The game language: one entry per language TotK ships (a Switch code that names it), each under its own name. The -// Switch's Portuguese is not among them. +// The game language: one entry per Mals language file the running game build ships (ui_text_game_langs), each under +// its own name. A code that names no entry exactly (older "en", "fr-CA" on a build without it) selects the first entry +// of the same catalog language. static void row_game_lang(void) { - static const struct { const char *code; UiTextId name; } langs[] = { - {"en-US", UI_MENU_LANGNAME_EN}, {"ja", UI_MENU_LANGNAME_JA}, {"fr", UI_MENU_LANGNAME_FR}, - {"de", UI_MENU_LANGNAME_DE}, {"it", UI_MENU_LANGNAME_IT}, {"es", UI_MENU_LANGNAME_ES}, - {"nl", UI_MENU_LANGNAME_NL}, {"ru", UI_MENU_LANGNAME_RU}, - {"zh-Hans", UI_MENU_LANGNAME_ZH_HANS}, {"zh-Hant", UI_MENU_LANGNAME_ZH_HANT}, {"ko", UI_MENU_LANGNAME_KO}, - }; - enum { N = sizeof langs / sizeof *langs + 1 }; - const char *vals[N], *names[N]; + UiGameLang langs[16]; + int n = ui_text_game_langs(rt_game_version, langs, 16); + const char *vals[17], *names[17]; vals[0] = ""; names[0] = menu_text(UI_MENU_GAME_LANG_AUTO); const char *now = cfg_value("TOTK_LANG"); int cur = *now ? -1 : 0; - for (int i = 1; i < N; i++) { + for (int i = 1; i <= n; i++) { vals[i] = langs[i - 1].code; names[i] = menu_text(langs[i - 1].name); - if (*now && !strcmp(ui_text_lang(now), ui_text_lang(vals[i]))) cur = i; // en-GB is English too + if (*now && !strcmp(now, vals[i])) cur = i; } - opt_choice_list("TOTK_LANG", UI_MENU_GAME_LANG, vals, names, N, UI_MENU_GAME_LANG_DESC, cur); + for (int i = 1; i <= n && *now && cur < 0; i++) + if (!strcmp(ui_text_lang(now), ui_text_lang(vals[i]))) cur = i; + opt_choice_list("TOTK_LANG", UI_MENU_GAME_LANG, vals, names, n + 1, UI_MENU_GAME_LANG_DESC, cur); } // The frame limit (frame_rate.c): unset, the older fps60 = 0 still means 30. The monitor choice shows the rate it gives. @@ -623,6 +662,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 %%"); @@ -634,16 +674,33 @@ static void tab_screen(float scale) { opt_choice_list("TOTK_ANISO", UI_MENU_ANISO, aniso, aniso_names, 5, UI_MENU_ANISO_DESC); opt_toggle("TOTK_LOD_DETAIL", UI_MENU_LOD_DETAIL, UI_MENU_LOD_DETAIL_DESC); opt_toggle("TOTK_LOD_STEADY", UI_MENU_LOD_STEADY, UI_MENU_LOD_STEADY_DESC); + row_shadow_scale(); + row_shadow_range(); + opt_slider("TOTK_PCSS", UI_MENU_PCSS, 0, 200, UI_MENU_PCSS_DESC, 10, "%.0f %%"); + opt_slider("TOTK_CONTACT_SHADOWS", UI_MENU_CONTACT_SHADOWS, 0, 100, UI_MENU_CONTACT_SHADOWS_DESC, 10, "%.0f %%"); + opt_toggle("TOTK_DLAA", UI_MENU_DLAA, UI_MENU_DLAA_DESC); + static const UiTextId dlss[] = {UI_MENU_DLSS_DLAA, UI_MENU_DLSS_QUALITY, UI_MENU_DLSS_BALANCED, + UI_MENU_DLSS_PERFORMANCE, UI_MENU_DLSS_ULTRA}; + opt_choice("TOTK_DLSS", UI_MENU_DLSS, dlss, 5, UI_MENU_DLSS_DESC); + static const UiTextId preset[] = {UI_MENU_DLSS_PRESET_DEFAULT, UI_MENU_DLSS_PRESET_K, UI_MENU_DLSS_PRESET_J}; + opt_choice("TOTK_DLSS_PRESET", UI_MENU_DLSS_PRESET, preset, 3, UI_MENU_DLSS_PRESET_DESC); + static const UiTextId chars[] = {UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM, + UI_MENU_DLSS_CHARS_FULL}; + opt_choice("TOTK_DLSS_CHAR_MASK", UI_MENU_DLSS_CHARS, chars, 4, UI_MENU_DLSS_CHARS_DESC); + opt_toggle("TOTK_FXAA", UI_MENU_FXAA, UI_MENU_FXAA_DESC); rows_color(); + opt_group(UI_MENU_HDR_GROUP); + opt_toggle("TOTK_HDR", UI_MENU_HDR, UI_MENU_HDR_DESC); + opt_slider("TOTK_HDR_PEAK_NITS", UI_MENU_HDR_PEAK, 400, 10000, UI_MENU_HDR_PEAK_DESC, 50, "%.0f"); + opt_slider("TOTK_HDR_GAME_NITS", UI_MENU_HDR_GAME, 80, 500, UI_MENU_HDR_GAME_DESC, 5, "%.0f"); + opt_slider("TOTK_HDR_UI_NITS", UI_MENU_HDR_UI, 80, 500, UI_MENU_HDR_UI_DESC, 5, "%.0f"); + opt_slider("TOTK_HDR_HUE", UI_MENU_HDR_HUE, 0, 100, UI_MENU_HDR_HUE_DESC, 5, "%.0f %%"); opt_end(); } static void tab_game(float scale) { opt_begin("##game", scale); row_fps_limit(); - opt_toggle("TOTK_REALTIME", UI_MENU_REALTIME, UI_MENU_REALTIME_DESC); - opt_toggle("TOTK_CLOTH_REALTIME", UI_MENU_CLOTH_REALTIME, UI_MENU_CLOTH_REALTIME_DESC); - opt_toggle("TOTK_HIGHFPS_FIX", UI_MENU_HIGHFPS_FIX, UI_MENU_HIGHFPS_FIX_DESC); row_game_lang(); static const UiTextId mode[] = {UI_MENU_MODE_DOCKED, UI_MENU_MODE_HANDHELD}; opt_choice("TOTK_OPERATION_MODE", UI_MENU_OPERATION_MODE, mode, 2, UI_MENU_OPERATION_MODE_DESC); @@ -654,17 +711,16 @@ static void tab_game(float scale) { static void tab_sound(float scale) { opt_begin("##sound", scale); opt_slider("TOTK_VOLUME", UI_MENU_VOLUME, 0, 100, UI_MENU_VOLUME_DESC, 5, "%.0f %%"); + static const UiTextId channels[] = {UI_MENU_CHANNELS_AUTO, UI_MENU_CHANNELS_STEREO, UI_MENU_CHANNELS_51}; + opt_choice("TOTK_AUDIO_CHANNELS", UI_MENU_AUDIO_CHANNELS, channels, 3, UI_MENU_AUDIO_CHANNELS_DESC); opt_end(); } -// The note under the layout. Xbox prompts (the Nintendo layout with the A/B and X/Y prompts swapped, romfs.c -// overlay_init) change the buttons at once but the prompts only at the next start: "files missing" without -// romfs-patches-xbox, "prompts change after restart" while the prompts in use differ from the choice. Automatic with a -// gamepad connected: "in use: Xbox". NULL otherwise. +// A confirmation is open (Controls: restart, Advanced: reset): B and Esc close it, not the menu; L / R stay on the page. +static bool g_modal; + +// The note under the layout: Automatic with a gamepad connected, the layout it picked ("in use: Xbox"). NULL otherwise. static const char *pad_layout_note(void) { - bool want = !strcasecmp(cfg_value("TOTK_PAD_LAYOUT"), "xbox_prompts"); - if (want && !romfs_xbox_prompts_ready()) return menu_text(UI_MENU_PAD_LAYOUT_PROMPTS_MISSING); - if (want != (romfs_xbox_prompts_active() != 0)) return menu_text(UI_MENU_PAD_LAYOUT_PROMPTS_RESTART); int got = input_pad_layout(); if (got < 0 || strcmp(cfg_value("TOTK_PAD_LAYOUT"), "auto")) return nullptr; static char note[96]; @@ -673,31 +729,149 @@ static const char *pad_layout_note(void) { return note; } +// The action a key presses, or -1: the walk key is a key of its own, but Shift is B in the default bindings, and a +// walk key that also presses something walks and does that something at the same time. The bindings come from a +// snapshot, and every key of an action is looked at: binds_refresh rebuilds the live table on the polling thread while +// the menu draws, and an action may hold more than the two keys its cells show. +static int key_action(const InputMap *m, int key) { + for (int a = 0; a < ACTION_COUNT; a++) { + uint8_t keys[INPUT_BINDING_MAX]; + int n = input_map_keys_of(m, a, keys, INPUT_BINDING_MAX); + for (int i = 0; i < n; i++) + if (keys[i] == key) return a; + } + return -1; +} + +// The walk key as the handful of keys a player is likely to pick. "none" rather than an empty value closes it: an +// unset key falls back to the default in the settings table, the same as a fresh installation. totk.ini takes any name +// input_map.c knows (walks of the keyboard sticks, not of the gamepad). A key the current bindings also use says which +// action it presses. +static void row_walk_key(void) { + static const char *const keys[] = {"none", "lshift", "lalt", "lctrl", "tab", "capslock"}; + const char *names[sizeof keys / sizeof *keys] = {menu_text(UI_MENU_OFF), "Shift", "Alt", "Ctrl", "Tab", "Caps Lock"}; + const char *cur = cfg_value("TOTK_WALK_KEY"); + static char note[64]; + const char *n = nullptr; + InputMap m; + input_map_snapshot(&m); + for (int k = KEY_A; k < KEY_COUNT && !n; k++) { + if (!input_key_name(k) || strcasecmp(input_key_name(k), cur)) continue; + int a = key_action(&m, k); + if (a >= 0 && input_action_name(a)) snprintf(note, sizeof note, menu_text(UI_MENU_WALK_KEY_TAKEN), input_action_name(a)), n = note; + } + opt_choice_list("TOTK_WALK_KEY", UI_MENU_WALK_KEY, keys, names, sizeof keys / sizeof *keys, UI_MENU_WALK_KEY_DESC, + -1, n); +} + +// The note under the prompts. The settings name a folder of prompts (input_prompts_folder: the prompts and the pad +// layout, as a layout that puts A on the right button needs the face buttons named the other way round), but the game +// reads its fonts and UI archive once at start (romfs.c overlay_init). While the folder in use differs: "files +// missing" when the setup did not write the one asked for, why it was left out at start (damaged, a game resolution +// that is not 16:9, a RomFS mod with the same files, TOTK_ROMFS_PATCHES), else "prompts change after restart", and only then +// *restart is set: a restart would bring the prompts asked for. NULL when they match. +static const char *prompts_note(bool *restart) { + *restart = false; + const char *layout = cfg_value("TOTK_PAD_LAYOUT"), *used = romfs_prompts(); + const char *want = input_prompts_folder(input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout), layout); + if (want == used || (want && used && !strcmp(want, used))) return nullptr; + if (want && !romfs_prompts_ready(want)) return menu_text(UI_MENU_PROMPTS_MISSING); + switch (romfs_prompts_off(want)) { + case ROMFS_PROMPTS_FAILED: return menu_text(UI_MENU_PROMPTS_FAILED); + case ROMFS_PROMPTS_SHAPE: return menu_text(UI_MENU_PROMPTS_OFF_SHAPE); + case ROMFS_PROMPTS_MODS: return menu_text(UI_MENU_PROMPTS_OFF_MODS); + case ROMFS_PROMPTS_PATCHES: return menu_text(UI_MENU_PROMPTS_OFF_PATCHES); + default: *restart = true; return menu_text(UI_MENU_PROMPTS_RESTART); + } +} + +// The confirmation of "Restart now", over the menu; Cancel has the cursor first. Restart writes totk.ini, then closes +// the game as Quit does and starts it again (gfx_window_request_restart). It waits while the game writes a save, and +// the game stays when totk.ini cannot be written, as the new prompts would not come back. From the Cheats tab +// (restart_rebuild) Restart also lets the next start rebuild for cheats whose last rebuild failed (cheats_rebuild_allow). +static bool restart_rebuild; +static void restart_confirm(void) { + g_modal = false; + ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + if (!ImGui::BeginPopupModal("##restart_confirm", nullptr, + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize)) + return; + g_modal = true; + ImGui::TextUnformatted(menu_text(UI_MENU_RESTART_CONFIRM)); + bool saving = fs_save_busy() != 0; + if (saving || !cfg_last_save_ok()) + ImGui::TextColored(ImVec4(0.95f, 0.45f, 0.35f, 1), "%s", + menu_text(saving ? UI_MENU_RESTART_SAVING : UI_MENU_SAVE_FAILED)); + ImGui::Spacing(); + const char *yes = menu_text(UI_MENU_RESTART_YES), *no = menu_text(UI_MENU_RESET_NO); + const ImGuiStyle &st = ImGui::GetStyle(); + float bw = fmaxf(ImGui::CalcTextSize(yes).x, ImGui::CalcTextSize(no).x) + st.FramePadding.x * 4; + bool close = ImGui::Button(no, ImVec2(bw, 0)); + if (ImGui::IsWindowAppearing()) ImGui::SetItemDefaultFocus(); + ImGui::SameLine(); + ImGui::BeginDisabled(saving); + if (ImGui::Button(yes, ImVec2(bw, 0)) && cfg_save()) { + if (restart_rebuild) cheats_rebuild_allow(); + gfx_window_request_restart(); + } + ImGui::EndDisabled(); + if (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false)) + close = true; + if (close) { + ImGui::CloseCurrentPopup(); + restart_rebuild = false; + opt_focus_key("##restart"); // back on the row it came from + } + ImGui::EndPopup(); +} + static void tab_controls(float scale) { opt_begin("##controls", scale); - static const UiTextId layout[] = {UI_MENU_PAD_LAYOUT_AUTO, UI_MENU_PAD_LAYOUT_NINTENDO, UI_MENU_PAD_LAYOUT_XBOX, - UI_MENU_PAD_LAYOUT_XBOX_PROMPTS}; - opt_choice("TOTK_PAD_LAYOUT", UI_MENU_PAD_LAYOUT, layout, 4, UI_MENU_PAD_LAYOUT_DESC, pad_layout_note()); + static const UiTextId layout[] = {UI_MENU_PAD_LAYOUT_AUTO, UI_MENU_PAD_LAYOUT_NINTENDO, UI_MENU_PAD_LAYOUT_XBOX}; + opt_choice("TOTK_PAD_LAYOUT", UI_MENU_PAD_LAYOUT, layout, 3, UI_MENU_PAD_LAYOUT_DESC, pad_layout_note()); + static const UiTextId prompts[] = {UI_MENU_PROMPTS_NINTENDO, UI_MENU_PROMPTS_XBOX}; + bool restart; + const char *note = prompts_note(&restart); + opt_choice("TOTK_PROMPTS", UI_MENU_PROMPTS, prompts, 2, UI_MENU_PROMPTS_DESC, note); + bool ask = restart && opt_button("##restart", UI_MENU_RESTART, UI_MENU_RESTART_DESC); opt_toggle("TOTK_MOTION", UI_MENU_MOTION, UI_MENU_MOTION_DESC); opt_slider("TOTK_RUMBLE_STRENGTH", UI_MENU_RUMBLE_STRENGTH, 0, 300, UI_MENU_RUMBLE_STRENGTH_DESC, 10, "%.0f %%"); opt_slider("TOTK_RUMBLE_CURVE", UI_MENU_RUMBLE_CURVE, 0.2f, 1.5f, UI_MENU_RUMBLE_CURVE_DESC, 0.05f, "%.2f"); opt_toggle("TOTK_RUMBLE_TRIGGERS", UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC); - opt_slider("TOTK_STICK_DEADZONE", UI_MENU_STICK_DEADZONE, 0, 50, UI_MENU_STICK_DEADZONE_DESC, 1, "%.0f %%"); opt_slider("TOTK_MOUSE_SENS", UI_MENU_MOUSE_SENS, 0, 10, UI_MENU_MOUSE_SENS_DESC, 0.1f, "%.1f"); + row_walk_key(); + opt_slider("TOTK_WALK_SCALE", UI_MENU_WALK_SCALE, 0.2f, 0.9f, UI_MENU_WALK_SCALE_DESC, 0.01f, "%.2f"); + // Analog sticks: one dead zone row per stick, falling back to TOTK_STICK_DEADZONE (both sticks) while unset. With + // a gamepad connected each row shows its stick's deflection now, before the dead zone: a drifting stick's resting + // value, to set it above. + opt_group(UI_MENU_GROUP_STICKS); + double now[2]; + bool pad = input_pad_stick_percent(now) != 0; + char stick_note[2][64]; + for (int s = 0; s < 2; s++) + snprintf(stick_note[s], sizeof stick_note[s], menu_text(UI_MENU_STICK_DEADZONE_NOW), now[s]); + opt_slider("TOTK_LSTICK_DEADZONE", UI_MENU_LSTICK_DEADZONE, 0, 50, UI_MENU_STICK_DEADZONE_DESC, 1, "%.0f %%", + pad ? stick_note[0] : nullptr, "TOTK_STICK_DEADZONE"); + opt_slider("TOTK_RSTICK_DEADZONE", UI_MENU_RSTICK_DEADZONE, 0, 50, UI_MENU_STICK_DEADZONE_DESC, 1, "%.0f %%", + pad ? stick_note[1] : nullptr, "TOTK_STICK_DEADZONE"); + opt_slider("TOTK_STICK_SENS", UI_MENU_STICK_SENS, 0.1f, 3, UI_MENU_STICK_SENS_DESC, 0.05f, "%.2f"); binds_rows(scale); opt_end(); binds_after_page(); + if (ask) ImGui::OpenPopup("##restart_confirm"); // at the level BeginPopupModal looks it up + restart_confirm(); } static void tab_menu(float scale) { opt_begin("##menu", scale); - static const UiTextId langs[] = {UI_MENU_MENU_LANG_AUTO, UI_MENU_LANGNAME_EN, UI_MENU_LANGNAME_RU}; - opt_choice("TOTK_MENU_LANG", UI_MENU_MENU_LANG, langs, 3, UI_MENU_MENU_LANG_DESC); + static const UiTextId langs[] = {UI_MENU_MENU_LANG_AUTO, UI_MENU_LANGNAME_EN, UI_MENU_LANGNAME_RU, + UI_MENU_LANGNAME_PT_BR, UI_MENU_LANGNAME_ZH_HANS}; + opt_choice("TOTK_MENU_LANG", UI_MENU_MENU_LANG, langs, sizeof langs / sizeof *langs, UI_MENU_MENU_LANG_DESC); + opt_toggle("TOTK_DISCORD_RPC", UI_MENU_DISCORD_RPC, UI_MENU_DISCORD_RPC_DESC); opt_end(); } // ---- Advanced: the testing and troubleshooting keys, the bug report helpers and the reset of every setting -static bool g_modal; // the reset confirmation is open: B and Esc close it, not the menu; L / R stay on the page // A thread count with no fixed default: Automatic (unset), then the common counts. static void row_threads(const char *key, UiTextId label, UiTextId desc) { @@ -779,7 +953,8 @@ static void tab_advanced(float scale) { opt_choice("TOTK_MEMORY", UI_MENU_MEMORY, mem, 4, UI_MENU_MEMORY_DESC); opt_toggle("TOTK_TEX_ALIAS", UI_MENU_TEX_ALIAS, UI_MENU_TEX_ALIAS_DESC); opt_slider("TOTK_TEX_VRAM_PRESSURE", UI_MENU_TEX_VRAM_PRESSURE, 0, 100, UI_MENU_TEX_VRAM_PRESSURE_DESC, 1, "%.0f%%"); - opt_toggle("TOTK_ASTC_BC", UI_MENU_ASTC_BC, UI_MENU_ASTC_BC_DESC); + static const UiTextId astc_bc[] = {UI_MENU_AUTO, UI_MENU_OFF, UI_MENU_ON}; // unset, 0, 1 + opt_choice("TOTK_ASTC_BC", UI_MENU_ASTC_BC, astc_bc, 3, UI_MENU_ASTC_BC_DESC); opt_group(UI_MENU_GROUP_FIXES); opt_toggle("TOTK_LOD_GUARD", UI_MENU_LOD_GUARD, UI_MENU_LOD_GUARD_DESC); @@ -805,6 +980,7 @@ static void tab_advanced(float scale) { row_set_or_not("TOTK_TRACE", UI_MENU_TRACE, UI_MENU_TRACE_DESC, "perf"); opt_toggle("TOTK_FLIGHT", UI_MENU_FLIGHT, UI_MENU_FLIGHT_DESC); opt_slider("TOTK_FLIGHT_SPIKE_MS", UI_MENU_FLIGHT_SPIKE_MS, 20, 1000, UI_MENU_FLIGHT_SPIKE_MS_DESC, 10, "%.0f"); + opt_choice("TOTK_PASS_CRUMBS", UI_MENU_PASS_CRUMBS, dyn, 3, UI_MENU_PASS_CRUMBS_DESC); // vk_crumbs.c pass marks static char rec_path[600]; // the recording goes beside the log if (!rec_path[0]) { char dir[512]; @@ -854,12 +1030,111 @@ static void tab_advanced(float scale) { } } -static const UiTextId kTabNames[TAB_COUNT] = {UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND, - UI_MENU_TAB_CONTROLS, UI_MENU_TAB_MENU, UI_MENU_TAB_ADVANCED}; +static const UiTextId kTabNames[TAB_COUNT] = {UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND, + UI_MENU_TAB_CONTROLS, UI_MENU_TAB_AMIIBO, UI_MENU_TAB_MENU, + UI_MENU_TAB_ADVANCED, UI_MENU_TAB_MODS, UI_MENU_TAB_CHEATS}; static int g_tab; // the tab shown static int g_tab_request = -1; // select this tab at the next layout; -1 none static int g_focus_page = -1; // this tab's first row takes the cursor once it is shown; -1 none +// A row of another tab: that tab opens with the row under the cursor. +static void go_to(int tab, const char *key) { + g_tab_request = tab; + opt_focus_key(key); +} + +// ---- Mods: the mods folder (mods.c) with switches and the load order, then the port's own patches. The folder was +// read at start; the rows change the order and switches for the next start. +static void mods_builtin_rows(void) { + opt_group(UI_MENU_MODS_BUILTIN); + char fps[64], res[64]; + const char *lim = cfg_value("TOTK_FPS_LIMIT"); + if (!strcmp(lim, "display")) snprintf(fps, sizeof fps, "%s", menu_text(UI_MENU_FPS_DISPLAY)); + else snprintf(fps, sizeof fps, "%s FPS", lim); + if (opt_button("##mods_fps60", UI_MENU_MODS_FPS60, UI_MENU_MODS_FPS60_DESC, fps)) go_to(TAB_GAME, "TOTK_FPS_LIMIT"); + int w = 0, h = 0; + sscanf(cfg_value("TOTK_OUTPUT_RES"), "%dx%d", &w, &h); + snprintf(res, sizeof res, "%d × %d", w, h); // U+00D7, as row_output_res shows it + if (opt_button("##mods_res", UI_MENU_MODS_RES, UI_MENU_MODS_RES_DESC, res)) go_to(TAB_SCREEN, "TOTK_OUTPUT_RES"); + opt_toggle("TOTK_REALTIME", UI_MENU_REALTIME, UI_MENU_REALTIME_DESC); + opt_toggle("TOTK_CLOTH_REALTIME", UI_MENU_CLOTH_REALTIME, UI_MENU_CLOTH_REALTIME_DESC); + opt_toggle("TOTK_HIGHFPS_FIX", UI_MENU_HIGHFPS_FIX, UI_MENU_HIGHFPS_FIX_DESC); + opt_button("##mods_quickmenu", UI_MENU_MODS_QUICKMENU, UI_MENU_MODS_QUICKMENU_DESC, + menu_text(romfs_patches_loaded() ? UI_MENU_ON : UI_MENU_OFF)); +} + +static void tab_mods(float scale) { + opt_begin("##mods", scale); + opt_group(UI_MENU_MODS_INSTALLED); + int n = mods_count(); + if (!n) opt_text(UI_MENU_MODS_NONE); + const char *after = mods_changed() ? menu_text(UI_MENU_AFTER_RESTART) : nullptr; + for (int i = 0; i < n; i++) { + const ModInfo *m = mods_at(i); + char id[32], desc[1536]; + snprintf(id, sizeof id, "##mod%d", i); + char over[512] = "", other[512] = ""; + if (m->overridden) snprintf(over, sizeof over, menu_text(UI_MENU_MODS_OVERRIDDEN), m->overridden, m->overridden_by); + if (m->other_version) snprintf(other, sizeof other, menu_text(UI_MENU_MODS_OTHER_VERSION), m->other_version); + char cheat[256] = ""; + if (m->kind & MOD_CHEATS) { // its cheats as read at start, or the file they come from + int count = 0; + for (int j = 0; j < cheats_count(); j++) + count += !strcmp(cheats_info(j)->mod, m->name) && cheats_info(j)->kind != CHEAT_HEADING; + if (count) snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS), count); + else snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS_FILE), cheats_file()); + } + char code[512] = "", problem[512] = ""; + if (m->nsets && m->overrides[0]) + snprintf(code, sizeof code, menu_text(UI_MENU_MODS_REPLACES_BUILTIN), m->nsets, m->overrides); + else if (m->nsets) + snprintf(code, sizeof code, menu_text(UI_MENU_MODS_EXEFS_ON), m->nsets); + switch (m->problem) { + case MOD_EMPTY: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_EMPTY)); break; + case MOD_PLUGIN: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_SUBSDK)); break; + case MOD_EXEFS_LATER: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_EXEFS_LATER)); break; + case MOD_EXEFS_UNSUPPORTED: + snprintf(problem, sizeof problem, menu_text(UI_MENU_MODS_EXEFS_UNSUPPORTED), m->note); + break; + case MOD_EXEFS_OTHER: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_EXEFS_OTHER)); break; + case MOD_BAD_NAME: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_BAD_NAME)); break; + default: break; + } + int k = snprintf(desc, sizeof desc, menu_text(UI_MENU_MODS_ROW_DESC), m->replaced, m->added); + const char *parts[] = {over, other, cheat, code, problem, menu_text(UI_MENU_MODS_MOVE_HINT)}; + for (const char *part : parts) + if (*part && k >= 0 && k < (int)sizeof desc) k += snprintf(desc + k, sizeof desc - k, "\n\n%s", part); + const char *note = m->enabled && m->problem != MOD_OK ? menu_text(UI_MENU_MODS_UNUSED) : after; + int s = opt_text_row(id, m->name, menu_text(m->enabled ? UI_MENU_ON : UI_MENU_OFF), note, desc, true); + if (s == 2 || s == -1 || s == 1) { + mods_set_enabled(i, !m->enabled); + opt_save_soon(); + } else if ((s == 3 && i > 0) || (s == 4 && i + 1 < n)) { + int to = s == 3 ? i - 1 : i + 1; + mods_move(i, to - i); + snprintf(id, sizeof id, "##mod%d", to); + opt_focus_key(id); // the cursor follows the mod + opt_save_soon(); + } + } + bool rebuild = + cheats_rebuild_pending() && opt_button("##restart", UI_MENU_CHEATS_REBUILD, UI_MENU_CHEATS_REBUILD_DESC); + static uint64_t open_failed; // plat_now_ns until which the button says it failed + if (opt_button("##mods_open", UI_MENU_MODS_OPEN, UI_MENU_MODS_OPEN_DESC, + plat_now_ns() < open_failed ? menu_text(UI_MENU_ACTION_FAILED) : "")) { + bool ok = mods_dir()[0] != 0; + if (ok) plat_file_mkdir_p(mods_dir()); + if (!ok || !plat_open_folder(mods_dir())) open_failed = plat_now_ns() + 3000000000ull; + } + mods_builtin_rows(); + opt_end(); + if (rebuild) { + restart_rebuild = true; + ImGui::OpenPopup("##restart_confirm"); + } + restart_confirm(); +} + // ---- the window: title, tabs (L / R or Q / E switch), the page, and the bottom row with "Continue" and "Quit game". // B on the gamepad and Esc close it. static void layout_menu(uint32_t w, uint32_t h, float scale) { @@ -904,9 +1179,18 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) { switch (k) { case TAB_SCREEN: tab_screen(scale); break; case TAB_GAME: tab_game(scale); break; + case TAB_MODS: tab_mods(scale); break; case TAB_SOUND: tab_sound(scale); break; case TAB_CONTROLS: tab_controls(scale); break; + case TAB_AMIIBO: tab_amiibo(scale); break; case TAB_MENU: tab_menu(scale); break; + case TAB_CHEATS: + if (tab_cheats(scale)) { + restart_rebuild = true; + ImGui::OpenPopup("##restart_confirm"); + } + restart_confirm(); + break; case TAB_ADVANCED: tab_advanced(scale); break; } ImGui::EndTabItem(); @@ -929,7 +1213,7 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) { ImGui::SameLine(); if (ImGui::Button(quit, ImVec2(bw, 0))) gfx_window_request_close(); bool was_modal = g_modal || binds_modal(); - if (g_tab != TAB_ADVANCED) g_modal = false; + if (g_tab != TAB_ADVANCED && g_tab != TAB_CONTROLS) g_modal = false; if (!was_modal && (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false))) set_open(0); ImGui::End(); @@ -1066,7 +1350,11 @@ static void perf_picture(const MenuTarget *t, const ImVec4 &c) { int s = cfg_int("TOTK_SHARPNESS"); snprintf(v[3], sizeof v[3], menu_text(UI_MENU_OV_FMT_FSR), s < 0 ? 0 : s > 100 ? 100 : s); } else { - snprintf(v[3], sizeof v[3], "%s", menu_text(t->scaler ? UI_MENU_OV_SCALE_BILINEAR : UI_MENU_OV_SCALE_NONE)); + // FSR enlarges only (vk_fsr_record): an image larger than the window is shrunk by the blit even with FSR selected + int down = t->scaler == 1 && !strcmp(cfg_value("TOTK_UPSCALER"), "fsr") && + (t->image_w > (int)t->width || t->image_h > (int)t->height); + snprintf(v[3], sizeof v[3], "%s", + menu_text(down ? UI_MENU_OV_SCALE_DOWN : t->scaler ? UI_MENU_OV_SCALE_BILINEAR : UI_MENU_OV_SCALE_NONE)); } double hz = frame_step_limit_hz(); snprintf(v[4], sizeof v[4], "%s", menu_text(UI_MENU_OV_NONE)); @@ -1103,17 +1391,30 @@ static void perf_full_draw(const MenuTarget *t, float scale) { ImGui::SetWindowFontScale(0.72f); const ImVec4 gpu_c(0.55f, 0.83f, 0.31f, 1), cpu_c(0.18f, 0.59f, 0.80f, 1), ram_c(0.80f, 0.45f, 0.70f, 1), fps_c(0.95f, 0.95f, 0.95f, 1), wait_c(0.93f, 0.76f, 0.33f, 1), pic_c(0.62f, 0.70f, 0.95f, 1); + // the port version and the game update first, so a screenshot in a bug report carries both; formatted once per + // menu language + static char ver[96]; + static const char *ver_fmt; + if (ver_fmt != menu_text(UI_MENU_VERSION)) { + ver_fmt = menu_text(UI_MENU_VERSION); + char game[16]; + snprintf(game, sizeof game, "%d.%d.%d", rt_game_version / 100, rt_game_version / 10 % 10, rt_game_version % 10); + snprintf(ver, sizeof ver, ver_fmt, rt_port_version, game); + } + ImGui::TextDisabled("%s", ver); if (ImGui::BeginTable("##perft", 4, ImGuiTableFlags_SizingFixedFit)) { - static const UiTextId labels[] = {UI_MENU_OV_GPU, UI_MENU_OV_VRAM, UI_MENU_OV_CPU, UI_MENU_OV_RAM}; - static const PerfCol c0[] = {{UI_MENU_OV_FPS, 888}}; - static const PerfCol c1[] = {{UI_MENU_OV_FMT_PCT, 100}, {UI_MENU_OV_FMT_GB, 888.8f}, {UI_MENU_OV_FRAME_MS, 888.8f}}; - static const PerfCol c2[] = {{UI_MENU_OV_FMT_TEMP, 188}, {UI_MENU_OV_FMT_GB_OF, 888.8f}, {UI_MENU_OV_MAX_MS, 8888.8f}}; + static const UiTextId labels[] = {UI_MENU_OV_GPU, UI_MENU_OV_VRAM, UI_MENU_OV_CPU, UI_MENU_OV_RAM, + UI_MENU_OV_FPS_LABEL, UI_MENU_OV_FRAME_LABEL}; + static const PerfCol c1[] = {{UI_MENU_OV_FMT_PCT, 100}, {UI_MENU_OV_FMT_GB, 888.8f}, {UI_MENU_OV_FMT_FPS, 888}, + {UI_MENU_OV_FRAME_MS, 888.8f}}; + static const PerfCol c2[] = {{UI_MENU_OV_FMT_TEMP, 188}, {UI_MENU_OV_FMT_GB_OF, 888.8f}, {UI_MENU_OV_LOW1, 888}, + {UI_MENU_OV_MAX_MS, 8888.8f}}; static const PerfCol c3[] = {{UI_MENU_OV_FMT_POWER, 888}, {UI_MENU_OV_FMT_MHZ, 8888}, {UI_MENU_OV_GAME_PCT, 100}, - {UI_MENU_OV_GAME_GB, 888.8f}}; - perf_column("##c0", c0, 1, labels, 4); - perf_column("##c1", c1, 3); - perf_column("##c2", c2, 3); - perf_column("##c3", c3, 4); + {UI_MENU_OV_GAME_GB, 888.8f}, {UI_MENU_OV_LOW01, 888}}; + perf_column("##c0", nullptr, 0, labels, 6); + perf_column("##c1", c1, 4); + perf_column("##c2", c2, 4); + perf_column("##c3", c3, 5); perf_label(UI_MENU_OV_GPU, gpu_c); perf_cell(UI_MENU_OV_FMT_PCT, s.gpu_pct); perf_cell(UI_MENU_OV_FMT_TEMP, s.gpu_temp_c); @@ -1130,12 +1431,16 @@ static void perf_full_draw(const MenuTarget *t, float scale) { perf_cell(UI_MENU_OV_FMT_GB, perf_gb(s.ram_used_mb)); perf_cell(UI_MENU_OV_FMT_GB_OF, perf_gb(s.ram_total_mb)); perf_cell(UI_MENU_OV_GAME_GB, perf_gb(s.ram_game_mb)); - ImGui::TableNextColumn(); // the frame row: no label, the rate in its place - char b[64]; - snprintf(b, sizeof b, menu_text(UI_MENU_OV_FPS), s.fps); - ImGui::TextColored(fps_c, "%s", b); + ImGui::TableNextRow(ImGuiTableRowFlags_None, 6 * scale); // a gap between the machine and the frames + // the frames: the rate over the last second with the lows over the last ten seconds, then the frame times + perf_label(UI_MENU_OV_FPS_LABEL, fps_c); + perf_cell(UI_MENU_OV_FMT_FPS, s.fps); + perf_cell(UI_MENU_OV_LOW1, s.low1_fps); + perf_cell(UI_MENU_OV_LOW01, s.low01_fps); + perf_label(UI_MENU_OV_FRAME_LABEL, fps_c); perf_cell(UI_MENU_OV_FRAME_MS, s.frame_ms_avg); perf_cell(UI_MENU_OV_MAX_MS, s.frame_ms_max); + ImGui::TableNextColumn(); ImGui::EndTable(); } float top = s.frame_ms_max > 50 ? s.frame_ms_max : 50; @@ -1183,10 +1488,12 @@ static void perf_draw(const MenuTarget *t, float scale) { extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) { *shot = NULL; + cheats_frame(frame); // the data cheats and the buttons of the cheats that need them held if (!g_test.parsed) test_parse(); if (g_test.next_set < g_test.n_sets && frame >= g_test.sets[g_test.next_set].frame) test_apply_sets(frame); if (g_perf_save.exchange(0)) cfg_save(); perf_tick(plat_now_ns(), t); + amiibo_tick(); bool open = g_open.load(std::memory_order_relaxed); if (!open) { if (g_save_pending) g_save_pending = opt_save_tick(1); // closed: the last change goes to totk.ini now diff --git a/runtime/ui/menu.h b/runtime/ui/menu.h index ae24384b..c68ae9f6 100644 --- a/runtime/ui/menu.h +++ b/runtime/ui/menu.h @@ -5,8 +5,9 @@ // // F1 or Esc on the keyboard and Minus and Plus held together (consumed in poll_merged, input_menu_gate in input.h) open it; // F1, Esc, B or "Continue" close it. While it is open the game gets no input (input_menu, input.h) and keeps running; the -// cursor is released from the mouse camera. Tabs Screen, Game, Sound, Controls and Menu (L / R or Q / E switch them) hold -// option rows (ui/option_rows.h) over the cfg settings; the bottom row has "Continue" and "Quit game". +// cursor is released from the mouse camera. Tabs Screen, Game, Mods, Sound, Controls and Menu (L / R or Q / E switch +// them) hold option rows (ui/option_rows.h) over the cfg settings and the mods folder (mods.h); the bottom row has +// "Continue" and "Quit game". // Test hooks (ui/menu.cpp): TOTK_MENU_TEST=<frame>[:<frames per page>] opens the menu at that presented frame and saves // one swapchain shot per tab into TOTK_MENU_TEST_DIR; TOTK_MENU_TEST_SET=<frame>:<key>=<value>[,...][;...] sets settings // at given frames without input (SAVE=1 writes totk.ini). @@ -60,6 +61,7 @@ int menu_font_check(void); // ---- what the menu asks of the platform side (gfx_window_win.c, gfx_window_sdl.c) void gfx_window_apply_settings(void); // TOTK_FULLSCREEN or TOTK_WINDOW_SIZE changed: the window follows void gfx_window_request_close(void); // as the window's close button: the process ends +void gfx_window_request_restart(void); // the same, then totk.exe starts again with the same command line int gfx_window_monitor_size(int *w, int *h); // pixels of the monitor the window is on; 0 when unknown double gfx_window_refresh_hz(void); // its refresh rate (TOTK_FPS_LIMIT=display); 0 when unknown unsigned gfx_window_dpi(void); // the window's DPI (96 at 100% display scale); any thread diff --git a/runtime/ui/option_rows.cpp b/runtime/ui/option_rows.cpp index 04199d01..6074ca12 100644 --- a/runtime/ui/option_rows.cpp +++ b/runtime/ui/option_rows.cpp @@ -33,15 +33,15 @@ static char g_focus_key[64]; // opt_focus_key: the row that takes th static bool g_held; // the last row's left / right has been held a while (steppers go faster) static float g_bottom; // opt_reserve_bottom: kept free under the panes static bool g_disable_next; // opt_disable_next: the next row is greyed out with g_disable_note -static UiTextId g_disable_note; +static const char *g_disable_note; // The description pane: this frame's hovered and selected rows, the page's first row, and the last one shown. struct Desc { char text[UI_DESC_BYTES]; }; static Desc g_hover, g_focus, g_first, g_shown; // disabled: the note of a row opt_disable_next greyed out, else NULL. -static void desc_set(Desc &d, UiTextId desc, const char *key, const CfgMeta *m, const char *disabled) { - int n = snprintf(d.text, sizeof d.text, "%s", menu_text(desc)); +static void desc_set(Desc &d, const char *desc, const char *key, const CfgMeta *m, const char *disabled) { + int n = snprintf(d.text, sizeof d.text, "%s", desc); if (n < 0 || n >= (int)sizeof d.text) return; if (disabled) snprintf(d.text + n, sizeof d.text - n, "\n\n%s", disabled); @@ -56,8 +56,11 @@ void opt_focus_first(void) { g_focus_req = true; } void opt_focus_key(const char *key) { snprintf(g_focus_key, sizeof g_focus_key, "%s", key); } void opt_reserve_bottom(float h) { g_bottom = h; } +float opt_bottom(void) { return g_bottom; } -void opt_disable_next(UiTextId note) { +void opt_disable_next(UiTextId note) { opt_disable_next_text(menu_text(note)); } + +void opt_disable_next_text(const char *note) { g_disable_next = true; g_disable_note = note; } @@ -107,11 +110,12 @@ struct RowView { const char *note; // under the value instead of the "default" mark; NULL for none }; -// One row: draws it, handles A / click and left / right while selected. Returns 2 (A / click), -1 / +1 (left / right) or -// 0; always 0 for a row the environment fixes (greyed out, still selectable so its description shows). -static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) { +// One row: draws it, handles A / click and left / right while selected. Returns 2 (A / click), -1 / +1 (left / right), +// 3 / 4 (move up / down, movable rows only) or 0; always 0 for a row the environment fixes (greyed out, still selectable +// so its description shows). movable: the top / left face buttons, Ctrl+Up / Ctrl+Down and two drawn arrows move it. +static int row_text(const char *key, const char *label, const char *desc, const RowView &v, bool movable) { const CfgMeta *m = cfg_meta(key); - const char *disabled = g_disable_next ? menu_text(g_disable_note) : NULL; + const char *disabled = g_disable_next ? g_disable_note : NULL; g_disable_next = false; bool off = disabled || cfg_from_env(key) != 0; ImGui::PushID(key); @@ -145,6 +149,13 @@ static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) // left and right belong to the row (navigation would otherwise move sideways into the description pane) ImGui::SetKeyOwner(k, ImGui::GetItemID()); } + if (movable) { + bool ctrl = ImGui::GetIO().KeyCtrl; + if (ImGui::IsKeyPressed(ImGuiKey_GamepadFaceUp, false) || (ctrl && ImGui::IsKeyPressed(ImGuiKey_UpArrow))) + step = 3; + if (ImGui::IsKeyPressed(ImGuiKey_GamepadFaceLeft, false) || (ctrl && ImGui::IsKeyPressed(ImGuiKey_DownArrow))) + step = 4; + } } if (foc) desc_set(g_focus, desc, key, m, disabled); if (hov) desc_set(g_hover, desc, key, m, disabled); @@ -174,11 +185,25 @@ static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) right = rx - gap; value_left = lx; } + if (movable) { // up and down arrows left of the value; a click on one moves the row + // a fixed value column (On / Off differ in width) keeps the arrows of the rows in one line + float aw = fs * 0.42f, gap = fs * 0.55f; + float cx = right - fmaxf(ImGui::CalcTextSize(v.value).x, fs * 2.8f) - gap * 2 - aw; + float uy = ty + fs * 0.12f, dy = ty + fs * 0.92f; + ImU32 ac = off ? kGrey : hov || foc ? kGold : IM_COL32(170, 170, 160, 255); + dl->AddTriangleFilled(ImVec2(cx, uy + aw * 0.7f), ImVec2(cx + aw, uy + aw * 0.7f), ImVec2(cx + aw * 0.5f, uy), ac); + dl->AddTriangleFilled(ImVec2(cx, dy - aw * 0.7f), ImVec2(cx + aw, dy - aw * 0.7f), ImVec2(cx + aw * 0.5f, dy), ac); + if (clicked && !off) { + ImVec2 mp = ImGui::GetIO().MousePos; + if (mp.x >= cx - gap * 0.5f && mp.x <= cx + aw + gap * 0.5f) step = mp.y < ty + fs * 0.52f ? 3 : 4; + } + value_left = cx - gap; + } if (v.value[0]) { float vx = right - ImGui::CalcTextSize(v.value).x; dl->AddText(ImVec2(vx, ty), off ? kGrey : v.is_default ? IM_COL32(205, 205, 195, 255) : IM_COL32(255, 255, 255, 255), v.value); - if (!v.arrows) value_left = vx; + if (!v.arrows && !movable) value_left = vx; } if (v.note || v.is_default) { // under the value, right-aligned with it const char *t = v.note ? v.note : menu_text(UI_MENU_DEFAULT_MARK); @@ -187,8 +212,7 @@ static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) } // the label and its note, clipped before the value ImVec4 clip(p.x, p.y, value_left - fs * 0.6f, p.y + h); - dl->AddText(font, fs, ImVec2(p.x + pad, ty), off ? kGrey : IM_COL32(240, 236, 224, 255), menu_text(label), NULL, 0, - &clip); + dl->AddText(font, fs, ImVec2(p.x + pad, ty), off ? kGrey : IM_COL32(240, 236, 224, 255), label, NULL, 0, &clip); const char *note = disabled ? disabled : off ? menu_text(UI_MENU_SET_BY_ENV) : m && !m->live ? menu_text(UI_MENU_AFTER_RESTART) @@ -198,6 +222,16 @@ static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) return off ? 0 : step; } +static int row(const char *key, UiTextId label, UiTextId desc, const RowView &v) { + return row_text(key, menu_text(label), menu_text(desc), v, false); +} + +int opt_text_row(const char *id, const char *label, const char *value, const char *note, const char *desc, + bool movable) { + RowView v = {value ? value : "", false, false, note}; + return row_text(id, label, desc, v, movable); +} + // ---- writing a setting: at once in memory (live settings follow), to totk.ini a moment later (opt_save_tick) static uint64_t g_save_at; // plat_now_ns deadline of the pending write; 0 none static bool g_save_logged; @@ -241,6 +275,26 @@ bool opt_button(const char *id, UiTextId label, UiTextId desc, const char *value return row(id, label, desc, v) == 2; // A or a click; left and right do nothing } +bool opt_toggle_text(const char *id, const char *label, const char *desc, bool on, const char *value) { + // off is the plain state: its value drawn dim, without the "default" mark + RowView v = {value ? value : menu_text(on ? UI_MENU_ON : UI_MENU_OFF), !on, false, on ? NULL : ""}; + return row_text(id, label, desc, v, false) != 0; // any step flips it +} + +void opt_group_text(const char *title) { + ImGui::Dummy(ImVec2(0, 6 * g_scale)); + ImGui::PushTextWrapPos(0); + ImGui::TextColored(ImVec4(0.93f, 0.76f, 0.33f, 1), "%s", title); + ImGui::PopTextWrapPos(); +} + +void opt_text_str(const char *text, bool warn) { + ImGui::PushTextWrapPos(0); + if (warn) ImGui::TextColored(ImVec4(0.95f, 0.66f, 0.40f, 1), "%s", text); + else ImGui::TextUnformatted(text); + ImGui::PopTextWrapPos(); +} + void opt_group(UiTextId title) { ImGui::Dummy(ImVec2(0, 6 * g_scale)); ImGui::TextColored(ImVec4(0.93f, 0.76f, 0.33f, 1), "%s", menu_text(title)); @@ -294,12 +348,18 @@ bool opt_choice(const char *key, UiTextId label, const UiTextId *names, int n, U return opt_choice_list(key, label, values, shown, n, desc, -1, note); } -bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId desc, float step, const char *fmt) { - const CfgMeta *m = cfg_meta(key); - double v = cfg_double(key), def = m ? strtod(m->def, NULL) : lo; +bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId desc, float step, const char *fmt, + const char *note, const char *fallback) { + const char *set = cfg_get(key); + const char *src = fallback && !(set && *set) ? fallback : key; // unset: show the value in effect, the fallback's + const CfgMeta *m = cfg_meta(src); + // A key without a table default of its own (the per-stick dead zones) defaults to its fallback: the "default" mark + // follows the fallback's default, not 0, even once the key is set. + if (fallback && m && !m->def[0]) m = cfg_meta(fallback); + double v = cfg_double(src), def = m && m->def[0] ? strtod(m->def, NULL) : lo; char shown[48]; snprintf(shown, sizeof shown, fmt, v); - RowView rv = {shown, fabs(v - def) < step * 0.01, true, NULL}; + RowView rv = {shown, fabs(v - def) < step * 0.01, true, note}; int s = row(key, label, desc, rv); if (!s) return false; int steps = g_held && (hi - lo) > 40 * step ? 10 : 1; // held: ten steps at a time over a long range @@ -310,7 +370,8 @@ bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId de char out[32]; if (step >= 1) snprintf(out, sizeof out, "%d", (int)lround(n)); else snprintf(out, sizeof out, "%g", round(n / step) * step); - apply(key, out); + // Landing on the fallback's value unsets the key (its line leaves totk.ini), so the row follows the fallback again. + apply(key, fallback && fabs(n - cfg_double(fallback)) < step * 0.01 ? "" : out); return true; } @@ -400,9 +461,9 @@ int opt_bind_row(const char *key, const char *label, UiTextId desc, const char * } char k[64]; snprintf(k, sizeof k, "TOTK_%s", key); - if (row_foc) desc_set(g_focus, desc, k, NULL, NULL); - if (row_hov) desc_set(g_hover, desc, k, NULL, NULL); - if (!g_first.text[0]) desc_set(g_first, desc, k, NULL, NULL); + if (row_foc) desc_set(g_focus, menu_text(desc), k, NULL, NULL); + if (row_hov) desc_set(g_hover, menu_text(desc), k, NULL, NULL); + if (!g_first.text[0]) desc_set(g_first, menu_text(desc), k, NULL, NULL); if (note && (row_foc || row_hov)) { // e.g. what moved here, after the description Desc &d = row_hov ? g_hover : g_focus; size_t n = strlen(d.text); diff --git a/runtime/ui/option_rows.h b/runtime/ui/option_rows.h index 6e03faf8..b15933b3 100644 --- a/runtime/ui/option_rows.h +++ b/runtime/ui/option_rows.h @@ -19,12 +19,15 @@ void opt_begin(const char *id, float scale); void opt_end(void); // The height the next pages leave free under their panes (the menu's bottom row). void opt_reserve_bottom(float h); +// The height opt_reserve_bottom keeps free, for a tab that lays out its own panes (ui/amiibo_page.cpp). +float opt_bottom(void); // The next page's first row takes the controller's cursor (opening the menu, switching tabs). void opt_focus_first(void); // The row of key (or button id) takes the cursor, scrolled into view, when it is next drawn. void opt_focus_key(const char *key); // The next row is greyed out like one the environment fixes, with note under its label and in its description. void opt_disable_next(UiTextId note); +void opt_disable_next_text(const char *note); // note must outlive the frame // Page text that is not a row: a group heading in the accent colour, a wrapped paragraph (warn: the warning colour). void opt_group(UiTextId title); @@ -45,9 +48,23 @@ bool opt_choice_list(const char *key, UiTextId label, const char *const *values, // A row that runs an action: true on A or a click. id: unique on the page and not a setting ("##copy_info"); value: // shown on the right ("" none). bool opt_button(const char *id, UiTextId label, UiTextId desc, const char *value = ""); +// A row whose label, value, note (under the value) and description are text, not catalog ids: a mod's folder name. +// Returns 2 (A / click), -1 / +1 (left / right), 3 / 4 (move up / down: the top / left face buttons, Ctrl+Up / +// Ctrl+Down, the drawn arrows; movable rows only) or 0. +int opt_text_row(const char *id, const char *label, const char *value, const char *note, const char *desc, + bool movable); +// Rows and text that are not settings of the cfg table: a label and description of the caller's own (the cheats of +// the cheat files). opt_toggle_text shows on / off (value: shown instead, NULL for On / Off) and returns true on A, a +// click, left or right; the caller changes its state. id: unique on the page, the environment check uses it. +bool opt_toggle_text(const char *id, const char *label, const char *desc, bool on, const char *value = nullptr); +void opt_group_text(const char *title); +void opt_text_str(const char *text, bool warn = false); // lo..hi in steps of step (left and right held for a second go ten times as fast); fmt formats the value ("%.0f %%"). +// note: a short line under the value in place of the "default" mark (NULL: none). fallback: the setting that applies +// while key is unset; the row shows its value then, and a change sets key. A key with no table default of its own takes +// the "default" mark from the fallback's default, and a step onto the fallback's value unsets key again. bool opt_slider(const char *key, UiTextId label, float lo, float hi, UiTextId desc, float step = 1, - const char *fmt = "%g"); + const char *fmt = "%g", const char *note = nullptr, const char *fallback = nullptr); // Binding rows (remapping on the Controls tab): a label and three cells, each selectable on its own (left / right move between them). // opt_bind_header: the column titles above them. opt_bind_row: key is the setting's name without TOTK_ (BIND_A; the diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 6a17dd5d..f2feff09 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -1,6 +1,7 @@ // Catalog of the text the host shows the player (ui_text.h). One array per catalog language, indexed by UiTextId: a new // string gets an id in ui_text.h and an entry in every language that has one (a missing entry falls back to English). -// The software keyboard has every language; the menu has English and Russian. +// The software keyboard has every language; the menu has English, Russian, Brazilian Portuguese and Simplified +// Chinese. #include <stdatomic.h> #include <string.h> @@ -16,19 +17,30 @@ typedef struct { const char *lang; const char *const *text; } Row; [UI_MENU_LANGNAME_EN] = "English", [UI_MENU_LANGNAME_JA] = "日本語", [UI_MENU_LANGNAME_FR] = "Français", \ [UI_MENU_LANGNAME_DE] = "Deutsch", [UI_MENU_LANGNAME_IT] = "Italiano", [UI_MENU_LANGNAME_ES] = "Español", \ [UI_MENU_LANGNAME_NL] = "Nederlands", [UI_MENU_LANGNAME_RU] = "Русский", \ - [UI_MENU_LANGNAME_ZH_HANS] = "简体中文", [UI_MENU_LANGNAME_ZH_HANT] = "繁體中文", [UI_MENU_LANGNAME_KO] = "한국어" + [UI_MENU_LANGNAME_ZH_HANS] = "简体中文", [UI_MENU_LANGNAME_ZH_HANT] = "繁體中文", [UI_MENU_LANGNAME_KO] = "한국어", \ + [UI_MENU_LANGNAME_PT_BR] = "Português (Brasil)", [UI_MENU_LANGNAME_EN_GB] = "English (UK)", \ + [UI_MENU_LANGNAME_FR_CA] = "Français (Canada)", [UI_MENU_LANGNAME_ES_419] = "Español (Latinoamérica)" // SWKBD: TITLE, PROMPT, DEFAULT_NAME (at most 9 UTF-16 units, no '@' '%' '\'), OK, CANCEL, PAD_HINT. #define SWKBD(title, prompt, name, ok, cancel, hint) \ [UI_SWKBD_TITLE] = title, [UI_SWKBD_PROMPT] = prompt, [UI_SWKBD_DEFAULT_NAME] = name, [UI_SWKBD_OK] = ok, \ [UI_SWKBD_CANCEL] = cancel, [UI_SWKBD_PAD_HINT] = hint, LANG_NAMES +// The DLSS model file and the executable, as the player sees them on this platform. +#ifdef _WIN32 +#define DLSS_FILE "nvngx_dlss.dll" +#define EXE_FILE "totk.exe" +#else +#define DLSS_FILE "libnvidia-ngx-dlss.so" +#define EXE_FILE "totk" +#endif + static const char *const text_en[UI_TEXT_COUNT] = { SWKBD("Text entry", "Enter a name", "Horse", "OK", "Cancel", "Controller: A = OK, B = Cancel"), [UI_MENU_TITLE] = "Settings", - [UI_MENU_TAB_SCREEN] = "Screen", [UI_MENU_TAB_GAME] = "Game", [UI_MENU_TAB_SOUND] = "Sound", - [UI_MENU_TAB_CONTROLS] = "Controls", [UI_MENU_TAB_MENU] = "Menu", [UI_MENU_TAB_ADVANCED] = "Advanced", + [UI_MENU_TAB_SCREEN] = "Screen", [UI_MENU_TAB_GAME] = "Game", [UI_MENU_TAB_MODS] = "Mods", [UI_MENU_TAB_SOUND] = "Sound", + [UI_MENU_TAB_CONTROLS] = "Controls", [UI_MENU_TAB_AMIIBO] = "Amiibo", [UI_MENU_TAB_MENU] = "Menu", [UI_MENU_TAB_ADVANCED] = "Advanced", [UI_MENU_CONTINUE] = "Continue", [UI_MENU_QUIT] = "Quit game", [UI_MENU_ON] = "On", [UI_MENU_OFF] = "Off", [UI_MENU_AUTO] = "Automatic", [UI_MENU_DEFAULT_MARK] = "default", [UI_MENU_AFTER_RESTART] = "after restart", [UI_MENU_SET_BY_ENV] = "set by environment variable", @@ -53,7 +65,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_OUTPUT_RES] = "Game resolution", [UI_MENU_OUTPUT_RES_DESC] = "The size the game draws its picture and interface in. Above 1920 x 1080 the game needs " "more video memory and GPU time. A wider size (21:9) widens the camera's view and moves " - "the HUD to the screen's corners; a taller one (16:10, Steam Deck) moves it to the top " + "the HUD to the screen's corners; a taller one (16:10, 3:2) moves it to the top " "and bottom edges.", [UI_MENU_RENDER_SCALE] = "3D render scale", [UI_MENU_RENDER_SCALE_DESC] = "Size of the 3D scene relative to the game resolution; the game's FSR scales it to " @@ -69,6 +81,12 @@ 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; 256 is coarser than the " + "game's own and is for integrated GPUs short of memory bandwidth.", + [UI_MENU_SHADOW_RES_GAME] = "Game (changes)", [UI_MENU_ANISO] = "Texture filtering", [UI_MENU_ANISO_DESC] = "Anisotropic filtering of surface textures: ground, roads and walls seen at a shallow angle stay sharp into the distance instead of blurring. Game: the level the game asks for, as on the Switch.", [UI_MENU_ANISO_GAME] = "Game", @@ -93,6 +111,59 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_COLOR_SATURATION_DESC] = "Colour strength against grey; 100 % leaves it.", [UI_MENU_COLOR_GAMMA] = "Gamma", [UI_MENU_COLOR_GAMMA_DESC] = "Below 1 brightens dark and middle tones, above 1 darkens them; 1 leaves them.", + [UI_MENU_HDR_GROUP] = "HDR", + [UI_MENU_HDR] = "HDR output", + [UI_MENU_HDR_DESC] = "Bright highlights on an HDR display. Needs HDR turned on in Windows (\"Use HDR\") or in a " + "Wayland compositor with colour management; without it the picture stays SDR. Applies after a " + "restart. The colour profile above is for SDR and is not used in HDR.", + [UI_MENU_HDR_PEAK] = "Peak brightness", + [UI_MENU_HDR_PEAK_DESC] = "Your display's peak in nits; the brightest highlights stop there.", + [UI_MENU_HDR_GAME] = "Game brightness", + [UI_MENU_HDR_GAME_DESC] = "Nits of the picture's white (paper white). 203 is the HDR reference; raise it in a " + "bright room.", + [UI_MENU_HDR_UI] = "HUD and menu brightness", + [UI_MENU_HDR_UI_DESC] = "Nits of the HUD's and this menu's white.", + [UI_MENU_HDR_HUE] = "Highlight hue correction", + [UI_MENU_HDR_HUE_DESC] = "Bright colours such as fire and sparks turn towards white as in the game's own SDR picture, " + "matching the light they cast; 0 keeps them fully saturated.", + [UI_MENU_SHADOW_SCALE] = "Shadow resolution", + [UI_MENU_SHADOW_SCALE_DESC] = "Draws the sun's shadow maps this many times larger on each side than the game's 960x960: " + "sharper shadow edges and leaf shadows. Takes effect at the next start.", + [UI_MENU_PCSS] = "Soft shadows", + [UI_MENU_PCSS_DESC] = "Sun shadows sharp where an object meets the ground and softer the further the shadow falls " + "(PCSS). 0 keeps the game's filter. Takes effect at the next start.", + [UI_MENU_CONTACT_SHADOWS] = "Contact shadows", + [UI_MENU_CONTACT_SHADOWS_DESC] = "Small sun shadows the shadow maps miss: at feet, under stones, in grass and creases. " + "Traced on the screen after lighting. Takes effect at the next start.", + [UI_MENU_SHADOW_RANGE] = "Shadow cascade range", + [UI_MENU_SHADOW_RANGE_DESC] = "Moves the steps where shadows turn softer (the game's 20, 40, 80 and 160 m) further " + "away. Spreads the shadow map over more ground: raise Shadow resolution with it. " + "Takes effect at the next start.", + [UI_MENU_DLAA] = "NVIDIA DLSS / DLAA", + [UI_MENU_DLAA_DESC] = "NVIDIA DLSS for the 3D scene (NVIDIA RTX GPUs; " DLSS_FILE " next to " EXE_FILE "): smoother " + "edges and less shimmer. The HUD is not touched. The mode below sets the resolution it works " + "from. Takes effect after a restart.", + [UI_MENU_DLSS_PRESET] = "DLSS model", + [UI_MENU_DLSS_PRESET_DESC] = "Which DLSS model runs. Automatic follows NVIDIA's advice per mode: K for DLAA, Quality " + "and Balanced, M for Performance, L for Ultra Performance. J: slightly less ghosting " + "behind moving characters, a little more flicker. K: the sharpest model in every mode. " + "Applies at once.", + [UI_MENU_DLSS_PRESET_DEFAULT] = "Automatic", [UI_MENU_DLSS_PRESET_K] = "K (sharpest)", + [UI_MENU_DLSS_PRESET_J] = "J (less ghosting)", + [UI_MENU_DLSS_CHARS] = "DLSS character trails", + [UI_MENU_DLSS_CHARS_DESC] = "Reduces the faint trails behind moving characters, animals and what they hold: DLSS takes " + "less of the previous frames there. Stronger removes more of the trails and leaves those " + "edges a little less smooth. Applies at once.", + [UI_MENU_DLSS_CHARS_OFF] = "Off", [UI_MENU_DLSS_CHARS_LIGHT] = "Light", [UI_MENU_DLSS_CHARS_MEDIUM] = "Medium", + [UI_MENU_DLSS_CHARS_FULL] = "Full", + [UI_MENU_DLSS] = "DLSS mode", + [UI_MENU_DLSS_DESC] = "DLAA draws the 3D scene at full resolution. Quality, Balanced, Performance and Ultra " + "Performance draw it at 67, 58, 50 and 33 % and let DLSS scale it up, which saves GPU time; " + "they replace the 3D render scale. Takes effect at once.", + [UI_MENU_DLSS_DLAA] = "DLAA (native)", [UI_MENU_DLSS_QUALITY] = "Quality", [UI_MENU_DLSS_BALANCED] = "Balanced", + [UI_MENU_DLSS_PERFORMANCE] = "Performance", [UI_MENU_DLSS_ULTRA] = "Ultra Performance", + [UI_MENU_FXAA] = "Game anti-aliasing (FXAA)", + [UI_MENU_FXAA_DESC] = "The game's own FXAA edge smoothing. It blurs fine detail a little; with DLAA on it is not needed.", [UI_MENU_FPS_LIMIT] = "Frame rate limit", [UI_MENU_FPS_LIMIT_DESC] = "The most frames per second the game draws. Above 60 is experimental and needs Game speed " @@ -118,7 +189,42 @@ static const char *const text_en[UI_TEXT_COUNT] = { "window loses focus, as on the Switch when Home is pressed; a load interrupted that " "way can stop for good.", + [UI_MENU_MODS_BUILTIN] = "Built into the port", [UI_MENU_MODS_INSTALLED] = "Installed mods", + [UI_MENU_MODS_NONE] = "No mods yet. Unpack a mod into the mods folder, one folder per mod with its romfs folder " + "inside, and restart the game. TKMM can export its merged mods into a folder there too.", + [UI_MENU_MODS_OPEN] = "Open mods folder", + [UI_MENU_MODS_OPEN_DESC] = "Shows the folder the game loads mods from. It is created if it is missing.", + [UI_MENU_MODS_ROW_DESC] = "Game files it replaces: %u. New files it adds: %u.", + [UI_MENU_MODS_OVERRIDDEN] = "%u of its files come from %s, which is higher in the list.", + [UI_MENU_MODS_MOVE_HINT] = "The top face button or Ctrl+Up moves it up, the left face button or Ctrl+Down moves it " + "down. When two mods change the same file, the higher one wins. Changes apply after a " + "restart.", + [UI_MENU_MODS_EMPTY] = "This folder has no romfs, exefs or cheats files.", + [UI_MENU_MODS_SUBSDK] = "This mod brings its own game code (a subsdk plugin or a replaced main). The port cannot " + "load code modules, so the whole mod stays off.", + [UI_MENU_MODS_EXEFS_LATER] = "This mod patches the game code (exefs). The game builds the patches in when it " + "starts next (a rebuild of a few minutes); until then the whole mod stays off.", + [UI_MENU_MODS_CHEATS] = "Cheats: %d. Switch them on the Cheats tab.", + [UI_MENU_MODS_CHEATS_FILE] = "Cheats for this game version are read at start from cheats\\%s in this mod.", + [UI_MENU_MODS_BAD_NAME] = "The folder name contains |, which the mod list uses between names. Rename the folder.", + [UI_MENU_MODS_OTHER_VERSION] = "%u of its files are made for another game version and are left out. Look for a " + "version of this mod made for your game version.", + [UI_MENU_MODS_UNUSED] = "not used", + [UI_MENU_MODS_FPS60] = "60 FPS patch", + [UI_MENU_MODS_FPS60_DESC] = "The port's own patch for 60 frames per second. Set it with Frame rate limit on the " + "Game tab.", + [UI_MENU_MODS_RES] = "Resolution patch", + [UI_MENU_MODS_RES_DESC] = "The port's own patch for resolutions above 1080p. Set it with Game resolution on the " + "Screen tab.", + [UI_MENU_MODS_QUICKMENU] = "Quick menu fix", + [UI_MENU_MODS_QUICKMENU_DESC] = "The port's fix for the black background of the weapon, shield and material menus " + "at 60 FPS.", + [UI_MENU_VOLUME] = "Volume", [UI_MENU_VOLUME_DESC] = "Master volume of the game.", + [UI_MENU_AUDIO_CHANNELS] = "Speakers", + [UI_MENU_AUDIO_CHANNELS_DESC] = "Stereo or 5.1 surround output. Automatic picks 5.1 when the Windows speaker setup " + "has six or more channels. Applies after a restart.", + [UI_MENU_CHANNELS_AUTO] = "Automatic", [UI_MENU_CHANNELS_STEREO] = "Stereo", [UI_MENU_CHANNELS_51] = "5.1 surround", [UI_MENU_MOTION] = "Motion controls", [UI_MENU_MOTION_DESC] = "Aiming with the controller's gyroscope.", [UI_MENU_RUMBLE_STRENGTH] = "Vibration strength", @@ -130,19 +236,47 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_PAD_LAYOUT] = "Button layout", [UI_MENU_PAD_LAYOUT_DESC] = "Which button is A. Nintendo: the right one, as on a Switch. Xbox: the bottom one, as " "labelled on an Xbox controller (cross on a PlayStation controller). Automatic picks " - "Nintendo for controllers with Nintendo labels and Xbox for the others. Xbox prompts: " - "A is the right one as with Nintendo, and the game shows the Xbox labels (B for A, A " - "for B, Y for X, X for Y); the prompts change after a restart. The letters are part of the " - "game's files, so a button you remap in Controls keeps the old letter on screen.", + "Nintendo for controllers with Nintendo labels and Xbox for the others. Which " + "controller the game's prompts show is set under Button prompts.", [UI_MENU_PAD_LAYOUT_AUTO] = "Automatic", [UI_MENU_PAD_LAYOUT_NINTENDO] = "Nintendo (A right)", - [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox (A bottom)", [UI_MENU_PAD_LAYOUT_XBOX_PROMPTS] = "Xbox prompts (A right)", + [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox (A bottom)", [UI_MENU_PAD_LAYOUT_IN_USE] = "in use: %s", - [UI_MENU_PAD_LAYOUT_PROMPTS_MISSING] = "prompt files missing: run the setup again", - [UI_MENU_PAD_LAYOUT_PROMPTS_RESTART] = "prompts change after restart", - [UI_MENU_STICK_DEADZONE] = "Stick dead zone", - [UI_MENU_STICK_DEADZONE_DESC] = "How far a gamepad stick moves before the game sees it. Raise it if the character or the camera moves on its own.", + [UI_MENU_PROMPTS] = "Button prompts", + [UI_MENU_PROMPTS_DESC] = "Which controller's buttons the game's prompts show. Nintendo: the game's own. Xbox: LB, " + "RB, LT, RT, Menu, View and a d-pad cross, and A, B, X and Y as labelled on the button " + "the layout above presses (with Nintendo (A right) the right button is labelled B, so the " + "prompts for A show B). The game reads its prompts at start, so they change after a " + "restart. They are part of the game's files, so a button you remap in Controls keeps its " + "old prompt on screen.", + [UI_MENU_PROMPTS_NINTENDO] = "Nintendo", [UI_MENU_PROMPTS_XBOX] = "Xbox", + [UI_MENU_PROMPTS_MISSING] = "prompt files missing: run the setup again", + [UI_MENU_PROMPTS_FAILED] = "prompt files damaged: run the setup again", + [UI_MENU_PROMPTS_RESTART] = "prompts change after restart", + [UI_MENU_PROMPTS_OFF_SHAPE] = "off at a game resolution not 16:9", + [UI_MENU_PROMPTS_OFF_MODS] = "off: a RomFS mod replaces their files", + [UI_MENU_PROMPTS_OFF_PATCHES] = "off while TOTK_ROMFS_PATCHES is set", + [UI_MENU_RESTART] = "Restart now", + [UI_MENU_RESTART_DESC] = "Closes the game and starts it again with the new prompts. Progress since your last save " + "is lost.", + [UI_MENU_RESTART_CONFIRM] = "Restart the game now? Progress since your last save is lost.", + [UI_MENU_RESTART_YES] = "Restart", + [UI_MENU_RESTART_SAVING] = "The game is saving: Restart is possible in a moment.", + [UI_MENU_GROUP_STICKS] = "Analog sticks", + [UI_MENU_LSTICK_DEADZONE] = "Left stick dead zone", [UI_MENU_RSTICK_DEADZONE] = "Right stick dead zone", + [UI_MENU_STICK_DEADZONE_DESC] = "How far a gamepad stick moves before the game sees it. Raise it if the character or the camera moves on its own: let go of the stick, read \"stick now\" and set the dead zone a few percent above it.", + [UI_MENU_STICK_DEADZONE_NOW] = "stick now: %.0f %%", [UI_MENU_MOUSE_SENS] = "Mouse camera sensitivity", [UI_MENU_MOUSE_SENS_DESC] = "How fast the mouse turns the camera.", + [UI_MENU_WALK_KEY] = "Walk key", + [UI_MENU_WALK_KEY_DESC] = "Hold it to tilt the keyboard's sticks part of the way, so the keyboard walks instead of " + "runs. The walk scale sets how far. A controller and the mouse are not affected.", + [UI_MENU_WALK_KEY_TAKEN] = "also %s", + [UI_MENU_WALK_SCALE] = "Walk scale", + [UI_MENU_WALK_SCALE_DESC] = "The tilt the keyboard's sticks get while the walk key is held. Half is a walk, the " + "full tilt a run.", + [UI_MENU_STICK_SENS] = "Stick camera speed", + [UI_MENU_STICK_SENS_DESC] = "Multiplies the game's own right-stick camera speed, top speed included, at each of the " + "game's five camera sensitivities. The mouse keeps its own setting.", [UI_BINDS_HELP] = "Select a cell and press A or Enter, then the key, mouse button or controller button. Delete or " "X clears a cell. A key in use moves here; a controller button swaps with the one it replaces.", @@ -190,8 +324,41 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_PAD_LPADDLE2] = "Left back button 2", [UI_PAD_RPADDLE2] = "Right back button 2", [UI_PAD_CROSS] = "Cross", [UI_PAD_CIRCLE] = "Circle", [UI_PAD_SQUARE] = "Square", [UI_PAD_TRIANGLE] = "Triangle", + [UI_MENU_AMIIBO_DIR] = "Amiibo folder", + [UI_MENU_AMIIBO_OPEN] = "Open", + [UI_MENU_AMIIBO_DEFAULT_DIR] = "Default folder", + [UI_MENU_AMIIBO_REFRESH] = "Refresh", + [UI_MENU_AMIIBO_PICK] = "Amiibo dumps (540-byte .bin or .nfc files):", + [UI_MENU_AMIIBO_NO_DIR] = "Cannot open the folder %s", + [UI_MENU_AMIIBO_READER] = "NFC reader", + [UI_MENU_AMIIBO_STATE] = "Reader:", + [UI_MENU_AMIIBO_STATE_IDLE] = "ready", + [UI_MENU_AMIIBO_STATE_SEARCHING] = "the game is waiting for an amiibo", + [UI_MENU_AMIIBO_STATE_FOUND] = "amiibo on the reader", + [UI_MENU_AMIIBO_STATE_MOUNTED] = "the game is reading the amiibo", + [UI_MENU_AMIIBO_STATE_OFF] = "off", + [UI_MENU_AMIIBO_FILE] = "Selected file:", + [UI_MENU_AMIIBO_NO_FILE] = "(no file selected)", + [UI_MENU_AMIIBO_TAP] = "Tap the amiibo", + [UI_MENU_AMIIBO_REMOVE] = "Lift the amiibo", + [UI_MENU_AMIIBO_AUTO_REMOVE] = "Lift the amiibo by itself after a tap", + [UI_MENU_AMIIBO_HOLD] = "Time on the reader", + [UI_MENU_AMIIBO_SECONDS] = "%.1f s", + [UI_MENU_AMIIBO_TAG] = "Amiibo on the reader:", + [UI_MENU_AMIIBO_NO_TAG] = "No amiibo on the reader.", + [UI_MENU_AMIIBO_TAG_FILE] = "File: %s", + [UI_MENU_AMIIBO_TAG_ID] = "Character %s, series %02x, number %04x, type %02x", + [UI_MENU_AMIIBO_TAG_UID] = "UID: %s", + [UI_MENU_AMIIBO_READY] = "Ready", + [UI_MENU_AMIIBO_TAPPED] = "Tapped: %s", + [UI_MENU_AMIIBO_LOAD_FAILED] = "Not an amiibo dump (540-byte NTAG215): %s", + [UI_MENU_AMIIBO_REMOVED] = "Amiibo lifted", + [UI_MENU_AMIIBO_STATUS] = "Status: %s", [UI_MENU_MENU_LANG] = "Menu language", [UI_MENU_MENU_LANG_DESC] = "Language of this menu and the overlay.", [UI_MENU_MENU_LANG_AUTO] = "Automatic (from Windows)", + [UI_MENU_DISCORD_RPC] = "Discord Rich Presence", + [UI_MENU_DISCORD_RPC_DESC] = "Shows \"Playing TotK Recomp\" with the play time in your Discord status while the " + "game runs. discord_app_id and discord_image in totk.ini choose the application.", [UI_MENU_ADV_WARNING] = "These settings are for testing and troubleshooting. Change them only when you know why.", [UI_MENU_GROUP_SCHED] = "Scheduling", [UI_MENU_GROUP_SHADERS] = "Shaders and pipelines", @@ -229,7 +396,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_MEMORY_VERIFY] = "Verify", [UI_MENU_TEX_ALIAS] = "Texture aliasing", [UI_MENU_TEX_ALIAS_DESC] = "A new texture created over a live texture's memory starts with that content instead of empty.", [UI_MENU_TEX_VRAM_PRESSURE] = "Texture memory limit", [UI_MENU_TEX_VRAM_PRESSURE_DESC] = "Percent of video memory above which textures the game released are dropped (they come back from storage when used again). 0 keeps them.", - [UI_MENU_ASTC_BC] = "Compress ASTC to BC3", [UI_MENU_ASTC_BC_DESC] = "With the GPU decoder: re-encodes decoded ASTC textures to BC3, a quarter of the video memory. For GPUs with 4 GB or less; textures look slightly blockier.", + [UI_MENU_ASTC_BC] = "Compress ASTC to BC3", [UI_MENU_ASTC_BC_DESC] = "With the GPU decoder: re-encodes decoded ASTC textures to BC3, a quarter of the video memory. Textures look slightly blockier. Auto: on for GPUs with 4 GB of video memory or less.", [UI_MENU_LOD_GUARD] = "LOD guard", [UI_MENU_LOD_GUARD_DESC] = "Prevents objects flashing in the wrong place or a flat far field for a frame, by waiting for the game to finish rewriting level-of-detail data before the GPU reads it.", [UI_MENU_FIX_C0] = "Constant buffer fix", [UI_MENU_FIX_C0_DESC] = "Fills the driver-reserved constant bank c0 (storage sizes, dispatch size, point size, base instance) instead of reading zeros.", @@ -260,6 +427,8 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_FLIGHT_DESC] = "Keeps a short history and saves it when a frame freezes.", [UI_MENU_FLIGHT_SPIKE_MS] = "Freeze threshold (ms)", [UI_MENU_FLIGHT_SPIKE_MS_DESC] = "Frame time that counts as a freeze.", + [UI_MENU_PASS_CRUMBS] = "GPU pass marks", + [UI_MENU_PASS_CRUMBS_DESC] = "After a GPU crash, the log names the render pass that was running. Automatic: AMD only. On: also NVIDIA, at a small cost.", [UI_MENU_INPUT_RECORD] = "Input recording", [UI_MENU_INPUT_RECORD_DESC] = "Writes your inputs to a file for bug reports.", [UI_MENU_OPEN_LOG] = "Open log folder", [UI_MENU_OPEN_LOG_DESC] = "Shows the folder with the log files.", @@ -272,6 +441,8 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_OV_FPS] = "%.0f FPS", [UI_MENU_OV_FRAME_MS] = "%.1f ms", [UI_MENU_OV_GAME_PCT] = "game %.0f%%", [UI_MENU_OV_MAX_MS] = "max %.1f ms", + [UI_MENU_OV_FPS_LABEL] = "FPS", [UI_MENU_OV_FRAME_LABEL] = "Frame", [UI_MENU_OV_FMT_FPS] = "%.0f", + [UI_MENU_OV_LOW1] = "1%% low %.0f", [UI_MENU_OV_LOW01] = "0.1%% low %.0f", [UI_MENU_OV_GPU] = "GPU", [UI_MENU_OV_CPU] = "CPU", [UI_MENU_OV_RAM] = "RAM", [UI_MENU_OV_VRAM] = "VRAM", [UI_MENU_OV_TEMP] = "Temperature", [UI_MENU_OV_POWER] = "Power", [UI_MENU_OV_CLOCK] = "Clock", [UI_MENU_OV_NONE] = "-", @@ -286,14 +457,45 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_OV_FMT_SIZE] = "%ux%u", [UI_MENU_OV_FMT_SCENE] = "%ux%u, %.0f%%", [UI_MENU_OV_FMT_FSR] = "AMD FSR 1, sharpness %d%%", [UI_MENU_OV_SCALE_NONE] = "none (1:1)", [UI_MENU_OV_SCALE_BILINEAR] = "bilinear", + [UI_MENU_OV_SCALE_DOWN] = "bilinear (FSR only enlarges)", + [UI_MENU_TAB_CHEATS] = "Cheats", + [UI_MENU_CHEATS_NONE] = "No cheats for this game version. A mod's cheats are an Atmosphere cheat file in its " + "cheats folder, named after the game's build: mods\\<mod>\\cheats\\%s. Restart the game to " + "read them. For cheats that change the game's code, the game rebuilds itself at start (a " + "few minutes).", + [UI_MENU_CHEATS_MOD] = "%s: %d cheats", + [UI_MENU_CHEAT_CODE_DESC] = "Changes the game's code. Switches at once.", + [UI_MENU_CHEAT_KEY_DESC] = "Changes the game's code while you hold %s. Switches at once.", + [UI_MENU_CHEAT_DATA_DESC] = "Writes the game's memory every frame while it is on.", + [UI_MENU_CHEAT_MASTER] = "always on", + [UI_MENU_CHEAT_MASTER_DESC] = "The file's master code: it is always on.", + [UI_MENU_CHEAT_RESTORE] = "not needed", + [UI_MENU_CHEAT_RESTORE_DESC] = "It only writes the game's own code back. Switching a cheat off already does that.", + [UI_MENU_CHEAT_REBUILD] = "after the rebuild", + [UI_MENU_CHEAT_REBUILD_DESC] = "This cheat changes the game's code and is new or edited since the game was built. " + "The game builds it in when it starts next (a few minutes), or use Rebuild the game " + "now.", + [UI_MENU_CHEAT_UNSUPPORTED] = "not supported", + [UI_MENU_CHEAT_UNSUPPORTED_DESC] = "This cheat cannot be used with this version of the game:", + [UI_MENU_CHEATS_REBUILD] = "Rebuild the game now", + [UI_MENU_CHEATS_REBUILD_DESC] = "Some cheats change the game's code and are not built into it: they are new or " + "edited, or their last rebuild failed or was cancelled. Closes the game; the setup " + "builds them in (a few minutes) and starts the game again. Progress since your " + "last save is lost.", + [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "This mod patches the game code (exefs) in a way the port cannot apply, so the " + "whole mod stays off. %s", + [UI_MENU_MODS_EXEFS_OTHER] = "Its code patches (exefs) are made for another game version.", + [UI_MENU_MODS_EXEFS_ON] = "Code patches in use: %d files.", + [UI_MENU_MODS_REPLACES_BUILTIN] = "Code patches in use: %d files. They replace the port's own patch %s, which " + "stays off while this mod is on.", }; static const char *const text_ru[UI_TEXT_COUNT] = { SWKBD("Ввод текста", "Введите имя", "Лошадь", "ОК", "Отмена", "Контроллер: A — ОК, B — отмена"), [UI_MENU_TITLE] = "Настройки", - [UI_MENU_TAB_SCREEN] = "Экран", [UI_MENU_TAB_GAME] = "Игра", [UI_MENU_TAB_SOUND] = "Звук", - [UI_MENU_TAB_CONTROLS] = "Управление", [UI_MENU_TAB_MENU] = "Меню", [UI_MENU_TAB_ADVANCED] = "Дополнительно", + [UI_MENU_TAB_SCREEN] = "Экран", [UI_MENU_TAB_GAME] = "Игра", [UI_MENU_TAB_MODS] = "Моды", [UI_MENU_TAB_SOUND] = "Звук", + [UI_MENU_TAB_CONTROLS] = "Управление", [UI_MENU_TAB_AMIIBO] = "Amiibo", [UI_MENU_TAB_MENU] = "Меню", [UI_MENU_TAB_ADVANCED] = "Дополнительно", [UI_MENU_CONTINUE] = "Продолжить", [UI_MENU_QUIT] = "Выйти из игры", [UI_MENU_ON] = "Вкл.", [UI_MENU_OFF] = "Выкл.", [UI_MENU_AUTO] = "Автоматически", [UI_MENU_DEFAULT_MARK] = "по умолчанию", @@ -320,7 +522,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_OUTPUT_RES] = "Разрешение игры", [UI_MENU_OUTPUT_RES_DESC] = "Размер, в котором игра рисует картинку и интерфейс. Выше 1920 × 1080 игре нужно больше " "видеопамяти и времени GPU. Более широкий размер (21:9) расширяет обзор камеры и " - "переносит интерфейс в углы экрана, более высокий (16:10, Steam Deck) переносит его к " + "переносит интерфейс в углы экрана, более высокий (16:10, 3:2) переносит его к " "верхнему и нижнему краям.", [UI_MENU_RENDER_SCALE] = "Масштаб 3D-отрисовки", [UI_MENU_RENDER_SCALE_DESC] = "Размер 3D-сцены относительно разрешения игры; FSR игры доводит её до этого размера. " @@ -336,6 +538,12 @@ 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 и видеопамяти; 256 грубее " + "собственного размера игры и предназначен для встроенных GPU с нехваткой пропускной способности памяти.", + [UI_MENU_SHADOW_RES_GAME] = "Как в игре (меняется)", [UI_MENU_ANISO] = "Фильтрация текстур", [UI_MENU_ANISO_DESC] = "Анизотропная фильтрация текстур поверхностей: земля, дороги и стены под острым углом остаются чёткими вдали, а не размываются. Игра: уровень, который запрашивает игра, как на Switch.", [UI_MENU_ANISO_GAME] = "Игра", @@ -361,6 +569,59 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_COLOR_SATURATION_DESC] = "Сила цвета относительно серого; 100 % — без изменений.", [UI_MENU_COLOR_GAMMA] = "Гамма", [UI_MENU_COLOR_GAMMA_DESC] = "Ниже 1 осветляет тёмные и средние тона, выше 1 затемняет их; 1 — без изменений.", + [UI_MENU_HDR_GROUP] = "HDR", + [UI_MENU_HDR] = "Вывод HDR", + [UI_MENU_HDR_DESC] = "Яркие блики на HDR-дисплее. Нужен включённый HDR в Windows («Использовать HDR») или " + "Wayland-композитор с управлением цветом; без него картинка остаётся SDR. Применяется после " + "перезапуска. Цветовой профиль выше — для SDR и в HDR не используется.", + [UI_MENU_HDR_PEAK] = "Пиковая яркость", + [UI_MENU_HDR_PEAK_DESC] = "Пик вашего дисплея в нитах; самые яркие блики останавливаются на нём.", + [UI_MENU_HDR_GAME] = "Яркость игры", + [UI_MENU_HDR_GAME_DESC] = "Ниты белого цвета картинки (paper white). 203 — эталон HDR; в светлой комнате " + "поднимите.", + [UI_MENU_HDR_UI] = "Яркость интерфейса и меню", + [UI_MENU_HDR_UI_DESC] = "Ниты белого цвета интерфейса игры и этого меню.", + [UI_MENU_HDR_HUE] = "Коррекция оттенка бликов", + [UI_MENU_HDR_HUE_DESC] = "Яркие цвета (огонь, искры) уходят к белому, как в SDR-картинке игры, и совпадают со светом, " + "который они дают; 0 оставляет их полностью насыщенными.", + [UI_MENU_SHADOW_SCALE] = "Разрешение теней", + [UI_MENU_SHADOW_SCALE_DESC] = "Карты теней от солнца рисуются во столько раз больше по каждой стороне, чем 960x960 " + "самой игры: края теней и тени листвы чётче. Действует после перезапуска.", + [UI_MENU_PCSS] = "Мягкие тени", + [UI_MENU_PCSS_DESC] = "Тени от солнца резкие там, где предмет касается земли, и мягче, чем дальше падает тень " + "(PCSS). 0 оставляет фильтр игры. Действует после перезапуска.", + [UI_MENU_CONTACT_SHADOWS] = "Контактные тени", + [UI_MENU_CONTACT_SHADOWS_DESC] = "Мелкие тени от солнца, которых нет на картах теней: у ног, под камнями, в траве и " + "складках. Считаются по экрану после освещения. Действует после перезапуска.", + [UI_MENU_SHADOW_RANGE] = "Дальность каскадов теней", + [UI_MENU_SHADOW_RANGE_DESC] = "Отодвигает границы, за которыми тени становятся мягче (в игре 20, 40, 80 и 160 м). " + "Карта теней растягивается на большую площадь, поэтому вместе с этим поднимайте " + "разрешение теней. Действует после перезапуска.", + [UI_MENU_DLAA] = "NVIDIA DLSS / DLAA", + [UI_MENU_DLAA_DESC] = "NVIDIA DLSS для 3D-сцены (видеокарты NVIDIA RTX; " DLSS_FILE " рядом с " EXE_FILE "): ровнее " + "края и меньше мерцания. Интерфейс не затрагивается. Режим ниже задаёт разрешение, с которым он " + "работает. Действует после перезапуска.", + [UI_MENU_DLSS_PRESET] = "Модель DLSS", + [UI_MENU_DLSS_PRESET_DESC] = "Какая модель DLSS работает. «Автоматически» следует совету NVIDIA для каждого режима: K " + "для DLAA, «Качества» и «Баланса», M для «Производительности», L для «Макс. " + "производительности». J: чуть меньше шлейфов за движущимися персонажами, немного больше " + "мерцания. K: самая чёткая модель во всех режимах. Действует сразу.", + [UI_MENU_DLSS_PRESET_DEFAULT] = "Автоматически", [UI_MENU_DLSS_PRESET_K] = "K (самая чёткая)", + [UI_MENU_DLSS_PRESET_J] = "J (меньше шлейфов)", + [UI_MENU_DLSS_CHARS] = "Шлейфы персонажей в DLSS", + [UI_MENU_DLSS_CHARS_DESC] = "Убирает слабые шлейфы за движущимися персонажами, животными и тем, что они держат: DLSS " + "берёт там меньше от прошлых кадров. Чем сильнее, тем меньше шлейфов и тем чуть менее " + "гладкие края. Действует сразу.", + [UI_MENU_DLSS_CHARS_OFF] = "Выкл.", [UI_MENU_DLSS_CHARS_LIGHT] = "Слабо", [UI_MENU_DLSS_CHARS_MEDIUM] = "Средне", + [UI_MENU_DLSS_CHARS_FULL] = "Полностью", + [UI_MENU_DLSS] = "Режим DLSS", + [UI_MENU_DLSS_DESC] = "DLAA рисует 3D-сцену в полном разрешении. «Качество», «Баланс», «Производительность» и " + "«Макс. производительность» рисуют её в 67, 58, 50 и 33 % и масштабируют через DLSS, что " + "экономит время видеокарты; они заменяют масштаб 3D-отрисовки. Действует сразу.", + [UI_MENU_DLSS_DLAA] = "DLAA (родное)", [UI_MENU_DLSS_QUALITY] = "Качество", [UI_MENU_DLSS_BALANCED] = "Баланс", + [UI_MENU_DLSS_PERFORMANCE] = "Производительность", [UI_MENU_DLSS_ULTRA] = "Макс. производительность", + [UI_MENU_FXAA] = "Сглаживание игры (FXAA)", + [UI_MENU_FXAA_DESC] = "Собственное сглаживание FXAA. Слегка размывает мелкие детали; с DLAA оно не нужно.", [UI_MENU_FPS_LIMIT] = "Ограничение частоты кадров", [UI_MENU_FPS_LIMIT_DESC] = "Наибольшее число кадров в секунду. Выше 60 — экспериментальный режим, нужна «Скорость " @@ -385,7 +646,40 @@ static const char *const text_ru[UI_TEXT_COUNT] = { "когда окно теряет фокус, как на Switch при нажатии Home; прерванная так загрузка " "может остановиться насовсем.", + [UI_MENU_MODS_BUILTIN] = "Встроенные в порт", [UI_MENU_MODS_INSTALLED] = "Установленные моды", + [UI_MENU_MODS_NONE] = "Модов пока нет. Распакуйте мод в папку mods, каждый мод в свою папку с папкой romfs внутри, и " + "перезапустите игру. TKMM тоже может выгрузить туда объединённые моды отдельной папкой.", + [UI_MENU_MODS_OPEN] = "Открыть папку модов", + [UI_MENU_MODS_OPEN_DESC] = "Показывает папку, из которой игра загружает моды. Если её нет, она будет создана.", + [UI_MENU_MODS_ROW_DESC] = "Заменяет файлов игры: %u. Добавляет новых файлов: %u.", + [UI_MENU_MODS_OVERRIDDEN] = "%u его файлов берутся из мода %s, он выше в списке.", + [UI_MENU_MODS_MOVE_HINT] = "Верхняя кнопка или Ctrl+Вверх поднимает мод, левая кнопка или Ctrl+Вниз опускает. Если " + "два мода меняют один файл, побеждает верхний. Изменения действуют после перезапуска.", + [UI_MENU_MODS_EMPTY] = "В этой папке нет файлов romfs, exefs или cheats.", + [UI_MENU_MODS_SUBSDK] = "Этот мод приносит свой код игры (плагин subsdk или заменённый main). Порт не загружает " + "модули кода, поэтому мод выключен целиком.", + [UI_MENU_MODS_EXEFS_LATER] = "Этот мод меняет код игры (exefs). Игра соберёт эти патчи при следующем запуске " + "(пересборка на несколько минут); до тех пор мод выключен целиком.", + [UI_MENU_MODS_CHEATS] = "Читов: %d. Они включаются на вкладке «Читы».", + [UI_MENU_MODS_CHEATS_FILE] = "Читы для этой версии игры читаются при запуске из cheats\\%s в этом моде.", + [UI_MENU_MODS_BAD_NAME] = "В имени папки есть символ |, которым список модов разделяет имена. Переименуйте папку.", + [UI_MENU_MODS_OTHER_VERSION] = "Файлов для другой версии игры: %u, они пропущены. Поищите версию этого мода для " + "вашей версии игры.", + [UI_MENU_MODS_UNUSED] = "не используется", + [UI_MENU_MODS_FPS60] = "Патч 60 FPS", + [UI_MENU_MODS_FPS60_DESC] = "Собственный патч порта для 60 кадров в секунду. Настраивается «Ограничением частоты " + "кадров» на вкладке «Игра».", + [UI_MENU_MODS_RES] = "Патч разрешения", + [UI_MENU_MODS_RES_DESC] = "Собственный патч порта для разрешений выше 1080p. Настраивается «Разрешением игры» на " + "вкладке «Экран».", + [UI_MENU_MODS_QUICKMENU] = "Исправление быстрого меню", + [UI_MENU_MODS_QUICKMENU_DESC] = "Исправление порта: чёрный фон меню оружия, щитов и материалов при 60 FPS.", + [UI_MENU_VOLUME] = "Громкость", [UI_MENU_VOLUME_DESC] = "Общая громкость игры.", + [UI_MENU_AUDIO_CHANNELS] = "Динамики", + [UI_MENU_AUDIO_CHANNELS_DESC] = "Стерео или объёмный звук 5.1. Автоматически выбирается 5.1, если в Windows " + "настроено шесть каналов или больше. Применяется после перезапуска.", + [UI_MENU_CHANNELS_AUTO] = "Автоматически", [UI_MENU_CHANNELS_STEREO] = "Стерео", [UI_MENU_CHANNELS_51] = "Объёмный 5.1", [UI_MENU_MOTION] = "Управление движением", [UI_MENU_MOTION_DESC] = "Прицеливание гироскопом контроллера.", [UI_MENU_RUMBLE_STRENGTH] = "Сила вибрации", @@ -397,20 +691,47 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_PAD_LAYOUT] = "Раскладка кнопок", [UI_MENU_PAD_LAYOUT_DESC] = "Какая кнопка работает как A. Nintendo: правая, как на Switch. Xbox: нижняя, как " "подписано на контроллере Xbox (крест на контроллере PlayStation). Автоматически: " - "Nintendo для контроллеров с подписями Nintendo, Xbox для остальных. Подсказки Xbox: " - "A справа, как в Nintendo, а игра показывает надписи Xbox (B вместо A, A вместо B, Y " - "вместо X, X вместо Y); подсказки меняются после перезапуска. Буквы записаны в файлах " - "игры, поэтому у кнопки, переназначенной на вкладке управления, на экране остаётся " - "прежняя буква.", + "Nintendo для контроллеров с подписями Nintendo, Xbox для остальных. Кнопки какого " + "контроллера показывают подсказки игры, задаётся в строке «Подсказки кнопок».", [UI_MENU_PAD_LAYOUT_AUTO] = "Автоматически", [UI_MENU_PAD_LAYOUT_NINTENDO] = "Nintendo (A справа)", - [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox (A снизу)", [UI_MENU_PAD_LAYOUT_XBOX_PROMPTS] = "Подсказки Xbox (A справа)", + [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox (A снизу)", [UI_MENU_PAD_LAYOUT_IN_USE] = "сейчас: %s", - [UI_MENU_PAD_LAYOUT_PROMPTS_MISSING] = "нет файлов подсказок: запустите установку снова", - [UI_MENU_PAD_LAYOUT_PROMPTS_RESTART] = "подсказки сменятся после перезапуска", - [UI_MENU_STICK_DEADZONE] = "Мёртвая зона стиков", - [UI_MENU_STICK_DEADZONE_DESC] = "Насколько нужно отклонить стик геймпада, чтобы игра это заметила. Увеличьте, если персонаж или камера двигаются сами.", + [UI_MENU_PROMPTS] = "Подсказки кнопок", + [UI_MENU_PROMPTS_DESC] = "Кнопки какого контроллера показывают подсказки игры. Nintendo: как в самой игре. Xbox: " + "LB, RB, LT, RT, Menu, View и крестовина, а A, B, X и Y подписаны так же, как кнопка, " + "которую нажимает раскладка выше (в Nintendo (A справа) правая кнопка подписана B, и " + "подсказки A показывают B). Игра читает подсказки при запуске, так что они меняются после " + "перезапуска. Подсказки записаны в файлах игры, поэтому у кнопки, переназначенной на " + "вкладке управления, на экране остаётся прежняя подсказка.", + [UI_MENU_PROMPTS_NINTENDO] = "Nintendo", [UI_MENU_PROMPTS_XBOX] = "Xbox", + [UI_MENU_PROMPTS_MISSING] = "нет файлов подсказок: запустите установку снова", + [UI_MENU_PROMPTS_FAILED] = "файлы подсказок повреждены: запустите установку снова", + [UI_MENU_PROMPTS_RESTART] = "подсказки сменятся после перезапуска", + [UI_MENU_PROMPTS_OFF_SHAPE] = "выкл. при разрешении игры не 16:9", + [UI_MENU_PROMPTS_OFF_MODS] = "выкл.: мод RomFS заменяет их файлы", + [UI_MENU_PROMPTS_OFF_PATCHES] = "выкл., пока задан TOTK_ROMFS_PATCHES", + [UI_MENU_RESTART] = "Перезапустить сейчас", + [UI_MENU_RESTART_DESC] = "Закрывает игру и запускает её снова с новыми подсказками. Прогресс после последнего " + "сохранения будет потерян.", + [UI_MENU_RESTART_CONFIRM] = "Перезапустить игру сейчас? Прогресс после последнего сохранения будет потерян.", + [UI_MENU_RESTART_YES] = "Перезапустить", + [UI_MENU_RESTART_SAVING] = "Игра сохраняется: перезапуск будет доступен через мгновение.", + [UI_MENU_GROUP_STICKS] = "Стики", + [UI_MENU_LSTICK_DEADZONE] = "Мёртвая зона левого стика", [UI_MENU_RSTICK_DEADZONE] = "Мёртвая зона правого стика", + [UI_MENU_STICK_DEADZONE_DESC] = "Насколько нужно отклонить стик геймпада, чтобы игра это заметила. Увеличьте, если персонаж или камера двигаются сами: отпустите стик, посмотрите «стик сейчас» и поставьте мёртвую зону на несколько процентов выше.", + [UI_MENU_STICK_DEADZONE_NOW] = "стик сейчас: %.0f %%", [UI_MENU_MOUSE_SENS] = "Чувствительность мыши", [UI_MENU_MOUSE_SENS_DESC] = "Как быстро мышь поворачивает камеру.", + [UI_MENU_WALK_KEY] = "Клавиша шага", + [UI_MENU_WALK_KEY_DESC] = "Удерживайте: стики клавиатуры отклоняются частично, и персонаж идёт, а не бежит. " + "Насколько — задаёт сила шага. Контроллер и мышь не затрагиваются.", + [UI_MENU_WALK_KEY_TAKEN] = "также %s", + [UI_MENU_WALK_SCALE] = "Сила шага", + [UI_MENU_WALK_SCALE_DESC] = "Отклонение стиков клавиатуры при удержании клавиши шага. Половина — шаг, полное " + "отклонение — бег.", + [UI_MENU_STICK_SENS] = "Скорость камеры (стик)", + [UI_MENU_STICK_SENS_DESC] = "Умножает скорость поворота камеры правым стиком, включая наибольшую, при каждой из пяти " + "чувствительностей камеры в игре. На мышь не влияет.", [UI_BINDS_HELP] = "Выберите ячейку и нажмите A или Enter, затем клавишу, кнопку мыши или кнопку контроллера. " "Delete или X очищает ячейку. Занятая клавиша переходит сюда; кнопка контроллера меняется " @@ -462,8 +783,41 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_PAD_LPADDLE2] = "Левая задняя кнопка 2", [UI_PAD_RPADDLE2] = "Правая задняя кнопка 2", [UI_PAD_CROSS] = "Крест", [UI_PAD_CIRCLE] = "Круг", [UI_PAD_SQUARE] = "Квадрат", [UI_PAD_TRIANGLE] = "Треугольник", + [UI_MENU_AMIIBO_DIR] = "Папка amiibo", + [UI_MENU_AMIIBO_OPEN] = "Открыть", + [UI_MENU_AMIIBO_DEFAULT_DIR] = "Папка по умолчанию", + [UI_MENU_AMIIBO_REFRESH] = "Обновить", + [UI_MENU_AMIIBO_PICK] = "Дампы amiibo (файлы .bin или .nfc по 540 байт):", + [UI_MENU_AMIIBO_NO_DIR] = "Не удаётся открыть папку %s", + [UI_MENU_AMIIBO_READER] = "NFC-считыватель", + [UI_MENU_AMIIBO_STATE] = "Считыватель:", + [UI_MENU_AMIIBO_STATE_IDLE] = "готов", + [UI_MENU_AMIIBO_STATE_SEARCHING] = "игра ждёт amiibo", + [UI_MENU_AMIIBO_STATE_FOUND] = "amiibo на считывателе", + [UI_MENU_AMIIBO_STATE_MOUNTED] = "игра читает amiibo", + [UI_MENU_AMIIBO_STATE_OFF] = "выключен", + [UI_MENU_AMIIBO_FILE] = "Выбранный файл:", + [UI_MENU_AMIIBO_NO_FILE] = "(файл не выбран)", + [UI_MENU_AMIIBO_TAP] = "Приложить amiibo", + [UI_MENU_AMIIBO_REMOVE] = "Убрать amiibo", + [UI_MENU_AMIIBO_AUTO_REMOVE] = "Убирать amiibo само после касания", + [UI_MENU_AMIIBO_HOLD] = "Время на считывателе", + [UI_MENU_AMIIBO_SECONDS] = "%.1f с", + [UI_MENU_AMIIBO_TAG] = "Amiibo на считывателе:", + [UI_MENU_AMIIBO_NO_TAG] = "На считывателе нет amiibo.", + [UI_MENU_AMIIBO_TAG_FILE] = "Файл: %s", + [UI_MENU_AMIIBO_TAG_ID] = "Персонаж %s, серия %02x, номер %04x, тип %02x", + [UI_MENU_AMIIBO_TAG_UID] = "UID: %s", + [UI_MENU_AMIIBO_READY] = "Готово", + [UI_MENU_AMIIBO_TAPPED] = "Приложен: %s", + [UI_MENU_AMIIBO_LOAD_FAILED] = "Не дамп amiibo (NTAG215, 540 байт): %s", + [UI_MENU_AMIIBO_REMOVED] = "Amiibo убран", + [UI_MENU_AMIIBO_STATUS] = "Состояние: %s", [UI_MENU_MENU_LANG] = "Язык меню", [UI_MENU_MENU_LANG_DESC] = "Язык этого меню и индикатора.", [UI_MENU_MENU_LANG_AUTO] = "Автоматически (из Windows)", + [UI_MENU_DISCORD_RPC] = "Discord Rich Presence", + [UI_MENU_DISCORD_RPC_DESC] = "Пока игра запущена, показывает «Играю в TotK Recomp» со временем игры в статусе " + "Discord. Приложение выбирают discord_app_id и discord_image в totk.ini.", [UI_MENU_ADV_WARNING] = "Эти настройки нужны для тестов и поиска неполадок. Меняйте их, только если знаете зачем.", [UI_MENU_GROUP_SCHED] = "Планирование", [UI_MENU_GROUP_SHADERS] = "Шейдеры и конвейеры", @@ -501,7 +855,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_MEMORY_VERIFY] = "Проверка", [UI_MENU_TEX_ALIAS] = "Наследование содержимого текстур", [UI_MENU_TEX_ALIAS_DESC] = "Новая текстура поверх памяти живой текстуры получает её содержимое, а не пустую.", [UI_MENU_TEX_VRAM_PRESSURE] = "Предел памяти текстур", [UI_MENU_TEX_VRAM_PRESSURE_DESC] = "Процент видеопамяти, выше которого освобождённые игрой текстуры удаляются (при повторном использовании загружаются снова). 0 — не удалять.", - [UI_MENU_ASTC_BC] = "Сжатие ASTC в BC3", [UI_MENU_ASTC_BC_DESC] = "С декодером на GPU: декодированные текстуры ASTC пересжимаются в BC3 и занимают вчетверо меньше видеопамяти. Для видеокарт с 4 ГБ и меньше; текстуры чуть более блочные.", + [UI_MENU_ASTC_BC] = "Сжатие ASTC в BC3", [UI_MENU_ASTC_BC_DESC] = "С декодером на GPU: декодированные текстуры ASTC пересжимаются в BC3 и занимают вчетверо меньше видеопамяти. Текстуры чуть более блочные. Авто: включено на видеокартах с 4 ГБ видеопамяти и меньше.", [UI_MENU_LOD_GUARD] = "Защита LOD", [UI_MENU_LOD_GUARD_DESC] = "Не даёт объектам мелькать не на своём месте, а дальнему плану — на кадр становиться плоским: ждёт, пока игра допишет данные уровней детализации, и только потом их читает GPU.", [UI_MENU_FIX_C0] = "Исправление константного буфера", [UI_MENU_FIX_C0_DESC] = "Заполняет зарезервированный драйвером банк констант c0 (размеры хранилищ, размер запуска, размер точки, базовый экземпляр) вместо чтения нулей.", @@ -534,6 +888,8 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_FLIGHT_DESC] = "Хранит короткую историю и сохраняет её, когда кадр подвисает.", [UI_MENU_FLIGHT_SPIKE_MS] = "Порог подвисания (мс)", [UI_MENU_FLIGHT_SPIKE_MS_DESC] = "Время кадра, которое считается подвисанием.", + [UI_MENU_PASS_CRUMBS] = "Метки проходов GPU", + [UI_MENU_PASS_CRUMBS_DESC] = "После сбоя GPU журнал называет проход отрисовки, который выполнялся. Автоматически: только AMD. Вкл.: также NVIDIA, с небольшой нагрузкой.", [UI_MENU_INPUT_RECORD] = "Запись ввода", [UI_MENU_INPUT_RECORD_DESC] = "Пишет ваш ввод в файл для отчёта об ошибке.", [UI_MENU_OPEN_LOG] = "Открыть папку журналов", [UI_MENU_OPEN_LOG_DESC] = "Показывает папку с файлами журнала.", @@ -546,6 +902,8 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_OV_FPS] = "%.0f FPS", [UI_MENU_OV_FRAME_MS] = "%.1f мс", [UI_MENU_OV_GAME_PCT] = "игра %.0f%%", [UI_MENU_OV_MAX_MS] = "макс. %.1f мс", + [UI_MENU_OV_FPS_LABEL] = "FPS", [UI_MENU_OV_FRAME_LABEL] = "Кадр", [UI_MENU_OV_FMT_FPS] = "%.0f", + [UI_MENU_OV_LOW1] = "1%% худших %.0f", [UI_MENU_OV_LOW01] = "0.1%% худших %.0f", [UI_MENU_OV_GPU] = "GPU", [UI_MENU_OV_CPU] = "ЦП", [UI_MENU_OV_RAM] = "ОЗУ", [UI_MENU_OV_VRAM] = "Видеопамять", [UI_MENU_OV_TEMP] = "Температура", [UI_MENU_OV_POWER] = "Мощность", [UI_MENU_OV_CLOCK] = "Частота", [UI_MENU_OV_NONE] = "-", @@ -560,6 +918,1087 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_OV_FMT_SIZE] = "%ux%u", [UI_MENU_OV_FMT_SCENE] = "%ux%u, %.0f%%", [UI_MENU_OV_FMT_FSR] = "AMD FSR 1, резкость %d%%", [UI_MENU_OV_SCALE_NONE] = "нет (1:1)", [UI_MENU_OV_SCALE_BILINEAR] = "билинейное", + [UI_MENU_OV_SCALE_DOWN] = "билинейное (FSR только увеличивает)", + [UI_MENU_TAB_CHEATS] = "Читы", + [UI_MENU_CHEATS_NONE] = "Для этой версии игры читов нет. Читы мода — это файл читов Atmosphere в его папке cheats " + "с именем по сборке игры: mods\\<мод>\\cheats\\%s. Перезапустите игру, чтобы они " + "прочитались. Для читов, меняющих код игры, игра пересоберётся сама при запуске (несколько " + "минут).", + [UI_MENU_CHEATS_MOD] = "%s — читов: %d", + [UI_MENU_CHEAT_CODE_DESC] = "Меняет код игры. Включается и выключается сразу.", + [UI_MENU_CHEAT_KEY_DESC] = "Меняет код игры, пока вы держите %s. Включается и выключается сразу.", + [UI_MENU_CHEAT_DATA_DESC] = "Пока включён, записывает память игры каждый кадр.", + [UI_MENU_CHEAT_MASTER] = "всегда вкл.", + [UI_MENU_CHEAT_MASTER_DESC] = "Главный код файла: включён всегда.", + [UI_MENU_CHEAT_RESTORE] = "не нужен", + [UI_MENU_CHEAT_RESTORE_DESC] = "Он только возвращает исходный код игры. Выключение чита уже делает это.", + [UI_MENU_CHEAT_REBUILD] = "после пересборки", + [UI_MENU_CHEAT_REBUILD_DESC] = "Этот чит меняет код игры и появился или изменился после сборки игры. Игра соберёт " + "его при следующем запуске (несколько минут), или нажмите «Пересобрать игру сейчас».", + [UI_MENU_CHEAT_UNSUPPORTED] = "не поддерживается", + [UI_MENU_CHEAT_UNSUPPORTED_DESC] = "Этот чит нельзя использовать с этой версией игры:", + [UI_MENU_CHEATS_REBUILD] = "Пересобрать игру сейчас", + [UI_MENU_CHEATS_REBUILD_DESC] = "Некоторые читы меняют код игры и ещё не собраны в неё: они новые или изменены, " + "либо прошлая пересборка не удалась или была отменена. Закрывает игру; установщик " + "собирает их (несколько минут) и снова запускает игру. Прогресс после последнего " + "сохранения будет потерян.", + [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "Этот мод меняет код игры (exefs) так, как порт применить не может, поэтому мод " + "выключен целиком. %s", + [UI_MENU_MODS_EXEFS_OTHER] = "Его патчи кода (exefs) сделаны для другой версии игры.", + [UI_MENU_MODS_EXEFS_ON] = "Патчи кода в работе: файлов: %d.", + [UI_MENU_MODS_REPLACES_BUILTIN] = "Патчи кода в работе: файлов: %d. Они заменяют собственный патч порта %s, " + "который выключен, пока этот мод включён.", +}; + +static const char *const text_pt_br[UI_TEXT_COUNT] = { + SWKBD("Inserir texto", "Insira um nome", "Cavalo", "OK", "Cancelar", "Controle: A = OK, B = Cancelar"), + + [UI_MENU_TITLE] = "Configurações", + [UI_MENU_TAB_SCREEN] = "Tela", [UI_MENU_TAB_GAME] = "Jogo", [UI_MENU_TAB_SOUND] = "Som", + [UI_MENU_TAB_CONTROLS] = "Controles", [UI_MENU_TAB_AMIIBO] = "Amiibo", [UI_MENU_TAB_MENU] = "Menu", [UI_MENU_TAB_ADVANCED] = "Avançado", + [UI_MENU_TAB_MODS] = "Mods", + [UI_MENU_MODS_BUILTIN] = "Embutidos no port", [UI_MENU_MODS_INSTALLED] = "Mods instalados", + [UI_MENU_MODS_NONE] = "Nenhum mod ainda. Descompacte um mod na pasta mods, uma pasta por mod com a pasta romfs dentro, " + "e reinicie o jogo. O TKMM também pode exportar os mods mesclados para uma pasta ali.", + [UI_MENU_MODS_OPEN] = "Abrir pasta de mods", + [UI_MENU_MODS_OPEN_DESC] = "Mostra a pasta de onde o jogo carrega os mods. Ela é criada se não existir.", + [UI_MENU_MODS_ROW_DESC] = "Arquivos do jogo que substitui: %u. Arquivos novos que adiciona: %u.", + [UI_MENU_MODS_OVERRIDDEN] = "%u dos seus arquivos vêm de %s, que está acima na lista.", + [UI_MENU_MODS_MOVE_HINT] = "O botão de cima ou Ctrl+Cima sobe o mod, o botão da esquerda ou Ctrl+Baixo desce. " + "Quando dois mods mudam o mesmo arquivo, vale o de cima. As mudanças valem após reiniciar.", + [UI_MENU_MODS_EMPTY] = "Esta pasta não tem arquivos romfs, exefs ou cheats.", + [UI_MENU_MODS_SUBSDK] = "Este mod traz código próprio do jogo (um plugin subsdk ou um main substituído). O port não " + "carrega módulos de código, então o mod inteiro fica desligado.", + [UI_MENU_MODS_EXEFS_LATER] = "Este mod altera o código do jogo (exefs). O jogo compila esses patches na próxima " + "vez que iniciar (uma recompilação de alguns minutos); até lá o mod inteiro fica " + "desligado.", + [UI_MENU_MODS_CHEATS] = "Cheats: %d. Ligue-os na aba Cheats.", + [UI_MENU_MODS_CHEATS_FILE] = "Os cheats desta versão do jogo são lidos ao iniciar de cheats\\%s neste mod.", + [UI_MENU_MODS_BAD_NAME] = "O nome da pasta contém |, que a lista de mods usa entre os nomes. Renomeie a pasta.", + [UI_MENU_MODS_OTHER_VERSION] = "%u dos seus arquivos são de outra versão do jogo e ficam de fora. Procure uma versão " + "deste mod feita para a sua versão do jogo.", + [UI_MENU_MODS_UNUSED] = "não usado", + [UI_MENU_MODS_FPS60] = "Patch de 60 FPS", + [UI_MENU_MODS_FPS60_DESC] = "O patch do próprio port para 60 quadros por segundo. Ajuste em Limite de quadros na aba " + "Jogo.", + [UI_MENU_MODS_RES] = "Patch de resolução", + [UI_MENU_MODS_RES_DESC] = "O patch do próprio port para resoluções acima de 1080p. Ajuste em Resolução do jogo na aba " + "Tela.", + [UI_MENU_MODS_QUICKMENU] = "Correção do menu rápido", + [UI_MENU_MODS_QUICKMENU_DESC] = "A correção do port para o fundo preto dos menus de armas, escudos e materiais a 60 " + "FPS.", + [UI_MENU_CONTINUE] = "Continuar", [UI_MENU_QUIT] = "Sair do jogo", + [UI_MENU_ON] = "Ligado", [UI_MENU_OFF] = "Desligado", [UI_MENU_AUTO] = "Automático", + [UI_MENU_DEFAULT_MARK] = "padrão", + [UI_MENU_AFTER_RESTART] = "após reiniciar", [UI_MENU_SET_BY_ENV] = "definido por variável de ambiente", + [UI_MENU_SAVE_FAILED] = "não foi possível salvar as configurações", + [UI_MENU_VERSION] = "TotK Recomp %s, jogo %s", + + [UI_MENU_FULLSCREEN] = "Tela cheia", [UI_MENU_FULLSCREEN_DESC] = "O jogo ocupa a tela inteira; F11 alterna.", + [UI_MENU_WINDOW_SIZE] = "Tamanho da janela", + [UI_MENU_WINDOW_SIZE_DESC] = "Tamanho da janela do jogo fora da tela cheia.", + [UI_MENU_WINDOW_SIZE_CUSTOM] = "Personalizado", + [UI_MENU_PRESENT] = "Apresentação", [UI_MENU_PRESENT_DESC] = "Como os quadros prontos chegam à tela.", + [UI_MENU_PRESENT_MAILBOX] = "Mailbox", [UI_MENU_PRESENT_VSYNC] = "VSync", [UI_MENU_PRESENT_IMMEDIATE] = "Imediato", + [UI_MENU_PRESENT_IN_USE] = "em uso: %s", + [UI_MENU_GPU] = "Placa de vídeo", [UI_MENU_GPU_DESC] = "O dispositivo Vulkan que desenha o jogo.", + [UI_MENU_GPU_AUTO] = "Automática", + [UI_MENU_PERF_OVERLAY] = "Indicador de desempenho", + [UI_MENU_PERF_OVERLAY_DESC] = "Taxa de quadros no canto superior direito. Completo adiciona os tempos de quadro com " + "gráfico, o uso de CPU, GPU e memória, as temperaturas e as esperas da thread de " + "renderização. F10 alterna os modos.", + [UI_MENU_PERF_OFF] = "Desligado", [UI_MENU_PERF_FPS] = "FPS", [UI_MENU_PERF_FULL] = "Completo", + [UI_PERF_LINE_FPS] = "%.0f fps", + [UI_MENU_GRAPHICS] = "Imagem", + [UI_MENU_OUTPUT_RES] = "Resolução do jogo", + [UI_MENU_OUTPUT_RES_DESC] = "O tamanho em que o jogo desenha a imagem e a interface. Acima de 1920 x 1080 o jogo " + "precisa de mais memória de vídeo e tempo de GPU. Um tamanho mais largo (21:9) amplia a " + "visão da câmera e leva o HUD para os cantos da tela; um mais alto (16:10, 3:2) " + "leva-o para as bordas de cima e de baixo.", + [UI_MENU_RENDER_SCALE] = "Escala da renderização 3D", + [UI_MENU_RENDER_SCALE_DESC] = "Tamanho da cena 3D em relação à resolução do jogo; o FSR do jogo a amplia até esse " + "tamanho. Acima de 100 % suaviza bordas serrilhadas (supersampling) com alto custo de " + "GPU.", + [UI_MENU_RENDER_SCALE_GAME] = "Jogo (83 %)", + [UI_MENU_UPSCALER] = "Escala para a janela", + [UI_MENU_UPSCALER_DESC] = "Como a imagem é ampliada quando a janela é maior que a resolução do jogo. O FSR mantém " + "bordas e detalhes mais nítidos que o bilinear.", + [UI_MENU_UPSCALER_FSR] = "AMD FSR 1", [UI_MENU_UPSCALER_BILINEAR] = "Bilinear", + [UI_MENU_ASPECT] = "Formato da imagem", + [UI_MENU_ASPECT_DESC] = "Quando o formato da janela difere do da resolução do jogo (uma imagem 16:9 numa tela " + "21:9): manter o formato da imagem com barras pretas ou esticá-la sobre a janela.", + [UI_MENU_ASPECT_FIT] = "Barras pretas", [UI_MENU_ASPECT_STRETCH] = "Esticar", + [UI_MENU_SHARPNESS] = "Nitidez do FSR", + [UI_MENU_SHARPNESS_DESC] = "Aumento de nitidez após a ampliação do FSR; 0 desliga.", + + [UI_MENU_FPS_LIMIT] = "Limite de quadros", + [UI_MENU_FPS_LIMIT_DESC] = "O máximo de quadros por segundo que o jogo desenha. Acima de 60 é experimental e " + "precisa de Velocidade do jogo pelo relógio.", + [UI_MENU_FPS_30] = "30 FPS", [UI_MENU_FPS_40] = "40 FPS", [UI_MENU_FPS_45] = "45 FPS", + [UI_MENU_FPS_60] = "60 FPS", [UI_MENU_FPS_90] = "90 FPS", [UI_MENU_FPS_120] = "120 FPS", [UI_MENU_FPS_144] = "144 FPS", + [UI_MENU_ASSUME_FOCUS] = "Continuar rodando fora de foco", + [UI_MENU_ASSUME_FOCUS_DESC] = "Ligado (o padrão), o jogo continua rodando enquanto você está em outra janela, e o " + "teclado fica sem efeito até a janela voltar para a frente. Desligado, ele pausa quando " + "a janela perde o foco, como no Switch ao apertar Home; um carregamento interrompido " + "assim pode parar de vez.", + [UI_MENU_FPS_DISPLAY] = "Taxa do monitor", [UI_MENU_FPS_DISPLAY_HZ] = "Taxa do monitor (%.0f Hz)", + [UI_MENU_REALTIME] = "Velocidade do jogo pelo relógio", + [UI_MENU_REALTIME_DESC] = "Avança o jogo pelo tempo real, para que um quadro lento não deixe o jogo mais lento.", + [UI_MENU_CLOTH_REALTIME] = "Tecidos pelo relógio", + [UI_MENU_CLOTH_REALTIME_DESC] = "Avança tecidos e cabelos pelo tempo real também.", + [UI_MENU_HIGHFPS_FIX] = "Correções para alta taxa de quadros", + [UI_MENU_HIGHFPS_FIX_DESC] = "Corrige a física que se comporta mal acima de 30 quadros por segundo e objetos que ficam transparentes ou invisíveis acima de 60.", + [UI_MENU_GAME_LANG] = "Idioma do jogo", [UI_MENU_GAME_LANG_DESC] = "Idioma dos textos e das vozes do jogo.", + [UI_MENU_GAME_LANG_AUTO] = "Automático", + [UI_MENU_OPERATION_MODE] = "Modo do console", + [UI_MENU_OPERATION_MODE_DESC] = "Se o jogo acha que está no modo TV ou no modo portátil.", + [UI_MENU_MODE_DOCKED] = "Modo TV", [UI_MENU_MODE_HANDHELD] = "Modo portátil", + + [UI_MENU_VOLUME] = "Volume", [UI_MENU_VOLUME_DESC] = "Volume geral do jogo.", + + [UI_MENU_MOTION] = "Controles de movimento", [UI_MENU_MOTION_DESC] = "Mirar com o giroscópio do controle.", + [UI_MENU_RUMBLE_STRENGTH] = "Intensidade da vibração", + [UI_MENU_RUMBLE_STRENGTH_DESC] = "Quão forte o controle vibra.", + [UI_MENU_RUMBLE_CURVE] = "Curva da vibração", + [UI_MENU_RUMBLE_CURVE_DESC] = "Valores menores deixam a vibração fraca mais forte.", + [UI_MENU_RUMBLE_TRIGGERS] = "Vibração dos gatilhos (Xbox)", + [UI_MENU_RUMBLE_TRIGGERS_DESC] = "Também vibra os gatilhos de um controle de Xbox.", + [UI_MENU_PAD_LAYOUT] = "Layout dos botões", + [UI_MENU_PAD_LAYOUT_DESC] = "Qual botão é o A. Nintendo: o da direita, como no Switch. Xbox: o de baixo, como " + "indicado no controle de Xbox (cruz no controle de PlayStation). Automático escolhe " + "Nintendo para controles com os rótulos da Nintendo e Xbox para os demais. Ícones de " + "Xbox: o A é o da direita, como no Nintendo, e o jogo mostra os rótulos do Xbox (B no " + "lugar de A, A no lugar de B, Y no lugar de X, X no lugar de Y); os ícones mudam após " + "reiniciar. As letras fazem parte dos arquivos do jogo, então um botão remapeado em " + "Controles continua com a letra antiga na tela.", + [UI_MENU_PAD_LAYOUT_AUTO] = "Automático", [UI_MENU_PAD_LAYOUT_NINTENDO] = "Nintendo (A à direita)", + [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox (A embaixo)", + [UI_MENU_PAD_LAYOUT_IN_USE] = "em uso: %s", + [UI_MENU_GROUP_STICKS] = "Analógicos", + [UI_MENU_LSTICK_DEADZONE] = "Zona morta do analógico esquerdo", + [UI_MENU_RSTICK_DEADZONE] = "Zona morta do analógico direito", + [UI_MENU_STICK_DEADZONE_DESC] = "Quanto um analógico do controle precisa se mover antes que o jogo perceba. Aumente " + "se o personagem ou a câmera se moverem sozinhos: solte o analógico, leia \"analógico " + "agora\" e coloque a zona morta alguns por cento acima.", + [UI_MENU_STICK_DEADZONE_NOW] = "analógico agora: %.0f %%", + [UI_MENU_MOUSE_SENS] = "Sensibilidade da câmera no mouse", + [UI_MENU_MOUSE_SENS_DESC] = "Quão rápido o mouse gira a câmera.", + + [UI_BINDS_HELP] = "Selecione uma célula e aperte A ou Enter, depois a tecla, o botão do mouse ou o botão do " + "controle. Delete ou X limpa a célula. Uma tecla já em uso vem para cá; um botão do controle " + "troca de lugar com o que ele substitui.", + [UI_BINDS_COL_KEY1] = "Tecla 1", [UI_BINDS_COL_KEY2] = "Tecla 2", [UI_BINDS_COL_PAD] = "Controle", + [UI_BINDS_GROUP_BUTTONS] = "Botões", [UI_BINDS_GROUP_MOVE] = "Movimento", [UI_BINDS_GROUP_CAMERA] = "Câmera", + [UI_BINDS_PRESS_KEY] = "Aperte uma tecla ou um botão do mouse para %s", + [UI_BINDS_PRESS_PAD] = "Aperte um botão do controle para %s", + [UI_BINDS_CANCEL_HINT] = "Esc cancela, ou aguarde %d s", + [UI_BINDS_TAKEN] = "%s saiu de %s", [UI_BINDS_SWAPPED] = "%s trocou de lugar com %s", + [UI_BINDS_RESET] = "Restaurar todos os botões", + [UI_BINDS_RESET_DESC] = "Volta às teclas e aos botões do controle padrão. O layout dos botões acima volta a valer: " + "os botões do controle remapeados ficam onde você os colocou até esta restauração.", + [UI_BIND_A] = "A", [UI_BIND_B] = "B", [UI_BIND_X] = "X", [UI_BIND_Y] = "Y", + [UI_BIND_LCLICK] = "Clique do analógico esquerdo", [UI_BIND_RCLICK] = "Clique do analógico direito", + [UI_BIND_L] = "L", [UI_BIND_R] = "R", [UI_BIND_ZL] = "ZL", [UI_BIND_ZR] = "ZR", [UI_BIND_PLUS] = "Mais", + [UI_BIND_MINUS] = "Menos", + [UI_BIND_LEFT] = "Direcional para a esquerda", [UI_BIND_UP] = "Direcional para cima", + [UI_BIND_RIGHT] = "Direcional para a direita", [UI_BIND_DOWN] = "Direcional para baixo", + [UI_BIND_LSTICK_LEFT] = "Mover para a esquerda", [UI_BIND_LSTICK_UP] = "Mover para a frente", + [UI_BIND_LSTICK_RIGHT] = "Mover para a direita", [UI_BIND_LSTICK_DOWN] = "Mover para trás", + [UI_BIND_RSTICK_LEFT] = "Câmera para a esquerda", [UI_BIND_RSTICK_UP] = "Câmera para cima", + [UI_BIND_RSTICK_RIGHT] = "Câmera para a direita", [UI_BIND_RSTICK_DOWN] = "Câmera para baixo", + [UI_BIND_DESC_A] = "Interagir, pegar, conversar.", [UI_BIND_DESC_B] = "Correr, cancelar, voltar.", + [UI_BIND_DESC_X] = "Pular.", [UI_BIND_DESC_Y] = "Atacar.", [UI_BIND_DESC_LCLICK] = "Agachar.", + [UI_BIND_DESC_RCLICK] = "Visão em primeira pessoa.", [UI_BIND_DESC_L] = "Habilidades.", + [UI_BIND_DESC_R] = "Arremessar a arma.", [UI_BIND_DESC_ZL] = "Escudo, travar a mira.", [UI_BIND_DESC_ZR] = "Arco.", + [UI_BIND_DESC_PLUS] = "Menu, inventário.", [UI_BIND_DESC_MINUS] = "Mapa.", + [UI_BIND_DESC_DPAD] = "Seleção rápida.", + [UI_BIND_DESC_MOVE] = "Move o Link como o analógico esquerdo. O analógico esquerdo do controle também sempre " + "funciona.", + [UI_BIND_DESC_CAMERA] = "Gira a câmera como o analógico direito. O mouse e o analógico direito do controle também " + "sempre funcionam.", + [UI_KEY_SPACE] = "Espaço", [UI_KEY_TAB] = "Tab", [UI_KEY_LSHIFT] = "Shift esquerdo", + [UI_KEY_LCTRL] = "Ctrl esquerdo", + [UI_KEY_UP] = "Seta para cima", [UI_KEY_DOWN] = "Seta para baixo", [UI_KEY_LEFT] = "Seta para a esquerda", + [UI_KEY_RIGHT] = "Seta para a direita", + [UI_KEY_MOUSE_L] = "Mouse esquerdo", [UI_KEY_MOUSE_R] = "Mouse direito", [UI_KEY_MOUSE_M] = "Mouse do meio", + [UI_KEY_MOUSE_X1] = "Mouse 4", [UI_KEY_MOUSE_X2] = "Mouse 5", + [UI_KEY_ENTER] = "Enter", [UI_KEY_BACKSPACE] = "Backspace", [UI_KEY_RSHIFT] = "Shift direito", + [UI_KEY_RCTRL] = "Ctrl direito", [UI_KEY_LALT] = "Alt", [UI_KEY_CAPSLOCK] = "Caps Lock", [UI_KEY_INSERT] = "Insert", + [UI_KEY_DELETE] = "Delete", [UI_KEY_HOME] = "Home", [UI_KEY_END] = "End", [UI_KEY_PAGEUP] = "Page Up", + [UI_KEY_PAGEDOWN] = "Page Down", [UI_KEY_NUM_FMT] = "Num %s", + [UI_PAD_SOUTH] = "Botão de baixo", [UI_PAD_EAST] = "Botão da direita", [UI_PAD_WEST] = "Botão da esquerda", + [UI_PAD_NORTH] = "Botão de cima", [UI_PAD_LSTICK] = "Analógico esquerdo", [UI_PAD_RSTICK] = "Analógico direito", + [UI_PAD_L] = "Bumper esquerdo", [UI_PAD_R] = "Bumper direito", [UI_PAD_START] = "Start", [UI_PAD_BACK] = "Back", + [UI_PAD_DPAD_LEFT] = "Direcional para a esquerda", [UI_PAD_DPAD_UP] = "Direcional para cima", + [UI_PAD_DPAD_RIGHT] = "Direcional para a direita", [UI_PAD_DPAD_DOWN] = "Direcional para baixo", + [UI_PAD_ZL] = "Gatilho esquerdo", [UI_PAD_ZR] = "Gatilho direito", + [UI_PAD_LPADDLE1] = "Botão traseiro esquerdo", [UI_PAD_RPADDLE1] = "Botão traseiro direito", + [UI_PAD_LPADDLE2] = "Botão traseiro esquerdo 2", [UI_PAD_RPADDLE2] = "Botão traseiro direito 2", + [UI_PAD_CROSS] = "Cruz", [UI_PAD_CIRCLE] = "Círculo", [UI_PAD_SQUARE] = "Quadrado", [UI_PAD_TRIANGLE] = "Triângulo", + + [UI_MENU_AMIIBO_DIR] = "Pasta de amiibo", + [UI_MENU_AMIIBO_OPEN] = "Abrir", + [UI_MENU_AMIIBO_DEFAULT_DIR] = "Pasta padrão", + [UI_MENU_AMIIBO_REFRESH] = "Atualizar", + [UI_MENU_AMIIBO_PICK] = "Dumps de amiibo (arquivos .bin ou .nfc de 540 bytes):", + [UI_MENU_AMIIBO_NO_DIR] = "Não foi possível abrir a pasta %s", + [UI_MENU_AMIIBO_READER] = "Leitor NFC", + [UI_MENU_AMIIBO_STATE] = "Leitor:", + [UI_MENU_AMIIBO_STATE_IDLE] = "pronto", + [UI_MENU_AMIIBO_STATE_SEARCHING] = "o jogo está esperando um amiibo", + [UI_MENU_AMIIBO_STATE_FOUND] = "amiibo no leitor", + [UI_MENU_AMIIBO_STATE_MOUNTED] = "o jogo está lendo o amiibo", + [UI_MENU_AMIIBO_STATE_OFF] = "desligado", + [UI_MENU_AMIIBO_FILE] = "Arquivo selecionado:", + [UI_MENU_AMIIBO_NO_FILE] = "(nenhum arquivo selecionado)", + [UI_MENU_AMIIBO_TAP] = "Encostar o amiibo", + [UI_MENU_AMIIBO_REMOVE] = "Afastar o amiibo", + [UI_MENU_AMIIBO_AUTO_REMOVE] = "Afastar o amiibo sozinho depois de encostar", + [UI_MENU_AMIIBO_HOLD] = "Tempo no leitor", + [UI_MENU_AMIIBO_SECONDS] = "%.1f s", + [UI_MENU_AMIIBO_TAG] = "Amiibo no leitor:", + [UI_MENU_AMIIBO_NO_TAG] = "Nenhum amiibo no leitor.", + [UI_MENU_AMIIBO_TAG_FILE] = "Arquivo: %s", + [UI_MENU_AMIIBO_TAG_ID] = "Personagem %s, série %02x, número %04x, tipo %02x", + [UI_MENU_AMIIBO_TAG_UID] = "UID: %s", + [UI_MENU_AMIIBO_READY] = "Pronto", + [UI_MENU_AMIIBO_TAPPED] = "Encostado: %s", + [UI_MENU_AMIIBO_LOAD_FAILED] = "Não é um dump de amiibo (NTAG215, 540 bytes): %s", + [UI_MENU_AMIIBO_REMOVED] = "Amiibo afastado", + [UI_MENU_AMIIBO_STATUS] = "Estado: %s", + [UI_MENU_MENU_LANG] = "Idioma do menu", [UI_MENU_MENU_LANG_DESC] = "Idioma deste menu e do indicador.", + [UI_MENU_MENU_LANG_AUTO] = "Automático (do Windows)", + [UI_MENU_DISCORD_RPC] = "Discord Rich Presence", + [UI_MENU_DISCORD_RPC_DESC] = "Mostra \"Jogando TotK Recomp\" com o tempo de jogo no seu status do Discord " + "enquanto o jogo roda. discord_app_id e discord_image no totk.ini escolhem a " + "aplicação.", + + [UI_MENU_ADV_WARNING] = "Estas configurações são para testes e solução de problemas. Altere-as só quando souber " + "por quê.", + [UI_MENU_GROUP_SCHED] = "Agendamento", [UI_MENU_GROUP_SHADERS] = "Shaders e pipelines", + [UI_MENU_GROUP_TEXMEM] = "Texturas e memória", [UI_MENU_GROUP_FIXES] = "Correções de renderização", + [UI_MENU_GROUP_VULKAN] = "Vulkan", [UI_MENU_GROUP_DIAG] = "Diagnóstico", + [UI_MENU_ASYNC_PREEMPT] = "Preempção de núcleo virtual", + [UI_MENU_ASYNC_PREEMPT_DESC] = "Deixa uma thread do jogo interromper outra no mesmo núcleo virtual.", + [UI_MENU_PREEMPT_OFF] = "Desligado", [UI_MENU_PREEMPT_ON] = "Ligado", [UI_MENU_PREEMPT_INHERIT] = "Só herança", + [UI_MENU_REPLAY_ASYNC] = "Preparo de quadro da GPU em thread própria", + [UI_MENU_REPLAY_ASYNC_DESC] = "Prepara os quadros para a GPU numa thread separada em vez da thread do jogo. Adiciona até um quadro de latência.", + [UI_MENU_TIMER_1MS] = "Timer do Windows de 1 ms", + [UI_MENU_TIMER_1MS_DESC] = "Aumenta a resolução do timer do sistema para 1 ms, para um ritmo de quadros mais estável.", + [UI_MENU_PIPE_WAIT] = "Tratamento de desenhos em espera", + [UI_MENU_PIPE_WAIT_DESC] = "Padrão mantém o agendamento atual, com um worker reservado para desenhos em espera; Antigo (draw) usa o agendamento anterior.", + [UI_MENU_PIPE_WAIT_DEFAULT] = "Padrão", [UI_MENU_PIPE_WAIT_DRAW] = "Antigo (draw)", + [UI_MENU_PIPE_SKIP] = "Desenhos pulados ao montar pipeline", + [UI_MENU_PIPE_SKIP_DESC] = "Quais desenhos são descartados em vez de esperar pelo seu pipeline.", + [UI_MENU_PIPE_SKIP_STRICT] = "Estrito", [UI_MENU_PIPE_SKIP_ANY] = "Qualquer", + [UI_MENU_PIPE_SKIP_FULLSCREEN] = "Passes de tela cheia", + [UI_MENU_LIB_PREBUILD] = "Pré-montagem das bibliotecas de estágio", + [UI_MENU_LIB_PREBUILD_DESC] = "Monta antes, em segundo plano, as bibliotecas de estágio dos shaders.", + [UI_MENU_LIB_PREBUILD_THREADS] = "Threads da pré-montagem das bibliotecas", + [UI_MENU_LIB_PREBUILD_THREADS_DESC] = "Threads usadas na pré-montagem; Automático escolhe um número pela sua CPU.", + [UI_MENU_PIPE_PREBUILD] = "Pré-montagem de pipelines", + [UI_MENU_PIPE_PREBUILD_DESC] = "Monta antes os pipelines conhecidos.", + [UI_MENU_PREWARM_XLATE] = "Pré-aquecimento do tradutor", + [UI_MENU_PREWARM_XLATE_DESC] = "Threads que traduzem os shaders conhecidos ao iniciar; Automático usa metade das threads da CPU, no máximo 16.", + [UI_MENU_GPL] = "Biblioteca de pipelines gráficos", + [UI_MENU_GPL_DESC] = "Usa a extensão de biblioteca de pipelines do Vulkan; desligue se o seu driver tiver problemas com ela.", + [UI_MENU_ASTC] = "Decodificação de texturas ASTC", + [UI_MENU_ASTC_DESC] = "Onde as texturas comprimidas em ASTC são decodificadas.", + [UI_MENU_ASTC_AUTO] = "Automático", [UI_MENU_ASTC_GPU] = "Shader da GPU", [UI_MENU_ASTC_NATIVE] = "Nativo", + [UI_MENU_ASTC_CPU] = "CPU", + [UI_MENU_MEMORY] = "Modo de memória da GPU", + [UI_MENU_MEMORY_DESC] = "Como a memória do jogo é compartilhada com a GPU.", + [UI_MENU_MEMORY_AUTO] = "Automático", [UI_MENU_MEMORY_IMPORT] = "Importar", [UI_MENU_MEMORY_MIRROR] = "Espelhar", + [UI_MENU_MEMORY_VERIFY] = "Verificar", + [UI_MENU_TEX_ALIAS] = "Aliasing de texturas", [UI_MENU_TEX_ALIAS_DESC] = "Uma textura nova criada sobre a memória de uma textura viva começa com esse conteúdo em vez de vazia.", + [UI_MENU_TEX_VRAM_PRESSURE] = "Limite de memória de texturas", [UI_MENU_TEX_VRAM_PRESSURE_DESC] = "Porcentagem da memória de vídeo acima da qual as texturas liberadas pelo jogo são descartadas (voltam do armazenamento quando usadas de novo). 0 mantém todas.", + [UI_MENU_ASTC_BC] = "Comprimir ASTC em BC3", [UI_MENU_ASTC_BC_DESC] = "Com o decodificador da GPU: recodifica as texturas ASTC decodificadas em BC3, que ocupa um quarto da memória de vídeo. As texturas ficam um pouco mais quadriculadas. Automático: ligado em GPUs com 4 GB de memória de vídeo ou menos.", + [UI_MENU_LOD_GUARD] = "Proteção de LOD", [UI_MENU_LOD_GUARD_DESC] = "Evita que objetos pisquem no lugar errado ou que o plano distante fique achatado por um quadro, esperando o jogo terminar de reescrever os dados de nível de detalhe antes que a GPU os leia.", + [UI_MENU_FIX_C0] = "Correção do buffer de constantes", + [UI_MENU_FIX_C0_DESC] = "Preenche o banco de constantes c0 reservado ao driver (tamanhos de armazenamento, tamanho do dispatch, tamanho do ponto, instância base) em vez de ler zeros.", + [UI_MENU_FIX_ZERO] = "Zerar memória nova da GPU", + [UI_MENU_FIX_ZERO_DESC] = "Novos pools de memória da GPU começam zerados, como a memória do Switch.", + [UI_MENU_BIND_SKIP] = "Pular bindings repetidos", + [UI_MENU_BIND_SKIP_DESC] = "Otimização: pula bindings que não mudaram.", + [UI_MENU_SHADER_MEMO] = "Memória de shaders", [UI_MENU_SHADER_MEMO_DESC] = "Otimização: lembra as buscas de shaders.", + [UI_MENU_PIPE_MEMO] = "Memória de pipeline", [UI_MENU_PIPE_MEMO_DESC] = "Otimização: reaproveita o pipeline do desenho anterior quando o estado se repete.", + [UI_MENU_TEX_MEMO] = "Memória de bindings de textura", [UI_MENU_TEX_MEMO_DESC] = "Otimização: lembra os descritores de textura entre desenhos.", + [UI_MENU_DESC_TEMPLATE] = "Templates de descritores", + [UI_MENU_DESC_TEMPLATE_DESC] = "Otimização: atualiza os descritores em lote.", + [UI_MENU_PUSH_SKIP] = "Pular push constants repetidas", + [UI_MENU_PUSH_SKIP_DESC] = "Otimização: pula push constants que não mudaram.", + [UI_MENU_SPLIT_DRAWS] = "Envio antecipado à GPU", + [UI_MENU_SPLIT_DRAWS_DESC] = "Envia o trabalho da GPU em partes a cada N desenhos entre passes, para a GPU começar mais cedo; 0 desliga.", + [UI_MENU_VK_VALIDATION] = "Camada de validação", + [UI_MENU_VK_VALIDATION_DESC] = "Verifica cada chamada Vulkan e deixa o jogo muito mais lento.", + [UI_MENU_VK_VALIDATION_MISSING] = "A camada de validação não está instalada.", + [UI_MENU_VK_DYN_VERTEX] = "Estado dinâmico de vértices", + [UI_MENU_VK_DYN_VERTEX_DESC] = "Força a entrada dinâmica de vértices ligada ou desligada; o padrão depende da GPU.", + [UI_MENU_VK_DYN_A2C] = "Alpha to coverage dinâmico", + [UI_MENU_VK_DYN_A2C_DESC] = "Força o alpha to coverage dinâmico ligado ou desligado. Padrão: desligado no driver da AMD " + "para RDNA 2 e anteriores, que com ele desenha os personagens pretos, ligado nos demais.", + [UI_MENU_VK_SUBGROUP_PIN] = "Fixar tamanho do subgrupo", + [UI_MENU_VK_SUBGROUP_PIN_DESC] = "Fixa o tamanho do subgrupo dos shaders.", + [UI_MENU_TRACE] = "Registro de desempenho", [UI_MENU_TRACE_DESC] = "Grava os tempos de quadro num arquivo.", + [UI_MENU_FLIGHT] = "Caixa-preta", + [UI_MENU_FLIGHT_DESC] = "Guarda um histórico curto e o salva quando um quadro trava.", + [UI_MENU_FLIGHT_SPIKE_MS] = "Limite de travamento (ms)", + [UI_MENU_FLIGHT_SPIKE_MS_DESC] = "Tempo de quadro que conta como travamento.", + [UI_MENU_PASS_CRUMBS] = "Marcas de passagem da GPU", + [UI_MENU_PASS_CRUMBS_DESC] = "Após uma falha da GPU, o log indica a passagem de renderização que estava em execução. Automático: só AMD. Ligado: também NVIDIA, com um pequeno custo.", + [UI_MENU_INPUT_RECORD] = "Gravação de comandos", + [UI_MENU_INPUT_RECORD_DESC] = "Grava os seus comandos num arquivo para relatórios de bug.", + [UI_MENU_OPEN_LOG] = "Abrir pasta de logs", [UI_MENU_OPEN_LOG_DESC] = "Mostra a pasta com os arquivos de log.", + [UI_MENU_COPY_INFO] = "Copiar informações do sistema", + [UI_MENU_COPY_INFO_DESC] = "Copia GPU, driver, CPU, RAM e as configurações alteradas para a área de transferência.", + [UI_MENU_COPY_INFO_DONE] = "Copiado", [UI_MENU_ACTION_FAILED] = "não funcionou", + [UI_MENU_RESET] = "Restaurar todas as configurações", [UI_MENU_RESET_DESC] = "Vale para todas as abas.", + [UI_MENU_RESET_CONFIRM] = "Restaurar todas as configurações para o padrão?", [UI_MENU_RESET_YES] = "Restaurar", + [UI_MENU_RESET_NO] = "Cancelar", + + [UI_MENU_OV_FPS] = "%.0f FPS", [UI_MENU_OV_FRAME_MS] = "%.1f ms", [UI_MENU_OV_GAME_PCT] = "jogo %.0f%%", + [UI_MENU_OV_MAX_MS] = "máx. %.1f ms", + [UI_MENU_OV_FPS_LABEL] = "FPS", [UI_MENU_OV_FRAME_LABEL] = "Quadro", [UI_MENU_OV_FMT_FPS] = "%.0f", + [UI_MENU_OV_LOW1] = "1%% piores %.0f", [UI_MENU_OV_LOW01] = "0.1%% piores %.0f", + [UI_MENU_OV_GPU] = "GPU", [UI_MENU_OV_CPU] = "CPU", [UI_MENU_OV_RAM] = "RAM", [UI_MENU_OV_VRAM] = "VRAM", + [UI_MENU_OV_TEMP] = "Temperatura", [UI_MENU_OV_POWER] = "Consumo", [UI_MENU_OV_CLOCK] = "Frequência", + [UI_MENU_OV_NONE] = "-", + [UI_MENU_OV_REPLAY] = "Preparo de quadro da GPU", [UI_MENU_OV_GPU_TIME] = "GPU ocupada por quadro", + [UI_MENU_OV_PIPE_WAIT] = "Esperas de pipeline", + [UI_MENU_OV_SHADER_WAIT] = "Esperas de shader", [UI_MENU_OV_TEX_UPLOADS] = "Envios de textura", + [UI_MENU_OV_FMT_PCT] = "%.0f%%", [UI_MENU_OV_FMT_TEMP] = "%.0f °C", [UI_MENU_OV_FMT_POWER] = "%.0f W", + [UI_MENU_OV_FMT_MHZ] = "%.0f MHz", [UI_MENU_OV_FMT_GB] = "%.1f GB", [UI_MENU_OV_FMT_GB_OF] = "/ %.1f GB", + [UI_MENU_OV_GAME_GB] = "jogo %.1f GB", [UI_MENU_OV_FMT_MS] = "%.2f ms", [UI_MENU_OV_PER_FRAME] = "%.1f por quadro", + [UI_MENU_OV_WINDOW] = "Janela", [UI_MENU_OV_OUTPUT] = "Imagem do jogo", [UI_MENU_OV_SCENE] = "Cena 3D", + [UI_MENU_OV_SCALING] = "Escala", [UI_MENU_OV_LIMIT] = "Limite de quadros", [UI_MENU_OV_PRESENT] = "Apresentação", + [UI_MENU_OV_FMT_SIZE] = "%ux%u", [UI_MENU_OV_FMT_SCENE] = "%ux%u, %.0f%%", + [UI_MENU_OV_FMT_FSR] = "AMD FSR 1, nitidez %d%%", [UI_MENU_OV_SCALE_NONE] = "nenhuma (1:1)", + [UI_MENU_OV_SCALE_BILINEAR] = "bilinear", + [UI_MENU_FOV] = "Campo de visão", + [UI_MENU_FOV_DESC] = "Quanto do mundo a câmera mostra, em porcentagem do original do jogo. Só na versão 1.4.3 do " + "jogo.", + [UI_MENU_SHADOW_RES] = "Tamanho do mapa de sombras", + [UI_MENU_SHADOW_RES_DESC] = "Tamanho do mapa de sombras do sol. O tamanho do jogo muda com a vista, até cerca de " + "400 perto do chão, então as sombras das folhas ficam quadriculadas e pulam; um " + "tamanho fixo as deixa paradas e mais nítidas. Acima de 1024 as sombras custam mais " + "tempo de GPU e memória de vídeo; 256 é mais grosseiro que o do jogo e serve para GPUs " + "integradas com pouca largura de banda de memória.", + [UI_MENU_SHADOW_RES_GAME] = "Do jogo (varia)", + [UI_MENU_ANISO] = "Filtragem de texturas", + [UI_MENU_ANISO_DESC] = "Filtragem anisotrópica das texturas de superfície: chão, estradas e paredes vistos em " + "ângulo raso continuam nítidos ao longe em vez de borrar. Jogo: o nível que o jogo pede, " + "como no Switch.", + [UI_MENU_ANISO_GAME] = "Jogo", + [UI_MENU_COLOR] = "Cor", + [UI_MENU_COLOR_PRESET] = "Perfil de cor", + [UI_MENU_COLOR_PRESET_DESC] = "Define os quatro valores abaixo. Vívido: mais contraste e saturação, sombras um " + "pouco mais abertas (feito para telas OLED). Clareza: um pouco mais de contraste, " + "saturação e brilho. Mexer em um valor torna o perfil Personalizado. O HUD muda com " + "a imagem; este menu não.", + [UI_MENU_COLOR_DEFAULT] = "Do jogo", + [UI_MENU_COLOR_VIVID] = "Vívido (OLED)", + [UI_MENU_COLOR_CLARITY] = "Clareza", + [UI_MENU_COLOR_CUSTOM] = "Personalizado", + [UI_MENU_COLOR_BRIGHTNESS] = "Brilho", + [UI_MENU_COLOR_BRIGHTNESS_DESC] = "Somado a todas as cores; 0 não muda nada.", + [UI_MENU_COLOR_CONTRAST] = "Contraste", + [UI_MENU_COLOR_CONTRAST_DESC] = "Afasta as cores do cinza médio (acima de 100 %) ou as aproxima dele; 100 % não " + "muda nada.", + [UI_MENU_COLOR_SATURATION] = "Saturação", + [UI_MENU_COLOR_SATURATION_DESC] = "Força da cor em relação ao cinza; 100 % não muda nada.", + [UI_MENU_COLOR_GAMMA] = "Gama", + [UI_MENU_COLOR_GAMMA_DESC] = "Abaixo de 1 clareia os tons escuros e médios, acima de 1 os escurece; 1 não muda " + "nada.", + [UI_MENU_HDR_GROUP] = "HDR", + [UI_MENU_HDR] = "Saída HDR", + [UI_MENU_HDR_DESC] = "Realces brilhantes em uma tela HDR. Precisa do HDR ligado no Windows (\"Usar HDR\") ou num " + "compositor Wayland com gerenciamento de cor; sem isso a imagem continua SDR. Vale após " + "reiniciar. O perfil de cor acima é para SDR e não é usado em HDR.", + [UI_MENU_HDR_PEAK] = "Brilho máximo", + [UI_MENU_HDR_PEAK_DESC] = "O pico da sua tela em nits; os realces mais claros param aí.", + [UI_MENU_HDR_GAME] = "Brilho do jogo", + [UI_MENU_HDR_GAME_DESC] = "Nits do branco da imagem (branco de papel). 203 é a referência de HDR; aumente num " + "ambiente claro.", + [UI_MENU_HDR_UI] = "Brilho do HUD e do menu", + [UI_MENU_HDR_UI_DESC] = "Nits do branco do HUD e deste menu.", + [UI_MENU_PROMPTS] = "Ícones de botões", + [UI_MENU_PROMPTS_DESC] = "Os botões de qual controle os ícones do jogo mostram. Nintendo: os do próprio jogo. " + "Xbox: LB, RB, LT, RT, Menu, Exibir e uma cruz direcional, e A, B, X e Y com a letra do " + "botão que o layout acima aperta (com Nintendo (A à direita) o botão da direita tem a " + "letra B, então os ícones de A mostram B). O jogo lê os ícones ao iniciar, então eles " + "mudam depois de reiniciar. Eles fazem parte dos arquivos do jogo, então um botão " + "remapeado em Controles continua com o ícone antigo na tela.", + [UI_MENU_PROMPTS_NINTENDO] = "Nintendo", + [UI_MENU_PROMPTS_XBOX] = "Xbox", + [UI_MENU_PROMPTS_MISSING] = "faltam os arquivos dos ícones: execute a instalação de novo", + [UI_MENU_PROMPTS_FAILED] = "arquivos dos ícones danificados: execute a instalação de novo", + [UI_MENU_PROMPTS_RESTART] = "os ícones mudam após reiniciar", + [UI_MENU_PROMPTS_OFF_SHAPE] = "desligados numa resolução de jogo que não é 16:9", + [UI_MENU_PROMPTS_OFF_MODS] = "desligados: um mod de RomFS substitui os arquivos deles", + [UI_MENU_PROMPTS_OFF_PATCHES] = "desligados enquanto TOTK_ROMFS_PATCHES está definido", + [UI_MENU_RESTART] = "Reiniciar agora", + [UI_MENU_RESTART_DESC] = "Fecha o jogo e o abre de novo com os novos ícones. O progresso desde o último " + "salvamento é perdido.", + [UI_MENU_RESTART_CONFIRM] = "Reiniciar o jogo agora? O progresso desde o último salvamento é perdido.", + [UI_MENU_RESTART_YES] = "Reiniciar", + [UI_MENU_RESTART_SAVING] = "O jogo está salvando: dá para reiniciar em um instante.", + [UI_MENU_STICK_SENS] = "Velocidade da câmera (analógico)", + [UI_MENU_STICK_SENS_DESC] = "Multiplica a velocidade da câmera do analógico direito do jogo, incluindo a máxima, " + "em cada uma das cinco sensibilidades de câmera do jogo. O mouse mantém a própria " + "configuração.", + [UI_MENU_WALK_KEY] = "Tecla de andar", + [UI_MENU_WALK_KEY_DESC] = "Segure para inclinar os analógicos do teclado só em parte, assim o teclado anda em vez " + "de correr. A intensidade do andar define quanto. Controle e mouse não são afetados.", + [UI_MENU_WALK_KEY_TAKEN] = "também %s", + [UI_MENU_WALK_SCALE] = "Intensidade do andar", + [UI_MENU_WALK_SCALE_DESC] = "A inclinação dos analógicos do teclado enquanto a tecla de andar está segura. Metade " + "é andar, a inclinação total é correr.", + [UI_MENU_OV_SCALE_DOWN] = "bilinear (o FSR só amplia)", + [UI_MENU_HDR_HUE] = "Correção de matiz dos realces", + [UI_MENU_HDR_HUE_DESC] = "Cores fortes como fogo e faíscas vão para o branco como na imagem SDR do próprio jogo e " + "combinam com a luz que emitem; 0 as mantém totalmente saturadas.", + [UI_MENU_SHADOW_SCALE] = "Resolução das sombras", + [UI_MENU_SHADOW_SCALE_DESC] = "Desenha os mapas de sombra do sol essa quantidade de vezes maiores em cada lado " + "que os 960x960 do jogo: bordas de sombra e sombras de folhas mais nítidas. Vale no " + "próximo início.", + [UI_MENU_SHADOW_RANGE] = "Alcance das cascatas de sombra", + [UI_MENU_SHADOW_RANGE_DESC] = "Afasta os degraus em que as sombras ficam mais suaves (20, 40, 80 e 160 m no " + "jogo). Espalha o mapa de sombras por mais terreno: aumente a Resolução das sombras " + "junto. Vale no próximo início.", + [UI_MENU_CONTACT_SHADOWS] = "Sombras de contato", + [UI_MENU_CONTACT_SHADOWS_DESC] = "Pequenas sombras do sol que os mapas de sombra não pegam: nos pés, sob pedras, " + "na grama e em dobras. Traçadas na tela depois da iluminação. Vale no próximo " + "início.", + [UI_MENU_PCSS] = "Sombras suaves", + [UI_MENU_PCSS_DESC] = "Sombras do sol nítidas onde um objeto toca o chão e mais suaves quanto mais longe a sombra " + "cai (PCSS). 0 mantém o filtro do jogo. Vale no próximo início.", + [UI_MENU_DLAA] = "NVIDIA DLSS / DLAA", + [UI_MENU_DLAA_DESC] = "NVIDIA DLSS para a cena 3D (placas NVIDIA RTX; nvngx_dlss.dll ao lado do totk.exe): bordas " + "mais suaves e menos cintilação. O HUD não é afetado. O modo abaixo define a resolução de " + "onde ele parte. Vale após reiniciar.", + [UI_MENU_DLSS] = "Modo do DLSS", + [UI_MENU_DLSS_DESC] = "DLAA desenha a cena 3D na resolução cheia. Qualidade, Equilibrado, Desempenho e Ultra " + "Desempenho a desenham em 67, 58, 50 e 33 % e deixam o DLSS ampliá-la, o que poupa tempo da " + "GPU; eles substituem a escala 3D. Vale na hora.", + [UI_MENU_DLSS_DLAA] = "DLAA (nativo)", + [UI_MENU_DLSS_QUALITY] = "Qualidade", + [UI_MENU_DLSS_BALANCED] = "Equilibrado", + [UI_MENU_DLSS_PERFORMANCE] = "Desempenho", [UI_MENU_DLSS_ULTRA] = "Ultra Desempenho", + [UI_MENU_DLSS_PRESET] = "Modelo do DLSS", + [UI_MENU_DLSS_PRESET_DESC] = "Qual modelo do DLSS roda. Automático segue a recomendação da NVIDIA para cada modo: K " + "para DLAA, Qualidade e Equilibrado, M para Desempenho, L para Ultra Desempenho. J: um " + "pouco menos de rastro atrás de personagens em movimento, um pouco mais de cintilação. K: " + "o modelo mais nítido em todos os modos. Vale na hora.", + [UI_MENU_DLSS_PRESET_DEFAULT] = "Automático", [UI_MENU_DLSS_PRESET_K] = "K (mais nítido)", + [UI_MENU_DLSS_PRESET_J] = "J (menos rastro)", + [UI_MENU_DLSS_CHARS] = "Rastros de personagens no DLSS", + [UI_MENU_DLSS_CHARS_DESC] = "Reduz os rastros fracos atrás de personagens, animais e do que eles seguram em movimento: " + "ali o DLSS usa menos dos quadros anteriores. Mais forte remove mais rastros e deixa essas " + "bordas um pouco menos suaves. Vale na hora.", + [UI_MENU_DLSS_CHARS_OFF] = "Desligado", [UI_MENU_DLSS_CHARS_LIGHT] = "Leve", [UI_MENU_DLSS_CHARS_MEDIUM] = "Médio", + [UI_MENU_DLSS_CHARS_FULL] = "Total", + [UI_MENU_FXAA] = "Antisserrilhado do jogo (FXAA)", + [UI_MENU_FXAA_DESC] = "A suavização de bordas FXAA do próprio jogo. Borra um pouco os detalhes finos; com o DLAA " + "ligado não é necessária.", + [UI_MENU_TAB_CHEATS] = "Cheats", + [UI_MENU_CHEATS_NONE] = "Nenhum cheat para esta versão do jogo. Os cheats de um mod são um arquivo de cheats do " + "Atmosphere na pasta cheats dele, com o nome da build do jogo: mods\\<mod>\\cheats\\%s. " + "Reinicie o jogo para lê-los. Para cheats que alteram o código do jogo, o jogo se recompila " + "sozinho ao iniciar (alguns minutos).", + [UI_MENU_CHEATS_MOD] = "%s: %d cheats", + [UI_MENU_CHEAT_CODE_DESC] = "Altera o código do jogo. Liga e desliga na hora.", + [UI_MENU_CHEAT_KEY_DESC] = "Altera o código do jogo enquanto você segura %s. Liga e desliga na hora.", + [UI_MENU_CHEAT_DATA_DESC] = "Escreve na memória do jogo a cada quadro enquanto está ligado.", + [UI_MENU_CHEAT_MASTER] = "sempre ligado", + [UI_MENU_CHEAT_MASTER_DESC] = "O código mestre do arquivo: está sempre ligado.", + [UI_MENU_CHEAT_RESTORE] = "desnecessário", + [UI_MENU_CHEAT_RESTORE_DESC] = "Ele só escreve de volta o código original do jogo. Desligar um cheat já faz isso.", + [UI_MENU_CHEAT_REBUILD] = "após recompilar", + [UI_MENU_CHEAT_REBUILD_DESC] = "Este cheat altera o código do jogo e é novo ou foi editado depois que o jogo foi " + "compilado. O jogo o compila na próxima vez que iniciar (alguns minutos), ou use " + "Recompilar o jogo agora.", + [UI_MENU_CHEAT_UNSUPPORTED] = "não suportado", + [UI_MENU_CHEAT_UNSUPPORTED_DESC] = "Este cheat não pode ser usado com esta versão do jogo:", + [UI_MENU_CHEATS_REBUILD] = "Recompilar o jogo agora", + [UI_MENU_CHEATS_REBUILD_DESC] = "Alguns cheats alteram o código do jogo e ainda não foram compilados nele: são " + "novos ou editados, ou a última recompilação falhou ou foi cancelada. Fecha o " + "jogo; o instalador os compila (alguns minutos) e inicia o jogo de novo. O " + "progresso desde o último salvamento é perdido.", + [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "Este mod altera o código do jogo (exefs) de um jeito que o port não consegue " + "aplicar, então o mod inteiro fica desligado. %s", + [UI_MENU_MODS_EXEFS_OTHER] = "Os patches de código dele (exefs) são para outra versão do jogo.", + [UI_MENU_MODS_EXEFS_ON] = "Patches de código em uso: %d arquivos.", + [UI_MENU_MODS_REPLACES_BUILTIN] = "Patches de código em uso: %d arquivos. Eles substituem o patch do próprio port " + "%s, que fica desligado enquanto este mod está ligado.", +}; + +// Simplified Chinese: this row covers the software keyboard and the whole menu, so the menu and the overlay read Chinese +// when the menu language is zh-Hans (ui_text.h menu_text). The rows are fixed width, so the strings stay close to the +// English length; an id without a Chinese entry falls back to English. +static const char *const text_zh_hans[UI_TEXT_COUNT] = { + SWKBD("文字输入", "请输入名字", "马儿", "确定", "取消", "手柄:A 确定,B 取消"), + + [UI_MENU_TITLE] = "设置", + [UI_MENU_TAB_SCREEN] = "显示", + [UI_MENU_TAB_GAME] = "游戏", + [UI_MENU_TAB_SOUND] = "声音", + [UI_MENU_TAB_CONTROLS] = "操作", + [UI_MENU_TAB_AMIIBO] = "amiibo", + [UI_MENU_TAB_MENU] = "菜单", + [UI_MENU_TAB_ADVANCED] = "高级", + [UI_MENU_TAB_MODS] = "模组", + [UI_MENU_MODS_BUILTIN] = "移植版内置", [UI_MENU_MODS_INSTALLED] = "已安装的模组", + [UI_MENU_MODS_NONE] = "还没有模组。把模组解压到 mods 文件夹,每个模组一个文件夹,里面放 romfs 文件夹,然后重启游戏。" + "TKMM 也可以把合并后的模组导出到那里的一个文件夹。", + [UI_MENU_MODS_OPEN] = "打开模组文件夹", + [UI_MENU_MODS_OPEN_DESC] = "显示游戏加载模组的文件夹。不存在时会创建它。", + [UI_MENU_MODS_ROW_DESC] = "替换的游戏文件:%u。新增的文件:%u。", + [UI_MENU_MODS_OVERRIDDEN] = "它的 %u 个文件改用列表中更靠上的 %s 的文件。", + [UI_MENU_MODS_MOVE_HINT] = "上方按钮或 Ctrl+上 上移,左侧按钮或 Ctrl+下 下移。两个模组修改同一个文件时,以靠上的为准。" + "更改在重启后生效。", + [UI_MENU_MODS_EMPTY] = "此文件夹中没有 romfs、exefs 或 cheats 文件。", + [UI_MENU_MODS_SUBSDK] = "此模组带有自己的游戏代码(subsdk 插件或替换的 main)。移植版无法加载代码模块,因此整个模组保持关闭。", + [UI_MENU_MODS_EXEFS_LATER] = "此模组修改游戏代码(exefs)。游戏会在下次启动时把这些补丁构建进去(重新构建需要几分钟);在此之前整个模组保持关闭。", + [UI_MENU_MODS_CHEATS] = "金手指:%d 个。在“金手指”标签页中开关。", + [UI_MENU_MODS_CHEATS_FILE] = "此游戏版本的金手指在启动时从此模组的 cheats\\%s 读取。", + [UI_MENU_MODS_BAD_NAME] = "文件夹名称含有 |,模组列表用它分隔名称。请重命名该文件夹。", + [UI_MENU_MODS_OTHER_VERSION] = "它有 %u 个文件是为其他游戏版本制作的,已被跳过。请寻找适用于你的游戏版本的此模组。", + [UI_MENU_MODS_UNUSED] = "未使用", + [UI_MENU_MODS_FPS60] = "60 FPS 补丁", + [UI_MENU_MODS_FPS60_DESC] = "移植版自带的 60 帧补丁。在“游戏”页的帧率上限中设置。", + [UI_MENU_MODS_RES] = "分辨率补丁", + [UI_MENU_MODS_RES_DESC] = "移植版自带的 1080p 以上分辨率补丁。在“显示”页的游戏分辨率中设置。", + [UI_MENU_MODS_QUICKMENU] = "快捷菜单修复", + [UI_MENU_MODS_QUICKMENU_DESC] = "移植版对 60 FPS 下武器、盾牌和材料快捷菜单黑色背景的修复。", + [UI_MENU_CONTINUE] = "继续", + [UI_MENU_QUIT] = "退出游戏", + [UI_MENU_ON] = "开", + [UI_MENU_OFF] = "关", + [UI_MENU_AUTO] = "自动", + [UI_MENU_DEFAULT_MARK] = "默认", + [UI_MENU_AFTER_RESTART] = "重启后生效", + [UI_MENU_SET_BY_ENV] = "由环境变量设定", + [UI_MENU_SAVE_FAILED] = "无法保存设置", + [UI_MENU_VERSION] = "TotK Recomp %s,游戏 %s", + [UI_MENU_FULLSCREEN] = "全屏", + [UI_MENU_FULLSCREEN_DESC] = "游戏铺满屏幕;按 F11 切换。", + [UI_MENU_WINDOW_SIZE] = "窗口大小", + [UI_MENU_WINDOW_SIZE_DESC] = "非全屏时游戏窗口的大小。", + [UI_MENU_WINDOW_SIZE_CUSTOM] = "自定义", + [UI_MENU_PRESENT] = "呈现模式", + [UI_MENU_PRESENT_DESC] = "渲染完成的帧如何显示到屏幕上。", + [UI_MENU_PRESENT_MAILBOX] = "Mailbox", + [UI_MENU_PRESENT_VSYNC] = "垂直同步", + [UI_MENU_PRESENT_IMMEDIATE] = "立即", + [UI_MENU_PRESENT_IN_USE] = "使用中:%s", + [UI_MENU_GPU] = "显卡", + [UI_MENU_GPU_DESC] = "用于渲染游戏的 Vulkan 设备。", + [UI_MENU_GPU_AUTO] = "自动", + [UI_MENU_PERF_OVERLAY] = "性能浮层", + [UI_MENU_PERF_OVERLAY_DESC] = "在右上角显示帧率。“完整”还会显示带图表的帧时间、CPU、" + "GPU 和内存占用、温度,以及渲染线程的等待时间。" + "按 F10 循环切换模式。", + [UI_MENU_PERF_OFF] = "关", + [UI_MENU_PERF_FPS] = "FPS", + [UI_MENU_PERF_FULL] = "完整", + + [UI_PERF_LINE_FPS] = "%.0f fps", + + [UI_MENU_GRAPHICS] = "画面", + [UI_MENU_OUTPUT_RES] = "游戏分辨率", + [UI_MENU_OUTPUT_RES_DESC] = "游戏绘制画面和界面的尺寸。" + "高于 1920 x 1080 会占用更多显存和 GPU 时间。" + "更宽的尺寸(21:9)会扩大视野,并把界面移到屏幕四角;" + "更高的尺寸(16:10、3:2)则移到上下边缘。", + [UI_MENU_RENDER_SCALE] = "3D 渲染比例", + [UI_MENU_RENDER_SCALE_DESC] = "3D 场景相对于游戏分辨率的尺寸;" + "游戏的 FSR 会将其缩放到该尺寸。" + "高于 100% 会平滑锯齿边缘(超采样),但 GPU 开销很高。", + [UI_MENU_RENDER_SCALE_GAME] = "游戏(83%)", + [UI_MENU_UPSCALER] = "放大到窗口", + [UI_MENU_UPSCALER_DESC] = "窗口大于游戏分辨率时画面的放大方式。" + "FSR 比双线性更能保留边缘和细节。", + [UI_MENU_UPSCALER_FSR] = "AMD FSR 1", + [UI_MENU_UPSCALER_BILINEAR] = "双线性", + [UI_MENU_ASPECT] = "画面宽高比", + [UI_MENU_ASPECT_DESC] = "当窗口宽高比和游戏分辨率不同时(21:9 屏幕上显示 16:9 画面):" + "用黑边保持画面比例,或拉伸铺满窗口。", + [UI_MENU_ASPECT_FIT] = "黑边", + [UI_MENU_ASPECT_STRETCH] = "拉伸", + [UI_MENU_FOV] = "视野", + [UI_MENU_FOV_DESC] = "镜头能看到多大范围,以游戏自身视野的百分比表示。" + "仅限游戏版本 1.4.3。", + [UI_MENU_SHARPNESS] = "FSR 锐度", + [UI_MENU_SHARPNESS_DESC] = "FSR 缩放后的锐化强度;0 为关闭。", + [UI_MENU_COLOR] = "色彩", + [UI_MENU_COLOR_PRESET] = "色彩配置", + [UI_MENU_COLOR_PRESET_DESC] = "同时设置下面四项。鲜艳:对比度和饱和度更高,暗部略微提亮(为 OLED 屏幕调校)。" + "清晰:对比度、饱和度和亮度都略高。改动任一项后,配置变为自定义。" + "HUD 随画面一起变化,本菜单不随之变化。", + [UI_MENU_COLOR_DEFAULT] = "游戏原样", [UI_MENU_COLOR_VIVID] = "鲜艳(OLED)", + [UI_MENU_COLOR_CLARITY] = "清晰", [UI_MENU_COLOR_CUSTOM] = "自定义", + [UI_MENU_COLOR_BRIGHTNESS] = "亮度", + [UI_MENU_COLOR_BRIGHTNESS_DESC] = "叠加到每种颜色上;0 表示不调整。", + [UI_MENU_COLOR_CONTRAST] = "对比度", + [UI_MENU_COLOR_CONTRAST_DESC] = "把颜色从中灰向外拉开(高于 100%)或向中灰收拢;" + "100% 表示不调整。", + [UI_MENU_COLOR_SATURATION] = "饱和度", + [UI_MENU_COLOR_SATURATION_DESC] = "颜色相对灰色的强度;100% 表示不调整。", + [UI_MENU_COLOR_GAMMA] = "伽马", + [UI_MENU_COLOR_GAMMA_DESC] = "小于 1 提亮暗部与中间调,大于 1 压暗;1 表示不调整。", + [UI_MENU_FPS_LIMIT] = "帧率上限", + [UI_MENU_FPS_LIMIT_DESC] = "游戏每秒绘制的最高帧数。高于 60 帧为实验性功能," + "需要开启“游戏速度跟随时钟”。", + [UI_MENU_FPS_30] = "30 FPS", + [UI_MENU_FPS_40] = "40 FPS", + [UI_MENU_FPS_45] = "45 FPS", + [UI_MENU_FPS_60] = "60 FPS", + [UI_MENU_FPS_90] = "90 FPS", + [UI_MENU_FPS_120] = "120 FPS", + [UI_MENU_FPS_144] = "144 FPS", + [UI_MENU_FPS_DISPLAY] = "显示器刷新率", + [UI_MENU_FPS_DISPLAY_HZ] = "显示器刷新率(%.0f Hz)", + [UI_MENU_REALTIME] = "游戏速度跟随时钟", + [UI_MENU_REALTIME_DESC] = "按真实时间推进游戏,帧率低时游戏速度也不会变慢。", + [UI_MENU_CLOTH_REALTIME] = "布料跟随时钟", + [UI_MENU_CLOTH_REALTIME_DESC] = "布料和头发也按真实时间推进。", + [UI_MENU_HIGHFPS_FIX] = "高帧率修正", + [UI_MENU_HIGHFPS_FIX_DESC] = "修正帧率高于 30 时出现异常的物理效果," + "以及帧率高于 60 时一直半透明或不可见的物体。", + [UI_MENU_GAME_LANG] = "游戏语言", + [UI_MENU_GAME_LANG_DESC] = "游戏文本和语音的语言。", + [UI_MENU_GAME_LANG_AUTO] = "自动", + [UI_MENU_OPERATION_MODE] = "主机模式", + [UI_MENU_OPERATION_MODE_DESC] = "游戏认为自己处于底座模式还是掌机模式。", + [UI_MENU_MODE_DOCKED] = "底座模式", + [UI_MENU_MODE_HANDHELD] = "掌机模式", + [UI_MENU_ASSUME_FOCUS] = "后台保持运行", + [UI_MENU_ASSUME_FOCUS_DESC] = "开启(默认)时,切到其他窗口游戏仍会继续运行," + "但键盘要等窗口回到前台才会响应。关闭时," + "窗口失去焦点即暂停,如同在 Switch 上按 Home 键;" + "这样被打断的读取可能会永久卡住。", + [UI_MENU_VOLUME] = "音量", + [UI_MENU_VOLUME_DESC] = "游戏的主音量。", + [UI_MENU_MOTION] = "体感操作", + [UI_MENU_MOTION_DESC] = "用手柄的陀螺仪瞄准。", + [UI_MENU_RUMBLE_STRENGTH] = "振动强度", + [UI_MENU_RUMBLE_STRENGTH_DESC] = "手柄振动的强弱。", + [UI_MENU_RUMBLE_CURVE] = "振动曲线", + [UI_MENU_RUMBLE_CURVE_DESC] = "数值越低,弱振动越强。", + [UI_MENU_RUMBLE_TRIGGERS] = "扳机振动(Xbox)", + [UI_MENU_RUMBLE_TRIGGERS_DESC] = "Xbox 手柄的扳机也会振动。", + [UI_MENU_PAD_LAYOUT] = "按键布局", + [UI_MENU_PAD_LAYOUT_DESC] = "哪个按键是 A。Nintendo:右侧按键,与 Switch 相同。Xbox:下方按键," + "与 Xbox 手柄上的标注一致(PlayStation 手柄上为叉键)。自动:" + "带 Nintendo 标注的手柄使用 Nintendo 布局,其他手柄使用 Xbox 布局。" + "Xbox 按键提示:A 仍是右侧键,但游戏显示 Xbox 标注(A 显示为 B、" + "B 显示为 A、X 显示为 Y、Y 显示为 X),重启后生效。" + "这些字母来自游戏文件,因此在“操作”中重映射过的按键," + "屏幕上仍显示原来的字母。", + [UI_MENU_PAD_LAYOUT_AUTO] = "自动", + [UI_MENU_PAD_LAYOUT_NINTENDO] = "Nintendo(A 在右)", + [UI_MENU_PAD_LAYOUT_XBOX] = "Xbox(A 在下)", + [UI_MENU_PAD_LAYOUT_IN_USE] = "使用中:%s", + [UI_MENU_GROUP_STICKS] = "摇杆", + [UI_MENU_LSTICK_DEADZONE] = "左摇杆死区", [UI_MENU_RSTICK_DEADZONE] = "右摇杆死区", + [UI_MENU_STICK_DEADZONE_DESC] = "手柄摇杆要推动多远游戏才会响应。如果角色或视角会自己移动," + "请调高此值:松开摇杆,查看“当前摇杆”,把死区设得比它高几个百分点。", + [UI_MENU_STICK_DEADZONE_NOW] = "当前摇杆:%.0f %%", + [UI_MENU_MOUSE_SENS] = "鼠标视角灵敏度", + [UI_MENU_MOUSE_SENS_DESC] = "鼠标转动视角的速度。", + + [UI_BINDS_HELP] = "选中一个格子后按 A 或 Enter,再按键、鼠标键或手柄按键。" + "Delete 或 X 清除格子。已占用的按键会移到这里;" + "手柄按键与它替换的那个对调。", + [UI_BINDS_COL_KEY1] = "按键 1", + [UI_BINDS_COL_KEY2] = "按键 2", + [UI_BINDS_COL_PAD] = "手柄", + [UI_BINDS_GROUP_BUTTONS] = "按键", + [UI_BINDS_GROUP_MOVE] = "移动", + [UI_BINDS_GROUP_CAMERA] = "视角", + [UI_BINDS_PRESS_KEY] = "请为“%s”按下一个键或鼠标键", + [UI_BINDS_PRESS_PAD] = "请为“%s”按下一个手柄按键", + [UI_BINDS_CANCEL_HINT] = "Esc 取消,或等待 %d 秒", + [UI_BINDS_TAKEN] = "%s 已从 %s 移走", + [UI_BINDS_SWAPPED] = "%s 已与 %s 对调", + [UI_BINDS_RESET] = "重置所有按键", + [UI_BINDS_RESET_DESC] = "恢复默认的键盘和手柄按键。上方的按键布局会再次生效:" + "重映射过的手柄按键会一直保留到此次重置。", + [UI_BIND_A] = "A", + [UI_BIND_B] = "B", + [UI_BIND_X] = "X", + [UI_BIND_Y] = "Y", + [UI_BIND_LCLICK] = "左摇杆按下", + [UI_BIND_RCLICK] = "右摇杆按下", + [UI_BIND_L] = "L", + [UI_BIND_R] = "R", + [UI_BIND_ZL] = "ZL", + [UI_BIND_ZR] = "ZR", + [UI_BIND_PLUS] = "+ 键", + [UI_BIND_MINUS] = "- 键", + [UI_BIND_LEFT] = "十字键左", + [UI_BIND_UP] = "十字键上", + [UI_BIND_RIGHT] = "十字键右", + [UI_BIND_DOWN] = "十字键下", + [UI_BIND_LSTICK_LEFT] = "向左移动", + [UI_BIND_LSTICK_UP] = "向前移动", + [UI_BIND_LSTICK_RIGHT] = "向右移动", + [UI_BIND_LSTICK_DOWN] = "向后移动", + [UI_BIND_RSTICK_LEFT] = "视角左转", + [UI_BIND_RSTICK_UP] = "视角上抬", + [UI_BIND_RSTICK_RIGHT] = "视角右转", + [UI_BIND_RSTICK_DOWN] = "视角下压", + [UI_BIND_DESC_A] = "互动、拾取、对话。", + [UI_BIND_DESC_B] = "奔跑、取消、返回。", + [UI_BIND_DESC_X] = "跳跃。", + [UI_BIND_DESC_Y] = "攻击。", + [UI_BIND_DESC_LCLICK] = "蹲下。", + [UI_BIND_DESC_RCLICK] = "第一人称视角。", + [UI_BIND_DESC_L] = "能力。", + [UI_BIND_DESC_R] = "投掷武器。", + [UI_BIND_DESC_ZL] = "举盾、锁定。", + [UI_BIND_DESC_ZR] = "弓箭。", + [UI_BIND_DESC_PLUS] = "菜单、背包。", + [UI_BIND_DESC_MINUS] = "地图。", + [UI_BIND_DESC_DPAD] = "快捷选择。", + [UI_BIND_DESC_MOVE] = "像左摇杆一样移动林克。手柄的左摇杆始终可用。", + [UI_BIND_DESC_CAMERA] = "像右摇杆一样转动视角。鼠标和手柄的右摇杆始终可用。", + [UI_KEY_SPACE] = "空格", + [UI_KEY_TAB] = "Tab", + [UI_KEY_LSHIFT] = "左 Shift", + [UI_KEY_LCTRL] = "左 Ctrl", + [UI_KEY_UP] = "上方向键", + [UI_KEY_DOWN] = "下方向键", + [UI_KEY_LEFT] = "左方向键", + [UI_KEY_RIGHT] = "右方向键", + [UI_KEY_MOUSE_L] = "鼠标左键", + [UI_KEY_MOUSE_R] = "鼠标右键", + [UI_KEY_MOUSE_M] = "鼠标中键", + [UI_KEY_MOUSE_X1] = "鼠标侧键 4", + [UI_KEY_MOUSE_X2] = "鼠标侧键 5", + [UI_KEY_ENTER] = "回车", + [UI_KEY_BACKSPACE] = "退格", + [UI_KEY_RSHIFT] = "右 Shift", + [UI_KEY_RCTRL] = "右 Ctrl", + [UI_KEY_LALT] = "Alt", + [UI_KEY_CAPSLOCK] = "Caps Lock", + [UI_KEY_INSERT] = "Insert", + [UI_KEY_DELETE] = "Delete", + [UI_KEY_HOME] = "Home", + [UI_KEY_END] = "End", + [UI_KEY_PAGEUP] = "Page Up", + [UI_KEY_PAGEDOWN] = "Page Down", + [UI_KEY_NUM_FMT] = "小键盘 %s", + [UI_PAD_SOUTH] = "下方按键", + [UI_PAD_EAST] = "右侧按键", + [UI_PAD_WEST] = "左侧按键", + [UI_PAD_NORTH] = "上方按键", + [UI_PAD_LSTICK] = "左摇杆", + [UI_PAD_RSTICK] = "右摇杆", + [UI_PAD_L] = "左肩键", + [UI_PAD_R] = "右肩键", + [UI_PAD_START] = "Start", + [UI_PAD_BACK] = "Back", + [UI_PAD_DPAD_LEFT] = "十字键左", + [UI_PAD_DPAD_UP] = "十字键上", + [UI_PAD_DPAD_RIGHT] = "十字键右", + [UI_PAD_DPAD_DOWN] = "十字键下", + [UI_PAD_ZL] = "左扳机", + [UI_PAD_ZR] = "右扳机", + [UI_PAD_LPADDLE1] = "左背键", + [UI_PAD_RPADDLE1] = "右背键", + [UI_PAD_LPADDLE2] = "左背键 2", + [UI_PAD_RPADDLE2] = "右背键 2", + [UI_PAD_CROSS] = "叉键", + [UI_PAD_CIRCLE] = "圆圈键", + [UI_PAD_SQUARE] = "方块键", + [UI_PAD_TRIANGLE] = "三角键", + + [UI_MENU_AMIIBO_DIR] = "amiibo 文件夹", + [UI_MENU_AMIIBO_OPEN] = "打开", + [UI_MENU_AMIIBO_DEFAULT_DIR] = "默认文件夹", + [UI_MENU_AMIIBO_REFRESH] = "刷新", + [UI_MENU_AMIIBO_PICK] = "amiibo 转储(540 字节的 .bin 或 .nfc 文件):", + [UI_MENU_AMIIBO_NO_DIR] = "无法打开文件夹 %s", + [UI_MENU_AMIIBO_READER] = "NFC 读取器", + [UI_MENU_AMIIBO_STATE] = "读取器:", + [UI_MENU_AMIIBO_STATE_IDLE] = "就绪", + [UI_MENU_AMIIBO_STATE_SEARCHING] = "游戏正在等待 amiibo", + [UI_MENU_AMIIBO_STATE_FOUND] = "amiibo 已放在读取器上", + [UI_MENU_AMIIBO_STATE_MOUNTED] = "游戏正在读取 amiibo", + [UI_MENU_AMIIBO_STATE_OFF] = "已关闭", + [UI_MENU_AMIIBO_FILE] = "已选文件:", + [UI_MENU_AMIIBO_NO_FILE] = "(未选择文件)", + [UI_MENU_AMIIBO_TAP] = "放上 amiibo", + [UI_MENU_AMIIBO_REMOVE] = "拿开 amiibo", + [UI_MENU_AMIIBO_AUTO_REMOVE] = "放上后自动拿开 amiibo", + [UI_MENU_AMIIBO_HOLD] = "停留时间", + [UI_MENU_AMIIBO_SECONDS] = "%.1f 秒", + [UI_MENU_AMIIBO_TAG] = "读取器上的 amiibo:", + [UI_MENU_AMIIBO_NO_TAG] = "读取器上没有 amiibo。", + [UI_MENU_AMIIBO_TAG_FILE] = "文件:%s", + [UI_MENU_AMIIBO_TAG_ID] = "角色 %s,系列 %02x,编号 %04x,类型 %02x", + [UI_MENU_AMIIBO_TAG_UID] = "UID:%s", + [UI_MENU_AMIIBO_READY] = "就绪", + [UI_MENU_AMIIBO_TAPPED] = "已放上:%s", + [UI_MENU_AMIIBO_LOAD_FAILED] = "不是 amiibo 转储(NTAG215,540 字节):%s", + [UI_MENU_AMIIBO_REMOVED] = "已拿开 amiibo", + [UI_MENU_AMIIBO_STATUS] = "状态:%s", + [UI_MENU_MENU_LANG] = "菜单语言", + [UI_MENU_MENU_LANG_DESC] = "本菜单和性能浮层的语言。", + [UI_MENU_MENU_LANG_AUTO] = "自动(跟随 Windows)", + [UI_MENU_DISCORD_RPC] = "Discord Rich Presence", + [UI_MENU_DISCORD_RPC_DESC] = "游戏运行时在 Discord 状态中显示“正在玩 TotK Recomp”及游戏时长。totk.ini 中的 " + "discord_app_id 和 discord_image 选择应用。", + [UI_MENU_ADV_WARNING] = "这些设置用于测试和排查问题,请在了解原因后再修改。", + [UI_MENU_GROUP_SCHED] = "调度", + [UI_MENU_GROUP_SHADERS] = "着色器和管线", + [UI_MENU_GROUP_TEXMEM] = "纹理和内存", + [UI_MENU_GROUP_FIXES] = "渲染修正", + [UI_MENU_GROUP_VULKAN] = "Vulkan", + [UI_MENU_GROUP_DIAG] = "诊断", + [UI_MENU_ASYNC_PREEMPT] = "虚拟核心抢占", + [UI_MENU_ASYNC_PREEMPT_DESC] = "允许同一虚拟核心上的一个游戏线程打断另一个。", + [UI_MENU_PREEMPT_OFF] = "关", + [UI_MENU_PREEMPT_ON] = "开", + [UI_MENU_PREEMPT_INHERIT] = "仅继承", + [UI_MENU_REPLAY_ASYNC] = "GPU 帧准备独立线程", + [UI_MENU_REPLAY_ASYNC_DESC] = "在单独的线程而不是游戏线程上为 GPU 准备帧。" + "最多增加一帧延迟。", + [UI_MENU_TIMER_1MS] = "1 ms Windows 计时器", + [UI_MENU_TIMER_1MS_DESC] = "将系统计时器精度提高到 1 ms,使帧间隔更平稳。", + [UI_MENU_PIPE_WAIT] = "等待中绘制的处理", + [UI_MENU_PIPE_WAIT_DESC] = "默认保留当前调度,并为等待中的绘制预留一个工作线程;" + "旧版(绘制)沿用先前的调度。", + [UI_MENU_PIPE_WAIT_DEFAULT] = "默认", + [UI_MENU_PIPE_WAIT_DRAW] = "旧版(绘制)", + [UI_MENU_PIPE_SKIP] = "构建管线时跳过的绘制", + [UI_MENU_PIPE_SKIP_DESC] = "哪些绘制会被直接丢弃,而不是等待其管线构建完成。", + [UI_MENU_PIPE_SKIP_STRICT] = "严格", + [UI_MENU_PIPE_SKIP_ANY] = "任意", + [UI_MENU_PIPE_SKIP_FULLSCREEN] = "全屏通道", + [UI_MENU_LIB_PREBUILD] = "阶段库预构建", + [UI_MENU_LIB_PREBUILD_DESC] = "在后台提前构建着色器阶段库。", + [UI_MENU_LIB_PREBUILD_THREADS] = "阶段库预构建线程数", + [UI_MENU_LIB_PREBUILD_THREADS_DESC] = "预构建使用的线程数;自动会根据你的 CPU 选择。", + [UI_MENU_PIPE_PREBUILD] = "管线预构建", + [UI_MENU_PIPE_PREBUILD_DESC] = "提前构建已知的管线。", + [UI_MENU_PREWARM_XLATE] = "着色器转译预热", + [UI_MENU_PREWARM_XLATE_DESC] = "启动时转译已知着色器的线程数;自动取 CPU 线程数的一半," + "最多 16。", + [UI_MENU_GPL] = "图形管线库", + [UI_MENU_GPL_DESC] = "使用 Vulkan 管线库扩展;驱动对此有问题时请关闭。", + [UI_MENU_ASTC] = "ASTC 纹理解码", + [UI_MENU_ASTC_DESC] = "压缩的 ASTC 纹理在哪里解码。", + [UI_MENU_ASTC_AUTO] = "自动", + [UI_MENU_ASTC_GPU] = "GPU 着色器", + [UI_MENU_ASTC_NATIVE] = "原生", + [UI_MENU_ASTC_CPU] = "CPU", + [UI_MENU_MEMORY] = "GPU 内存模式", + [UI_MENU_MEMORY_DESC] = "游戏内存如何与 GPU 共享。", + [UI_MENU_MEMORY_AUTO] = "自动", + [UI_MENU_MEMORY_IMPORT] = "导入", + [UI_MENU_MEMORY_MIRROR] = "镜像", + [UI_MENU_MEMORY_VERIFY] = "校验", + [UI_MENU_TEX_ALIAS] = "纹理别名", + [UI_MENU_TEX_ALIAS_DESC] = "在仍在使用的纹理内存上新建纹理时,新纹理沿用原有内容," + "而不是从空白开始。", + [UI_MENU_TEX_VRAM_PRESSURE] = "纹理显存上限", + [UI_MENU_TEX_VRAM_PRESSURE_DESC] = "显存占用超过这个百分比后,游戏已释放的纹理会被丢弃," + "再次用到时从存储重新加载。0 表示不丢弃。", + [UI_MENU_ASTC_BC] = "ASTC 压缩为 BC3", + [UI_MENU_ASTC_BC_DESC] = "配合 GPU 解码器使用:把解码后的 ASTC 纹理重新编码为 BC3," + "显存占用只有四分之一。纹理会出现轻微色块。" + "自动:显存 4 GB 及以下的显卡上开启。", + [UI_MENU_LOD_GUARD] = "LOD 保护", + [UI_MENU_LOD_GUARD_DESC] = "等待游戏改写完细节层次数据后再让 GPU 读取," + "避免物体错位闪现,或远景有一帧变平。", + [UI_MENU_FIX_C0] = "常量缓冲区修正", + [UI_MENU_FIX_C0_DESC] = "填充驱动保留的常量组 c0(存储缓冲区大小、派发大小、点大小、" + "基础实例),而不是读取零值。", + [UI_MENU_FIX_ZERO] = "清零新的 GPU 内存", + [UI_MENU_FIX_ZERO_DESC] = "新的 GPU 内存池初始内容为零,与 Switch 内存一致。", + [UI_MENU_BIND_SKIP] = "跳过重复绑定", + [UI_MENU_BIND_SKIP_DESC] = "优化:跳过没有变化的绑定。", + [UI_MENU_SHADER_MEMO] = "着色器查找缓存", + [UI_MENU_SHADER_MEMO_DESC] = "优化:记住着色器的查找结果。", + [UI_MENU_PIPE_MEMO] = "管线缓存", + [UI_MENU_PIPE_MEMO_DESC] = "优化:状态重复时复用上一次绘制的管线。", + [UI_MENU_TEX_MEMO] = "纹理绑定缓存", + [UI_MENU_TEX_MEMO_DESC] = "优化:在多次绘制之间记住纹理描述符。", + [UI_MENU_DESC_TEMPLATE] = "描述符模板", + [UI_MENU_DESC_TEMPLATE_DESC] = "优化:批量更新描述符。", + [UI_MENU_PUSH_SKIP] = "跳过重复推送常量", + [UI_MENU_PUSH_SKIP_DESC] = "优化:跳过没有变化的推送常量。", + [UI_MENU_SPLIT_DRAWS] = "提前提交 GPU 工作", + [UI_MENU_SPLIT_DRAWS_DESC] = "在渲染通道之间每隔 N 次绘制分批提交 GPU 工作,让 GPU 更早开始;" + "0 为关闭。", + [UI_MENU_VK_VALIDATION] = "验证层", + [UI_MENU_VK_VALIDATION_DESC] = "检查每一次 Vulkan 调用,会大幅拖慢游戏。", + [UI_MENU_VK_VALIDATION_MISSING] = "未安装验证层。", + [UI_MENU_VK_DYN_VERTEX] = "动态顶点状态", + [UI_MENU_VK_DYN_VERTEX_DESC] = "强制开关动态顶点输入;默认值取决于显卡。", + [UI_MENU_VK_DYN_A2C] = "动态 Alpha 覆盖", + [UI_MENU_VK_DYN_A2C_DESC] = "强制开关动态 Alpha 覆盖。默认:AMD RDNA 2 及更早的驱动上为关闭," + "开启会导致人物变黑;其他显卡上为开启。", + [UI_MENU_VK_SUBGROUP_PIN] = "固定子群大小", + [UI_MENU_VK_SUBGROUP_PIN_DESC] = "固定着色器的子群大小。", + [UI_MENU_TRACE] = "性能日志", + [UI_MENU_TRACE_DESC] = "把每帧耗时写入文件。", + [UI_MENU_FLIGHT] = "飞行记录仪", + [UI_MENU_FLIGHT_DESC] = "保留一小段历史记录,在画面卡住时保存。", + [UI_MENU_FLIGHT_SPIKE_MS] = "卡顿阈值(ms)", + [UI_MENU_FLIGHT_SPIKE_MS_DESC] = "算作卡顿的帧时间。", + [UI_MENU_PASS_CRUMBS] = "GPU 渲染通道标记", + [UI_MENU_PASS_CRUMBS_DESC] = "GPU 崩溃后,日志会写出当时正在运行的渲染通道。自动:仅 AMD。开:NVIDIA 也启用,有少量开销。", + [UI_MENU_INPUT_RECORD] = "输入录制", + [UI_MENU_INPUT_RECORD_DESC] = "把你的输入写入文件,用于反馈问题。", + [UI_MENU_OPEN_LOG] = "打开日志文件夹", + [UI_MENU_OPEN_LOG_DESC] = "显示存放日志文件的文件夹。", + [UI_MENU_COPY_INFO] = "复制系统信息", + [UI_MENU_COPY_INFO_DESC] = "把显卡、驱动、CPU、内存和改动过的设置复制到剪贴板。", + [UI_MENU_COPY_INFO_DONE] = "已复制", + [UI_MENU_ACTION_FAILED] = "操作失败", + [UI_MENU_RESET] = "将所有设置恢复为默认值", + [UI_MENU_RESET_DESC] = "对所有标签页生效。", + [UI_MENU_RESET_CONFIRM] = "要将所有设置恢复为默认值吗?", + [UI_MENU_RESET_YES] = "重置", + [UI_MENU_RESET_NO] = "取消", + [UI_MENU_OV_FPS] = "%.0f FPS", + [UI_MENU_OV_FRAME_MS] = "%.1f ms", + [UI_MENU_OV_GAME_PCT] = "游戏 %.0f%%", + [UI_MENU_OV_MAX_MS] = "最长 %.1f ms", + [UI_MENU_OV_FPS_LABEL] = "FPS", [UI_MENU_OV_FRAME_LABEL] = "帧时间", [UI_MENU_OV_FMT_FPS] = "%.0f", + [UI_MENU_OV_LOW1] = "1%% 低 %.0f", [UI_MENU_OV_LOW01] = "0.1%% 低 %.0f", + [UI_MENU_OV_GPU] = "GPU", + [UI_MENU_OV_CPU] = "CPU", + [UI_MENU_OV_RAM] = "内存", + [UI_MENU_OV_VRAM] = "显存", + [UI_MENU_OV_TEMP] = "温度", + [UI_MENU_OV_POWER] = "功耗", + [UI_MENU_OV_CLOCK] = "频率", + [UI_MENU_OV_NONE] = "-", + [UI_MENU_OV_REPLAY] = "GPU 帧准备", + [UI_MENU_OV_GPU_TIME] = "每帧 GPU 忙碌时间", + [UI_MENU_OV_PIPE_WAIT] = "管线等待", + [UI_MENU_OV_SHADER_WAIT] = "着色器等待", + [UI_MENU_OV_TEX_UPLOADS] = "纹理上传", + [UI_MENU_OV_FMT_PCT] = "%.0f%%", + [UI_MENU_OV_FMT_TEMP] = "%.0f °C", + [UI_MENU_OV_FMT_POWER] = "%.0f W", + [UI_MENU_OV_FMT_MHZ] = "%.0f MHz", + [UI_MENU_OV_FMT_GB] = "%.1f GB", + [UI_MENU_OV_FMT_GB_OF] = "/ %.1f GB", + [UI_MENU_OV_GAME_GB] = "游戏 %.1f GB", + [UI_MENU_OV_FMT_MS] = "%.2f ms", + [UI_MENU_OV_PER_FRAME] = "每帧 %.1f", + [UI_MENU_OV_WINDOW] = "窗口", + [UI_MENU_OV_OUTPUT] = "游戏画面", + [UI_MENU_OV_SCENE] = "3D 场景", + [UI_MENU_OV_SCALING] = "缩放", + [UI_MENU_OV_LIMIT] = "帧率上限", + [UI_MENU_OV_PRESENT] = "呈现模式", + [UI_MENU_OV_FMT_SIZE] = "%ux%u", + [UI_MENU_OV_FMT_SCENE] = "%ux%u,%.0f%%", + [UI_MENU_OV_FMT_FSR] = "AMD FSR 1,锐度 %d%%", + [UI_MENU_OV_SCALE_NONE] = "无(1:1)", + [UI_MENU_OV_SCALE_BILINEAR] = "双线性", + [UI_MENU_SHADOW_RES] = "阴影贴图尺寸", + [UI_MENU_SHADOW_RES_DESC] = "太阳阴影贴图的尺寸。游戏自身的尺寸随视角变化,靠近地面时降到约 400,因此树叶阴影显得块状并且跳动;固定尺寸能让阴影稳定、更清晰。高于 1024 时阴影会占用更多 " + "GPU 时间和显存;256 比游戏自身的尺寸更粗糙,适合显存带宽不足的集成显卡。", + [UI_MENU_SHADOW_RES_GAME] = "游戏默认(会变化)", + [UI_MENU_ANISO] = "纹理过滤", + [UI_MENU_ANISO_DESC] = "表面纹理的各向异性过滤:以小角度看到的地面、道路和墙壁在远处依然清晰,而不会模糊。游戏:使用游戏请求的级别,与 Switch 上相同。", + [UI_MENU_ANISO_GAME] = "游戏", + [UI_MENU_HDR_GROUP] = "HDR", + [UI_MENU_HDR] = "HDR 输出", + [UI_MENU_HDR_DESC] = "在 HDR 显示器上呈现明亮的高光。需要在 Windows 中开启 HDR(“使用 HDR”),或使用带色彩管理的 Wayland 合成器;否则画面保持 " + "SDR。重启后生效。上方的色彩配置用于 SDR,在 HDR 下不使用。", + [UI_MENU_HDR_PEAK] = "峰值亮度", + [UI_MENU_HDR_PEAK_DESC] = "显示器的峰值亮度(尼特);最亮的高光止于此。", + [UI_MENU_HDR_GAME] = "游戏亮度", + [UI_MENU_HDR_GAME_DESC] = "画面白色(纸白)的尼特值。203 为 HDR 参考值;在明亮的房间里可调高。", + [UI_MENU_HDR_UI] = "HUD 和菜单亮度", + [UI_MENU_HDR_UI_DESC] = "HUD 和本菜单白色的尼特值。", + [UI_MENU_PROMPTS] = "按键提示", + [UI_MENU_PROMPTS_DESC] = "游戏的按键提示显示哪种手柄的按键。Nintendo:游戏原有的提示。Xbox:LB、RB、LT、RT、Menu、View 和十字键,以及上方布局所按下的按键上标注的 " + "A、B、X、Y(使用 Nintendo(A 在右)时右侧按键标为 B,因此 A 的提示显示 " + "B)。游戏在启动时读取按键提示,因此重启后才会改变。提示属于游戏文件,在“操作”中重新映射的按键在屏幕上仍显示原来的提示。", + [UI_MENU_PROMPTS_NINTENDO] = "Nintendo", + [UI_MENU_PROMPTS_XBOX] = "Xbox", + [UI_MENU_PROMPTS_MISSING] = "缺少按键提示文件:请重新运行安装程序", + [UI_MENU_PROMPTS_FAILED] = "按键提示文件已损坏:请重新运行安装程序", + [UI_MENU_PROMPTS_RESTART] = "按键提示重启后生效", + [UI_MENU_PROMPTS_OFF_SHAPE] = "游戏分辨率不是 16:9 时关闭", + [UI_MENU_PROMPTS_OFF_MODS] = "已关闭:RomFS 模组替换了它们的文件", + [UI_MENU_PROMPTS_OFF_PATCHES] = "设置了 TOTK_ROMFS_PATCHES 时关闭", + [UI_MENU_RESTART] = "立即重启", + [UI_MENU_RESTART_DESC] = "关闭游戏并以新的按键提示重新启动。上次存档之后的进度将会丢失。", + [UI_MENU_RESTART_CONFIRM] = "现在重启游戏?上次存档之后的进度将会丢失。", + [UI_MENU_RESTART_YES] = "重启", + [UI_MENU_RESTART_SAVING] = "游戏正在存档:稍后即可重启。", + [UI_MENU_STICK_SENS] = "摇杆镜头速度", + [UI_MENU_STICK_SENS_DESC] = "在游戏的五档镜头灵敏度下,按倍数调整右摇杆的镜头转动速度(包括最高速度)。鼠标使用自己的设置。", + [UI_MENU_WALK_KEY] = "步行键", + [UI_MENU_WALK_KEY_DESC] = "按住时键盘模拟的摇杆只推到一部分,使键盘操作走路而不是奔跑。推动幅度由步行幅度决定。手柄和鼠标不受影响。", + [UI_MENU_WALK_KEY_TAKEN] = "同时为 %s", + [UI_MENU_WALK_SCALE] = "步行幅度", + [UI_MENU_WALK_SCALE_DESC] = "按住步行键时键盘摇杆的推动幅度。一半为走路,推满为奔跑。", + [UI_MENU_OV_SCALE_DOWN] = "双线性(FSR 只能放大)", + [UI_MENU_HDR_HUE] = "高光色调校正", + [UI_MENU_HDR_HUE_DESC] = "火焰、火花等明亮色彩像游戏自身的 SDR 画面一样趋向白色,与它们发出的光一致;0 保持完全饱和。", + [UI_MENU_SHADOW_SCALE] = "阴影分辨率", + [UI_MENU_SHADOW_SCALE_DESC] = "将太阳阴影贴图的每条边绘制为游戏 960x960 的若干倍:阴影边缘和树叶阴影更清晰。下次启动时生效。", + [UI_MENU_SHADOW_RANGE] = "阴影级联范围", + [UI_MENU_SHADOW_RANGE_DESC] = "把阴影变柔和的分界(游戏中为 20、40、80 和 160 米)推得更远。阴影贴图会覆盖更大的地面,请同时提高阴影分辨率。下次启动时生效。", + [UI_MENU_CONTACT_SHADOWS] = "接触阴影", + [UI_MENU_CONTACT_SHADOWS_DESC] = "阴影贴图遗漏的细小太阳阴影:脚下、石头下、草丛和褶皱中。在光照之后于屏幕空间追踪。下次启动时生效。", + [UI_MENU_PCSS] = "柔和阴影", + [UI_MENU_PCSS_DESC] = "物体接触地面处的太阳阴影锐利,阴影投得越远越柔和(PCSS)。0 保留游戏自身的过滤。下次启动时生效。", + [UI_MENU_DLAA] = "NVIDIA DLSS / DLAA", + [UI_MENU_DLAA_DESC] = "用于 3D 场景的 NVIDIA DLSS(NVIDIA RTX 显卡;nvngx_dlss.dll 放在 totk.exe 旁):边缘更平滑,闪烁更少。HUD " + "不受影响。下方的模式决定它的工作分辨率。重启后生效。", + [UI_MENU_DLSS] = "DLSS 模式", + [UI_MENU_DLSS_DESC] = "DLAA 以全分辨率绘制 3D 场景。质量、平衡、性能和超级性能分别以 67%、58%、50% 和 33% 绘制,再由 DLSS 放大,以节省 GPU 时间;它们会取代 3D 渲染比例。立即生效。", + [UI_MENU_DLSS_DLAA] = "DLAA(原生)", + [UI_MENU_DLSS_QUALITY] = "质量", + [UI_MENU_DLSS_BALANCED] = "平衡", + [UI_MENU_DLSS_PERFORMANCE] = "性能", + [UI_MENU_DLSS_ULTRA] = "超级性能", + [UI_MENU_DLSS_PRESET] = "DLSS 模型", + [UI_MENU_DLSS_PRESET_DESC] = "运行哪个 DLSS 模型。自动按 NVIDIA 对各模式的建议:DLAA、质量和平衡用 K,性能用 M,超级性能用 L。" + "J:移动角色身后的拖影略少,闪烁略多。K:所有模式都用最清晰的模型。立即生效。", + [UI_MENU_DLSS_PRESET_DEFAULT] = "自动", [UI_MENU_DLSS_PRESET_K] = "K(最清晰)", + [UI_MENU_DLSS_PRESET_J] = "J(拖影更少)", + [UI_MENU_DLSS_CHARS] = "DLSS 角色拖影", + [UI_MENU_DLSS_CHARS_DESC] = "减少移动中的角色、动物及其手持物品身后的淡淡拖影:DLSS 在这些位置少用之前的帧。越强拖影越少," + "这些边缘也略欠平滑。立即生效。", + [UI_MENU_DLSS_CHARS_OFF] = "关", [UI_MENU_DLSS_CHARS_LIGHT] = "弱", [UI_MENU_DLSS_CHARS_MEDIUM] = "中", + [UI_MENU_DLSS_CHARS_FULL] = "强", + [UI_MENU_FXAA] = "游戏抗锯齿(FXAA)", + [UI_MENU_FXAA_DESC] = "游戏自身的 FXAA 边缘平滑。会让细节略微模糊;开启 DLAA 时不需要。", + [UI_MENU_TAB_CHEATS] = "金手指", + [UI_MENU_CHEATS_NONE] = "此游戏版本没有金手指。模组的金手指是它的 cheats 文件夹中以游戏构建命名的 Atmosphere " + "金手指文件:mods\\<模组>\\cheats\\%s。重启游戏后才会读取。对于修改游戏代码的金手指," + "游戏会在启动时自行重新构建(几分钟)。", + [UI_MENU_CHEATS_MOD] = "%s:%d 个金手指", + [UI_MENU_CHEAT_CODE_DESC] = "修改游戏代码。立即开关。", + [UI_MENU_CHEAT_KEY_DESC] = "按住 %s 时修改游戏代码。立即开关。", + [UI_MENU_CHEAT_DATA_DESC] = "开启时每帧写入游戏内存。", + [UI_MENU_CHEAT_MASTER] = "始终开启", + [UI_MENU_CHEAT_MASTER_DESC] = "文件的主代码:始终开启。", + [UI_MENU_CHEAT_RESTORE] = "无需使用", + [UI_MENU_CHEAT_RESTORE_DESC] = "它只会写回游戏原本的代码。关闭金手指时已经会这样做。", + [UI_MENU_CHEAT_REBUILD] = "重新构建后", + [UI_MENU_CHEAT_REBUILD_DESC] = "此金手指修改游戏代码,并且是在游戏构建后新增或修改的。游戏会在下次启动时把它构建进去(几分钟),也可以使用“立即重新构建游戏”。", + [UI_MENU_CHEAT_UNSUPPORTED] = "不支持", + [UI_MENU_CHEAT_UNSUPPORTED_DESC] = "此金手指不能用于此游戏版本:", + [UI_MENU_CHEATS_REBUILD] = "立即重新构建游戏", + [UI_MENU_CHEATS_REBUILD_DESC] = "有些金手指会修改游戏代码,但尚未构建进游戏:它们是新增或修改的,或上次重新构建失败或被取消。将关闭游戏;安装程序会把它们构建进去(几分钟)并重新启动游戏。上次存档之后的进度会丢失。", + [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "此模组修改游戏代码(exefs)的方式本移植无法应用,因此整个模组保持关闭。%s", + [UI_MENU_MODS_EXEFS_OTHER] = "它的代码补丁(exefs)是为其他游戏版本制作的。", + [UI_MENU_MODS_EXEFS_ON] = "正在使用的代码补丁:%d 个文件。", + [UI_MENU_MODS_REPLACES_BUILTIN] = "正在使用的代码补丁:%d 个文件。它们取代本移植自带的补丁 %s,此模组开启时该补丁保持关闭。", }; #define SWKBD_ONLY(name, ...) static const char *const name[UI_TEXT_COUNT] = {SWKBD(__VA_ARGS__)} @@ -571,19 +2010,18 @@ SWKBD_ONLY(text_es, "Introducir texto", "Introduce un nombre", "Caballo", "Acept "Mando: A = Aceptar, B = Cancelar"); SWKBD_ONLY(text_nl, "Tekstinvoer", "Voer een naam in", "Paard", "OK", "Annuleren", "Controller: A = OK, B = Annuleren"); SWKBD_ONLY(text_pt, "Inserir texto", "Introduz um nome", "Cavalo", "OK", "Cancelar", "Comando: A = OK, B = Cancelar"); -SWKBD_ONLY(text_zh_hans, "文字输入", "请输入名字", "马儿", "确定", "取消", "手柄:A 确定,B 取消"); SWKBD_ONLY(text_zh_hant, "文字輸入", "請輸入名字", "馬兒", "確定", "取消", "手把:A 確定,B 取消"); SWKBD_ONLY(text_ko, "문자 입력", "이름을 입력하세요", "말", "확인", "취소", "컨트롤러: A 확인, B 취소"); static const Row rows[] = { {"en", text_en}, {"ja", text_ja}, {"fr", text_fr}, {"de", text_de}, {"it", text_it}, {"es", text_es}, {"nl", text_nl}, {"pt", text_pt}, {"ru", text_ru}, {"zh-Hans", text_zh_hans}, {"zh-Hant", text_zh_hant}, - {"ko", text_ko}, + {"ko", text_ko}, {"pt-BR", text_pt_br}, }; const char *ui_text_lang(const char *code) { static const struct { const char *code, *lang; } map[] = { - {"en-US", "en"}, {"en-GB", "en"}, {"fr-CA", "fr"}, {"es-419", "es"}, {"pt-BR", "pt"}, + {"en-US", "en"}, {"en-GB", "en"}, {"fr-CA", "fr"}, {"es-419", "es"}, {"zh-CN", "zh-Hans"}, {"zh-TW", "zh-Hant"}, }; for (size_t i = 0; i < sizeof map / sizeof *map; i++) @@ -591,6 +2029,22 @@ const char *ui_text_lang(const char *code) { return code; } +// Every game language: the Mals files EUen/USen, USfr/EUfr, USes/EUes ship in both builds; USpt only from 1.4.3. +int ui_text_game_langs(int game_version, UiGameLang *out, int max) { + static const struct { const char *code; UiTextId name; int since; } all[] = { + {"en-US", UI_MENU_LANGNAME_EN, 0}, {"en-GB", UI_MENU_LANGNAME_EN_GB, 0}, {"ja", UI_MENU_LANGNAME_JA, 0}, + {"fr", UI_MENU_LANGNAME_FR, 0}, {"fr-CA", UI_MENU_LANGNAME_FR_CA, 0}, {"de", UI_MENU_LANGNAME_DE, 0}, + {"it", UI_MENU_LANGNAME_IT, 0}, {"es", UI_MENU_LANGNAME_ES, 0}, {"es-419", UI_MENU_LANGNAME_ES_419, 0}, + {"pt-BR", UI_MENU_LANGNAME_PT_BR, 143}, {"nl", UI_MENU_LANGNAME_NL, 0}, {"ru", UI_MENU_LANGNAME_RU, 0}, + {"zh-Hans", UI_MENU_LANGNAME_ZH_HANS, 0}, {"zh-Hant", UI_MENU_LANGNAME_ZH_HANT, 0}, + {"ko", UI_MENU_LANGNAME_KO, 0}, + }; + int n = 0; + for (size_t i = 0; i < sizeof all / sizeof *all && n < max; i++) + if (game_version >= all[i].since) out[n++] = (UiGameLang){all[i].code, all[i].name}; + return n; +} + static const Row *find_row(const char *lang) { for (size_t i = 0; i < sizeof rows / sizeof *rows; i++) if (!strcmp(rows[i].lang, lang)) return &rows[i]; @@ -609,14 +2063,32 @@ const char *ui_text_in(const char *lang, UiTextId id) { const char *ui_text(UiTextId id) { return ui_text_in(ui_text_lang(ov_language()), id); } -// The menu language, re-read when cfg_version changes. TOTK_MENU_LANG "ru" or "en"; anything else (unset included) -// follows the system UI language: Russian for "ru", "ru-RU", ..., else English. +// A language's own strings, for the font that draws them: the merged system font must cover the whole menu of the +// language, not only its name (ui/menu.cpp fonts_up). +void ui_text_visit(const char *lang, void (*visit)(void *ctx, const char *text), void *ctx) { + const Row *r = find_row(lang); + if (!r) return; + for (int id = 0; id < (int)UI_TEXT_COUNT; id++) + if (r->text[id] && *r->text[id]) visit(ctx, r->text[id]); +} + +// The menu language, re-read when cfg_version changes. TOTK_MENU_LANG names a catalog language ("en", "ru", "pt-BR", +// "zh-Hans"); unset, it follows the system UI language, through the same code map the game language uses ("zh-CN" -> +// "zh-Hans"). Windows names a region with it ("ru-RU"), so a tag without menu text tries its language alone ("ru"). A +// language whose row has no menu text (only the software keyboard does) shows English, so other Windows locales keep the +// English menu. static const char *resolve_menu_lang(void) { const char *v = cfg_value("TOTK_MENU_LANG"); - if (v && !strcmp(v, "ru")) return "ru"; - if (v && !strcmp(v, "en")) return "en"; const char *sys = plat_ui_language(); - return sys && (!strcmp(sys, "ru") || !strncmp(sys, "ru-", 3)) ? "ru" : "en"; + const char *lang = v && *v ? ui_text_lang(v) : sys && *sys ? ui_text_lang(sys) : "en"; + if (ui_text_defined(lang, UI_MENU_TITLE)) return lang; + static char base[16]; // menu_text calls this on one thread at a time, on a cfg_version change + size_t n = strcspn(lang, "-"); + if (n < sizeof base && lang[n]) { + memcpy(base, lang, n), base[n] = 0; + if (ui_text_defined(base, UI_MENU_TITLE)) return find_row(base)->lang; + } + return "en"; } const char *menu_text(UiTextId id) { diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 1decc917..998b558b 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -11,11 +11,12 @@ typedef enum { UI_SWKBD_CANCEL, UI_SWKBD_PAD_HINT, // how a controller answers - // Menu: window, tabs, bottom row. UI_MENU_FIRST..UI_MENU_LAST_ID carry English and Russian texts. + // Menu: window, tabs, bottom row. UI_MENU_FIRST..UI_MENU_LAST_ID carry English, Russian, Brazilian Portuguese and + // Simplified Chinese texts. UI_MENU_FIRST, UI_MENU_TITLE = UI_MENU_FIRST, - UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND, UI_MENU_TAB_CONTROLS, UI_MENU_TAB_MENU, - UI_MENU_TAB_ADVANCED, + UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_MODS, UI_MENU_TAB_SOUND, UI_MENU_TAB_CONTROLS, UI_MENU_TAB_AMIIBO, + UI_MENU_TAB_MENU, UI_MENU_TAB_ADVANCED, UI_MENU_CONTINUE, UI_MENU_QUIT, UI_MENU_ON, UI_MENU_OFF, UI_MENU_AUTO, UI_MENU_DEFAULT_MARK, // "default" beside the default value UI_MENU_AFTER_RESTART, // note on a setting that is read at start @@ -37,6 +38,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_ANISO, UI_MENU_ANISO_DESC, UI_MENU_ANISO_GAME, UI_MENU_LOD_DETAIL, UI_MENU_LOD_DETAIL_DESC, UI_MENU_LOD_STEADY, UI_MENU_LOD_STEADY_DESC, UI_MENU_SHARPNESS, UI_MENU_SHARPNESS_DESC, @@ -45,6 +47,17 @@ typedef enum { UI_MENU_COLOR_CUSTOM, UI_MENU_COLOR_BRIGHTNESS, UI_MENU_COLOR_BRIGHTNESS_DESC, UI_MENU_COLOR_CONTRAST, UI_MENU_COLOR_CONTRAST_DESC, UI_MENU_COLOR_SATURATION, UI_MENU_COLOR_SATURATION_DESC, UI_MENU_COLOR_GAMMA, UI_MENU_COLOR_GAMMA_DESC, + UI_MENU_HDR_GROUP, // group heading over the HDR rows + UI_MENU_HDR, UI_MENU_HDR_DESC, UI_MENU_HDR_PEAK, UI_MENU_HDR_PEAK_DESC, UI_MENU_HDR_GAME, UI_MENU_HDR_GAME_DESC, + UI_MENU_HDR_UI, UI_MENU_HDR_UI_DESC, UI_MENU_HDR_HUE, UI_MENU_HDR_HUE_DESC, + UI_MENU_SHADOW_SCALE, UI_MENU_SHADOW_SCALE_DESC, + UI_MENU_SHADOW_RANGE, UI_MENU_SHADOW_RANGE_DESC, + UI_MENU_CONTACT_SHADOWS, UI_MENU_CONTACT_SHADOWS_DESC, + UI_MENU_PCSS, UI_MENU_PCSS_DESC, + UI_MENU_DLAA, UI_MENU_DLAA_DESC, UI_MENU_DLSS, UI_MENU_DLSS_DESC, UI_MENU_DLSS_DLAA, UI_MENU_DLSS_QUALITY, + UI_MENU_DLSS_BALANCED, UI_MENU_DLSS_PERFORMANCE, UI_MENU_DLSS_ULTRA, UI_MENU_DLSS_PRESET, UI_MENU_DLSS_PRESET_DESC, + UI_MENU_DLSS_PRESET_DEFAULT, UI_MENU_DLSS_PRESET_K, UI_MENU_DLSS_PRESET_J, UI_MENU_DLSS_CHARS, UI_MENU_DLSS_CHARS_DESC, + UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM, UI_MENU_DLSS_CHARS_FULL, UI_MENU_FXAA, UI_MENU_FXAA_DESC, // Game UI_MENU_FPS_LIMIT, UI_MENU_FPS_LIMIT_DESC, UI_MENU_FPS_30, UI_MENU_FPS_40, UI_MENU_FPS_45, UI_MENU_FPS_60, UI_MENU_FPS_90, @@ -57,8 +70,28 @@ typedef enum { UI_MENU_OPERATION_MODE, UI_MENU_OPERATION_MODE_DESC, UI_MENU_MODE_DOCKED, UI_MENU_MODE_HANDHELD, UI_MENU_ASSUME_FOCUS, UI_MENU_ASSUME_FOCUS_DESC, + // Mods: the port's own patches and the player's mods folder (mods.c) + UI_MENU_MODS_BUILTIN, UI_MENU_MODS_INSTALLED, + UI_MENU_MODS_NONE, // no mod folder yet: where to unpack one + UI_MENU_MODS_OPEN, UI_MENU_MODS_OPEN_DESC, + UI_MENU_MODS_ROW_DESC, // printf: files it replaces, files it adds (as of this start) + UI_MENU_MODS_OVERRIDDEN, // printf: files a higher mod serves instead, that mod's name + UI_MENU_MODS_MOVE_HINT, + UI_MENU_MODS_EMPTY, UI_MENU_MODS_SUBSDK, UI_MENU_MODS_EXEFS_LATER, UI_MENU_MODS_BAD_NAME, + UI_MENU_MODS_EXEFS_UNSUPPORTED, // printf: the file and why + UI_MENU_MODS_EXEFS_OTHER, + UI_MENU_MODS_EXEFS_ON, // printf: its ExeFS patch files in use + UI_MENU_MODS_REPLACES_BUILTIN, // printf: its ExeFS patch files in use, the port's own patch sets they turn off + UI_MENU_MODS_OTHER_VERSION, // printf: files made for another game version, left out + UI_MENU_MODS_UNUSED, // under the switch of a mod the port cannot use + UI_MENU_MODS_CHEATS, // printf: its cheats as read at start + UI_MENU_MODS_CHEATS_FILE, // printf: <build id>.txt, when none were read + UI_MENU_MODS_FPS60, UI_MENU_MODS_FPS60_DESC, UI_MENU_MODS_RES, UI_MENU_MODS_RES_DESC, + UI_MENU_MODS_QUICKMENU, UI_MENU_MODS_QUICKMENU_DESC, + // Sound UI_MENU_VOLUME, UI_MENU_VOLUME_DESC, + UI_MENU_AUDIO_CHANNELS, UI_MENU_AUDIO_CHANNELS_DESC, UI_MENU_CHANNELS_AUTO, UI_MENU_CHANNELS_STEREO, UI_MENU_CHANNELS_51, // Controls UI_MENU_MOTION, UI_MENU_MOTION_DESC, @@ -66,10 +99,17 @@ typedef enum { UI_MENU_RUMBLE_CURVE, UI_MENU_RUMBLE_CURVE_DESC, UI_MENU_RUMBLE_TRIGGERS, UI_MENU_RUMBLE_TRIGGERS_DESC, UI_MENU_PAD_LAYOUT, UI_MENU_PAD_LAYOUT_DESC, UI_MENU_PAD_LAYOUT_AUTO, UI_MENU_PAD_LAYOUT_NINTENDO, - UI_MENU_PAD_LAYOUT_XBOX, UI_MENU_PAD_LAYOUT_XBOX_PROMPTS, UI_MENU_PAD_LAYOUT_IN_USE, - UI_MENU_PAD_LAYOUT_PROMPTS_MISSING, UI_MENU_PAD_LAYOUT_PROMPTS_RESTART, - UI_MENU_STICK_DEADZONE, UI_MENU_STICK_DEADZONE_DESC, + UI_MENU_PAD_LAYOUT_XBOX, UI_MENU_PAD_LAYOUT_IN_USE, + UI_MENU_PROMPTS, UI_MENU_PROMPTS_DESC, UI_MENU_PROMPTS_NINTENDO, UI_MENU_PROMPTS_XBOX, + // the notes under the prompts: their files are missing or damaged, they change after restart, or why they are off + UI_MENU_PROMPTS_MISSING, UI_MENU_PROMPTS_FAILED, UI_MENU_PROMPTS_RESTART, UI_MENU_PROMPTS_OFF_SHAPE, + UI_MENU_PROMPTS_OFF_MODS, UI_MENU_PROMPTS_OFF_PATCHES, + // "Restart now" under the prompts while they change after restart, and its confirmation (Cancel: UI_MENU_RESET_NO) + UI_MENU_RESTART, UI_MENU_RESTART_DESC, UI_MENU_RESTART_CONFIRM, UI_MENU_RESTART_YES, UI_MENU_RESTART_SAVING, + UI_MENU_GROUP_STICKS, UI_MENU_LSTICK_DEADZONE, UI_MENU_RSTICK_DEADZONE, UI_MENU_STICK_DEADZONE_DESC, + UI_MENU_STICK_DEADZONE_NOW, UI_MENU_STICK_SENS, UI_MENU_STICK_SENS_DESC, UI_MENU_MOUSE_SENS, UI_MENU_MOUSE_SENS_DESC, + UI_MENU_WALK_KEY, UI_MENU_WALK_KEY_DESC, UI_MENU_WALK_KEY_TAKEN, UI_MENU_WALK_SCALE, UI_MENU_WALK_SCALE_DESC, // Controls tab: the remapping rows (ui/binds_page.cpp) UI_BINDS_HELP, UI_BINDS_COL_KEY1, UI_BINDS_COL_KEY2, UI_BINDS_COL_PAD, @@ -101,13 +141,26 @@ typedef enum { UI_PAD_ZR, UI_PAD_LPADDLE1, UI_PAD_RPADDLE1, UI_PAD_LPADDLE2, UI_PAD_RPADDLE2, UI_PAD_CROSS, UI_PAD_CIRCLE, UI_PAD_SQUARE, UI_PAD_TRIANGLE, + // Amiibo tab (ui/amiibo_page.cpp): the dump folder and its files, the emulated NFC reader and its state, the + // amiibo on it and the last action + UI_MENU_AMIIBO_DIR, UI_MENU_AMIIBO_OPEN, UI_MENU_AMIIBO_DEFAULT_DIR, UI_MENU_AMIIBO_REFRESH, UI_MENU_AMIIBO_PICK, + UI_MENU_AMIIBO_NO_DIR, UI_MENU_AMIIBO_READER, UI_MENU_AMIIBO_STATE, UI_MENU_AMIIBO_STATE_IDLE, + UI_MENU_AMIIBO_STATE_SEARCHING, UI_MENU_AMIIBO_STATE_FOUND, UI_MENU_AMIIBO_STATE_MOUNTED, + UI_MENU_AMIIBO_STATE_OFF, UI_MENU_AMIIBO_FILE, UI_MENU_AMIIBO_NO_FILE, UI_MENU_AMIIBO_TAP, UI_MENU_AMIIBO_REMOVE, + UI_MENU_AMIIBO_AUTO_REMOVE, UI_MENU_AMIIBO_HOLD, UI_MENU_AMIIBO_SECONDS, UI_MENU_AMIIBO_TAG, + UI_MENU_AMIIBO_NO_TAG, UI_MENU_AMIIBO_TAG_FILE, UI_MENU_AMIIBO_TAG_ID, UI_MENU_AMIIBO_TAG_UID, + UI_MENU_AMIIBO_READY, UI_MENU_AMIIBO_TAPPED, UI_MENU_AMIIBO_LOAD_FAILED, UI_MENU_AMIIBO_REMOVED, + UI_MENU_AMIIBO_STATUS, + // Menu tab UI_MENU_MENU_LANG, UI_MENU_MENU_LANG_DESC, UI_MENU_MENU_LANG_AUTO, + UI_MENU_DISCORD_RPC, UI_MENU_DISCORD_RPC_DESC, // Language names, each in its own language and the same in every row (game language and menu language lists) UI_MENU_LANGNAME_EN, UI_MENU_LANGNAME_JA, UI_MENU_LANGNAME_FR, UI_MENU_LANGNAME_DE, UI_MENU_LANGNAME_IT, UI_MENU_LANGNAME_ES, UI_MENU_LANGNAME_NL, UI_MENU_LANGNAME_RU, UI_MENU_LANGNAME_ZH_HANS, - UI_MENU_LANGNAME_ZH_HANT, UI_MENU_LANGNAME_KO, + UI_MENU_LANGNAME_ZH_HANT, UI_MENU_LANGNAME_KO, UI_MENU_LANGNAME_PT_BR, UI_MENU_LANGNAME_EN_GB, + UI_MENU_LANGNAME_FR_CA, UI_MENU_LANGNAME_ES_419, // Advanced UI_MENU_ADV_WARNING, @@ -147,6 +200,7 @@ typedef enum { UI_MENU_TRACE, UI_MENU_TRACE_DESC, UI_MENU_FLIGHT, UI_MENU_FLIGHT_DESC, UI_MENU_FLIGHT_SPIKE_MS, UI_MENU_FLIGHT_SPIKE_MS_DESC, + UI_MENU_PASS_CRUMBS, UI_MENU_PASS_CRUMBS_DESC, UI_MENU_INPUT_RECORD, UI_MENU_INPUT_RECORD_DESC, UI_MENU_OPEN_LOG, UI_MENU_OPEN_LOG_DESC, UI_MENU_COPY_INFO, UI_MENU_COPY_INFO_DESC, UI_MENU_COPY_INFO_DONE, @@ -155,6 +209,8 @@ typedef enum { // Performance overlay UI_MENU_OV_FPS, UI_MENU_OV_FRAME_MS, UI_MENU_OV_GAME_PCT, UI_MENU_OV_MAX_MS, + UI_MENU_OV_FPS_LABEL, UI_MENU_OV_FRAME_LABEL, UI_MENU_OV_FMT_FPS, // the frame rows: their labels, the rate + UI_MENU_OV_LOW1, UI_MENU_OV_LOW01, // printf: the fps of the slowest 1% / 0.1% of the frames, no unit (the overlay column stays narrow) UI_MENU_OV_GPU, UI_MENU_OV_CPU, UI_MENU_OV_RAM, UI_MENU_OV_VRAM, UI_MENU_OV_TEMP, UI_MENU_OV_POWER, UI_MENU_OV_CLOCK, UI_MENU_OV_NONE, UI_MENU_OV_REPLAY, UI_MENU_OV_GPU_TIME, UI_MENU_OV_PIPE_WAIT, UI_MENU_OV_SHADER_WAIT, UI_MENU_OV_TEX_UPLOADS, @@ -167,7 +223,16 @@ typedef enum { UI_MENU_OV_FMT_SCENE, // "%ux%u, %.0f%%": the scene and its share of the game image's width UI_MENU_OV_FMT_FSR, // "AMD FSR 1, sharpness %d%%" UI_MENU_OV_SCALE_NONE, UI_MENU_OV_SCALE_BILINEAR, - UI_MENU_LAST_ID = UI_MENU_OV_SCALE_BILINEAR, + UI_MENU_OV_SCALE_DOWN, // FSR is selected but the image is larger than the window: FSR only enlarges + // the Cheats tab (cheats.c): the file, the kinds of cheat and why one cannot be switched + UI_MENU_TAB_CHEATS, + UI_MENU_CHEATS_NONE, // "%s": <build id>.txt + UI_MENU_CHEATS_MOD, // "%s": the mod, "%d": its cheats + UI_MENU_CHEATS_REBUILD, UI_MENU_CHEATS_REBUILD_DESC, // restart into the rebuild for unbuilt code cheats + UI_MENU_CHEAT_CODE_DESC, UI_MENU_CHEAT_KEY_DESC /* "%s": the buttons */, UI_MENU_CHEAT_DATA_DESC, + UI_MENU_CHEAT_MASTER, UI_MENU_CHEAT_MASTER_DESC, UI_MENU_CHEAT_RESTORE, UI_MENU_CHEAT_RESTORE_DESC, + UI_MENU_CHEAT_REBUILD, UI_MENU_CHEAT_REBUILD_DESC, UI_MENU_CHEAT_UNSUPPORTED, UI_MENU_CHEAT_UNSUPPORTED_DESC, + UI_MENU_LAST_ID = UI_MENU_CHEAT_UNSUPPORTED_DESC, UI_TEXT_COUNT } UiTextId; @@ -179,13 +244,23 @@ enum { UI_DESC_BYTES = 2048 }; // UTF-8 text for id in the language of ov_language(); English when the catalog has no row for that language. const char *ui_text(UiTextId id); // The same for an explicit catalog language ("en", "ja", "fr", "de", "it", "es", "nl", "pt", "ru", "zh-Hans", -// "zh-Hant", "ko"); an empty entry gives the English text. +// "zh-Hant", "ko", "pt-BR"); an empty entry gives the English text. const char *ui_text_in(const char *lang, UiTextId id); +// Calls visit(ctx, text) for every string the row of lang has its own text for (an id that falls back to English is not +// visited): the font builder covers the glyphs of the text a language shows (ui/menu.cpp). +void ui_text_visit(const char *lang, void (*visit)(void *ctx, const char *text), void *ctx); // 1 when the row of lang has its own text for id (no fallback to English), for the probe. int ui_text_defined(const char *lang, UiTextId id); // The catalog language for a Switch language code ("en-GB" -> "en", "zh-CN" -> "zh-Hans", ...). const char *ui_text_lang(const char *switch_code); -// Menu text: ui_text_in(menu language, id). The menu language is TOTK_MENU_LANG ("en" / "ru"); unset, Russian when the -// system UI language is Russian, else English. Follows a live change of the setting. +// The game languages the F1 menu offers for a game build (rt_game_version): one entry per Mals language file the build +// ships, each with the Switch code TOTK_LANG takes and the id of its name. 1.4.3 adds Brazilian Portuguese (USpt). +// Fills out (room for max entries) and returns the count. +typedef struct { const char *code; UiTextId name; } UiGameLang; +int ui_text_game_langs(int game_version, UiGameLang *out, int max); + +// Menu text: ui_text_in(menu language, id). The menu language is TOTK_MENU_LANG ("en", "ru", "pt-BR", "zh-Hans"); +// unset, the system UI language when the catalog has menu text for it (English, Russian, Brazilian Portuguese, +// Simplified Chinese), else English. Follows a live change of the setting. const char *menu_text(UiTextId id); diff --git a/runtime/vk.h b/runtime/vk.h index 51b66104..3bc6f7ef 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -33,7 +33,10 @@ typedef struct VkCtx { int has_custom_border_color, has_eds, has_eds2, has_eds3, has_vertex_input_dynamic_state, has_conditional_rendering, has_transform_feedback, has_provoking_vertex, has_device_fault, has_memory_budget, has_robustness2, has_buffer_device_address, has_fragment_shader_interlock, has_buffer_marker, - has_astc_ldr; // textureCompressionASTC_LDR (enabled when reported) + has_nv_checkpoints, // VK_NV_device_diagnostic_checkpoints (TOTK_PASS_CRUMBS=1, vk_crumbs.c) + has_astc_ldr, // textureCompressionASTC_LDR (enabled when reported) + has_calibrated, // VK_KHR/EXT_calibrated_timestamps (GPU idle split in the perf line) + has_hdr_metadata; // VK_EXT_hdr_metadata (vk_hdr.c vk_hdr_metadata) // Feedback loops (maxwell.md 1.9, vk_exec_draw.c feedback_prepare): a draw samples a target of its open pass that // an earlier draw of the pass wrote. TOTK_FEEDBACK=0: VK_FEEDBACK_OFF (sampled in GENERAL without a barrier, the // old behaviour); VK_FEEDBACK_LAYOUT with VK_EXT_attachment_feedback_loop_layout and dynamicRenderingLocalRead @@ -85,6 +88,45 @@ void vk_crumbs_release(void); void vk_crumb(VkCommandBuffer cb, int kind, uint64_t n); void vk_crumbs_report(void); // logs the two codes and what they name +// ---- pass marks (vk_crumbs.c): the replay (vk_exec.c vk_exec_pass_begin / vk_exec_pass_end, vk_exec_draw.c +// op_dispatch) marks where each render pass and dispatch starts and where it ends, so that a device loss names the +// pass the GPU was in. A mark is a number; a ring of VK_PASS_MARKS records keeps what it was. With +// VK_AMD_buffer_marker the GPU writes the number of the last mark it started (TOP_OF_PIPE, before vkCmdBeginRendering +// or the dispatch) and of the last it finished (BOTTOM_OF_PIPE, after vkCmdEndRendering or the dispatch) into the +// breadcrumb buffer; with VK_NV_device_diagnostic_checkpoints the same two points are checkpoints, read with +// vkGetQueueCheckpointData(2)NV after the loss. TOTK_PASS_CRUMBS: unset, AMD buffer marks only; 1, also NVIDIA +// checkpoints (the extension is enabled at device creation); 0, none. +#define VK_PASS_MARKS 4096 // power of two +enum { VK_MARK_PASS = 1, VK_MARK_DISPATCH }; +typedef struct { + uint32_t id; // the mark this record belongs to (0: never written) + uint8_t kind; // VK_MARK_* + uint8_t ncolor; // colour attachments + uint32_t step; // index of the pass, or of the dispatch, within its submission, from 0 + uint32_t w, h, d; // render area and layers; a dispatch: group counts (0 0 0: indirect) + uint32_t draws; // draws recorded in the pass; UINT32_MAX until its end is recorded + uint64_t fence; // the submission: the timeline value it signals + VkFormat color[8], depth; // attachment formats (VK_FORMAT_UNDEFINED: none) + const GfxShader *first[2], *last[2];// vertex and fragment shader of the first and last draw; a dispatch: first[0] +} VkPassMark; +// Pass marks wanted and possible (TOTK_PASS_CRUMBS and the extensions): set by vk_crumbs_init. +extern int vk_pass_marks_on; +// vk_device.c create_device, before vkCreateDevice: 1 when TOTK_PASS_CRUMBS asks for NVIDIA checkpoints. +int vk_pass_marks_want_nv(void); +// The next mark: records its start into cb (through the recorder thread when it owns cb) and returns its ring record, +// id, kind and fence set, for the caller to fill. NULL when pass marks are off. Replay thread only. +VkPassMark *vk_pass_mark_begin(VkCommandBuffer cb, int kind, uint64_t fence); +void vk_pass_mark_end(VkCommandBuffer cb, const VkPassMark *m); // records the end of mark m into cb +// The recorder thread's side of both (vk_rec.c): the marker and checkpoint commands themselves. +void vk_pass_mark_cmd(VkCommandBuffer cb, uint32_t id, int end); +// AMD only: the last mark the GPU started and the last it finished (0: none); 0 without buffer marks. +int vk_pass_marks_read(uint32_t *started, uint32_t *finished); +const VkPassMark *vk_pass_mark_record(uint32_t id); // the ring record of mark id; NULL once a later mark took its slot +// The report's text for a started and a finished mark, three lines without a trailing newline: which marks (source: +// the extension they came from), then each with its ring record. vk_pass_marks_report logs it; the probe checks it. +void vk_pass_marks_format(const char *source, uint32_t started, uint32_t finished, char *o, size_t size); +void vk_pass_marks_report(void); // vk_device_fault_report: the AMD words and the NVIDIA checkpoints, formatted + // ---- window and present (gfx_present.c). Host threads only. // vk_present_image: blits the crop rectangle {x, y, w, h} of src (NULL, or w/h <= 0: the whole w x h image), scaled // to the swapchain extent, into the acquired swapchain image and presents it. src is in layout, which is @@ -107,6 +149,26 @@ void vk_fsr_release(void); // before the device goes away // scales that one instead, crop {0, 0, w, h}); VK_NULL_HANDLE = nothing recorded. src is in GENERAL, fmt its format. VkImage vk_color_record(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, const int crop[4]); void vk_color_release(void); // before the device goes away +// HDR output (vk_hdr.c). vk_hdr_wanted: TOTK_HDR, read once. vk_hdr_surface_format: the HDR10 surface format when wanted +// and offered (1). vk_hdr_set_active: gfx_present.c says whether the swapchain is HDR10 (vk_hdr_active). vk_hdr_encode: +// like vk_color_record, src's crop as PQ code values (RGBA16F, crop {0, 0, w, h}). vk_hdr_ui_target / vk_hdr_ui_compose: +// the in-game menu's RGBA8 target, then blended onto the swapchain image. vk_hdr_to_srgb8: 10-bit PQ pixels to sRGB bytes. +int vk_hdr_wanted(void); +void vk_hdr_off(void); // probes that read back 8-bit swapchain pixels: SDR whatever the setting +int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out); +void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc); +void vk_hdr_set_active(int on); +int vk_hdr_active(void); +VkImage vk_hdr_encode(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, const int crop[4]); +VkImageView vk_hdr_ui_target(VkCommandBuffer cb, uint32_t w, uint32_t h); +void vk_hdr_ui_compose(VkCommandBuffer cb, VkImageView dst_view, VkFormat fmt, uint32_t w, uint32_t h); +void vk_hdr_to_srgb8(const uint8_t *in, uint8_t *out, size_t px, int a2r); +void vk_hdr_release(void); // before the device goes away +// The support buffer's HDR parameters (vk_exec_draw.c support_fill, read by shaders/hdr_tonemap.frag). +void vk_hdr_support(float out[4]); +// vk_contact.c: screen-space contact shadows on the game's composed scene colour (TOTK_CONTACT_SHADOWS) +int vk_contact_strength(void); // percent, 0 = off +void vk_contact_release(void); // before the device goes away void vk_present_flip(void); // Every submission to ctx->gfx_queue (and xfer_queue when shared) takes this lock; vk_present_image and the swapchain // rebuild do. Recursive. @@ -193,9 +255,20 @@ void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size); void vk_usnap_begin(const uint64_t *handles, int n, uint64_t fence); void vk_usnap_end(uint64_t fence); int vk_usnap_find(uint64_t addr, uint64_t n, uint64_t align, VkDescriptorBufferInfo *out); +const uint8_t *vk_usnap_map(void); // a host copy of the current arena, at the offsets vk_usnap_find gives +const uint8_t *vk_usnap_bytes(uint64_t addr, uint64_t n); // the copy of [addr, addr+n) as of the submit, NULL: none void vk_usnap_drop(uint64_t addr, uint64_t n); // the replay makes the GPU write [addr, addr+n) of guest memory uint64_t vk_usnap_gen(void); // changes whenever a binding may resolve differently void vk_usnap_shutdown(void); // vk_exec_draw_shutdown: frees the arenas +// vk_mvprev.c: last frame's copy of the scene's object (c7) and bone (c5) blocks, for per-object motion vectors. +// vk_mvprev_step: the distance of the game's other copy of a vertex-stage block the snapshot also copies, 0: none. +// vk_mvprev_find: last frame's block of that object (prev, its address; how, MVPREV_*), 0: none. +enum { MVPREV_NONE, MVPREV_SELF, MVPREV_SIDE, MVPREV_NEARER }; +uint64_t vk_mvprev_step(int stage, int index, uint64_t size); +int vk_mvprev_on(void); +void vk_mvprev_want(uint32_t index_mask); // also keep these binding indices' last-frame copies (bit n: index n) // DLAA on and TOTK_DLAA_OBJMV not 0, as of the last present +int vk_mvprev_find(int index, uint64_t addr, uint64_t size, uint64_t *prev, int *how); +void vk_mvprev_frame(void); // each present, beside vk_dlaa_frame void vk_mem_mirror_begin(void); int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence); int vk_mem_mirror_pending(void); @@ -302,7 +375,14 @@ typedef struct { uint8_t fb_usage; // created with ATTACHMENT_FEEDBACK_LOOP usage (VK_FEEDBACK_LAYOUT, attachable images) uint8_t fb_learned; // a draw sampled it while it was a written attachment of the open pass: its passes // attach it in ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT (vk_exec.c vk_exec_pass_begin) + uint8_t scale; // 1, or TOTK_SHADOW_SCALE for a shadow map made larger than the game's (vk_texture.c + // shadow_scale_of): width and height above are the larger ones, the game's regions + // are multiplied by it } VkTexImage; +// The window texture the backend presents (vk_backend.c): *image and *format become the float picture the game copied +// into it, when the last draw into it was that copy and the swapchain is HDR10. +void vk_hdr_present_source(const VkTexImage *win, VkImage *image, VkFormat *format); +void vk_hdr_image_gone(VkImage image); const VkTexImage *vk_texture_get(const tex_t *t); // created and made current; NULL for BUFFER or unmapped formats // A texture slot; a slot whose texture was finalized follows the live texture at the same storage (hle_nvn.c). const VkTexImage *vk_texture_by_id(int id, view_t *view, int *has_view); @@ -321,6 +401,7 @@ uint32_t vk_texture_slots_gen(void); // its storage address is created, dropped, released or taken back into use. Unlocked; read it before the lookup it // guards (a lookup may create an entry). 0 when the slot was never registered: nothing is kept under 0. uint64_t vk_texture_slot_token(int id); +void vk_sampler_dlss_bias(float bias); // texture LOD bias added for DLSS upscaling (material samplers) uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage void vk_texture_storage_bound_locked(const VkTexImage *img); // bound as a storage image in the recording submission diff --git a/runtime/vk_astc.c b/runtime/vk_astc.c index f4acaa69..0b91e70f 100644 --- a/runtime/vk_astc.c +++ b/runtime/vk_astc.c @@ -298,9 +298,20 @@ static int create_pipeline(VkCtx *c) { } // TOTK_ASTC_BC=1: the BC3 encoder, when the device samples BC3 (every desktop GPU does); 0 leaves ASTC as RGBA8. +// Unset: on when the largest device-local heap is under 5 GiB. There RGBA8 ASTC, four times the size of BC3, fills +// video memory (a GTX 1650 4 GiB went from 10-23 to 3-12 ms of GPU time a frame, issue #87); BC3 loses some precision +// (normal maps), so GPUs with more memory keep RGBA8. static int create_bc_pipeline(VkCtx *c) { const char *e = cfg_get("TOTK_ASTC_BC"); - if (!e || !*e || *e == '0') return 0; + VkDeviceSize vram = 0; + for (uint32_t h = 0; h < c->mem_props.memoryHeapCount && h < VK_MAX_MEMORY_HEAPS; h++) + if (c->mem_props.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT && c->mem_props.memoryHeaps[h].size > vram) + vram = c->mem_props.memoryHeaps[h].size; + int on = e && *e ? *e != '0' : vram < (5ull << 30); + if (!(e && *e)) + rt_log("vk: TOTK_ASTC_BC unset: BC3 %s (largest device-local heap %llu MiB)", on ? "on" : "off", + (unsigned long long)(vram >> 20)); + if (!on) return 0; VkFormatProperties p; vkGetPhysicalDeviceFormatProperties(c->phys, VK_FORMAT_BC3_SRGB_BLOCK, &p); VkFormatFeatureFlags need = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT; diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 37566c5d..5ba6f542 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -29,6 +29,7 @@ #include "vk.h" #include "vk_exec.h" #include "platform/platform.h" +#include "vk_dlaa.h" #define WEAK __attribute__((weak)) @@ -100,6 +101,9 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin uint64_t value) { static volatile int32_t n_skipped; vk_exec_frame_log(t, frame); + vk_dlaa_frame(); + vk_mvsurvey_frame(); + vk_mvprev_frame(); vk_mem_census(frame); if (vk_dump_armed) vk_dump_present(frame); __atomic_store_n(&gfx_counters.validation_errors, (uint64_t)vk_validation_errors_total(), __ATOMIC_RELAXED); @@ -116,6 +120,7 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin } PresentJob j = {.image = img->image, .format = img->format, .w = (int)img->width, .h = (int)img->height, .frame = frame, .at = at_ns, .queued = queued, .value = value}; + vk_hdr_present_source(img, &j.image, &j.format); // TOTK_HDR: the float picture the game copied into it, same size if (crop) { j.has_crop = 1; for (int i = 0; i < 4; i++) j.crop[i] = crop[i]; @@ -163,7 +168,10 @@ static uint64_t ck_reads, ck_diff, ck_diff_unexplained, ck_diff_idle; int vk_replay_async(void) { static int on = -1; if (on < 0) { - on = cfg_bool("TOTK_REPLAY_ASYNC"); // off by default: no measured gain yet, one more frame of latency + // On by default: where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in + // the Depths went from 450-686 frames over 50 ms a minute to 10-13, issue #88); where the replay or the game + // threads bound the frame it neither gains nor loses. It adds up to a frame of latency. + on = cfg_bool("TOTK_REPLAY_ASYNC"); rt_log("vk: replay %s (TOTK_REPLAY_ASYNC=%d)", on ? "on its own thread, at most one frame behind the game" : "on the submitting thread", on); } @@ -312,6 +320,7 @@ static void replay_log(uint64_t frame, int total) { // vk_backend_shutdown. void gfx_exit_log(void) { static int32_t done; + rec_check_log(); if (gfx_replay_async() && r_started && !plat_xchg32(&done, 1)) replay_log(game_frames, 1); } diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index 43d00b0c..0c3a86d8 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -6,8 +6,7 @@ // - minStorageBufferOffsetAlignment above 4 (TOTK_VK_SSBO_ALIGN=<power of two> raises it): the translator computes // storage offsets from the guest address rounded down to that alignment (Ryujinx's unaligned storage path), and // vk_exec_draw.c ssbo_info binds the rounded-down address, so shader writes reach guest memory instead of a copy. -// - no graphics pipeline library with fast linking (TOTK_GPL=0 forces it; the default on AMD's RDNA 1/2 driver): -// vk_caps.gpl = 0, the extension stays disabled, and pipelines are built whole. +// - no graphics pipeline library with fast linking (TOTK_GPL=0 forces it): vk_caps.gpl = 0, the extension stays disabled, and pipelines are built whole. // - no 32-lane pin (no subgroupSizeControl, 32 outside min..max, or TOTK_VK_SUBGROUP_PIN=0): stages run at the // device's subgroupSize (SPIR-V 1.3 without a required size) and the translator is told that size. // TOTK_VK_SUBGROUP=<n> pins n instead of 32 where the driver allows it. @@ -46,15 +45,18 @@ void vk_caps_driver(VkPhysicalDevice p) { vk_caps.amd_sc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && strstr(drv.driverInfo, "shader compiler"); } -// TOTK_GPL=0/1 forces graphics pipeline libraries off or on. Default off on AMD's RDNA 1/2 driver branch: every boot -// that lost the device there had a cold pipeline cache and draws waiting on fast-linked pipelines, and a run with GPL -// off booted (issue #5); Savage Wind Recomp turns GPL off on RDNA 4 under RADV for a GPU hang at the title screen. +// TOTK_GPL=0 turns graphics pipeline libraries off; they are on wherever the driver has fast linking. AMD's RDNA 1/2 +// driver branch had them off for a while: its device losses at boot (issue #5) came with a cold pipeline cache, but +// their cause was GPU writes past 3D BC images (vk_exec.c, "3D BC render targets"). With that fixed, the RDNA 1/2 +// driver boots with GPL from a cold cache and fast-links without a failure, while with GPL off every new pipeline is +// built whole on the draw's path (frames of 0.5-1.2 s on an RX 6600, issue #381). Savage Wind Recomp turns GPL off on +// RDNA 4 under RADV for a GPU hang at the title screen. int vk_caps_gpl(const VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT *f, const VkPhysicalDeviceGraphicsPipelineLibraryPropertiesEXT *p) { gpl_lib = f->graphicsPipelineLibrary; gpl_fast = p->graphicsPipelineLibraryFastLinking; - const char *e = cfg_get("TOTK_GPL"); // no fixed default: it depends on the driver, see above - vk_caps.gpl = gpl_lib && gpl_fast && (e && *e ? strcmp(e, "0") != 0 : !vk_caps.amd_sc); + const char *e = cfg_get("TOTK_GPL"); + vk_caps.gpl = gpl_lib && gpl_fast && !(e && !strcmp(e, "0")); return vk_caps.gpl; } @@ -137,10 +139,8 @@ void vk_caps_init(VkCtx *c) { has_ext(ep, ne, "VK_EXT_graphics_pipeline_library"), depth_ok(p, VK_FORMAT_D16_UNORM), depth_ok(p, VK_FORMAT_D24_UNORM_S8_UINT), depth_ok(p, VK_FORMAT_X8_D24_UNORM_PACK32), depth_ok(p, VK_FORMAT_D32_SFLOAT), depth_ok(p, VK_FORMAT_D32_SFLOAT_S8_UINT)); - const char *ge = cfg_get("TOTK_GPL"); rt_log("vk caps: gpl %s fast-link %s (graphicsPipelineLibrary %u%s)", vk_caps.gpl ? "yes" : "no", - gpl_fast ? "yes" : "no", (unsigned)gpl_lib, - !gpl_lib || !gpl_fast || vk_caps.gpl ? "" : ge && *ge ? ", off by TOTK_GPL=0" : ", off on AMD's RDNA 1/2 driver"); + gpl_fast ? "yes" : "no", (unsigned)gpl_lib, !gpl_lib || !gpl_fast || vk_caps.gpl ? "" : ", off by TOTK_GPL=0"); rt_log("vk caps: fallbacks: float64 %s, storage buffers %s %u, subgroup pin %u on stages %#x, translator subgroup vs/fs/cs %u/%u/%u", vk_caps.float64 ? "native" : "softfloat64", vk_caps.sbuf_align ? "rounded down to" : "exact, alignment", (unsigned)l->minStorageBufferOffsetAlignment, want, (unsigned)vk_caps.sg_pin_stages, told[0], told[1], told[2]); diff --git a/runtime/vk_contact.c b/runtime/vk_contact.c new file mode 100644 index 00000000..74eae718 --- /dev/null +++ b/runtime/vk_contact.c @@ -0,0 +1,188 @@ +// Screen-space contact shadows (TOTK_CONTACT_SHADOWS, percent strength, 0 = off). TotK fills a full-resolution +// G-buffer whose targets include a B10G11R11 scene colour, an R32F linear depth (view distance / far) and a D32F depth +// (is_gbuffer), lights it in half resolution, and then composites the lighting into that scene colour in one pass +// whose only target is the scene colour, with a D16 depth that sorts pixels into material classes (is_composite). +// When the pass after the composite begins, or a dispatch or copy comes first, runtime/shaders/contact_shadow.comp +// marches each pixel a short way toward the sun through the linear depth and darkens the scene colour where the ray +// passes behind nearby geometry: the small shadows at feet, under stones and in grass that the shadow maps are too +// coarse for. Transparent surfaces, the sky and post-processing come after, untouched. +// +// Inputs from the game, read from the fragment stage's uniform block c4 bound in the composite pass (guest memory, +// so it may be one frame newer than the replay): the projection scales c4[7].x and c4[8].y, the far plane c4[14].y, +// and the first sun cascade's depth row c4[44].xyz, whose negated direction points from a view-space position toward +// the sun (the G-buffer shaders compare dot(c4[44].xyz, pos) + c4[44].w with the cascade map). The scene colour images +// get STORAGE usage when the setting is on (vk_texture.c create_tex). +#include "vk.h" + +#include <math.h> +#include <string.h> + +#include "cfg.h" +#include "contact_shadow_spv.h" +#include "host.h" +#include "vk_exec.h" + +static struct { + int tried, ok, strength, debug, logged; + PFN_vkCmdPushDescriptorSetKHR push; + VkDescriptorSetLayout dsl; + VkPipelineLayout layout; + VkPipeline pipe; + VkSampler sampler; + const VkTexImage *scene, *depth; // the last G-buffer's scene colour and linear depth + uint32_t w, h; + int pending; // the composite into scene ran; the march waits for the next pass + uint64_t c4; +} C; + +int vk_contact_strength(void) { + if (!C.tried) { + int s = cfg_int("TOTK_CONTACT_SHADOWS"); + C.strength = s < 0 ? 0 : s > 100 ? 100 : s; + } + return C.strength; +} + +static int init(VkCtx *c) { + C.tried = 1; + vk_contact_strength(); + if (!C.strength) return 0; + const char *e = cfg_get("TOTK_CONTACT_DEBUG"); // 1: the frame shows the contact shadow factor in grey + C.debug = e && *e && strcmp(e, "0"); + C.push = (PFN_vkCmdPushDescriptorSetKHR)vkGetDeviceProcAddr(c->device, "vkCmdPushDescriptorSetKHR"); + VkDescriptorSetLayoutBinding b[2] = { + {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}, + {.binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}}; + VkDescriptorSetLayoutCreateInfo dci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, + .bindingCount = 2, .pBindings = b}; + VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, 64}; + VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1, + .pSetLayouts = &C.dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr}; + VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_NEAREST, + .minFilter = VK_FILTER_NEAREST, .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .maxLod = 0}; + VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof contact_shadow_spv, .pCode = contact_shadow_spv}; + VkShaderModule m = VK_NULL_HANDLE; + if (!C.push || vkCreateDescriptorSetLayout(c->device, &dci, NULL, &C.dsl) != VK_SUCCESS || + vkCreatePipelineLayout(c->device, &li, NULL, &C.layout) != VK_SUCCESS || + vkCreateSampler(c->device, &si, NULL, &C.sampler) != VK_SUCCESS || + vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS) { + rt_log("vk: contact shadows off: their pipeline could not be created"); + return 0; + } + VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"}, + .layout = C.layout}; + VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &C.pipe); + vkDestroyShaderModule(c->device, m, NULL); + if (r != VK_SUCCESS) { + rt_log("vk: contact shadows off: their pipeline could not be created"); + return 0; + } + rt_log("vk: contact shadows on, strength %d%%%s", C.strength, C.debug ? " (debug view)" : ""); + return C.ok = 1; +} + +// A G-buffer pass: a B10G11R11 scene colour, an R32F linear depth and a D32F depth, all one size, at least 640 wide. +static int is_gbuffer(const VkExec *ex, const VkTexImage **scene, const VkTexImage **depth) { + *scene = *depth = NULL; + if (ex->pass_noatt || ex->pass_w < 640 || ex->depth.format != VK_FORMAT_D32_SFLOAT || ex->depth.level) return 0; + for (uint32_t i = 0; i < ex->pass_ncolor && i < 8; i++) { + const VkExecTarget *t = &ex->color[i]; + if (!t->image || t->level || t->width != ex->pass_w || t->height != ex->pass_h) continue; + if (t->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32 && !*scene) *scene = t->img; + if (t->format == VK_FORMAT_R32_SFLOAT && !*depth) *depth = t->img; + } + return *scene && *depth && ((*scene)->usage & VK_IMAGE_USAGE_STORAGE_BIT); +} + +static void mem_barrier(VkCommandBuffer cb) { + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT}; + VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &d); +} + +void vk_contact_flush(VkExec *ex) { + if (!C.pending) return; + C.pending = 0; + if (ex->in_pass) return; + const float *u = (const float *)(uintptr_t)C.c4; + float lx = -u[44 * 4 + 0], ly = -u[44 * 4 + 1], lz = -u[44 * 4 + 2], ll = sqrtf(lx * lx + ly * ly + lz * lz); + float px = u[7 * 4 + 0], py = u[8 * 4 + 1], zfar = u[14 * 4 + 1]; + if (!(ll > 1e-6f) || !(px > 0) || !(py > 0) || !(zfar > 1)) return; + VkImageView in = vk_exec_target_view(C.depth, VK_FORMAT_R32_SFLOAT, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT, + 0, 0, 1, 0); + VkImageView out = vk_exec_target_view(C.scene, VK_FORMAT_B10G11R11_UFLOAT_PACK32, VK_IMAGE_VIEW_TYPE_2D, + VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1, 0); + if (!in || !out) return; + VkCommandBuffer cb = ex->cb; + float pcv[16] = {px, py, zfar, (float)C.strength / 100.0f, + lx / ll, ly / ll, lz / ll, 0.6f, + (float)C.w, (float)C.h, 1.0f / (float)C.w, 1.0f / (float)C.h, + (float)C.debug, 0, 0, 0}; + if (!C.logged) { + C.logged = 1; + rt_log("vk: contact shadows on a %ux%u scene, sun toward %.3f %.3f %.3f (view space), far %g", C.w, C.h, + pcv[4], pcv[5], pcv[6], zfar); + } + mem_barrier(cb); // the compose passes' writes, before the march reads and writes them + VkDescriptorImageInfo ii = {C.sampler, in, VK_IMAGE_LAYOUT_GENERAL}, oi = {VK_NULL_HANDLE, out, VK_IMAGE_LAYOUT_GENERAL}; + VkWriteDescriptorSet w[2] = { + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &oi}}; + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, C.pipe); + rec_push(C.push, cb, VK_PIPELINE_BIND_POINT_COMPUTE, C.layout, 0, 2, w); + vkCmdPushConstants(cb, C.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pcv, pcv); + vkCmdDispatch(cb, (C.w + 7) / 8, (C.h + 7) / 8, 1); + mem_barrier(cb); // and what follows sees the darkened colour + ex->recorded += 6; + vk_exec_draw_new_cb(); // the game's bound compute pipeline and descriptors were replaced: bind them again +} + +// The lighting composite: the remembered scene colour as the only target, with a D16 depth of its size. +static int is_composite(const VkExec *ex) { + return C.scene && ex->pass_ncolor >= 1 && ex->color[0].img == C.scene && !ex->color[0].level && + ex->depth.format == VK_FORMAT_D16_UNORM && ex->pass_w == C.w && ex->pass_h == C.h; +} + +void vk_contact_pass_begin(VkExec *ex) { + if (!C.tried && !init(vk_ctx())) return; + if (!C.ok) return; + const VkTexImage *scene, *depth; + // The camera block: c4 of the fragment stage, 0x940 bytes. The composite binds it only after its pass begins, so + // the newest one seen at any pass start serves. + const p_bind_buf *b = &ex->ubo[1][1]; + if (b->addr && b->size == 0x940) C.c4 = b->addr; + if (is_gbuffer(ex, &scene, &depth)) { + C.pending = 0; + C.scene = scene, C.depth = depth, C.w = ex->pass_w, C.h = ex->pass_h; + return; + } + if (is_composite(ex)) { + C.pending = C.c4 != 0; + return; + } + vk_contact_flush(ex); +} + +void vk_contact_release(void) { + VkCtx *c = vk_ctx(); + if (!c || !C.tried) return; + if (C.pipe) vkDestroyPipeline(c->device, C.pipe, NULL); + if (C.sampler) vkDestroySampler(c->device, C.sampler, NULL); + if (C.layout) vkDestroyPipelineLayout(c->device, C.layout, NULL); + if (C.dsl) vkDestroyDescriptorSetLayout(c->device, C.dsl, NULL); + memset(&C, 0, sizeof C); +} diff --git a/runtime/vk_crumbs.c b/runtime/vk_crumbs.c index 74a391a2..3d34e024 100644 --- a/runtime/vk_crumbs.c +++ b/runtime/vk_crumbs.c @@ -4,19 +4,31 @@ // device loss word 0 names the newest point the GPU reached and word 1 the newest point it had finished: the hang lies // between them. A code is the crumb kind in the top 4 bits and its number in the low 28. The markers cost two small // writes per crumb, and crumbs mark submissions, upload batches, ASTC chunks and presents, not draws. +// +// Pass marks (vk.h vk_pass_mark_begin) use words 2 and 3 of the same buffer: the replay marks the start and the end of +// every render pass and dispatch, and a ring of records says what each mark was. The design follows Savage Wind +// Recomp's GPU checkpoints (refs/SavageWindRecomp runtime/nvn/vulkan/exec.c ck_mark and vk_checkpoints_report: a mark +// per pass and dispatch, numbers in a ring of 4096, text only at the device loss); the code is ours. #include <stdint.h> #include <stdio.h> +#include <string.h> +#include "cfg.h" #include "host.h" #include "vk.h" static PFN_vkCmdWriteBufferMarkerAMD write_marker; static VkBuffer buf; static VmaAllocation alloc; -static volatile uint32_t *words; +static volatile uint32_t *words; // 0, 1: crumbs; 2, 3: pass marks -void vk_crumbs_init(VkCtx *c) { - if (!c->has_buffer_marker) return; +// Pass marks (below) +int vk_pass_marks_on; +static int pm_amd; // marks into words 2 and 3 +static PFN_vkCmdSetCheckpointNV set_checkpoint; +static void pass_marks_init(VkCtx *c); + +static void buffer_init(VkCtx *c) { write_marker = (PFN_vkCmdWriteBufferMarkerAMD)vkGetDeviceProcAddr(c->device, "vkCmdWriteBufferMarkerAMD"); VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 16, .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT}; @@ -30,14 +42,20 @@ void vk_crumbs_init(VkCtx *c) { return; } words = info.pMappedData; - words[0] = words[1] = 0; + words[0] = words[1] = words[2] = words[3] = 0; rt_log("vk: GPU breadcrumbs on (VK_AMD_buffer_marker)"); } +void vk_crumbs_init(VkCtx *c) { + if (c->has_buffer_marker) buffer_init(c); + pass_marks_init(c); +} + void vk_crumbs_release(void) { VkCtx *c = vk_ctx(); if (buf && c) vmaDestroyBuffer(c->vma, buf, alloc); buf = VK_NULL_HANDLE, alloc = NULL, words = NULL, write_marker = NULL; + vk_pass_marks_on = pm_amd = 0, set_checkpoint = NULL; } void vk_crumb(VkCommandBuffer cb, int kind, uint64_t n) { @@ -65,3 +83,183 @@ void vk_crumbs_report(void) { describe(words[1], finished, sizeof finished); rt_log("vk: GPU breadcrumbs: the GPU last reached %s; everything before %s had finished", reached, finished); } + +// ---- pass marks + +// Cost per pass or dispatch: about twenty stores into the ring and two commands, one at the start and one at the end, +// each a marker write or a checkpoint. Per draw nothing, except that the branch of vk_exec_draw.c prepare_draw that +// records changed pipeline state also notes the shaders of a pass's first draw. A full barrier already precedes every +// pass and dispatch (vk_exec.c vk_exec_pass_begin, vk_exec_draw.c op_dispatch), so the end-of-pipe write adds no wait. +static VkPassMark ring[VK_PASS_MARKS]; +static uint32_t seq; // marks made; only the replay thread writes it + +// TOTK_PASS_CRUMBS: unset, buffer marks where VK_AMD_buffer_marker exists; 1, also NVIDIA checkpoints; 0, neither. +int vk_pass_marks_want_nv(void) { + const char *e = cfg_get("TOTK_PASS_CRUMBS"); + return e && *e && strcmp(e, "0") != 0; +} + +static void pass_marks_init(VkCtx *c) { + const char *e = cfg_get("TOTK_PASS_CRUMBS"); + int off = e && !strcmp(e, "0"); + set_checkpoint = c->has_nv_checkpoints + ? (PFN_vkCmdSetCheckpointNV)vkGetDeviceProcAddr(c->device, "vkCmdSetCheckpointNV") : NULL; + pm_amd = !off && write_marker && words; + vk_pass_marks_on = pm_amd || set_checkpoint; + if (vk_pass_marks_on) + rt_log("vk: pass marks on (%s%s%s; TOTK_PASS_CRUMBS=0 turns them off%s)", pm_amd ? "VK_AMD_buffer_marker" : "", + pm_amd && set_checkpoint ? " and " : "", set_checkpoint ? "VK_NV_device_diagnostic_checkpoints" : "", + set_checkpoint ? "" : ", =1 adds NVIDIA checkpoints where the driver has them"); + else + rt_log("vk: pass marks off (%s)", off ? "TOTK_PASS_CRUMBS=0" + : "no VK_AMD_buffer_marker; TOTK_PASS_CRUMBS=1 asks for NVIDIA checkpoints"); +} + +void vk_pass_mark_cmd(VkCommandBuffer cb, uint32_t id, int end) { + if (pm_amd) + write_marker(cb, end ? VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT : VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, buf, + end ? 12 : 8, id); + if (set_checkpoint) set_checkpoint(cb, (const void *)(uintptr_t)((uint64_t)id << 1 | (end != 0))); +} + +VkPassMark *vk_pass_mark_begin(VkCommandBuffer cb, int kind, uint64_t fence) { + if (!vk_pass_marks_on || !cb) return NULL; + uint32_t id = ++seq; + if (!id) id = ++seq; // 0 means no mark + VkPassMark *m = &ring[id & (VK_PASS_MARKS - 1)]; + m->id = id, m->kind = (uint8_t)kind, m->fence = fence, m->draws = UINT32_MAX; + rec_pass_mark(cb, id, 0); + return m; +} + +void vk_pass_mark_end(VkCommandBuffer cb, const VkPassMark *m) { + if (m && cb) rec_pass_mark(cb, m->id, 1); +} + +int vk_pass_marks_read(uint32_t *started, uint32_t *finished) { + if (!pm_amd) return 0; + *started = words[2], *finished = words[3]; + return 1; +} + +const VkPassMark *vk_pass_mark_record(uint32_t id) { + const VkPassMark *m = &ring[id & (VK_PASS_MARKS - 1)]; + return id && m->id == id ? m : NULL; +} + +// The first 8 bytes of the shader's input id (gfx_shader.c), the start of its stored input's name <cache>/in/<id>.in. +static unsigned long long shader_id(const GfxShader *s) { + if (!s) return 0; + const uint8_t *iid = gfx_shader_input_id(s); + unsigned long long v = 0; + for (int i = 0; i < 8; i++) v = v << 8 | iid[i]; + return v; +} + +static void describe_mark(uint32_t id, char *o, size_t size) { + const VkPassMark *m = vk_pass_mark_record(id); + if (!id) { + snprintf(o, size, "nothing yet"); + return; + } + if (!m) { + snprintf(o, size, "no record (a later mark took its slot)"); + return; + } + if (m->kind == VK_MARK_DISPATCH) { + snprintf(o, size, "dispatch %u of submission %llu: %ux%ux%u groups%s, cs %016llx", m->step, + (unsigned long long)m->fence, m->w, m->h, m->d, m->w ? "" : " (indirect)", shader_id(m->first[0])); + return; + } + size_t n = (size_t)snprintf(o, size, "pass %u of submission %llu: %ux%u, %u layer%s, colour formats", m->step, + (unsigned long long)m->fence, m->w, m->h, m->d, m->d == 1 ? "" : "s"); + for (int i = 0; i < m->ncolor && i < 8 && n < size; i++) + n += (size_t)snprintf(o + n, size - n, "%s%d", i ? "," : " ", (int)m->color[i]); + if (!m->ncolor && n < size) n += (size_t)snprintf(o + n, size - n, " none"); + if (n < size) n += (size_t)snprintf(o + n, size - n, ", depth format %d", (int)m->depth); + if (n >= size) return; + if (m->draws == UINT32_MAX) + snprintf(o + n, size - n, ", still recording; first draw vs %016llx fs %016llx", shader_id(m->first[0]), + shader_id(m->first[1])); + else if (!m->draws) + snprintf(o + n, size - n, ", no draws"); + else + snprintf(o + n, size - n, ", %u draws; first vs %016llx fs %016llx, last vs %016llx fs %016llx", m->draws, + shader_id(m->first[0]), shader_id(m->first[1]), shader_id(m->last[0]), shader_id(m->last[1])); +} + +void vk_pass_marks_format(const char *source, uint32_t started, uint32_t finished, char *o, size_t size) { + char a[640], b[640]; + describe_mark(started, a, sizeof a); + describe_mark(finished, b, sizeof b); + snprintf(o, size, + "vk: pass marks (%s, %u made): the GPU last started mark %u and last finished mark %u\n" + "vk: last started %u: %s\n" + "vk: last finished %u: %s", + source, (unsigned)seq, (unsigned)started, (unsigned)finished, (unsigned)started, a, (unsigned)finished, b); +} + +static void log_lines(char *t) { + for (char *line = t, *nl; line && *line; line = nl ? nl + 1 : NULL) { + nl = strchr(line, '\n'); + if (nl) *nl = 0; + rt_log("%s", line); + } +} + +// vkGetQueueCheckpointData2NV, else vkGetQueueCheckpointDataNV, on the graphics queue (the replay's): one entry per +// stage with the last checkpoint that stage executed. A checkpoint is the mark's id times 2, plus 1 at its end, so the +// top-of-pipe entry names the last start or end the GPU reached, and the bottom-of-pipe entry the last one before which +// everything had finished: at a start, that is the end of the mark before it. +static void nv_report(void) { + VkCtx *c = vk_ctx(); + if (!c || !set_checkpoint) return; + PFN_vkGetQueueCheckpointData2NV get2 = + (PFN_vkGetQueueCheckpointData2NV)vkGetDeviceProcAddr(c->device, "vkGetQueueCheckpointData2NV"); + PFN_vkGetQueueCheckpointDataNV get1 = + (PFN_vkGetQueueCheckpointDataNV)vkGetDeviceProcAddr(c->device, "vkGetQueueCheckpointDataNV"); + enum { MAXN = 32 }; + uint64_t stage[MAXN], marker[MAXN]; + uint32_t n = 0; + if (get2) { + VkCheckpointData2NV d[MAXN]; + get2(c->gfx_queue, &n, NULL); + if (n > MAXN) n = MAXN; + for (uint32_t i = 0; i < n; i++) d[i] = (VkCheckpointData2NV){.sType = VK_STRUCTURE_TYPE_CHECKPOINT_DATA_2_NV}; + get2(c->gfx_queue, &n, d); + for (uint32_t i = 0; i < n; i++) stage[i] = d[i].stage, marker[i] = (uint64_t)(uintptr_t)d[i].pCheckpointMarker; + } else if (get1) { + VkCheckpointDataNV d[MAXN]; + get1(c->gfx_queue, &n, NULL); + if (n > MAXN) n = MAXN; + for (uint32_t i = 0; i < n; i++) d[i] = (VkCheckpointDataNV){.sType = VK_STRUCTURE_TYPE_CHECKPOINT_DATA_NV}; + get1(c->gfx_queue, &n, d); + for (uint32_t i = 0; i < n; i++) stage[i] = d[i].stage, marker[i] = (uint64_t)(uintptr_t)d[i].pCheckpointMarker; + } else { + rt_log("vk: pass marks: no vkGetQueueCheckpointDataNV entry point"); + return; + } + uint32_t started = 0, finished = 0; + for (uint32_t i = 0; i < n; i++) { + uint32_t id = (uint32_t)(marker[i] >> 1); + int end = (int)(marker[i] & 1); + rt_log("vk: checkpoint of stage %#llx: the %s of mark %u", (unsigned long long)stage[i], end ? "end" : "start", id); + if (stage[i] == VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT) started = id; + if (stage[i] == VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT) finished = end ? id : id ? id - 1 : 0; + } + if (!n) rt_log("vk: pass marks: the driver reported no checkpoints"); + char t[2048]; + vk_pass_marks_format("VK_NV_device_diagnostic_checkpoints", started, finished, t, sizeof t); + log_lines(t); +} + +void vk_pass_marks_report(void) { + if (!vk_pass_marks_on) return; + uint32_t started, finished; + char t[2048]; + if (vk_pass_marks_read(&started, &finished)) { + vk_pass_marks_format("VK_AMD_buffer_marker", started, finished, t, sizeof t); + log_lines(t); + } + nv_report(); +} diff --git a/runtime/vk_device.c b/runtime/vk_device.c index e92e6de0..a063955e 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -10,6 +10,7 @@ #include "host.h" #include "vk.h" #include "platform/platform.h" +#include "vk_dlaa.h" static VkCtx g_vk; static int g_ready; @@ -41,6 +42,17 @@ static int layer_present(const char *name) { return found; } +static int instance_ext_present(const char *name) { + uint32_t n = 0; + vkEnumerateInstanceExtensionProperties(NULL, &n, NULL); + VkExtensionProperties *e = calloc(n ? n : 1, sizeof *e); + vkEnumerateInstanceExtensionProperties(NULL, &n, e); + int found = 0; + for (uint32_t i = 0; i < n; i++) if (!strcmp(e[i].extensionName, name)) found = 1; + free(e); + return found; +} + // Mesa's drivers (RADV on Linux and the Steam Deck) keep a shader cache of their own beside our pipeline cache, by // default in ~/.cache and up to 1 GB: about 35 MB in a minute of the title screen. A Deck's /home is small and the game // usually sits on the SD card, so unless MESA_SHADER_CACHE_DIR names a folder, it goes to mesa-cache beside the exe. @@ -56,7 +68,7 @@ static void mesa_cache_beside_exe(void) { static int create_instance(void) { mesa_cache_beside_exe(); - const char *ext[8]; + const char *ext[12]; uint32_t n_ext = (uint32_t)gfx_window_vk_extensions(ext, 7); const char *layers[] = {"VK_LAYER_KHRONOS_validation"}; int validate = cfg_bool("TOTK_VK_VALIDATION"); @@ -65,6 +77,9 @@ static int create_instance(void) { validate = 0; } if (validate) ext[n_ext++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; + if (vk_hdr_wanted() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats + ext[n_ext++] = VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME; + vk_dlaa_instance_exts(ext, &n_ext, 12); VkApplicationInfo app = {.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "TotK Recomp", .pEngineName = "TotK Recomp", .apiVersion = VK_API_VERSION_1_3}; VkInstanceCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app, @@ -209,7 +224,7 @@ static int create_device(void) { vkEnumerateDeviceExtensionProperties(g_vk.phys, NULL, &ne, NULL); VkExtensionProperties *ep = calloc(ne ? ne : 1, sizeof *ep); vkEnumerateDeviceExtensionProperties(g_vk.phys, NULL, &ne, ep); - const char *ext[28]; + const char *ext[48]; uint32_t n_ext = 0; for (size_t i = 0; i < sizeof k_required_ext / sizeof *k_required_ext; i++) ext[n_ext++] = k_required_ext[i]; @@ -246,9 +261,23 @@ static int create_device(void) { CHAIN(fbl, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT); CHAIN(lr, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_LOCAL_READ_FEATURES_KHR); } + vk_dlaa_device_exts(ep, ne, ext, &n_ext, 40); // NGX's; the eight slots after it stay for the seven below if (has_ext(ep, ne, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME)) { g_vk.has_memory_budget = 1; ext[n_ext++] = VK_EXT_MEMORY_BUDGET_EXTENSION_NAME; } + // GPU timestamps on the CPU clock (vk_sync.c): the perf line splits GPU idle time into waiting for a submission or not. + if (has_ext(ep, ne, VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 2; ext[n_ext++] = VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; } + else if (has_ext(ep, ne, VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME)) { g_vk.has_calibrated = 1; ext[n_ext++] = VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME; } + if (vk_hdr_wanted() && has_ext(ep, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME)) { // the display's peak (vk_hdr_metadata) + g_vk.has_hdr_metadata = 1; + ext[n_ext++] = VK_EXT_HDR_METADATA_EXTENSION_NAME; + } // GPU breadcrumbs for a lost device (vk_crumbs.c): AMD's drivers and RADV report it. if (has_ext(ep, ne, VK_AMD_BUFFER_MARKER_EXTENSION_NAME)) { g_vk.has_buffer_marker = 1; ext[n_ext++] = VK_AMD_BUFFER_MARKER_EXTENSION_NAME; } + // Pass marks on NVIDIA (vk_crumbs.c): checkpoints only with TOTK_PASS_CRUMBS=1, since NVIDIA documents a cost for + // the extension. NVIDIA's drivers report it, AMD's do not. + if (vk_pass_marks_want_nv() && has_ext(ep, ne, VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME)) { + g_vk.has_nv_checkpoints = 1; + ext[n_ext++] = VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME; + } // Graphics pipeline library: chained last so it can be dropped again when vk_caps_gpl declines it. void **gpl_link = NULL; if (has_ext(ep, ne, VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME) && has_ext(ep, ne, VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME)) { @@ -368,7 +397,7 @@ int vk_init(void) { plat_mutex_lock(&g_lock); if (!g_ready) { memset(&g_vk, 0, sizeof g_vk); - if (create_instance() && pick_device() && create_device()) g_ready = 1, vk_headroom_start(); + if (create_instance() && pick_device() && create_device()) g_ready = 1, vk_headroom_start(), vk_dlaa_init(); else destroy_all(); } int ok = g_ready; @@ -407,6 +436,7 @@ void vk_device_fault_report(VkResult r) { if (done) { plat_mutex_unlock(&lock); return; } done = 1; vk_crumbs_report(); + vk_pass_marks_report(); VkCtx *c = vk_ctx(); PFN_vkGetDeviceFaultInfoEXT get = c && c->has_device_fault ? (PFN_vkGetDeviceFaultInfoEXT)vkGetDeviceProcAddr(c->device, "vkGetDeviceFaultInfoEXT") @@ -477,11 +507,11 @@ int probe_vkdevice(void) { c->device_index, c->props.deviceName, VK_API_VERSION_MAJOR(v), VK_API_VERSION_MINOR(v), VK_API_VERSION_PATCH(v), c->gfx_family, c->xfer_family, c->xfer_shared ? " (shared queue)" : "", c->min_imported_host_alignment); printf("probe-vkdevice: optional: custom_border=%d eds=%d eds2=%d eds3=%d vertex_input=%d cond_render=%d xfb=%d provoking=%d " - "robustness2=%d fault=%d budget=%d bda=%d interlock=%d validation=%d\n", + "robustness2=%d fault=%d budget=%d bda=%d interlock=%d buffer_marker=%d nv_checkpoints=%d validation=%d\n", c->has_custom_border_color, c->has_eds, c->has_eds2, c->has_eds3, c->has_vertex_input_dynamic_state, c->has_conditional_rendering, c->has_transform_feedback, c->has_provoking_vertex, c->has_robustness2, c->has_device_fault, c->has_memory_budget, c->has_buffer_device_address, c->has_fragment_shader_interlock, - c->validation); + c->has_buffer_marker, c->has_nv_checkpoints, c->validation); vk_shutdown(); printf("probe-vkdevice: %s (validation errors %ld)\n", g_errors_total ? "FAILED" : "passed", g_errors_total); return g_errors_total ? 1 : 0; diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c new file mode 100644 index 00000000..db375da9 --- /dev/null +++ b/runtime/vk_dlaa.c @@ -0,0 +1,1271 @@ +// DLAA: NVIDIA DLSS at the scene's own size (TOTK_DLAA, totk.ini dlaa). NGX is loaded at run time from the driver's +// NGX core: on Windows _nvngx.dll, found through HKLM\SOFTWARE\NVIDIA Corporation\Global\NGXCore; on Linux +// libnvidia-ngx.so.1, which the NVIDIA driver installs on the library path. NGX loads the DLSS model from the folder of +// the executable: nvngx_dlss.dll on Windows, libnvidia-ngx-dlss.so.<version> on Linux. No NVIDIA SDK code or header is +// compiled in: the few NGX types used are declared here from the SDK's documented ABI, the way the Metroid Dread +// recomp's dlss.c reaches the core (read for behaviour only). +// +// The core's parameter object is a C++ class whose virtual functions are called through its vtable: the Set overloads +// in slots 0-7 and the Get overloads in 8-15. MSVC (the Windows core) lays out each group of overloads in reverse +// declaration order, the Itanium C++ ABI (the Linux core, built with GCC) in declaration order. vtable_ok checks the +// layout with a set-then-get round trip before anything relies on it. +// +// Where DLAA runs in the frame, the jitter and the motion vectors: see the sections below. +#ifdef _WIN32 +#include <windows.h> +#endif +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wchar.h> + +#include "cfg.h" +#include "host.h" +#include "platform/platform.h" +#include "vk.h" +#include "vk_dlaa.h" +#include "vk_exec.h" +#include "vk_rec.h" +#include "dlaa_mv_spv.h" + +typedef int NgxResult; // NVSDK_NGX_Result: 1 success, 0xBAD00000 | n failures +#define NGX_OK(r) (((unsigned)(r) & 0xFFF00000u) != 0xBAD00000u) +#define NGX_VERSION_API 0x15 +#define NGX_FEATURE_SUPERSAMPLING 1 +#define NGX_PERFQ_DLAA 5 // NVSDK_NGX_PerfQuality_Value: 0 performance, 1 balanced, 2 quality, 3 ultra performance +typedef struct NgxParam NgxParam; +typedef struct NgxHandle NgxHandle; +typedef struct { void *slot[17]; } NgxParamVtbl; +struct NgxParam { const NgxParamVtbl *vt; }; +typedef struct { // NVSDK_NGX_ImageViewInfo_VK + VkImageView view; + VkImage image; + VkImageSubresourceRange range; + VkFormat format; + unsigned width, height; +} NgxImageView; +typedef struct { // NVSDK_NGX_Resource_VK + union { NgxImageView img; struct { VkBuffer buffer; unsigned bytes; } buf; } r; + int type; // 0 image view, 1 buffer + _Bool rw; +} NgxResourceVK; + +typedef NgxResult (*PFN_Init)(unsigned long long app, const wchar_t *path, VkInstance, VkPhysicalDevice, VkDevice, + PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr, int version, const void *info); +typedef NgxResult (*PFN_ReqExt)(unsigned *ni, const char ***ie, unsigned *nd, const char ***de); +typedef NgxResult (*PFN_Params)(NgxParam **); +typedef NgxResult (*PFN_DestroyParams)(NgxParam *); +typedef NgxResult (*PFN_Create)(VkCommandBuffer, int feature, NgxParam *, NgxHandle **); +typedef NgxResult (*PFN_Evaluate)(VkCommandBuffer, const NgxHandle *, const NgxParam *, void *callback); +typedef NgxResult (*PFN_Release)(NgxHandle *); + +static struct { + void *core; // the NGX core library (an HMODULE on Windows) + PFN_Init init; + PFN_ReqExt req_ext; + PFN_Params caps, alloc_params; + PFN_DestroyParams destroy_params; + PFN_Create create; + PFN_Evaluate evaluate; + PFN_Release release; + int tried, loaded, available; + int wanted; // -1 not read yet +} N = {.wanted = -1}; + +typedef void (*SetI)(NgxParam *, const char *, int); +typedef void (*SetU)(NgxParam *, const char *, unsigned); +typedef void (*SetF)(NgxParam *, const char *, float); +typedef void (*SetP)(NgxParam *, const char *, void *); +typedef NgxResult (*GetI)(const NgxParam *, const char *, int *); +typedef NgxResult (*GetU)(const NgxParam *, const char *, unsigned *); +typedef NgxResult (*GetF)(const NgxParam *, const char *, float *); +typedef NgxResult (*GetP)(const NgxParam *, const char *, void **); +// The SDK declares Set(u64), Set(float), Set(double), Set(u32), Set(int), Set(ID3D11Resource *), Set(ID3D12Resource *), +// Set(void *), then the Get overloads in the same order, then Reset. +#ifdef _WIN32 +enum { S_P = 0, S_D12, S_D11, S_I, S_U32, S_D, S_F, S_U64, G_P, G_D12, G_D11, G_I, G_U32, G_D, G_F, G_U64 }; +#else +enum { S_U64 = 0, S_F, S_D, S_U32, S_I, S_D11, S_D12, S_P, G_U64, G_F, G_D, G_U32, G_I, G_D11, G_D12, G_P }; +#endif +static void p_seti(NgxParam *p, const char *n, int v) { ((SetI)p->vt->slot[S_I])(p, n, v); } +static void p_setu(NgxParam *p, const char *n, unsigned v) { ((SetU)p->vt->slot[S_U32])(p, n, v); } +static void p_setf(NgxParam *p, const char *n, float v) { ((SetF)p->vt->slot[S_F])(p, n, v); } +static void p_setp(NgxParam *p, const char *n, void *v) { ((SetP)p->vt->slot[S_P])(p, n, v); } +static int p_geti(const NgxParam *p, const char *n, int *v) { return NGX_OK(((GetI)p->vt->slot[G_I])(p, n, v)); } +static int p_getu(const NgxParam *p, const char *n, unsigned *v) { return NGX_OK(((GetU)p->vt->slot[G_U32])(p, n, v)); } +static int p_getf(const NgxParam *p, const char *n, float *v) { return NGX_OK(((GetF)p->vt->slot[G_F])(p, n, v)); } +static int p_getp(const NgxParam *p, const char *n, void **v) { return NGX_OK(((GetP)p->vt->slot[G_P])(p, n, v)); } + +#ifdef _WIN32 +#define DLSS_MODEL "nvngx_dlss.dll" +#else +#define DLSS_MODEL "libnvidia-ngx-dlss.so.<version>" +#endif + +// NGX is brought up only when dlaa is on at start: its device extensions can only be asked for at device creation, and +// loading the driver's NGX core on every NVIDIA machine would cost start time and change the device with DLAA off. So +// dlaa applies after a restart; switched off, nothing else of DLAA runs. +int vk_dlaa_wanted(void) { + static int wanted = -1; + if (wanted < 0) wanted = cfg_bool("TOTK_DLAA"); + return wanted; +} + +static int core_load(void) { + if (N.tried) return N.loaded; + N.tried = 1; +#ifdef _WIN32 + char dir[MAX_PATH] = {0}, path[MAX_PATH + 32]; + DWORD n = sizeof dir; + if (RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", "FullPath", RRF_RT_REG_SZ, + NULL, dir, &n) != ERROR_SUCCESS) { + rt_log("dlaa: the driver has no NGX core (registry NGXCore missing): DLAA off"); + return 0; + } + snprintf(path, sizeof path, "%s\\_nvngx.dll", dir); + N.core = LoadLibraryA(path); + if (!N.core) { + rt_log("dlaa: cannot load %s (error %lu): DLAA off", path, GetLastError()); + return 0; + } +#define FN(f, name) N.f = (void *)GetProcAddress((HMODULE)N.core, name) +#else + // The driver's NGX core; the dynamic loader finds it where the driver put it (/usr/lib, /usr/lib/x86_64-linux-gnu). + const char *path = "libnvidia-ngx.so.1"; + N.core = plat_lib_open(path); + if (!N.core) { + rt_log("dlaa: cannot load %s (not an NVIDIA driver, or one without NGX): DLAA off", path); + return 0; + } +#define FN(f, name) N.f = plat_lib_sym(N.core, name) +#endif + FN(init, "NVSDK_NGX_VULKAN_Init_Ext2"); + FN(req_ext, "NVSDK_NGX_VULKAN_RequiredExtensions"); + FN(caps, "NVSDK_NGX_VULKAN_GetCapabilityParameters"); + FN(alloc_params, "NVSDK_NGX_VULKAN_AllocateParameters"); + FN(destroy_params, "NVSDK_NGX_VULKAN_DestroyParameters"); + FN(create, "NVSDK_NGX_VULKAN_CreateFeature"); + FN(evaluate, "NVSDK_NGX_VULKAN_EvaluateFeature"); + FN(release, "NVSDK_NGX_VULKAN_ReleaseFeature"); +#undef FN + if (!N.init || !N.req_ext || !N.caps || !N.alloc_params || !N.create || !N.evaluate || !N.release) { + rt_log("dlaa: the NGX core %s lacks Vulkan entry points: DLAA off", path); + return 0; + } + N.loaded = 1; + rt_log("dlaa: NGX core %s", path); + return 1; +} + +static int listed(const char *const *list, uint32_t n, const char *s) { + for (uint32_t i = 0; i < n; i++) + if (!strcmp(list[i], s)) return 1; + return 0; +} + +void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max) { + unsigned ni = 0, nd = 0; + const char **ie = NULL, **de = NULL; + if (!vk_dlaa_wanted() || !core_load() || !NGX_OK(N.req_ext(&ni, &ie, &nd, &de))) return; + for (unsigned i = 0; i < ni; i++) + if (!listed(ext, *n, ie[i]) && *n < max) ext[(*n)++] = ie[i]; +} + +void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n, + uint32_t max) { + unsigned ni = 0, nd = 0; + const char **ie = NULL, **de = NULL; + if (!vk_dlaa_wanted() || !core_load() || !NGX_OK(N.req_ext(&ni, &ie, &nd, &de))) return; + for (unsigned i = 0; i < nd; i++) { + int ok = 0; + for (uint32_t k = 0; k < nhave && !ok; k++) ok = !strcmp(have[k].extensionName, de[i]); + if (!ok) rt_log("dlaa: the device lacks %s, which NGX asks for", de[i]); + else if (!listed(ext, *n, de[i]) && *n < max) ext[(*n)++] = de[i]; + } +} + +static int vtable_ok(void) { + NgxParam *p = NULL; + if (!NGX_OK(N.alloc_params(&p)) || !p) return 0; + int i = 0; + unsigned u = 0; + float f = 0; + void *v = NULL; + p_seti(p, "TotK.Test.I", -12345); + p_setu(p, "TotK.Test.U", 4000000000u); + p_setf(p, "TotK.Test.F", 1.25f); + p_setp(p, "TotK.Test.P", (void *)p); + int ok = p_geti(p, "TotK.Test.I", &i) && i == -12345 && p_getu(p, "TotK.Test.U", &u) && u == 4000000000u && + p_getf(p, "TotK.Test.F", &f) && f == 1.25f && p_getp(p, "TotK.Test.P", &v) && v == (void *)p; + if (N.destroy_params) N.destroy_params(p); + if (!ok) rt_log("dlaa: NGX parameter layout check failed (i=%d u=%u f=%g p=%p): DLAA off", i, u, f, v); + return ok; +} + +#ifndef _WIN32 +// UTF-8 to UTF-32 wchar_t, without the C library's locale (the runtime never sets one, so mbstowcs would stop at the +// first non-ASCII byte). Returns 0 on malformed input or when out has no room. +static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) { + const unsigned char *u = (const unsigned char *)in; + size_t n = 0; + while (*u) { + unsigned c = *u++, more = c < 0x80 ? 0 : c >= 0xF0 ? 3 : c >= 0xE0 ? 2 : c >= 0xC0 ? 1 : 4; + if (more == 4) return 0; + if (more) c &= 0x3F >> more; + for (unsigned k = 0; k < more; k++, u++) { + if ((*u & 0xC0) != 0x80) return 0; + c = c << 6 | (*u & 0x3F); + } + if (n + 1 >= cap) return 0; + out[n++] = (wchar_t)c; + } + out[n] = 0; + return 1; +} +#endif + +void vk_dlaa_init(void) { + if (!vk_dlaa_wanted() || !core_load()) return; +#ifdef _WIN32 + wchar_t wdir[MAX_PATH]; + DWORD len = GetModuleFileNameW(NULL, wdir, MAX_PATH); + while (len && wdir[len - 1] != L'\\') len--; + wdir[len] = 0; +#else + // The executable's folder, where NGX looks for libnvidia-ngx-dlss.so and writes its logs, as a wchar_t string: + // 32-bit code points here, as the Linux core expects. + char dir[PLAT_PATH_MAX]; + wchar_t wdir[PLAT_PATH_MAX]; + char *cut = plat_exe_path(dir, sizeof dir) ? plat_last_sep(dir) : NULL; + if (!cut || !utf8_to_wide((cut[1] = 0, dir), wdir, PLAT_PATH_MAX)) { + rt_log("dlaa: cannot name the executable's folder for NGX: DLAA off"); + return; + } +#endif + // 231313132: the application id NVIDIA's DLSS samples use for builds without an NVIDIA-issued id + const VkCtx *c = vk_ctx(); + NgxResult r = N.init(231313132ull, wdir, c->instance, c->phys, c->device, vkGetInstanceProcAddr, + vkGetDeviceProcAddr, NGX_VERSION_API, NULL); + if (!NGX_OK(r)) { + rt_log("dlaa: NGX init failed (%08x): DLAA off", (unsigned)r); + return; + } + if (!vtable_ok()) return; + NgxParam *caps = NULL; + int avail = 0, driver = 0; + if (NGX_OK(N.caps(&caps)) && caps) { + p_geti(caps, "SuperSampling.Available", &avail); + p_geti(caps, "SuperSampling.NeedsUpdatedDriver", &driver); + } + N.available = avail != 0; + rt_log("dlaa: NGX up; DLSS %s%s", avail ? "available" : "NOT available (is " DLSS_MODEL " next to the executable?)", + driver ? "; the driver needs an update" : ""); +} + +int vk_dlaa_available(void) { return vk_dlaa_wanted() && N.available; } + +// ---------------------------------------------------------------- where DLAA runs in TotK's frame +// Measured (TOTK_DUMP, frame 2100 at Lookout Landing, 2560x1440 scene): the G-buffer passes draw into five targets with +// the scene depth (d32f at the scene size), one of them the R11G11B10F light target; lighting and the transparents +// then draw into that light target alone, the last time with the scene depth attached; the first pass narrower than a +// quarter of the scene starts the bloom chain, which reads it. After the bloom come the tonemap and post passes, then +// the HUD is drawn into the same target and a last pass copies it to the 8-bit output. +// So: after the G-buffer, a pass at the scene size with exactly one colour target (R11G11B10F or RGBA16F) and the D32F +// depth arms the frame with that pair; the next pass at most a quarter as wide ends the scene (no jitter after it) and, +// when the scene is as wide as the output, runs DLAA on the armed target first. Bloom, tonemap, post and HUD then all +// work on the anti-aliased picture. +// Upscaling (a scene smaller than the output: TOTK_DLSS quality/balanced/performance, or the game's own 5/6): measured +// at a 960x536 scene, 1920x1080 output, the bloom and its composite write back into the light target at the scene size, +// then one full-screen draw (the FXAA shader below, which also scales) reads it into an output-sized R11G11B10F target +// with no depth, and the HUD is drawn over that target at the output size. DLSS runs at the begin of that pass: it +// reads the light target and its result is blitted into the pass's target, whose scaling draw is then skipped. The +// HUD, drawn afterwards into the same target, is never jittered nor passed through DLSS. +// The scaling draw is not always that FXAA shader: at a 1280x720 scene and 1920x1080 output (1.4.3, dlss = quality) +// the game's tonemap (guest fs 7e114c4330dc212a) reads the light target and writes the output-sized target itself, +// over DLSS's picture, so the jittered scene reached the screen and the whole picture shook. So from DLSS's evaluation +// to the frame's end, draws in passes larger than the light target sample DLSS's output in its place +// (vk_dlaa_swap_img, applied by vk_exec_draw.c tex_info): whichever shader scales, it scales DLSS's picture. +static struct { + VkExecTarget color, depth; // the armed pair, valid in the frame it was armed + int armed, done, gbuf; // this frame: armed on the light target, DLAA ran, the G-buffer was drawn + int ended, up; // this frame: the scene is drawn (jitter over), DLSS wrote the scaled output + struct Size { // a DLSS feature and its work images per scene size (dynamic resolution flips it) + uint32_t w, h, ow, oh; // render size, output size + VkImage out, mv; + VmaAllocation out_mem, mv_mem; + VkImageView out_view, mv_view; + VkImage mask, chars[2]; // the bias mask DLSS reads, the character class mask of this and last frame (R8) + VmaAllocation mask_mem, chars_mem[2]; + VkImageView mask_view, chars_view[2]; + int chars_cur, chars_ok; // chars[chars_cur] is written next; chars_ok: the other holds last frame's + NgxHandle *feat; + uint64_t used; // evals count when it last ran + } sizes[5], *last; // last: the one the previous DLAA frame ran with + VkImage last_color; // the target DLAA ran on last frame: another one means a new history + NgxParam *params; + int failed, reset; + uint64_t evals, frames_missed; + // jitter (vk_dlaa_viewport) + VkImage jdepth; // the scene depth of the last frame DLAA ran on: passes drawing with it are jittered + float jx, jy; // the offset the next scene draws take, render pixels (window x right, y down) + float ux, uy; // the offset this frame's scene draws took (DLSS and the motion vectors get these) + uint32_t jframe; + int jit_ok, jit_used, hold; // jitter allowed this frame / a draw used it / frames left without it + uint64_t holds; + // camera (vk_dlaa_draw) and motion vectors (mv_pass) + uint64_t cam_addr; // this frame's scene camera block (c4 of a scene draw's vertex stage), 0: none seen + VkBuffer camb; // two CAM_SLOT slots: this frame's block and last frame's + VmaAllocation camb_mem; + VkBuffer mvm; // MVM_SLOTS pairs of blocks for dlaa_mv.comp: the identity and the reprojection (mv_pass) + VmaAllocation mvm_mem; + uint8_t *mvm_map; + uint32_t mvm_slot; + double cam_prev[16]; // last frame's view-projection (rows 3-6 of its block), for the reprojection + int cam_slot, cam_prev_ok; // the slot this frame writes / the other slot holds last frame's camera + VkDescriptorSetLayout mv_dsl; + VkPipelineLayout mv_layout; + VkPipeline mv_pipe; + VkSampler mv_samp; + int mv_ok; // -1 not built, 0 failed, 1 ready + uint64_t mv_frames, mv_none, masked; // masked: frames with the characters' bias mask + uint64_t why[3]; // history resets: scene size changed, no camera, scene target changed + int on; // the dlaa setting, read at each present (the menu switches it live) + int preset, preset_dirty; // the DLSS model features are created with (preset_value); a change pending + VkExecTarget ids; // this frame's G-buffer target 0 (object classes in green), valid with ids_ok + int ids_ok; + unsigned cfg_seen; // cfg_version when preset was read + uint64_t last_eval_ns; // plat_now_ns of the previous evaluation (FrameTimeDeltaInMsec) +} D = {.mv_ok = -1, .cfg_seen = ~0u}; + +const VkTexImage *vk_dlaa_swap_img; +VkImageView vk_dlaa_swap_view; + +static int dl_active(void) { return N.available && !D.failed && D.on; } + +static void dl_fail(const char *what, NgxResult r) { + rt_log("dlaa: %s failed (%08x): DLAA off for this run", what, (unsigned)r); + D.failed = 1; +} + +static int image2d(uint32_t w, uint32_t h, VkFormat f, VkImageUsageFlags usage, VkImage *img, VmaAllocation *mem, + VkImageView *view) { + const VkCtx *c = vk_ctx(); + VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = f, + .extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + if (vmaCreateImage(c->vma, &ii, &ai, img, mem, NULL) != VK_SUCCESS) return 0; + VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = *img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = f, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + return vkCreateImageView(c->device, &vi, NULL, view) == VK_SUCCESS; +} + +static void free_size(struct Size *e) { + const VkCtx *c = vk_ctx(); + vkDeviceWaitIdle(c->device); // a third scene size: the evicted images may still be in flight + if (e->out_view) vkDestroyImageView(c->device, e->out_view, NULL); + if (e->mv_view) vkDestroyImageView(c->device, e->mv_view, NULL); + if (e->out) vmaDestroyImage(c->vma, e->out, e->out_mem); + if (e->mv) vmaDestroyImage(c->vma, e->mv, e->mv_mem); + if (e->mask_view) vkDestroyImageView(c->device, e->mask_view, NULL); + if (e->mask) vmaDestroyImage(c->vma, e->mask, e->mask_mem); + for (int i = 0; i < 2; i++) { + if (e->chars_view[i]) vkDestroyImageView(c->device, e->chars_view[i], NULL); + if (e->chars[i]) vmaDestroyImage(c->vma, e->chars[i], e->chars_mem[i]); + } + if (e->feat) N.release(e->feat); + memset(e, 0, sizeof *e); +} + +// The entry for a scene and output size: its output (RGBA16F at the output size, written by DLSS, then blitted into +// the target) and motion vectors (RG16F at the scene size, render pixels), both kept in GENERAL like every image of the +// back end. TotK's dynamic resolution switches between a few sizes; each keeps its feature, the least recently used +// goes for a new one. +static struct Size *size_for(VkCommandBuffer cb, uint32_t w, uint32_t h, uint32_t ow, uint32_t oh) { + struct Size *e = NULL, *lru = &D.sizes[0]; + for (int i = 0; i < 5; i++) { + if (D.sizes[i].w == w && D.sizes[i].h == h && D.sizes[i].ow == ow && D.sizes[i].oh == oh) return &D.sizes[i]; + if (!D.sizes[i].out && !e) e = &D.sizes[i]; + if (D.sizes[i].used < lru->used) lru = &D.sizes[i]; + } + if (!e) { + if (D.last == lru) D.last = NULL; + free_size(e = lru); + } + VkImageUsageFlags u = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT; + VkImageUsageFlags u8 = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + if (!image2d(ow, oh, VK_FORMAT_R16G16B16A16_SFLOAT, u, &e->out, &e->out_mem, &e->out_view) || + !image2d(w, h, VK_FORMAT_R16G16_SFLOAT, u | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &e->mv, &e->mv_mem, &e->mv_view) || + !image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->mask, &e->mask_mem, &e->mask_view) || + !image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->chars[0], &e->chars_mem[0], &e->chars_view[0]) || + !image2d(w, h, VK_FORMAT_R8_UNORM, u8, &e->chars[1], &e->chars_mem[1], &e->chars_view[1])) + return NULL; + VkImageMemoryBarrier2 ib[5]; + VkImage im[5] = {e->out, e->mv, e->mask, e->chars[0], e->chars[1]}; + for (int i = 0; i < 5; i++) + ib[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, + .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = im[i], + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 5, + .pImageMemoryBarriers = ib}; + vkCmdPipelineBarrier2(cb, &di); + e->w = w, e->h = h, e->ow = ow, e->oh = oh; + return e; +} + +static void full_barrier(VkCommandBuffer cb) { + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &di); +} + +static NgxResourceVK res_image(VkImageView v, VkImage img, VkFormat f, uint32_t w, uint32_t h, VkImageAspectFlags a, + int rw) { + NgxResourceVK r; + memset(&r, 0, sizeof r); + r.r.img = (NgxImageView){v, img, {a, 0, 1, 0, 1}, f, w, h}; + r.type = 0; + r.rw = rw; + return r; +} + +// ---------------------------------------------------------------- timing (TOTK_DLAA_TIMES=1) +// GPU timestamps around the camera motion dispatch, the per-object motion pass and DLSS's evaluation, and the CPU +// time of NGX's call and of draining the recorder before it, summed and logged every TIMES_SUMMARY frames: +// dlaa times: gpu camera motion a ms, object motion b ms, DLSS c ms (frames timed); cpu: recorder drain d ms, NGX +// evaluate e ms, motion pass recording f ms +// Each frame takes its own slot of TIMES_SLOTS; a slot's results are read when it comes round again (by then the GPU is +// long done with it; not ready: that frame is left out). +enum { T_MV0, T_MV1, T_OBJ, T_DL0, T_DL1, T_N }; +#define TIMES_SLOTS 8u +#define TIMES_SUMMARY 600 +static struct { + int on; // -1 not read + VkQueryPool pool; + uint32_t slot; + int used[TIMES_SLOTS]; // the slot holds a frame's timestamps (all T_N written) + double gpu[3]; // ms summed: camera motion, object motion, DLSS + uint64_t frames, cpu_drain, cpu_ngx, cpu_mv; // ns summed +} T = {.on = -1}; + +static int times_on(void) { + if (T.on < 0) { + T.on = rt_knob("DLAA_TIMES", "0"); + VkQueryPoolCreateInfo qi = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, + .queryType = VK_QUERY_TYPE_TIMESTAMP, .queryCount = TIMES_SLOTS * T_N}; + if (T.on && vkCreateQueryPool(vk_ctx()->device, &qi, NULL, &T.pool) != VK_SUCCESS) T.on = 0; + } + return T.on; +} + +static void times_mark(VkCommandBuffer cb, int which) { + if (times_on()) vkCmdWriteTimestamp2(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, T.pool, T.slot * T_N + (uint32_t)which); +} + +// At the start of a frame's timing (mv_pass): the slot's last results are read and it is reset. +static void times_begin(VkCommandBuffer cb) { + if (!times_on()) return; + T.slot = (T.slot + 1) % TIMES_SLOTS; + if (T.used[T.slot]) { + uint64_t q[T_N]; + if (vkGetQueryPoolResults(vk_ctx()->device, T.pool, T.slot * T_N, T_N, sizeof q, q, sizeof q[0], + VK_QUERY_RESULT_64_BIT) == VK_SUCCESS) { + double ns = vk_ctx()->props.limits.timestampPeriod / 1e6; // ticks to ms + T.gpu[0] += (double)(q[T_MV1] - q[T_MV0]) * ns, T.gpu[1] += (double)(q[T_OBJ] - q[T_MV1]) * ns; + T.gpu[2] += (double)(q[T_DL1] - q[T_DL0]) * ns; + if (++T.frames % TIMES_SUMMARY == 0) { + rt_log("dlaa times: gpu camera motion %.3f ms, object motion %.3f ms, DLSS %.3f ms (%llu frames timed); " + "cpu: recorder drain %.3f ms, NGX evaluate %.3f ms, motion pass recording %.3f ms", + T.gpu[0] / TIMES_SUMMARY, T.gpu[1] / TIMES_SUMMARY, T.gpu[2] / TIMES_SUMMARY, + (unsigned long long)T.frames, T.cpu_drain / 1e6 / TIMES_SUMMARY, T.cpu_ngx / 1e6 / TIMES_SUMMARY, + T.cpu_mv / 1e6 / TIMES_SUMMARY); + T.gpu[0] = T.gpu[1] = T.gpu[2] = 0, T.cpu_drain = T.cpu_ngx = T.cpu_mv = 0; + } + } + } + T.used[T.slot] = 0; + vkCmdResetQueryPool(cb, T.pool, T.slot * T_N, T_N); +} + +// ---------------------------------------------------------------- motion vectors +// The camera's motion first (runtime/shaders/dlaa_mv.comp): this frame's scene camera block is copied out of the +// replay's uniform snapshot (vk_usnap.c, as of the submit call) into one slot of camb, last frame's stays in the other, +// and the compute pass reprojects every pixel's depth through the two (inv4 and mv_pass below). Then the per-object +// pass (vk_exec_draw.c, vk_mvobj_execute) writes the motion of whatever moves on its own over it. +#define CAM_BYTES 256u // vec4 rows 0-15: view, view-projection (3-6), projection, inverse view +#define CAM_BLOCK 0x940u // the whole block, kept for the per-object motion pass's vertex shaders (vk_exec_draw.c) +#define CAM_SLOT 0xa00u // camb slot stride +#define MVM_SLOTS 16u // frames the GPU may still be reading the reprojection blocks of + +static int mv_init(void) { + if (D.mv_ok >= 0) return D.mv_ok; + D.mv_ok = 0; + const VkCtx *c = vk_ctx(); + VkDescriptorSetLayoutBinding b[8] = { + {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {1, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {3, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {5, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {6, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {7, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}}; + VkDescriptorSetLayoutCreateInfo dci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, + .bindingCount = 8, .pBindings = b}; + VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, 24}; + VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_NEAREST, + .minFilter = VK_FILTER_NEAREST, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE}; + VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = sizeof dlaa_mv_spv, + .pCode = dlaa_mv_spv}; + VkShaderModule m = VK_NULL_HANDLE; + VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = 2 * CAM_SLOT, + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + if (vkCreateDescriptorSetLayout(c->device, &dci, NULL, &D.mv_dsl) != VK_SUCCESS) goto fail; + VkBufferCreateInfo mbi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = MVM_SLOTS * 2 * CAM_BYTES, + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT}; + VmaAllocationCreateInfo mai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | + VMA_ALLOCATION_CREATE_MAPPED_BIT}; + VmaAllocationInfo minfo; + if (vmaCreateBuffer(c->vma, &mbi, &mai, &D.mvm, &D.mvm_mem, &minfo) != VK_SUCCESS) goto fail; + D.mvm_map = minfo.pMappedData; + VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1, + .pSetLayouts = &D.mv_dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr}; + if (vkCreatePipelineLayout(c->device, &li, NULL, &D.mv_layout) != VK_SUCCESS || + vkCreateSampler(c->device, &si, NULL, &D.mv_samp) != VK_SUCCESS || + vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS || + vmaCreateBuffer(c->vma, &bi, &ai, &D.camb, &D.camb_mem, NULL) != VK_SUCCESS) + goto fail; + VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"}, + .layout = D.mv_layout}; + VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &D.mv_pipe); + vkDestroyShaderModule(c->device, m, NULL); + if (r != VK_SUCCESS) goto fail; + return D.mv_ok = 1; +fail: + rt_log("dlaa: the motion-vector pass could not be set up: DLAA runs with zero motion"); + return 0; +} + +// The reprojection from this frame's clip space to last frame's, last * inverse(this), in double precision: the shader +// took each pixel back to world space through a float inverse of the view-projection, and with TotK's world +// coordinates in the thousands and its far plane the result was noise of a fraction of a pixel and more on distant +// surfaces, which DLSS followed into a shimmer like heat haze (roofs and trees, most of all in the upscaling modes). +// The shader is unchanged: its "this frame" block gets the identity and its "last frame" block this matrix, so it +// applies just the one matrix to each pixel. A still camera gives the identity, and zero motion exactly. +static int inv4(const double m[16], double o[16]) { + double a[4][8]; + for (int r = 0; r < 4; r++) + for (int k = 0; k < 8; k++) a[r][k] = k < 4 ? m[r * 4 + k] : (k - 4 == r); + for (int col = 0; col < 4; col++) { + int piv = col; + for (int r = col + 1; r < 4; r++) + if (fabs(a[r][col]) > fabs(a[piv][col])) piv = r; + if (fabs(a[piv][col]) < 1e-300) return 0; + for (int k = 0; k < 8; k++) { double t = a[col][k]; a[col][k] = a[piv][k]; a[piv][k] = t; } + double d = a[col][col]; + for (int k = 0; k < 8; k++) a[col][k] /= d; + for (int r = 0; r < 4; r++) + if (r != col) { + double f = a[r][col]; + for (int k = 0; k < 8; k++) a[r][k] -= f * a[col][k]; + } + } + for (int r = 0; r < 4; r++) + for (int k = 0; k < 4; k++) o[r * 4 + k] = a[r][k + 4]; + return 1; +} + +// The pass, recorded through the recorder like the replay's own commands. 0: no camera this frame (the vectors stay +// zero and DLSS starts a new history). bias > 0 with this frame's G-buffer classes (D.ids) also writes e->mask, the +// bias mask (dlaa_mv.comp); *masked says whether it did. +static int mv_pass(VkExec *ex, struct Size *e, VkImageView zv, float jx, float jy, float bias, int *masked) { + *masked = 0; + VkCommandBuffer cb = ex->cb; + uint32_t w = e->w, h = e->h; + VkDescriptorBufferInfo cam; + if (!mv_init() || !D.cam_addr || !vk_usnap_find(D.cam_addr, CAM_BLOCK, 4, &cam)) { + D.mv_none++; + D.cam_prev_ok = 0; + e->chars_ok = 0; + return 0; + } + // this frame's view-projection as the snapshot holds it, for the reprojection; kept as last frame's for the next + const float *cr = (const float *)vk_usnap_bytes(D.cam_addr + 48, 64); + double cur_vp[16], m[16], ci[16]; + int have_m = 0; + if (cr) { + for (int i = 0; i < 16; i++) cur_vp[i] = cr[i]; + if (D.cam_prev_ok && inv4(cur_vp, ci)) { + for (int r = 0; r < 4; r++) + for (int k = 0; k < 4; k++) { + double v = 0; + for (int j = 0; j < 4; j++) v += D.cam_prev[r * 4 + j] * ci[j * 4 + k]; + m[r * 4 + k] = v; + } + have_m = 1; + } + } + int s = D.cam_slot, had_prev = D.cam_prev_ok; + VkBufferCopy bc = {cam.offset, (VkDeviceSize)s * CAM_SLOT, CAM_BLOCK}; + vkCmdCopyBuffer(cb, cam.buffer, D.camb, 1, &bc); + full_barrier(cb); + D.cam_slot ^= 1, D.cam_prev_ok = cr != NULL; + if (cr) memcpy(D.cam_prev, cur_vp, sizeof cur_vp); + if (!had_prev || !have_m) { // nothing to reproject into: zero motion, new history + e->chars_ok = 0; + return 0; + } + VkDescriptorImageInfo zi = {D.mv_samp, zv, VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo oi = {VK_NULL_HANDLE, e->mv_view, VK_IMAGE_LAYOUT_GENERAL}; + uint32_t ms = D.mvm_slot++ % MVM_SLOTS; + float *blk = (float *)(D.mvm_map + (size_t)ms * 2 * CAM_BYTES); // rows 3-6 of each block are read + memset(blk, 0, 2 * CAM_BYTES); + for (int r = 0; r < 4; r++) + for (int k = 0; k < 4; k++) + blk[12 + r * 4 + k] = r == k ? 1.0f : 0.0f, blk[64 + 12 + r * 4 + k] = (float)m[r * 4 + k]; + vmaFlushAllocation(vk_ctx()->vma, D.mvm_mem, (VkDeviceSize)ms * 2 * CAM_BYTES, 2 * CAM_BYTES); + VkDescriptorBufferInfo cur = {D.mvm, (VkDeviceSize)ms * 2 * CAM_BYTES, CAM_BYTES}; + VkDescriptorBufferInfo prev = {D.mvm, (VkDeviceSize)ms * 2 * CAM_BYTES + CAM_BYTES, CAM_BYTES}; + // The class mask needs the G-buffer at the scene size; otherwise bindings 4-7 get the mask images as stand-ins + // (the shader returns before reading them with bias 0). + const VkExecTarget *ids = &D.ids; + int use = bias > 0 && D.ids_ok && ids->image && ids->width == w && ids->height == h && !ids->is3d; + VkImageView iv = use ? vk_exec_target_view(ids->img, ids->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT, + ids->level, ids->base_layer, 1, 0) + : e->mask_view; + int cur_c = e->chars_cur; + VkDescriptorImageInfo ii = {D.mv_samp, iv, VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo mki = {VK_NULL_HANDLE, e->mask_view, VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo cpi = {VK_NULL_HANDLE, e->chars_view[cur_c ^ 1], VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo coi = {VK_NULL_HANDLE, e->chars_view[cur_c], VK_IMAGE_LAYOUT_GENERAL}; + VkWriteDescriptorSet wr[8] = { + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &zi}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &oi}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 2, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &cur}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 3, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .pBufferInfo = &prev}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 4, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 5, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &mki}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 6, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &cpi}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 7, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &coi}}; + times_begin(cb); + times_mark(cb, T_MV0); + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, D.mv_pipe); + rec_push(vk_pipeline_push_fn(), cb, VK_PIPELINE_BIND_POINT_COMPUTE, D.mv_layout, 0, 8, wr); + float pc[6] = {(float)w, (float)h, jx, jy, use ? bias : 0.0f, use && e->chars_ok ? 1.0f : 0.0f}; + vkCmdPushConstants(cb, D.mv_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, pc); + vkCmdDispatch(cb, (w + 7) / 8, (h + 7) / 8, 1); + full_barrier(cb); + times_mark(cb, T_MV1); + vk_mvcheck_camera(cb, e->mv, w, h, jx, jy); // TOTK_MV_CHECK (test) + // the characters' own motion over the camera's (vk_exec_draw.c, per-object motion) + uint64_t c0 = T.on > 0 ? kern_now_ns() : 0; + if (vk_mvobj_execute(ex, D.cam_addr, e->mv_view, zv, D.depth.format, w, h)) full_barrier(cb); + if (T.on > 0) T.cpu_mv += kern_now_ns() - c0; + times_mark(cb, T_OBJ); + if (T.on > 0) T.used[T.slot] = 1; // the DLSS marks follow in evaluate + vk_mvcheck_final(cb, ex->fence, e->mv, D.color.image, D.color.format, D.color.level, D.color.base_layer, w, h, jx, jy); + D.mv_frames++; + if (use) e->chars_cur ^= 1, e->chars_ok = 1, D.masked++; + else e->chars_ok = 0; + *masked = use; + return 1; +} + +// Last frame's camera block for the per-object motion pass, recorded before this frame's mv_pass: that pass writes +// this frame's into slot cam_slot and leaves the other slot, last frame's, as it is. 0: none (no history). +int vk_dlaa_prev_camera(VkDescriptorBufferInfo *out, float jitter[2]) { + if (!dl_active() || !D.cam_prev_ok || !D.camb) return 0; + *out = (VkDescriptorBufferInfo){D.camb, (VkDeviceSize)(D.cam_slot ^ 1) * CAM_SLOT, CAM_BLOCK}; + jitter[0] = D.jit_ok ? D.jx : 0.0f, jitter[1] = D.jit_ok ? D.jy : 0.0f; + return 1; +} + +// ---------------------------------------------------------------- reactive mask +// Particles, smoke, sparks and other transparents have no motion vectors of their own: the motion image holds the +// surface's behind them, and DLSS carried their old colour along that surface (trails behind sparks, smoke smeared +// with the camera). DLSS takes a mask of where to trust this frame's colour over the history (NGX +// "DLSS.Input.Bias.Current.Color.Mask"). It is made from the picture itself, at no cost on the CPU: the light target is +// copied when the transparents start (the pass that arms DLAA, vk_dlaa_pass) and compared with what DLSS gets +// (runtime/dlaa_mask_spv.h, tools/make_dlaa_mask.py): the share of a pixel's colour that changed, from MASK_THR on, +// times MASK_GAIN. Off by default (TOTK_DLSS_REACTIVE=1 turns it on, TOTK_DLAA_MASKDEBUG=1 shows the mask instead of +// the picture): the pass that arms also draws the sky and blended fog layers over the solid scene, and the mask +// covered the sky, sunlit trees and roofs and a wide fog region besides the particles (seen with MASKDEBUG at 19:45 in +// the first save), where DLSS would give up its anti-aliasing. It needs the particle draws alone (drawn again into the +// mask, like the motion draws) before it can be on. In the upscaling modes the light target DLSS reads also holds the +// bloom by then; the copy would have to be compared at the end of the scene instead. +#include "dlaa_mask_spv.h" +#define MASK_THR 0.1f +#define MASK_GAIN 2.5f +#define SNAPS 3 + +static struct { + struct Snap { // per light target size and format: its copy at arming, and the mask + uint32_t w, h; + VkFormat f; + VkImage before, mask; + VmaAllocation before_mem, mask_mem; + VkImageView before_view, mask_view; + uint64_t used; + } s[SNAPS]; + struct Snap *cur; // this frame's copy, NULL: none + VkDescriptorSetLayout dsl; + VkPipelineLayout layout; + VkPipeline pipe; + int ok, knob; // ok: -1 not built, 0 failed, 1 ready; knob: -1 not read + uint64_t made, missed; // frames with a mask; frames DLSS ran without the copy +} R = {.ok = -1, .knob = -1}; + +static int mask_init(void) { + if (R.knob < 0) R.knob = rt_knob("DLSS_REACTIVE", "0"); + if (!R.knob) return 0; + if (R.ok >= 0) return R.ok; + R.ok = 0; + const VkCtx *c = vk_ctx(); + VkDescriptorSetLayoutBinding b[3] = { + {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}, + {2, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT, NULL}}; + VkDescriptorSetLayoutCreateInfo dci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, + .bindingCount = 3, .pBindings = b}; + VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, 16}; + VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = sizeof dlaa_mask_spv, + .pCode = dlaa_mask_spv}; + VkShaderModule m = VK_NULL_HANDLE; + if (vkCreateDescriptorSetLayout(c->device, &dci, NULL, &R.dsl) != VK_SUCCESS) goto fail; + VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1, + .pSetLayouts = &R.dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr}; + if (vkCreatePipelineLayout(c->device, &li, NULL, &R.layout) != VK_SUCCESS || + vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS) + goto fail; + VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"}, + .layout = R.layout}; + VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &R.pipe); + vkDestroyShaderModule(c->device, m, NULL); + if (r != VK_SUCCESS) goto fail; + return R.ok = 1; +fail: + rt_log("dlaa: the reactive mask pass could not be set up: DLSS runs without it"); + return 0; +} + +static void free_snap(struct Snap *s) { + const VkCtx *c = vk_ctx(); + vkDeviceWaitIdle(c->device); // a fourth size: the evicted images may still be in flight + if (s->before_view) vkDestroyImageView(c->device, s->before_view, NULL); + if (s->mask_view) vkDestroyImageView(c->device, s->mask_view, NULL); + if (s->before) vmaDestroyImage(c->vma, s->before, s->before_mem); + if (s->mask) vmaDestroyImage(c->vma, s->mask, s->mask_mem); + memset(s, 0, sizeof *s); +} + +static struct Snap *snap_for(VkCommandBuffer cb, uint32_t w, uint32_t h, VkFormat f) { + static uint64_t tick; + struct Snap *e = NULL, *lru = &R.s[0]; + for (int i = 0; i < SNAPS; i++) { + if (R.s[i].w == w && R.s[i].h == h && R.s[i].f == f) return R.s[i].used = ++tick, &R.s[i]; + if (!R.s[i].before && !e) e = &R.s[i]; + if (R.s[i].used < lru->used) lru = &R.s[i]; + } + if (!e) free_snap(e = lru); + VkImageUsageFlags u = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + if (!image2d(w, h, f, u, &e->before, &e->before_mem, &e->before_view) || + !image2d(w, h, VK_FORMAT_R8_UNORM, u | VK_IMAGE_USAGE_STORAGE_BIT, &e->mask, &e->mask_mem, &e->mask_view)) { + free_snap(e); + return NULL; + } + VkImageMemoryBarrier2 ib[2]; + VkImage im[2] = {e->before, e->mask}; + for (int i = 0; i < 2; i++) + ib[i] = (VkImageMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, + .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = im[i], + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 2, + .pImageMemoryBarriers = ib}; + vkCmdPipelineBarrier2(cb, &di); + e->w = w, e->h = h, e->f = f, e->used = ++tick; + return e; +} + +// At arming: the light target as it is before the transparents, outside any pass. +static void mask_snapshot(VkExec *ex) { + R.cur = NULL; + if (mask_init() <= 0) return; + const VkExecTarget *t = &D.color; + struct Snap *s = snap_for(ex->cb, t->width, t->height, t->format); + if (!s) return; + vk_exec_barrier(ex); + VkImageCopy c = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, t->level, t->base_layer, 1}, + .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .extent = {t->width, t->height, 1}}; + vkCmdCopyImage(ex->cb, t->image, VK_IMAGE_LAYOUT_GENERAL, s->before, VK_IMAGE_LAYOUT_GENERAL, 1, &c); + full_barrier(ex->cb); + ex->recorded += 2; + R.cur = s; +} + +// Before DLSS: the mask from the copy and the light target (view cv). NULL: none this frame. +static struct Snap *mask_pass(VkCommandBuffer cb, VkImageView cv, uint32_t w, uint32_t h) { + struct Snap *s = R.cur; + R.cur = NULL; + if (mask_init() <= 0) return NULL; + if (!s || s->w != w || s->h != h || s->f != D.color.format) return R.missed++, NULL; + VkDescriptorImageInfo bi = {D.mv_samp, s->before_view, VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo ai = {D.mv_samp, cv, VK_IMAGE_LAYOUT_GENERAL}; + VkDescriptorImageInfo mi = {VK_NULL_HANDLE, s->mask_view, VK_IMAGE_LAYOUT_GENERAL}; + VkWriteDescriptorSet wr[3] = { + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &bi}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ai}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 2, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &mi}}; + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, R.pipe); + rec_push(vk_pipeline_push_fn(), cb, VK_PIPELINE_BIND_POINT_COMPUTE, R.layout, 0, 3, wr); + float pc[4] = {(float)w, (float)h, MASK_THR, MASK_GAIN}; + vkCmdPushConstants(cb, R.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, pc); + vkCmdDispatch(cb, (w + 7) / 8, (h + 7) / 8, 1); + full_barrier(cb); + R.made++; + return s; +} + +// ---------------------------------------------------------------- jitter +// The scene's draws (every pass drawing with the scene depth, until the DLAA point) have their viewport moved by a +// sub-pixel Halton(2,3) offset, which shifts what they rasterise as a jittered projection would; full-screen passes +// that shade by window position are unaffected. Jitter only follows a frame DLSS ran on: a jittered frame that DLSS +// does not resolve shakes on screen, so a missed DLAA point stops the jitter for a second. +static float halton(uint32_t i, uint32_t b) { + float f = 1, r = 0; + for (; i; i /= b) f /= (float)b, r += f * (float)(i % b); + return r; +} + +void vk_dlaa_viewport(VkExec *ex, VkViewport *v) { + if (!D.jit_ok || D.ended || !ex->depth.image || ex->depth.image != D.jdepth) return; + v->x += D.jx, v->y += D.jy; + D.ux = D.jx, D.uy = D.jy, D.jit_used = 1; +} + +void vk_dlaa_draw(VkExec *ex) { + if (!dl_active() || D.ended) return; + static int survey = -1; // TOTK_DLAA_SURVEY=1 (test): what is drawn into the armed light target + if (survey < 0) survey = rt_knob("DLAA_SURVEY", "0"); + static uint64_t dfn[16]; // scene draws writing depth, by depth test (reversed depth: GREATER/GEQUAL) + if (survey && ex->depth.image && ex->depth.image == D.jdepth && ex->pipe.ds.dt && ex->pipe.ds.dw) + dfn[ex->pipe.ds.df & 15]++; + if (survey && D.armed && ex->color[0].image == D.color.image) { + static uint64_t n[2][2][2], arms, frames, last_frame; // [blend][depth write][depth test] + const VkPipeState *s = &ex->pipe; + n[s->color.blend_en & 1][s->ds.dw][s->ds.dt]++; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != last_frame) last_frame = f, frames++; + static uint64_t ps; + if (ex->pass_serial != ps) ps = ex->pass_serial, arms++; + if (frames % 300 == 0) { + static uint64_t logged; + if (logged != frames) { + logged = frames; + rt_log("dlaa survey: scene draws writing depth by depth test (NVN func 1-8: never less equal lequal " + "greater notequal gequal always): %llu %llu %llu %llu %llu %llu %llu %llu", + (unsigned long long)dfn[1], (unsigned long long)dfn[2], (unsigned long long)dfn[3], + (unsigned long long)dfn[4], (unsigned long long)dfn[5], (unsigned long long)dfn[6], + (unsigned long long)dfn[7], (unsigned long long)dfn[8]); + rt_log("dlaa survey: %llu frames, %.1f passes a frame into the light target after arming; draws a frame " + "(blend/depth write/depth test): 000 %.1f 001 %.1f 010 %.1f 011 %.1f 100 %.1f 101 %.1f 110 %.1f 111 %.1f", + (unsigned long long)frames, (double)arms / frames, (double)n[0][0][0] / frames, (double)n[0][0][1] / frames, + (double)n[0][1][0] / frames, (double)n[0][1][1] / frames, (double)n[1][0][0] / frames, + (double)n[1][0][1] / frames, (double)n[1][1][0] / frames, (double)n[1][1][1] / frames); + } + } + } + const p_bind_buf *b = &ex->ubo[0][1]; // constant bank c4: the replay keeps user banks from c3 on + const VkExecTarget *z = &ex->depth; + if (b->size == 0x940 && b->addr && z->image && z->width >= DLAA_SCENE_MIN_W && z->format == VK_FORMAT_D32_SFLOAT && + (!D.jdepth || z->image == D.jdepth)) + D.cam_addr = b->addr; +} + +// The jitter moves on right after DLAA ran, not at the present: the replay can record the next frame's scene before +// the present of this one is handled, and a step taken there landed in the middle of a frame (worse after an off/on +// switch, which shifted that order). Jitter needs a frame DLSS ran on; a frame without the DLAA point stops it. +// The sequence is as long as NVIDIA's DLSS guide asks for the upscaling ratio, 8 x (output / scene)^2 phases (18 in +// Quality, 24 in Balanced, 32 in Performance, 72 in Ultra Performance), and 16 at least, the length DLAA has used from the start. +static void next_jitter(uint32_t w, uint32_t ow) { + static int nj = -1; + if (nj < 0) nj = !rt_knob("DLAA_JITTER", "1"); // TOTK_DLAA_JITTER=0: no jitter (test) + double r = w ? (double)ow / w : 1.0; + uint32_t phases = (uint32_t)ceil(8.0 * r * r - 1e-6); + if (phases < 16) phases = 16; + if (phases > 72) phases = 72; + uint32_t i = D.jframe++ % phases + 1; + D.jx = halton(i, 2) - 0.5f, D.jy = halton(i, 3) - 0.5f; + D.jit_ok = !D.hold && !nj; +} + +// The DLSS model (TOTK_DLSS_PRESET, live): default takes NVIDIA's model per mode (evaluate: K for DLAA, Quality and +// Balanced, M for Performance, L for Ultra Performance), 0 here. A letter a..m takes that model in every mode: k pins +// K, j is the other transformer model, which NVIDIA describes as slightly less ghosting at the cost of extra flicker, e +// the older CNN model, cheaper on slower RTX cards. The value is an NVSDK_NGX_DLSS_Hint_Render_Preset (1 A ... 13 M), +// read when a feature is created; a change recreates the features (preset_check). +static int preset_value(void) { + const char *v = cfg_get("TOTK_DLSS_PRESET"); + int c = v && v[0] && !v[1] ? v[0] | 0x20 : 0; + return c >= 'a' && c <= 'm' ? c - 'a' + 1 : 0; +} + +// How strongly DLSS favours the current frame on characters and where they just were (TOTK_DLSS_CHAR_MASK, percent, +// live; 0 off). Where a character's vectors are the camera's only (dlaa_objmv = 0, or a draw the per-object pass +// skips), one that moves on its own drags a faint copy of itself through DLSS's history; the bias mask makes DLSS take +// less of the history there, at the cost of some of its anti-aliasing on those pixels. +static float char_bias(void) { + const char *v = cfg_get("TOTK_DLSS_CHAR_MASK"); + long n = v && *v ? strtol(v, NULL, 10) : 65; + return n <= 0 ? 0.0f : n >= 100 ? 1.0f : (float)n / 100.0f; +} + +// The quality setting DLSS is created with, from the scene's share of the output width. +static int perf_quality(uint32_t w, uint32_t ow) { + if (w >= ow) return NGX_PERFQ_DLAA; + double k = (double)w / ow; + return k >= 0.66 ? 2 : k >= 0.57 ? 1 : k >= 0.49 ? 0 : 3; +} + +// dst: the target DLSS's picture goes to, NULL for the light target itself (DLAA in place). +static void evaluate(VkExec *ex, const VkExecTarget *dst) { + const VkExecTarget *t = &D.color, *z = &D.depth; + if (!dst) dst = t; + uint32_t w = t->width, h = t->height, ow = dst->width, oh = dst->height; + VkCommandBuffer cb = ex->cb; + vk_exec_barrier(ex); // what drew the scene is done (no pass is open here) + if (D.preset_dirty) { // another model: features are created with it, so every size gets a new one + for (int i = 0; i < 3; i++) + if (D.sizes[i].out) free_size(&D.sizes[i]); + D.last = NULL, D.preset_dirty = 0; + } + struct Size *e = size_for(cb, w, h, ow, oh); + if (!e) return dl_fail("work image creation", 0); + if (e != D.last) D.reset = 1, D.why[0]++, e->chars_ok = 0; // another size's history is from another moment + if (!D.params && !NGX_OK(N.alloc_params(&D.params))) return dl_fail("parameter allocation", 0); + NgxParam *p = D.params; + VkImageView zv = vk_exec_target_view(z->img, z->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, z->level, + z->base_layer, 1, 0); + float jx = D.jit_used ? D.ux : 0.0f, jy = D.jit_used ? D.uy : 0.0f; + static int no_mv = -1; + if (no_mv < 0) no_mv = !rt_knob("DLAA_MV", "1"); // TOTK_DLAA_MV=0: zero motion (test) + int masked = 0; + if (no_mv || !mv_pass(ex, e, zv, jx, jy, char_bias(), &masked)) { + VkClearColorValue zero = {{0, 0, 0, 0}}; + VkImageSubresourceRange all = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; + vkCmdClearColorImage(cb, e->mv, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &all); + full_barrier(cb); + if (!no_mv) D.reset = 1, D.why[1]++; // no camera: the history cannot be moved along + } + VkImageView cv = vk_exec_target_view(t->img, t->format, VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT, t->level, + t->base_layer, 1, 0); + struct Snap *mk = D.mv_samp ? mask_pass(cb, cv, w, h) : NULL; + int timed = T.on > 0 && T.used[T.slot]; // mv_pass began this frame's slot + if (timed) times_mark(cb, T_DL0); + uint64_t c0 = timed ? kern_now_ns() : 0; + rec_drain(); // NGX records into the command buffer itself: the recorder thread must have made every call so far + if (timed) T.cpu_drain += kern_now_ns() - c0; + if (!e->feat) { + p_setu(p, "CreationNodeMask", 1); + p_setu(p, "VisibilityNodeMask", 1); + p_setu(p, "Width", w); + p_setu(p, "Height", h); + p_setu(p, "OutWidth", ow); + p_setu(p, "OutHeight", oh); + p_seti(p, "PerfQualityValue", perf_quality(w, ow)); + // IsHDR | MVLowRes | AutoExposure: linear scene light, motion vectors at the render size, no exposure texture + p_seti(p, "DLSS.Feature.Create.Flags", (1 << 0) | (1 << 1) | (1 << 6)); + p_seti(p, "DLSS.Enable.Output.Subrects", 0); + // The model per mode (NVSDK_NGX_DLSS_Hint_Render_Preset: 1 A ... 11 K, 12 L, 13 M), as NVIDIA recommends for + // DLSS 4.5 (310.x): K (transformer) for DLAA, Quality and Balanced, M for Performance, L for Ultra Performance. + // M and L cost more on cards without FP8 (RTX 20/30). dlss_preset (D.preset, preset_check) takes one model for + // every mode instead. + int pk = D.preset ? D.preset : 11, pm = D.preset ? D.preset : 13, pl = D.preset ? D.preset : 12; + p_setu(p, "DLSS.Hint.Render.Preset.DLAA", (unsigned)pk); + p_setu(p, "DLSS.Hint.Render.Preset.UltraQuality", (unsigned)pk); + p_setu(p, "DLSS.Hint.Render.Preset.Quality", (unsigned)pk); + p_setu(p, "DLSS.Hint.Render.Preset.Balanced", (unsigned)pk); + p_setu(p, "DLSS.Hint.Render.Preset.Performance", (unsigned)pm); + p_setu(p, "DLSS.Hint.Render.Preset.UltraPerformance", (unsigned)pl); + NgxResult r = N.create(cb, NGX_FEATURE_SUPERSAMPLING, p, &e->feat); + if (!NGX_OK(r) || !e->feat) return dl_fail("DLSS feature creation", r); + D.reset = 1; + int q = perf_quality(w, ow), pr = q == 0 ? pm : q == 3 ? pl : pk; + rt_log("dlaa: DLSS feature created at %ux%u -> %ux%u, quality value %d, preset %c (scene format %d, depth format " + "%d)", w, h, ow, oh, q, 'A' + pr - 1, (int)t->format, (int)z->format); + } + if (D.last_color != t->image) D.reset = 1, D.why[2]++, D.last_color = t->image; + NgxResourceVK rc = res_image(cv, t->image, t->format, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0); + NgxResourceVK ro = res_image(e->out_view, e->out, VK_FORMAT_R16G16B16A16_SFLOAT, ow, oh, VK_IMAGE_ASPECT_COLOR_BIT, 1); + NgxResourceVK rz = res_image(zv, z->image, z->format, w, h, VK_IMAGE_ASPECT_DEPTH_BIT, 0); + NgxResourceVK rm = res_image(e->mv_view, e->mv, VK_FORMAT_R16G16_SFLOAT, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0); + p_setp(p, "Color", &rc); + p_setp(p, "Output", &ro); + p_setp(p, "Depth", &rz); + p_setp(p, "MotionVectors", &rm); + p_setf(p, "Jitter.Offset.X", jx); + p_setf(p, "Jitter.Offset.Y", jy); + p_setf(p, "Sharpness", 0.0f); + p_seti(p, "Reset", D.reset); + // One bias mask: the reactive one (TOTK_DLSS_REACTIVE, a test) where it ran, else the characters' (mv_pass), else + // none. The parameter object is reused, so it is cleared explicitly. + NgxResourceVK rk = mk ? res_image(mk->mask_view, mk->mask, VK_FORMAT_R8_UNORM, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0) + : res_image(e->mask_view, e->mask, VK_FORMAT_R8_UNORM, w, h, VK_IMAGE_ASPECT_COLOR_BIT, 0); + p_setp(p, "DLSS.Input.Bias.Current.Color.Mask", mk || masked ? (void *)&rk : NULL); + p_setu(p, "DLSS.Input.Bias.Current.Color.Subrect.Base.X", 0); + p_setu(p, "DLSS.Input.Bias.Current.Color.Subrect.Base.Y", 0); + p_setf(p, "MV.Scale.X", 1.0f); + p_setf(p, "MV.Scale.Y", 1.0f); + p_setu(p, "DLSS.Render.Subrect.Dimensions.Width", w); + p_setu(p, "DLSS.Render.Subrect.Dimensions.Height", h); + p_setf(p, "DLSS.Pre.Exposure", 1.0f); + p_setf(p, "DLSS.Exposure.Scale", 1.0f); + // The time since the previous evaluation, which DLSS weighs its history by; 60 fps before the first and after a + // gap (a pause, a load), where no history carries over anyway. + uint64_t now = plat_now_ns(); + double dt = D.last_eval_ns ? (now - D.last_eval_ns) / 1e6 : 0; + D.last_eval_ns = now; + p_setf(p, "FrameTimeDeltaInMsec", dt > 0 && dt < 100 ? (float)dt : 16.67f); + c0 = timed ? kern_now_ns() : 0; + NgxResult r = N.evaluate(cb, e->feat, p, NULL); + if (timed) T.cpu_ngx += kern_now_ns() - c0; + if (!NGX_OK(r)) return dl_fail("DLSS evaluation", r); + if (timed) times_mark(cb, T_DL1); // through the recorder, after NGX's own commands + D.reset = 0; + D.last = e, e->used = D.evals + 1; + full_barrier(cb); + vk_mvcheck_output(cb, e->out, ow, oh); // TOTK_MV_CHECK (test) + // the result into the scene target (DLAA: the bloom, the post passes and the HUD read it and draw over it) or into + // the scaling pass's target (the HUD draws over it) + VkImageBlit b = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, + .srcOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}}, + .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, dst->level, dst->base_layer, 1}, + .dstOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}}}; + // TOTK_DLAA_MVDEBUG=1 (test): the motion vectors in place of the picture, x in red and y in green, render pixels + // as scene light (the post passes still run over them) + static int mvdebug = -1; + if (mvdebug < 0) mvdebug = rt_knob("DLAA_MVDEBUG", "0"); + static int maskdebug = -1; + if (maskdebug < 0) maskdebug = rt_knob("DLAA_MASKDEBUG", "0"); + if (mvdebug || (maskdebug && mk)) b.srcOffsets[1] = (VkOffset3D){(int32_t)w, (int32_t)h, 1}; + vkCmdBlitImage(cb, mvdebug ? e->mv : maskdebug && mk ? mk->mask : e->out, VK_IMAGE_LAYOUT_GENERAL, dst->image, VK_IMAGE_LAYOUT_GENERAL, 1, &b, + VK_FILTER_NEAREST); + full_barrier(cb); + if (dst != t) vk_dlaa_swap_img = t->img, vk_dlaa_swap_view = e->out_view; // the scaling draw reads DLSS's picture + ex->recorded += 4; + vk_exec_draw_new_cb(); // NGX bound pipelines and descriptor sets of its own: the next draw binds everything again + D.jdepth = z->image; + next_jitter(w, ow); + if (++D.evals == 1 || D.evals == 300 || D.evals % 3600 == 0) // 300: one early line with the frames counted + rt_log("dlaa: %llu frames anti-aliased (scene %ux%u, output %ux%u; %llu frames without the DLAA point, %llu with camera " + "motion, %llu with the characters' mask, %llu without a camera, %llu jitter holds; resets: %llu size, %llu " + "camera, %llu target; reactive mask %llu, without its copy %llu)", + (unsigned long long)D.evals, w, h, ow, oh, (unsigned long long)D.frames_missed, + (unsigned long long)D.mv_frames, (unsigned long long)D.masked, (unsigned long long)D.mv_none, + (unsigned long long)D.holds, (unsigned long long)D.why[0], (unsigned long long)D.why[1], + (unsigned long long)D.why[2], (unsigned long long)R.made, (unsigned long long)R.missed); +} + +static int scene_color(VkFormat f) { + return f == VK_FORMAT_B10G11R11_UFLOAT_PACK32 || f == VK_FORMAT_R16G16B16A16_SFLOAT; +} + +void vk_dlaa_pass(VkExec *ex) { + const VkExecTarget *c = &ex->color[0], *z = &ex->depth; + if (!dl_active() || D.done) return; + // The G-buffer: several colour targets with a D32F depth of at least DLAA_SCENE_MIN_W wide. Only the light target drawn after it + // arms the frame; at night the game draws into the light target alone with the scene depth before the G-buffer too, + // and DLAA then ran at a small pass ahead of the whole scene, which reached the screen jittered. + if (ex->pass_ncolor >= 3 && z->image && z->format == VK_FORMAT_D32_SFLOAT && z->width >= DLAA_SCENE_MIN_W) { + D.gbuf = 1, D.armed = 0; + // target 0 is the object-class buffer (RGBA8, the scene size): the characters' bias mask reads its green. A + // smaller pass of the same shape (640x360, seen while loading) does not replace the scene's in the same frame. + if (c->image && c->format == VK_FORMAT_R8G8B8A8_UNORM && c->width == z->width && c->height == z->height && + c->samples <= 1 && !c->is3d && (!D.ids_ok || c->width >= D.ids.width)) + D.ids = *c, D.ids_ok = 1; + return; + } + // the scene depth is D32F; a D16 target of the scene size also serves one composite pass before the transparents. + // A smaller pair never takes over an armed one: the camera rune draws a half-size RGBA16F target with a half-size + // D32F depth after the lighting (852x480 under a 1704x960 scene), and DLSS ran on that and wrote its picture into + // the scene's own light target, which left every object black. + if (D.gbuf && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT && + c->width == z->width && c->height == z->height && c->width >= DLAA_SCENE_MIN_W && + (!D.armed || (c->width >= D.color.width && c->height >= D.color.height)) && + c->samples <= 1 && !c->is3d) { + int first = !D.armed; + D.color = *c, D.depth = *z, D.armed = 1; + if (first) mask_snapshot(ex); // the lit scene, before the transparents (reactive mask) + return; + } + if (!D.armed) return; + if (!D.ended) { + if (!ex->pass_w || ex->pass_w * 4 > D.color.width) return; + D.ended = 1; + unsigned o[2], s[2]; + res_sizes(o, s); + if (D.color.width >= o[0]) D.done = 1, evaluate(ex, NULL); // DLAA in place; else wait for the scaling pass + return; + } + // the game's scaling pass: one colour target of the output size, no depth + unsigned o[2], s[2]; + res_sizes(o, s); + if (ex->pass_ncolor == 1 && c->image && !z->image && scene_color(c->format) && c->width > D.color.width && + c->width == o[0] && c->height == o[1] && c->samples <= 1 && !c->is3d) { + D.done = D.up = 1; + evaluate(ex, c); + } +} + +// TOTK_DLSS, read at each present (the menu switches it live): the scene size of the mode (resolution.c) and the +// texture bias that goes with it while DLSS runs. +static void dlss_mode(void) { + static char mode[32]; + static int bias_on = -1; + const char *m = cfg_value("TOTK_DLSS"); + if (strcmp(m, mode)) { + snprintf(mode, sizeof mode, "%s", m); + res_dlss_mode(m); + bias_on = -1; + } + int b = dl_active(); + if (b != bias_on) bias_on = b, vk_sampler_dlss_bias(b ? res_dlss_lod_bias() : 0.0f); +} + + +// The preset, read again when a setting changed (the menu); a different one recreates the features at the next DLAA run. +static void preset_check(void) { + unsigned v = cfg_version(); + if (v == D.cfg_seen) return; + D.cfg_seen = v; + int p = preset_value(); + if (p == D.preset) return; + if (D.evals) { + if (p) rt_log("dlaa: DLSS preset %c in every mode; the features are created again", 'A' + p - 1); + else rt_log("dlaa: DLSS presets per mode; the features are created again"); + } + D.preset = p, D.preset_dirty = 1, D.reset = 1; +} + +void vk_dlaa_frame(void) { + vk_dlaa_swap_img = NULL, vk_dlaa_swap_view = VK_NULL_HANDLE; + dlss_mode(); + preset_check(); + int on = N.available && cfg_bool("TOTK_DLAA"); + if (on != D.on) { + rt_log("dlaa: %s", on ? "on" : "off"); + D.on = on; + // either way the next DLAA frame starts a new history from an unjittered frame + D.reset = 1, D.cam_prev_ok = 0, D.jit_ok = D.jit_used = D.armed = D.done = D.gbuf = D.ended = D.up = 0; + D.hold = 0; + return; + } + if (!dl_active()) return; + if (!D.done) D.frames_missed++; + if (D.jit_used && !D.done) D.hold = 60, D.holds++; // jittered but not resolved: that frame shook + else if (D.hold) D.hold--; + if (!D.done) D.jit_ok = 0; + D.armed = D.done = D.jit_used = D.gbuf = D.ended = D.up = D.ids_ok = 0; + D.cam_addr = 0; +} + +// ---------------------------------------------------------------- the game's FXAA (TOTK_FXAA, live) +// TotK's FXAA is two full-screen passes after the bloom, just before the HUD: one copies the scene target into a +// scratch target, the other draws the anti-aliased result back over the scene (found by diffing frame dumps with the +// game's own switch, ModelSceneExtensionParam EnableFXAA, on and off). With fxaa = 0 the draws of that second pass's +// fragment shader are skipped, so the scene reaches the HUD as it was. Shaders are known by the FNV-1a hash of their +// SPIR-V, as vk_dump.c names them; a translator change gives new hashes and the switch then does nothing. +// With a scene smaller than the output (the game's own 5/6 render scale, or a DLSS mode) the same shader draws the +// scaled picture into the output-sized target, in a pass without the depth attachment the in-place FXAA has: there it +// is always drawn, whatever fxaa says (skipping it would leave the 3D picture out), unless DLSS already wrote that +// target this frame. +// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, and the one with patches +// 0010 and 0011 (the shadow filter spread) applied. A scaling draw this list misses still scales DLSS's picture, not +// the jittered scene: after DLSS ran, passes larger than the light target sample DLSS's output (vk_dlaa_swap_img). +static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull}; + +static uint64_t spirv_hash(const GfxShader *s) { + static struct { const GfxShader *s; uint64_t h; } tab[8192]; + uintptr_t k = ((uintptr_t)s >> 4) * 0x9E3779B97F4A7C15ull; + for (uint32_t i = 0, j = (uint32_t)(k >> 51); i < 8192; i++, j = (j + 1) & 8191) { + if (tab[j].s == s) return tab[j].h; + if (!tab[j].s) { + uint64_t words = 0, h = 0xcbf29ce484222325ull; + const uint8_t *b = (const uint8_t *)gfx_shader_spirv(s, &words); + for (uint64_t n = 0; b && n < words * 4; n++) h = (h ^ b[n]) * 0x100000001b3ull; + tab[j].s = s, tab[j].h = h; + return h; + } + } + return 0; // table full: no skip +} + +int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs) { + static uint64_t frame_read = ~0ull; + static int fxaa = 1; + // fxaa = 0 reports once whether it found the game's FXAA draw: the first skip, or a minute of frames (3600) with + // none, which is what a translator change that moves FXAA_FS looks like on every GPU, with or without DLSS + static uint64_t fx_off_frames; + static int fx_found, fx_reported; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame_read) { // once a frame: the menu switches it live + frame_read = f, fxaa = cfg_bool("TOTK_FXAA"); + if (!fxaa && !fx_found && !fx_reported && ++fx_off_frames == 3600) { + fx_reported = 1; + rt_log("dlaa: fxaa = 0 found no draw of the game's FXAA in %llu frames (its fragment shader is not in " + "FXAA_FS): the FXAA stays on", (unsigned long long)fx_off_frames); + } + } + if (fxaa && !D.up) return 0; // nothing to skip: no shader lookup per draw at the defaults + if (!fs) return 0; + uint64_t h = spirv_hash(fs); + int known = 0; + for (size_t i = 0; i < sizeof FXAA_FS / sizeof FXAA_FS[0]; i++) known |= h == FXAA_FS[i]; + if (!known) return 0; + if (!ex->depth.image) return D.up; // the scaling pass, which DLSS replaced + if (!fxaa && !fx_found) { + fx_found = 1; + rt_log("dlaa: fxaa = 0 skips the game's FXAA (fragment shader %016llx)", (unsigned long long)h); + } + return !fxaa; +} + diff --git a/runtime/vk_dlaa.h b/runtime/vk_dlaa.h new file mode 100644 index 00000000..82b98113 --- /dev/null +++ b/runtime/vk_dlaa.h @@ -0,0 +1,48 @@ +// DLAA (vk_dlaa.c): NVIDIA DLSS at the scene's own size, behind TOTK_DLAA (totk.ini dlaa = 1). +#pragma once +#include "vk.h" + +// The narrowest scene the DLSS pass and the motion passes recognise by its D32F depth: Ultra Performance at a 1080p +// output renders 640 wide, smaller windows less. +#define DLAA_SCENE_MIN_W 320u + +int vk_dlaa_wanted(void); // the setting is on (the device may still lack DLSS) +// vk_device.c: NGX's instance and device extensions join the lists (room for max names); vk_dlaa_init after the +// device exists brings NGX up and checks that DLSS is available. +void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max); +void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n, uint32_t max); +void vk_dlaa_init(void); +// 1 when dlaa is on and NGX came up with DLSS (after vk_dlaa_init): a DLSS mode's smaller scene is scaled up by DLSS. +int vk_dlaa_available(void); +// vk_exec.c vk_exec_pass_begin, once the pass's targets are resolved and before its barrier: arms the frame on the +// scene's light target or, at the first quarter-width pass after it, runs DLAA on that target. Each present ends the +// frame (vk_dlaa_frame). +typedef struct VkExec VkExec; +void vk_dlaa_pass(VkExec *ex); +void vk_dlaa_frame(void); +// vk_exec_draw.c, each draw before its fixed state is recorded: notes the scene camera block (vk_dlaa_draw), and moves +// the viewport of the scene's draws by the frame's jitter (vk_dlaa_viewport, when the viewport is recorded). +void vk_dlaa_draw(VkExec *ex); +void vk_dlaa_viewport(VkExec *ex, VkViewport *v); +// vk_mvsurvey.c (TOTK_MV_SURVEY): notes the uniform blocks of each scene draw, beside vk_dlaa_draw; each present +// compares them with the previous frames' and logs a summary now and then. +void vk_mvsurvey_draw(VkExec *ex); +void vk_mvsurvey_frame(void); +void vk_mvprev_draw(VkExec *ex); // vk_mvprev.c: notes the scene draw's object and bone blocks +// Per-object motion (vk_exec_draw.c): the moving scene draws are listed again during the replay with +// their vertex shader's last-frame variant; vk_dlaa.c mv_pass records them over the camera motion into ex->cb (and +// leaves every binding of the replay to be set again; draws whose camera block is not cam are left out). +// vk_dlaa_prev_camera: last frame's camera block and this frame's jitter, 0 when there is no history. +int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth, VkFormat depth_fmt, uint32_t w, + uint32_t h); +int vk_dlaa_prev_camera(VkDescriptorBufferInfo *out, float jitter[2]); +// vk_mvcheck.c (TOTK_MV_CHECK, test): mv_pass hands it the motion image after the camera pass and, after the object +// pass, the motion image again and the scene light target it ran on; it measures how well the vectors fit the picture. +void vk_mvcheck_camera(VkCommandBuffer cb, VkImage mv, uint32_t w, uint32_t h, float jx, float jy); +void vk_mvcheck_final(VkCommandBuffer cb, uint64_t fence, VkImage mv, VkImage cimg, VkFormat cfmt, uint32_t level, + uint32_t layer, uint32_t w, uint32_t h, float jx, float jy); +const char *vk_mvobj_frame_masks(void); // vk_exec_draw.c: the last motion list, per position and changed banks +void vk_mvcheck_output(VkCommandBuffer cb, VkImage out, uint32_t ow, uint32_t oh); // after DLSS on a check frame +// vk_exec_draw.c, once a draw's fragment shader is known: 1 skips the draw (the game's FXAA while TOTK_FXAA is 0, its +// scaling draw when DLSS wrote the scaled picture). +int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs); diff --git a/runtime/vk_dump.c b/runtime/vk_dump.c index ce87e8f5..6f7e1e94 100644 --- a/runtime/vk_dump.c +++ b/runtime/vk_dump.c @@ -18,8 +18,8 @@ // as x/2+1/2, UNORM and sRGB bytes written as stored. Caps: MAX_PASSES passes copied, PNG_CAP bytes of PNGs. // TOTK_DUMP_TEX=<id>|<w>x<h>[,...]: those texture slots, or 2D textures of that size, are copied where first sampled, // whatever their format. -// TOTK_DUMP_UBO=1: each listed pipeline also prints the first 16 vec4 of its user uniform blocks (c3 and up), read from -// guest memory when the entry is listed. A block the submission rewrites with UpdateUniformBuffer shows its value from +// TOTK_DUMP_UBO=<n>: each listed pipeline also prints the first n vec4 of its user uniform blocks (c3 and up; 1 means 16, +// the cap is 4096), read from guest memory when the entry is listed. A block the submission rewrites with UpdateUniformBuffer shows its value from // before the submission (vk_exec_draw.c upd_note). // TOTK_DUMP_SKIP_RUNS=1: a render pass with the same targets as the render pass before it is logged but not copied // (the terrain decals draw one pass each into the G-buffer and would use up both caps). @@ -48,7 +48,7 @@ static uint64_t dump_frame, log_from; // log_from: TOTK_DUMP_LOG, first frame static char dir[400]; static FILE *passes_f, *images_f; static int seq, finished, copied; // copied: passes and dispatches of the dump frame whose images were copied -static int skip_runs, dump_ubo; // TOTK_DUMP_SKIP_RUNS, TOTK_DUMP_UBO +static int skip_runs, dump_ubo; // TOTK_DUMP_SKIP_RUNS, TOTK_DUMP_UBO (vec4 per block, 0 for none) static uint32_t want_tex[16][2]; // TOTK_DUMP_TEX: slot id and 0, or width and height static int nwant_tex; static uint64_t last_targets; // targets_sig of the last render pass; 0 after a dispatch @@ -576,7 +576,7 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info) if (u->addr && vk_mem_resolve(u->addr, 1, &ms)) { uint64_t n = u->size && u->size < ms.avail ? u->size : ms.avail; const float *f = (const float *)(uintptr_t)u->addr; - for (uint64_t k = 0; k < 16 && (k + 1) * 16 <= n; k++) + for (uint64_t k = 0; k < (uint64_t)dump_ubo && (k + 1) * 16 <= n; k++) sp(b, "%s[%llu] %g %g %g %g", k % 4 ? " " : "\n ", (unsigned long long)k, f[4 * k], f[4 * k + 1], f[4 * k + 2], f[4 * k + 3]); } else { @@ -870,7 +870,10 @@ void vk_dump_init(void) { const char *sr = cfg_get("TOTK_DUMP_SKIP_RUNS"); skip_runs = sr && *sr && *sr != '0'; const char *du = cfg_get("TOTK_DUMP_UBO"); - dump_ubo = du && *du && *du != '0'; + dump_ubo = du && *du ? atoi(du) : 0; + if (dump_ubo == 1) dump_ubo = 16; + if (dump_ubo > 4096) dump_ubo = 4096; + if (dump_ubo < 0) dump_ubo = 0; const char *wt = cfg_get("TOTK_DUMP_TEX"); for (char *q = (char *)wt; q && *q && nwant_tex < 16; q += *q == ',') { uint32_t *w = want_tex[nwant_tex++]; diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 98224dad..2709471d 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -27,6 +27,7 @@ #include "tex.h" #include "vk.h" #include "vk_exec.h" +#include "vk_dlaa.h" #define WEAK __attribute__((weak)) #define CALL_DEPTH 8 @@ -345,6 +346,7 @@ static void resolve_target(const tex_t *t, const view_t *v, int has_v, int depth } o->image = img->image; o->img = img; + o->scale = img->scale ? img->scale : 1; o->format = fmt; o->view_type = layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; o->aspect = depth_slot ? img->aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) @@ -380,7 +382,7 @@ static void resolve_rt(VkExec *ex) { ex->rt_resolved = 1; const p_set_rt *r = &ex->rt; int n = r->n < 0 ? 0 : r->n > 8 ? 8 : r->n; - uint32_t w = UINT32_MAX, h = UINT32_MAX, layers = UINT32_MAX, samples = 0; + uint32_t w = UINT32_MAX, h = UINT32_MAX, layers = UINT32_MAX, samples = 0, scale = 1; for (int i = 0; i < 8; i++) { VkExecTarget *t = &ex->color[i]; resolve_target(i < n ? r->color[i] : NULL, &r->cview[i], i < n && r->has_cview[i], 0, t); @@ -391,6 +393,7 @@ static void resolve_rt(VkExec *ex) { if (t->height < h) h = t->height; if (t->layers < layers) layers = t->layers; if (!samples) samples = t->samples; + if (t->scale > scale) scale = t->scale; } VkExecTarget *d = &ex->depth; resolve_target(r->depth, &r->dview, r->has_dview, 1, d); @@ -402,6 +405,7 @@ static void resolve_rt(VkExec *ex) { if (d->height < h) h = d->height; if (d->layers < layers) layers = d->layers; if (!samples) samples = d->samples; + if (d->scale > scale) scale = d->scale; } ex->pass_noatt = w == UINT32_MAX; if (ex->pass_noatt) { @@ -413,6 +417,7 @@ static void resolve_rt(VkExec *ex) { ex->vp[0][1], ex->vp[0][2], ex->vp[0][3]); } ex->pass_w = w, ex->pass_h = h, ex->pass_layers = layers; + ex->pass_scale = ex->pass_noatt ? 1 : scale; ex->pass_ncolor = (uint32_t)n; ex->pass_samples = samples ? samples : 1; ex->pipe.samples = (uint8_t)ex->pass_samples; @@ -571,6 +576,8 @@ void vk_exec_barrier(VkExec *ex) { barrier_to(ex, NULL, NULL, 0); } void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); + vk_contact_pass_begin(ex); + vk_dlaa_pass(ex); // DLAA runs before the bloom chain's first pass (vk_dlaa.c) // Learned targets (VkTexImage.fb_learned) are attached in the feedback layout VkImage want[9]; VkImageAspectFlags wasp[9]; @@ -605,6 +612,20 @@ void vk_exec_pass_begin(VkExec *ex) { if (d->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) da = a; if (d->aspect & VK_IMAGE_ASPECT_STENCIL_BIT) sa = a; } + // The start mark goes between the barrier and vkCmdBeginRendering, its end right after vkCmdEndRendering + // (vk_exec_pass_end): outside the pass, and without breaking it. + VkPassMark *m = ex->mark = vk_pass_mark_begin(ex->cb, VK_MARK_PASS, ex->fence); + if (m) { + m->step = ex->mark_passes++; + m->w = ex->pass_w, m->h = ex->pass_h, m->d = ex->pass_layers; + m->ncolor = (uint8_t)(ex->pass_ncolor < 8 ? ex->pass_ncolor : 8); + for (int i = 0; i < m->ncolor; i++) m->color[i] = ex->pass_color[i]; + m->depth = ex->pass_depth ? ex->pass_depth : ex->pass_stencil; + // The shaders bound now are the first draw's when it repeats the pipeline state; when it does not, + // vk_exec_draw.c prepare_draw replaces them as it records the new state. + vk_exec_draw_shaders(&m->first[0], &m->first[1]); + ex->mark_draws0 = vk_exec_frame.draws; + } VkRenderingInfo ri = {.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {ex->pass_w, ex->pass_h}}, .layerCount = ex->pass_layers, .colorAttachmentCount = ex->pass_ncolor, .pColorAttachments = ca, @@ -622,6 +643,14 @@ void vk_exec_pass_end(VkExec *ex) { vkCmdEndRendering(ex->cb); ex->recorded++; ex->in_pass = 0; + if (ex->mark) { + // vk_exec_frame_log resets the frame counters at a present (TOTK_TRACE=gfx): then only the draws since it count + uint64_t d = vk_exec_frame.draws, d0 = ex->mark_draws0; + ex->mark->draws = (uint32_t)(d >= d0 ? d - d0 : d); + if (ex->mark->draws) vk_exec_draw_shaders(&ex->mark->last[0], &ex->mark->last[1]); + vk_pass_mark_end(ex->cb, ex->mark); + ex->mark = NULL; + } for (int i = 0; i < ex->pass_ncolor && i < 8; i++) // stand-ins of 3D BC targets go to their textures if (ex->color[i].image && ex->color[i].img && ex->color[i].img->image != ex->color[i].image) bc_copy_back(ex, &ex->color[i]); @@ -667,8 +696,9 @@ VkBuffer vk_exec_inline_buffer(VkDeviceSize size) { VkRect2D vk_exec_scissor(const VkExec *ex) { int64_t w = ex->pass_w, h = ex->pass_h; if (!ex->has_scissor) return (VkRect2D){{0, 0}, {(uint32_t)w, (uint32_t)h}}; - int64_t x0 = ex->scissor.x, y0 = ex->scissor.y; - int64_t x1 = x0 + ex->scissor.w, y1 = y0 + ex->scissor.h; + int64_t k = ex->pass_scale > 1 ? ex->pass_scale : 1; + int64_t x0 = ex->scissor.x * k, y0 = ex->scissor.y * k; + int64_t x1 = x0 + (int64_t)ex->scissor.w * k, y1 = y0 + (int64_t)ex->scissor.h * k; if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > w) x1 = w; @@ -893,6 +923,8 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t ex->fence = fence; ex->recorded = 0; ex->in_pass = 0; + ex->mark = NULL; + ex->mark_passes = ex->mark_dispatches = 0; ex->dirty = VK_EXEC_DIRTY_ALL; ex->rt_resolved = 0; // images may have changed since the last command buffer ex->nviews = 0; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6236929e..9e61e34a 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -35,6 +35,7 @@ typedef struct { uint32_t width, height, samples; // extent of that level; with blocks, in blocks int32_t nvn_format; // NVNformat of the view (VkPipeState.color_fmt / depth_fmt) const VkTexImage *img; // the image (fb_usage, fb_learned) + uint8_t scale; // VkTexImage.scale } VkExecTarget; // The replay state. One instance lives for the run: NVN state is queue state and carries over from one submission to @@ -46,12 +47,19 @@ typedef struct VkExec { int recorded; // Vulkan commands recorded so far; every handler adds the ones it records int in_pass; // inside vkCmdBeginRendering uint32_t dirty; // VK_EXEC_DIRTY_* + // Pass marks (vk.h vk_pass_mark_begin): the open pass's record (NULL: none or marks off), vk_exec_frame.draws when + // it began, and the passes and dispatches this submission marked so far. + VkPassMark *mark; + uint64_t mark_draws0; + uint32_t mark_passes, mark_dispatches; // ---- render targets (OP_SET_RT) and the current pass p_set_rt rt; // last OP_SET_RT payload (a change ends the open pass); rt.n 0, no depth: none int rt_resolved; // color[] and depth hold this command buffer's images for rt VkExecTarget color[8], depth; // resolved targets uint32_t pass_w, pass_h, pass_layers; // render area and layer count of the open (or next) pass + uint32_t pass_scale; // its targets' VkTexImage.scale: game viewports and scissors + // are multiplied by it int pass_noatt; // the pass has no attachment: its size follows the draws (vk_exec_noatt_dims) VkFormat pass_color[8], pass_depth, pass_stencil; // attachment formats of the pass (VkPipelineRenderingCreateInfo); // VK_FORMAT_UNDEFINED: none @@ -123,9 +131,14 @@ void vk_exec_inline_end(int reopen); // vk_exec_draw.c: forget what the command buffer being recorded has bound (pipeline, sets, dynamic state, vertex // buffers): the replay continues in a new one (vk_sync_split). void vk_exec_draw_new_cb(void); +// vk_exec_draw.c: the vertex and fragment shader of the last draw recorded into this command buffer (NULL: none yet). +void vk_exec_draw_shaders(const GfxShader **vs, const GfxShader **fs); VkBuffer vk_exec_inline_buffer(VkDeviceSize size); // vk_exec.c: the stand-in a 3D BC render target is drawn through goes with its image (vk_exec_image_gone). void vk_exec_bc_gone(VkImage image); +// vk_hdr.c: with TOTK_HDR=1, notes a draw into a presented window texture (vk_hdr_track: set by the first present). +extern int vk_hdr_track; +void vk_hdr_draw(VkExec *ex, const VkPipeInfo *info); // Scissor 0 clipped to the render area of the open pass; extent 0: nothing to draw. VkRect2D vk_exec_scissor(const VkExec *ex); void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set @@ -175,6 +188,13 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV // vk_exec_xfer.c: OP_COPY_B2B, OP_COPY_B2T, OP_COPY_T2B, OP_COPY_T2T, OP_CLEAR_TEX, OP_SIGNAL_EVENT, OP_REPORT_COUNTER, // OP_RESET_COUNTER, OP_RENDER_ENABLE, OP_RENDER_COND, OP_CLEAR_BUF. void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload); +// vk_dlaa.c: once DLSS has scaled the scene this frame, passes larger than the scene's light target vk_dlaa_swap_img +// sample DLSS's output vk_dlaa_swap_view in its place, so the game's scaling draw scales DLSS's picture. +extern const VkTexImage *vk_dlaa_swap_img; +extern VkImageView vk_dlaa_swap_view; +// vk_contact.c: a pass about to begin (its targets resolved, no pass open), and a dispatch or copy about to be recorded +void vk_contact_pass_begin(VkExec *ex); +void vk_contact_flush(VkExec *ex); // vk_exec_xfer.c, samples-passed counting: a draw about to be recorded in the open pass (begins an occlusion query // when counting and none is active), and the open pass about to end (ends the active query). void vk_exec_oq_draw(VkExec *ex); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 976d3285..45bd084a 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -28,12 +28,14 @@ #include "vk.h" #include "vk_exec.h" #include "platform/platform.h" +#include "vk_dlaa.h" #define MAX_BINDINGS 128 #define MAX_DESC 1024 #define CHUNK_BYTES (4u << 20) #define ZERO_BYTES 65536u -#define SUPPORT_BYTES 1408u // Ryujinx SupportBuffer (std140): alpha_test, is_bgra[8], viewport_inverse, viewport_size, ... +#define SUPPORT_BYTES 1456u // Ryujinx SupportBuffer (std140): alpha_test, is_bgra[8], viewport_inverse, viewport_size, ...; + // 1408 pcss and 1424 shadow_spread (translator patches 0010, 0011), 1440 HDR (support_fill) #define NVN_STAGE_VS 0 #define NVN_STAGE_FS 1 #define NVN_STAGE_CS 5 @@ -196,6 +198,21 @@ static void cb_done(void *arg) { // completion thread void vk_exec_draw_new_cb(void) { memset(&B, 0, sizeof B); } +void vk_exec_draw_shaders(const GfxShader **vs, const GfxShader **fs) { + *vs = B.st_valid ? B.st.vs : NULL; + *fs = B.st_valid ? B.st.fs : NULL; +} + +// Marks a dispatch about to be recorded (vk.h vk_pass_mark_begin); the caller ends it after the dispatch. +static VkPassMark *dispatch_mark(VkExec *ex, const GfxShader *cs, uint32_t x, uint32_t y, uint32_t z) { + VkPassMark *m = vk_pass_mark_begin(ex->cb, VK_MARK_DISPATCH, ex->fence); + if (!m) return NULL; + m->step = ex->mark_dispatches++; + m->w = x, m->h = y, m->d = z, m->draws = 0, m->ncolor = 0; + m->first[0] = cs, m->first[1] = m->last[0] = m->last[1] = NULL; + return m; +} + static CbRes *cb_res(VkExec *ex) { if (cur && cur_fence == ex->fence) return cur; cur = calloc(1, sizeof *cur); @@ -232,8 +249,13 @@ static uint8_t *scratch(VkExec *ex, uint64_t size, VkBuffer *buf, VkDeviceSize * .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT}; + // HOST_COHERENT: the CPU fills these chunks and never flushes them. Intel's integrated GPUs (ANV, Meteor Lake) + // offer host-cached memory only without coherence, which VMA picks for random host access: the GPU then read + // what an earlier submission left in the chunk (an UpdateUniformBuffer payload of another draw: a lighting + // shader's step count of 0x3f800000 instead of 128, and a loop of a billion steps). VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT}; + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT}; VmaAllocationInfo info; VkResult res = vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &c->buf, &c->alloc, &info); if (res != VK_SUCCESS) rt_fatal("vk: transient buffer of %llu bytes failed (%d)", (unsigned long long)n, (int)res); @@ -349,7 +371,8 @@ static void dummies_init(void) { VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT}; VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST, - .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT}; + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT}; // the memset below is not flushed VmaAllocationInfo info; if (vmaCreateBuffer(c->vma, &bi, &ai, &Z.zbuf, &Z.zalloc, &info) != VK_SUCCESS) rt_fatal("vk: zero buffer failed"); memset(info.pMappedData, 0, ZERO_BYTES); @@ -567,6 +590,7 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV void vk_exec_image_gone(VkImage image) { vk_exec_bc_gone(image); + vk_hdr_image_gone(image); if (!views_by_image.klen) return; for (ViewNode *n = nvn_hm_del(&views_by_image, &image), *next; n; n = next) { next = n->next; @@ -707,6 +731,8 @@ static void answers_for(VkExec *ex, int st, const uint8_t *ctl, uint32_t mask, G // *present: the program has this stage. Returns NULL when it has not or the translation failed. pending (graphics // stages, vk_pipeline_async_on): a shader not translated yet is queued and NULL returned with *pending = 1. await: a // shader queued earlier is waited for (gfx_shader_await) instead. +static int sf_mvprev; // shader_for asks for the vertex shader's last-frame variant (per-object motion) + static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pending, int await) { const uint8_t *ctl, *code; uint32_t len; @@ -717,6 +743,7 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin GfxShaderAnswers a; uint64_t serial = ex->prog->serial; answers_for(ex, st, ctl, slot_mask_of(serial, st), &a); + a.mvprev = (uint8_t)(sf_mvprev && st == NVN_STAGE_VS); ShaderEnt *set = NULL; if (serial) { uint64_t h = (serial * 0x9E3779B97F4A7C15ull ^ (uint64_t)st) ^ nvn_hm_hash(&a, sizeof a); // a is zero-padded @@ -734,7 +761,7 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin } int gs = st == NVN_STAGE_VS ? GFX_STAGE_VS : st == NVN_STAGE_FS ? GFX_STAGE_FS : GFX_STAGE_CS; const GfxShader *s = await ? gfx_shader_await(gs, ctl, code, len, &a) - : pending && vk_pipeline_async_on() ? gfx_shader_get_async(gs, ctl, code, len, &a, pending) + : pending && (vk_pipeline_async_on() || a.mvprev) ? gfx_shader_get_async(gs, ctl, code, len, &a, pending) : gfx_shader_get(gs, ctl, code, len, NULL, &a); if (pending && *pending) return NULL; if (s) slot_mask_learn(serial, st, s); @@ -871,7 +898,14 @@ static void limits_init(void) { __atomic_store_n(&LIM.ubo_max, l->maxUniformBufferRange, __ATOMIC_RELEASE); } +// Per-object motion (section at the end): while it records a draw again, these replace the uniform bindings of the +// vertex stage's slots 18-20 (last frame's camera, bones and place) and the fragment stage's slot 3 (its parameters). +static int mo_ovr_on; +static VkDescriptorBufferInfo mo_ovr[2][32]; // [vertex, fragment][slot]; buffer 0: not replaced + static VkDescriptorBufferInfo ubo_info(VkExec *ex, int st, int slot) { + if (mo_ovr_on && (st == NVN_STAGE_VS || st == NVN_STAGE_FS) && slot >= 0 && slot < 32 && mo_ovr[st][slot].buffer) + return mo_ovr[st][slot]; if (!__atomic_load_n(&LIM.ubo_max, __ATOMIC_ACQUIRE)) limits_init(); uint64_t max = LIM.ubo_max, align = LIM.ubo_align; VkDescriptorBufferInfo bi; @@ -1374,9 +1408,13 @@ static Twin *twin_update(VkExec *ex, const VkTexImage *img) { } // The image descriptor of slot h; in a dispatch, a compare-sampled colour image the driver cannot compare is swapped -// for its depth twin. +// for its depth twin; in a pass larger than it, the scene's light target, once DLSS scaled it this frame, for DLSS's +// output (vk_dlaa.c). static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, VkDescriptorImageInfo *out) { tex_info_lookup(ex, r, h, storage, separate, out); + if (h && !storage && vk_dlaa_swap_img && ex->in_pass && ex->pass_w > vk_dlaa_swap_img->width + 1 && + vk_texture_by_id_locked((int)(h & 0xfffff), NULL, NULL) == vk_dlaa_swap_img) + out->imageView = vk_dlaa_swap_view, out->imageLayout = VK_IMAGE_LAYOUT_GENERAL; if (!twin_dispatch || storage || separate || !h || !(r->type & 0x400)) return; VkImageViewType vt = res_view_type(r->type); if (vt != VK_IMAGE_VIEW_TYPE_2D && vt != VK_IMAGE_VIEW_TYPE_2D_ARRAY) return; @@ -1419,11 +1457,47 @@ static VkDescriptorSet alloc_set(VkExec *ex, VkDescriptorSetLayout layout) { } static void support_in(const VkExec *ex, float in[3]) { // what support_fill reads: viewport size, point size - in[0] = ex->in_pass && ex->has_vp ? ex->vp[0][2] : (float)ex->pass_w; - in[1] = ex->in_pass && ex->has_vp ? ex->vp[0][3] : (float)ex->pass_h; + float k = ex->pass_scale > 1 ? (float)ex->pass_scale : 1.0f; + in[0] = ex->in_pass && ex->has_vp ? ex->vp[0][2] * k : (float)ex->pass_w; + in[1] = ex->in_pass && ex->has_vp ? ex->vp[0][3] * k : (float)ex->pass_h; in[2] = ex->point_size > 0 ? ex->point_size : 1.0f; } +// TOTK_PCSS (percent, 0 = off): the pcss field of the support buffer, which translator patch 0010 reads in every +// explicit-LOD compare on a 2D array texture (the sun cascades). x: penumbra width in UV per unit of light-space depth +// between receiver and blocker (0 keeps the game's single compare), y and z: the narrowest and widest disc radius, +// w: the blocker search radius, all in UV of the cascade map. Radii are in UV, so they hold at any TOTK_SHADOW_SCALE. +// TOTK_PCSS_K overrides the depth-to-penumbra factor at 100 percent, for tuning. +static const float *pcss_params(void) { + static float p[4] = {-1, 0, 0, 0}; + if (p[0] < 0) { + int pct = cfg_int("TOTK_PCSS"); + pct = pct < 0 ? 0 : pct > 200 ? 200 : pct; + const char *k = cfg_get("TOTK_PCSS_K"); + float k100 = k && *k ? (float)atof(k) : 0.5f; + p[0] = k100 * (float)pct / 100.0f; + p[1] = 0.35f / 960.0f, p[2] = 5.0f / 960.0f, p[3] = 4.0f / 960.0f; + if (pct) rt_log("vk: soft shadows (PCSS) at %d%% (depth factor %g)", pct, p[0]); + } + return p; +} + +// The shadow_spread field of the support buffer: the factor translator patch 0011 applies to the lighting shaders' sun +// shadow filter offset (element 56 .xy of their lighting block), 1 / TOTK_SHADOW_SCALE. The game spaces its four +// compares for its own shadow map texels; with maps TOTK_SHADOW_SCALE times larger, the same offset in UV spans that +// many more texels and the near cascade shows four separate copies of each edge. Dividing by the scale keeps the game's +// spacing in texels. The offset is in UV of the shadow map, so the output resolution plays no part. The scale is read +// once, as vk_texture.c shadow_scale reads it: the maps keep the size they were made with. +static const float *shadow_spread_params(void) { + static float p[4] = {0, 0, 0, 0}; + if (p[0] == 0) { + int scale = cfg_int("TOTK_SHADOW_SCALE"); + scale = scale < 1 ? 1 : scale > 4 ? 4 : scale; + p[0] = 1.0f / (float)scale; + } + return p; +} + static void support_fill(uint8_t *b, float w, float h, float point_size) { memset(b, 0, SUPPORT_BYTES); float inv[4] = {w > 0 ? 2.0f / w : 0, h > 0 ? 2.0f / h : 0, 0, 0}, size[4] = {w, h, 0, 0}; @@ -1431,7 +1505,14 @@ static void support_fill(uint8_t *b, float w, float h, float point_size) { memcpy(b + 160, size, 16); const float one = 1.0f; memcpy(b + 1392, &point_size, 4); // point_size (translator patch 0008): vertex shaders that write none output it + memcpy(b + 1408, pcss_params(), 16); // pcss (translator patch 0010): soft sun shadows in the cascade compares + memcpy(b + 1424, shadow_spread_params(), 16); // shadow_spread (translator patch 0011): sun shadow filter width for (int i = 0; i < 73; i++) memcpy(b + 192 + 16 * i, &one, 4); // render scale 1: the host renders at guest size + if (vk_hdr_wanted()) { // read by the HDR tonemap only (shaders/hdr_tonemap.frag), which declares it past the translator's + float hdr[4]; + vk_hdr_support(hdr); + memcpy(b + 1440, hdr, 16); + } } // TOTK_BIND_STATS=1: bindings and descriptors per draw of each stage (Task 2.2's choice of the pushed stage), and how @@ -1845,14 +1926,23 @@ static void bind_pipeline(VkExec *ex, VkPipelineBindPoint bp, VkPipeline p) { if (!k) B.st_valid = 0; } +static VkViewport draw_viewport(VkExec *ex) { + float x = 0, y = 0, w = (float)ex->pass_w, h = (float)ex->pass_h; + if (ex->has_vp) { + float k = ex->pass_scale > 1 ? (float)ex->pass_scale : 1.0f; + x = ex->vp[0][0] * k, y = ex->vp[0][1] * k, w = ex->vp[0][2] * k, h = ex->vp[0][3] * k; + } + if (w < 1) w = 1; + if (h < 1) h = 1; + VkViewport v = {x, y + h, w, -h, ex->depth_range[0], ex->depth_range[1]}; // the y flip: negative height + vk_dlaa_viewport(ex, &v); // the scene's draws move by the DLAA jitter + return v; +} + static void record_fixed(VkExec *ex) { VkCommandBuffer cb = ex->cb; if (ex->dirty & VK_EXEC_DIRTY_VIEWPORT) { - float x = 0, y = 0, w = (float)ex->pass_w, h = (float)ex->pass_h; - if (ex->has_vp) x = ex->vp[0][0], y = ex->vp[0][1], w = ex->vp[0][2], h = ex->vp[0][3]; - if (w < 1) w = 1; - if (h < 1) h = 1; - VkViewport v = {x, y + h, w, -h, ex->depth_range[0], ex->depth_range[1]}; // the y flip: negative height + VkViewport v = draw_viewport(ex); vkCmdSetViewportWithCount(cb, 1, &v); ex->recorded++; } @@ -2041,6 +2131,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (fs_wait) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, NULL, 1); } if (!vs || (has_fs && !fs)) return 0; + if (vk_dlaa_skip_draw(ex, fs)) return 0; // the game's FXAA off, or its scaling replaced by DLSS (vk_dlaa.c) uint32_t fbl = feedback_prepare(ex, fs); VkPipeState s = ex->pipe; s.vs = vs, s.fs = fs; @@ -2110,7 +2201,11 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { vk_pipeline_set_dynamic(ex->cb, &s); ex->recorded++; B.st = s, B.st_valid = 1; + if (ex->mark && vk_exec_frame.draws == ex->mark_draws0) ex->mark->first[0] = vs, ex->mark->first[1] = fs; } + vk_dlaa_draw(ex); + vk_mvsurvey_draw(ex); + vk_mvprev_draw(ex); record_fixed(ex); repeat_stats(ex, info); bind_resources(ex, info); @@ -2118,10 +2213,767 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { feedback_written(ex, &s); bind_vertex_buffers(ex); if (vk_dump_armed) vk_dump_draw(ex, &s, p, info); + if (vk_hdr_track) vk_hdr_draw(ex, info); vk_exec_oq_draw(ex); return 1; } +// ---- per-object motion +// The camera pass (vk_dlaa.c mv_pass) gives everything the camera's motion, so a character that moves with the camera, +// or on its own, gets the wrong one and DLSS throws its history away there. Each solid scene draw whose position reads +// an object's own uniform blocks (bones c5, place c7, c6; mvobj_draw_ says which are left out and why) is therefore +// drawn a second time (listed during the replay, MQ, and recorded by mv_pass) with its vertex shader's last-frame +// variant (translator patch 0015: the position again from last frame's blocks, bound at slot bank + 15, and last +// frame's camera, written to a spare output) and the fragment shader of vk_mvobj_fs.h, which writes last frame's window +// position minus this frame's. The draw keeps everything else of the original: vertex and index buffers, the other +// uniform blocks, the jittered viewport and the scene depth, tested with the game's own test and its equal case +// (LEQUAL, GEQUAL) without writes, so only the visible surface writes its vector. Last frame's blocks: +// vk_mvprev_find and mo_prev_block; the camera: vk_dlaa_prev_camera. A draw whose variant or pipeline is still +// building, or whose last-frame blocks are not in the snapshot, keeps the camera's motion. TOTK_DLAA_OBJMV=0 turns it +// off (vk_mvprev.c). +#include "vk_mvobj_fs.h" +static const char MO_FS_RES[] = "{\"key\":\"mvobj\",\"stage\":\"ps\",\"notes\":[],\"cbufs\":[{\"binding\":1,\"slot\":3}]," + "\"sbufs\":[],\"textures\":[],\"images\":[]}"; +static struct { + const GfxShader *fs[16]; // per input Location + int failed; + uint64_t cand, still, draws, nodata, building, ran, dropped; + uint64_t ns_check, ns_rec, n_rec, ns_emit, ns_still; + uint64_t ns_part[3], n_part[3], misses, cyc[3], ncyc, big; // checks: filter, data and stillness (memo misses), variant and pipeline // time spent: checks and lookups of every candidate; recording the draws again // cand: draws whose vertex shader reads c5 or c7 + uint64_t why[5]; // nodata by cause: camera, bones, place, bones copy, place copy + uint64_t no_out, storage, no_fs; // variant without the extra output, with storage resources; no fs + uint64_t other_cam; // listed draws left out: their camera block is not the scene camera + uint64_t lk[14][5]; // last-frame lookups per binding index: none, self, side, nearer, refused (another object) + uint64_t e_draws, e_pipe, e_dyn, e_push, e_pushw, e_sets, e_vb, e_setw, e_setreuse; // emitted commands (summary) +} MO; + +// The motion draws of the frame, kept as a list while the scene replays and recorded into the replay's own command +// buffer by vk_mvobj_execute, through the recorder thread (vk_rec.h) like every other draw: recording them straight +// into a secondary command buffer made the driver's work for each one on the replay thread. What a draw shares with +// the one before it (pipeline, dynamic state, viewport, buffers, descriptors) is recorded once; a push descriptor set +// is pushed again only where a binding changed (pushes update the set in place while its layout stays bound). +typedef struct { + VkPipeline pipe; + const VkPipeInfo *info; + uint32_t st, desc, vb; // indices into MQ.st, MQ.desc (the draw's packed descriptors) and MQ.vb + VkDescriptorSet set[2]; // allocated sets; VK_NULL_HANDLE for a pushed one + VkViewport v; + VkRect2D sc; + float bias[3]; + VkBuffer ib; + VkDeviceSize ib_off; + VkIndexType it; + uint32_t count, instances, base_instance; + int32_t base_vertex; + uint64_t cam; // the draw's camera block (c4), this frame's +} MoDraw; +typedef struct { VkBuffer b[17]; VkDeviceSize o[17], sz[17], str[17]; } MoVb; +static struct { + uint64_t frame, fence; // the frame and submission the list belongs to + VkFormat depth_fmt; + MoDraw *d; + VkPipeState *st; + VkDescSlot *desc; + MoVb *vb; + int n, cap, nst, cst, nvb, cvb; + uint32_t ndesc, cdesc; + NVNbufferRange vin[17]; // the vertex ranges MQ.vb[nvb - 1] was resolved from, under vk_mem_gen vgen + uint64_t vgen; + float sup_in[3]; // the support buffer last filled for the list and its inputs + VkDescriptorBufferInfo sup; + float par_in[4]; // the fragment shader's parameters last written and their inputs + VkDescriptorBufferInfo par; + VkDescriptorSetLayout aset_layout[2]; // the last allocated set per set number, with its descriptors + VkDescriptorSet aset[2]; + uint32_t aset_n[2]; + VkDescSlot aset_d[2][MAX_DESC]; +} MQ; + +static void *mo_grow(void *p, int *cap, int need, size_t elem) { + if (need <= *cap) return p; + int c = *cap ? *cap : 256; + while (c < need) c *= 2; + if (!(p = realloc(p, elem * (size_t)c))) rt_fatal("vk_exec: out of memory"); + *cap = c; + return p; +} + +// The position bank masks of the draws considered (gfx_shader_mvprev_banks), with counts, for the summary. +static struct { uint32_t mask; uint64_t n; int ubo, ssbo, tex; } MM[32]; // ubo, ssbo, tex: the first vertex shader's +static void mo_mask_note(uint32_t banks, const GfxShader *vs) { + for (int i = 0; i < 32; i++) + if (MM[i].mask == banks || !MM[i].n) { + if (!MM[i].n) { + const GfxShaderRes *r; + MM[i].ubo = gfx_shader_resources(vs, GFX_RES_UBO, &r), MM[i].ssbo = gfx_shader_resources(vs, GFX_RES_SSBO, &r); + MM[i].tex = gfx_shader_resources(vs, GFX_RES_TEX, &r); + } + MM[i].mask = banks, MM[i].n++; + return; + } +} + +static void mo_drop(void) { + if (MQ.n) MO.dropped++; + MQ.n = MQ.nst = MQ.nvb = 0, MQ.ndesc = 0, MQ.frame = 0; +} + +static uint64_t mo_gen = 1; // the list's snapshot (mo_prev_cached), bumped by mo_begin +static struct { uint32_t mask, chg, n; } MF[16]; // this list's draws per position banks and changed banks +static char fm_last[400]; // the last executed list's, formatted (vk_mvobj_frame_masks) + +// Starts this frame's list (the old one, never executed, is dropped). +static void mo_begin(VkExec *ex, uint64_t frame) { + mo_drop(); + mo_gen++; + memset(MF, 0, sizeof MF); + MQ.frame = frame, MQ.fence = ex->fence, MQ.depth_fmt = ex->depth.format; + memset(MQ.vin, 0, sizeof MQ.vin); + MQ.vgen = 0, MQ.sup.buffer = MQ.par.buffer = VK_NULL_HANDLE; + memset(MQ.aset, 0, sizeof MQ.aset); +} + +static const GfxShader *mo_fs(int loc) { + if (loc < 0 || loc >= 16) return NULL; + if (!MO.fs[loc]) { + uint32_t w[sizeof mvobj_fs_spv / 4]; + memcpy(w, mvobj_fs_spv, sizeof w); + w[MVOBJ_FS_LOCATION_WORD] = (uint32_t)loc; + MO.fs[loc] = gfx_shader_own(w, sizeof w / 4, MO_FS_RES); + if (!MO.fs[loc]) MO.failed = 1, rt_log("vk_exec: the per-object motion fragment shader is malformed: off"); + } + return MO.fs[loc]; +} + +// The write of binding b from its packed descriptors. +static VkWriteDescriptorSet mo_write(const VkPipeBinding *b, const VkDescSlot *d) { + VkWriteDescriptorSet w = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = b->binding, + .descriptorCount = b->count ? b->count : 1, .descriptorType = b->type}; + if (b->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER || b->type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE || + b->type == VK_DESCRIPTOR_TYPE_SAMPLER) + w.pImageInfo = &d->image; + else + w.pBufferInfo = &d->buffer; // VkDescSlot is 24 bytes, as VkDescriptorBufferInfo: arrays line up + return w; +} + +// The descriptors of a motion draw, packed into MQ.desc at q->desc without the binding caches of the command buffer +// being replayed (B, DS, RS), which then stay true for its next draw: uniform buffers (through ubo_info, so with +// mo_ovr), the support buffer and sampled textures. An allocated set is written here (or the last one reused when it +// would hold the same); a pushed set is pushed by mo_emit. 0 for a pipeline with storage buffers, images or texel +// buffers (not drawn again). +static int mo_desc(VkExec *ex, const VkPipeInfo *info, MoDraw *q) { + if (info->n_bindings > MAX_BINDINGS) return 0; + uint32_t total = 0; + for (uint32_t i = 0; i < info->n_bindings; i++) total += info->bindings[i].count ? info->bindings[i].count : 1; + if (total > MAX_DESC) return 0; + int cdesc = (int)MQ.cdesc; + MQ.desc = mo_grow(MQ.desc, &cdesc, (int)(MQ.ndesc + total), sizeof *MQ.desc); + MQ.cdesc = (uint32_t)cdesc; + VkDescSlot *d = MQ.desc + MQ.ndesc; + memset(d, 0, total * sizeof *d); // unused bytes zero: equal descriptors compare equal + for (uint32_t i = 0, nd = 0; i < info->n_bindings; i++) { + const VkPipeBinding *b = &info->bindings[i]; + const GfxShaderRes *r = b->res; + uint32_t cnt = b->count ? b->count : 1; + VkDescSlot *o = &d[nd]; + int st = nvn_stage_of(b->stage); + if (b->kind == VK_PIPE_SUPPORT) { + float in[3]; + support_in(ex, in); + if (!MQ.sup.buffer || memcmp(in, MQ.sup_in, sizeof in)) { + VkBuffer sb; + VkDeviceSize so; + support_fill(scratch(ex, SUPPORT_BYTES, &sb, &so), in[0], in[1], in[2]); + MQ.sup = (VkDescriptorBufferInfo){sb, so, SUPPORT_BYTES}; + memcpy(MQ.sup_in, in, sizeof in); + } + o->buffer = MQ.sup; + } else if (b->kind == GFX_RES_UBO) { + for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ubo_info(ex, st, r->slot + (int)k); + } else if (b->kind == GFX_RES_SSBO && !(r->flags & 1)) { // read only: instance data of foliage and such + for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ssbo_info(ex, st, r, 0); + } else if (b->kind == GFX_RES_TEX && b->type != VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) { + vk_texture_lock(); + for (uint32_t k = 0; k < cnt; k++) { + int slot = r->slot + (int)k; + tex_info(ex, r, slot >= 0 && slot < 128 ? ex->tex[st][slot] : 0, 0, r->separate, &o[k].image); + } + vk_texture_unlock(); + } else { + return 0; + } + nd += cnt; + } + uint32_t n_sets = info->n_sets ? info->n_sets : 1; + q->set[0] = q->set[1] = VK_NULL_HANDLE; + for (uint32_t s = 0, i0 = 0, k0 = 0; s < n_sets && s < 2; s++) { + uint32_t i1 = i0, k1 = k0; + while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) + k1 += info->bindings[i1].count ? info->bindings[i1].count : 1, i1++; + if (i1 > i0 && !(s ? info->push1 : info->push)) { + VkDescriptorSetLayout sl = s ? info->set_layout1 : info->set_layout; + if (MQ.aset[s] && MQ.aset_layout[s] == sl && MQ.aset_n[s] == k1 - k0 && + !memcmp(MQ.aset_d[s], d + k0, (k1 - k0) * sizeof *d)) { + q->set[s] = MQ.aset[s]; + MO.e_setreuse++; + } else { + static VkWriteDescriptorSet w[MAX_BINDINGS]; + VkDescriptorSet set = alloc_set(ex, sl); + for (uint32_t i = i0, k = k0; i < i1; k += info->bindings[i].count ? info->bindings[i].count : 1, i++) + w[i - i0] = mo_write(&info->bindings[i], d + k), w[i - i0].dstSet = set; + vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w, 0, NULL); + q->set[s] = MQ.aset[s] = set, MQ.aset_layout[s] = sl, MQ.aset_n[s] = k1 - k0; + memcpy(MQ.aset_d[s], d + k0, (k1 - k0) * sizeof *d); + MO.e_setw++; + } + } + i0 = i1, k0 = k1; + } + q->desc = MQ.ndesc; + MQ.ndesc += total; + return 1; +} + +// The draw's vertex buffers as bind_vertex_buffers resolves them, into MQ.vb; the previous entry again while the +// ranges and vk_mem_gen are the same (most motion draws in a row are one model's parts). +static uint32_t mo_vertex_buffers(VkExec *ex) { + uint64_t gen = vk_mem_gen(); + if (MQ.nvb && MQ.vgen == gen && !memcmp(MQ.vin, ex->vbuf, sizeof MQ.vin)) { + const MoVb *l = &MQ.vb[MQ.nvb - 1]; + int same = 1; + if (!vk_pipeline_vertex_input_dynamic()) + for (int b = 0; b < 16 && same; b++) same = l->str[b] == ex->pipe.vstream[b].stride; + if (same) return (uint32_t)MQ.nvb - 1; + } + MQ.vb = mo_grow(MQ.vb, &MQ.cvb, MQ.nvb + 1, sizeof *MQ.vb); + MoVb *v = &MQ.vb[MQ.nvb]; + for (int b = 0; b < 17; b++) { + v->b[b] = Z.zbuf, v->o[b] = 0, v->sz[b] = ZERO_BYTES, v->str[b] = b < 16 ? ex->pipe.vstream[b].stride : 0; + if (b == VK_PIPE_ZERO_BINDING) continue; + const NVNbufferRange *r = &ex->vbuf[b]; + VkMemSpan sp; + if (!r->address || !vk_mem_resolve(r->address, 1, &sp)) continue; + VK_MEM_USE(sp); + v->b[b] = sp.buffer, v->o[b] = sp.offset, v->sz[b] = r->size && r->size < sp.avail ? r->size : sp.avail; + } + memcpy(MQ.vin, ex->vbuf, sizeof MQ.vin); + MQ.vgen = gen; + return (uint32_t)MQ.nvb++; +} + +// Records the list into cb (the replay's command buffer, inside the motion pass's rendering), skipping what the draw +// before already set. +static void mo_emit(VkCommandBuffer cb) { + int dyn_vi = vk_pipeline_vertex_input_dynamic(); + VkPipeline pipe = VK_NULL_HANDLE; + uint32_t st = UINT32_MAX, vb = UINT32_MAX; + VkPipelineLayout lay[2] = {0}; + const VkPipeInfo *pinfo[2] = {0}; + int pdesc[2] = {-1, -1}; // the pushed set's descriptors (MQ.desc index) under lay/pinfo, -1 none + VkDescriptorSet bound[2] = {0}; + const MoDraw *p = NULL; + for (int n = 0; n < MQ.n; p = &MQ.d[n++]) { + const MoDraw *q = &MQ.d[n]; + const VkPipeInfo *info = q->info; + if (q->pipe != pipe) vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, q->pipe), pipe = q->pipe, MO.e_pipe++; + if (q->st != st) vk_pipeline_set_dynamic(cb, &MQ.st[q->st]), st = q->st, MO.e_dyn++; + if (!p || memcmp(&q->v, &p->v, sizeof q->v)) vkCmdSetViewportWithCount(cb, 1, &q->v); + if (!p || memcmp(&q->sc, &p->sc, sizeof q->sc)) vkCmdSetScissorWithCount(cb, 1, &q->sc); + if (!p || memcmp(q->bias, p->bias, sizeof q->bias)) vkCmdSetDepthBias(cb, q->bias[0], q->bias[1], q->bias[2]); + uint32_t n_sets = info->n_sets ? info->n_sets : 1; + const VkDescSlot *d = MQ.desc + q->desc; + for (uint32_t s = 0, i0 = 0, k0 = 0; s < n_sets && s < 2; s++) { + uint32_t i1 = i0, k1 = k0; + while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) + k1 += info->bindings[i1].count ? info->bindings[i1].count : 1, i1++; + if (i1 > i0) { + if (s ? info->push1 : info->push) { + // the bindings that differ from the last push of this layout and pipeline (all after another) + int same = lay[s] == info->layout && pinfo[s] == info && pdesc[s] >= 0; + const VkDescSlot *pd = same ? MQ.desc + pdesc[s] : NULL; + VkWriteDescriptorSet w[MAX_BINDINGS]; + uint32_t nw = 0; + for (uint32_t i = i0, k = k0; i < i1; i++) { + uint32_t cnt = info->bindings[i].count ? info->bindings[i].count : 1; + if (!same || memcmp(d + k, pd + k, cnt * sizeof *d)) w[nw++] = mo_write(&info->bindings[i], d + k); + k += cnt; + } + if (nw) rec_push(vk_pipeline_push_fn(), cb, VK_PIPELINE_BIND_POINT_GRAPHICS, info->layout, s, nw, w); + MO.e_push += nw != 0, MO.e_pushw += nw; + lay[s] = info->layout, pinfo[s] = info, pdesc[s] = (int)q->desc, bound[s] = VK_NULL_HANDLE; + } else if (q->set[s] != bound[s] || lay[s] != info->layout) { + vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, info->layout, s, 1, &q->set[s], 0, NULL); + bound[s] = q->set[s], lay[s] = info->layout, pinfo[s] = info, pdesc[s] = -1, MO.e_sets++; + } + } + i0 = i1, k0 = k1; + } + if (q->vb != vb) { + const MoVb *v = &MQ.vb[q->vb]; + vkCmdBindVertexBuffers2(cb, 0, 17, v->b, v->o, v->sz, dyn_vi ? NULL : v->str); + vb = q->vb, MO.e_vb++; + } + if (!p || q->ib != p->ib || q->ib_off != p->ib_off || q->it != p->it) vkCmdBindIndexBuffer(cb, q->ib, q->ib_off, q->it); + vkCmdDrawIndexed(cb, q->count, q->instances, 0, q->base_vertex, q->base_instance); + MO.e_draws++; + } +} + +static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off, uint64_t t0); +static void mvobj_draw(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off) { + if (MO.failed || !vk_mvprev_on() || !B.st_valid) return; + static int timed = -1; + if (timed < 0) timed = rt_knob("MVOBJ_TIMES", "0"); // TOTK_MVOBJ_TIMES=1: the summary's times (the clock reads cost) + if (!timed) return mvobj_draw_(ex, d, ib, ib_off, 0); + uint64_t t0 = kern_now_ns(); + mvobj_draw_(ex, d, ib, ib_off, t0); + MO.ns_check += kern_now_ns() - t0; +} + +// TOTK_MVOBJ_BANKS=1 (test): for scene draws, the words of each other uniform bank (c3, c6, c8-c16) that changed +// since the same shader's draw last frame, logged at frame 3000: where the time that moves wind and water lives +static void mvobj_banks(VkExec *ex) { + static int on = -1; + if (on < 0) on = rt_knob("MVOBJ_BANKS", "0"); + if (!on || !B.st_valid) return; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (frame < 2999 || frame > 3000) return; + static struct { const GfxShader *vs; int bank; uint64_t frame; float w[64]; int n; } tab[4096]; + static int logged; + const GfxShaderRes *ur; + int nu = gfx_shader_resources(B.st.vs, GFX_RES_UBO, &ur); + for (int i = 0; i < nu; i++) { + int bank = ur[i].slot; + if (bank < 3 || bank == 4 || bank == 5 || bank == 7 || bank > 16) continue; + const p_bind_buf *u = &ex->ubo[NVN_STAGE_VS][bank - 3]; + uint32_t n = u->size < 256 ? (uint32_t)u->size / 4 : 64; + const float *now = n ? (const float *)vk_usnap_bytes(u->addr, n * 4) : NULL; + if (!now) continue; + uint32_t k = (uint32_t)(((uintptr_t)B.st.vs ^ (uint64_t)bank * 0x9E3779B97F4A7C15ull) * 0xff51afd7ed558ccdull >> 52); + if (frame == 2999) { + tab[k].vs = B.st.vs, tab[k].bank = bank, tab[k].frame = frame, tab[k].n = (int)n; + memcpy(tab[k].w, now, n * 4); + } else if (tab[k].vs == B.st.vs && tab[k].bank == bank && tab[k].frame == 2999 && logged < 80) { + char line[700]; + int o = 0, nd = 0; + for (int j = 0; j < tab[k].n && j < (int)n && o < 600; j++) + if (memcmp(&tab[k].w[j], &now[j], 4)) nd++, o += snprintf(line + o, sizeof line - o, " [%d] %g>%g", j, tab[k].w[j], now[j]); + if (nd) logged++, rt_log("mvbanks: vs %p c%d size %#llx: %d words%s", (const void *)B.st.vs, bank, + (unsigned long long)u->size, nd, line); + tab[k].frame = 0; + } + } +} + +// 1: old is cur a little changed or the same (every float within 10 %, at least 1.0 and at most 4.0); *changed: not +// the same. The cap: TotK's world positions are in the thousands, and 10 % of them let another object's block a few +// units away pass as this one moved; nothing moves 4 units in a frame. +static int mo_similar(const uint8_t *cur, const uint8_t *old, uint64_t n, int *changed) { + *changed = 0; + if (!memcmp(cur, old, n)) return 1; + // word by word as integers; float arithmetic only on two normal floats (the blocks hold integers too, and + // denormal arithmetic took this loop 1.9 million cycles a block) + const uint32_t *x = (const uint32_t *)cur, *y = (const uint32_t *)old; + for (uint64_t w = 0; w < n / 4; w++) { + if (x[w] == y[w]) continue; + uint32_t ex = x[w] >> 23 & 0xff, ey = y[w] >> 23 & 0xff; + if (!ex || ex == 0xff || !ey || ey == 0xff) continue; // not both normal floats: an integer or a tiny value + float fx, fy; + memcpy(&fx, &x[w], 4), memcpy(&fy, &y[w], 4); + float dd = fabsf(fx - fy), tol = 0.1f * fabsf(fy); + if (dd > (tol < 1.0f ? 1.0f : tol > 4.0f ? 4.0f : tol)) return 0; + } + *changed = 1; + return 1; +} + +// Last frame's copy of a block, accepted when it is this frame's block a little changed (mo_similar, +// which see): a block of another object the address rule found would make motion up. 1 and *out: found (the block itself +// when it is unchanged, *changed 0); 0: none, the draw keeps this frame's block there. +static int mo_prev_block(int index, const p_bind_buf *u, VkDescriptorBufferInfo *out, int *changed) { + uint64_t max = LIM.ubo_max, n = u->size < max ? u->size : max, pa; + int how; + *changed = 0; + // c9 and c11 keep this frame's block. With the copies the address rule finds for c11 and last frame's c9, the + // c4 c7 c9 c11 draws (walls, cliffs, arches) got several pixels of motion under a still camera (TOTK_MV_CHECK, + // those draws alone: 34 % of the picture moving; with c11 kept, 0.8 %). With c11 kept, their c9 still changes every + // frame while nothing moves (the check's per-draw line: about 120 such draws a frame changed in c9 alone), the + // object scale the picture shows is 0.6-0.8 of theirs, and with Link stopped against a cliff 40-77 % of the picture + // got 1-3 px of motion that flipped direction, three times the warp error of the camera's vectors. With c9 kept too, + // the turning run is as good (object warp error 0.46 of the camera's). Why their copies are wrong is not known. + // TOTK_MVPREV_SKIP=<hex bank mask> (test) replaces the default a00 (bit n: cn). + static long skip = -2; + if (skip == -2) { + const char *e = cfg_get("TOTK_MVPREV_SKIP"); + skip = e && *e ? strtol(e, NULL, 16) : 1L << 9 | 1L << 11; + } + if (skip >> (index + 3) & 1) return 0; + uint64_t c0 = __builtin_ia32_rdtsc(); + int found = vk_mvprev_find(index, u->addr, u->size, &pa, &how); + uint64_t c1 = __builtin_ia32_rdtsc(); + MO.cyc[0] += c1 - c0, MO.ncyc++, MO.big += n; + if (index >= 0 && index < 14) MO.lk[index][found ? how : MVPREV_NONE]++; + if (!found || !vk_usnap_find(pa, n, LIM.ubo_align, out)) return 0; + if (how == MVPREV_SELF) return 1; // bound at the same address last frame: not written again + const uint8_t *map = vk_usnap_map(), *cur = vk_usnap_bytes(u->addr, n); + uint64_t c2 = __builtin_ia32_rdtsc(); + MO.cyc[1] += c2 - c1; + if (!cur) return 0; + const uint8_t *old = map + out->offset; + static long trust = -2; // TOTK_MVPREV_TRUST=<hex bank mask> (test): the copy found is taken without the check + if (trust == -2) { + const char *e = cfg_get("TOTK_MVPREV_TRUST"); + trust = e && *e ? strtol(e, NULL, 16) : 0; + } + if (trust >> (index + 3) & 1) { + *changed = memcmp(cur, old, n) != 0; + return 1; + } + int r = mo_similar(cur, old, n, changed); + MO.cyc[2] += __builtin_ia32_rdtsc() - c2; + if (!r && index >= 0 && index < 14) MO.lk[index][4]++; + return r; +} + +// mo_prev_block once per block and list: many draws share their blocks (the scene's c14, a model's c7 and c9 across +// its parts), and the lookup with its comparison was most of the checks' time. +static int mo_prev_cached(int index, const p_bind_buf *u, VkDescriptorBufferInfo *out, int *changed) { + static struct { uint64_t gen, addr, size; int index, r, changed; VkDescriptorBufferInfo out; } bc[4096]; + uint32_t i = (uint32_t)((u->addr ^ (uint64_t)index << 56 ^ u->size << 40) * 0x9E3779B97F4A7C15ull >> 52); + if (bc[i].gen == mo_gen && bc[i].addr == u->addr && bc[i].size == u->size && bc[i].index == index) { + *out = bc[i].out, *changed = bc[i].changed; + return bc[i].r; + } + int r = mo_prev_block(index, u, out, changed); + bc[i].gen = mo_gen, bc[i].addr = u->addr, bc[i].size = u->size, bc[i].index = index; + bc[i].r = r, bc[i].changed = *changed, bc[i].out = *out; + return r; +} + +static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSize ib_off, uint64_t t0) { + mvobj_banks(ex); + const VkExecTarget *z = &ex->depth; + if (!z->image || z->width < DLAA_SCENE_MIN_W || z->format != VK_FORMAT_D32_SFLOAT || B.st.samples > 1 || B.st.raster_discard) + return; + // solid geometry only, which tests and writes the scene depth: effects, particles and screen quads write none, and + // drawn again they would put their motion on the surfaces behind them (DLSS takes those from the surface) + if (!B.st.ds.dt || !B.st.ds.dw) return; + // a vertex shader that reads a user bank besides the camera's (c4) may move on its own + const GfxShaderRes *ur; + int nu = gfx_shader_resources(B.st.vs, GFX_RES_UBO, &ur), reads = 0; + for (int i = 0; i < nu; i++) reads |= ur[i].slot >= 3 && ur[i].slot <= 16 && ur[i].slot != 4; + if (!reads) return; + MO.cand++; + uint64_t tp = t0 ? kern_now_ns() : 0; + if (t0) MO.ns_part[0] += tp - t0, MO.n_part[0]++; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (MQ.frame != frame || MQ.fence != ex->fence || MQ.depth_fmt != z->format) mo_begin(ex, frame); + if (!__atomic_load_n(&LIM.ubo_max, __ATOMIC_ACQUIRE)) limits_init(); + // the variant: the translation of the same code and answers, kept per shader once found; it names the banks the + // position depends on (0: none, the shader is skipped from then on) + static struct { const GfxShader *of, *vs; uint32_t banks; } vmemo[1024]; + uint32_t vi = (uint32_t)((uintptr_t)B.st.vs * 0x9E3779B97F4A7C15ull >> 54); + if (vmemo[vi].of != B.st.vs) { + int has, pending = 0; + sf_mvprev = 1; + const GfxShader *v = shader_for(ex, NVN_STAGE_VS, &has, &pending, 0); + sf_mvprev = 0; + if (!v || pending) { + MO.building++; + return; + } + GfxShaderIo io[32]; + uint32_t extra = gfx_shader_outputs(v, io) & ~gfx_shader_outputs(B.st.vs, io); + uint32_t banks = extra ? gfx_shader_mvprev_banks(v) & 0x1fff8u : 0; + vmemo[vi].of = B.st.vs, vmemo[vi].vs = v, vmemo[vi].banks = banks; + vk_mvprev_want((banks & ~(1u << 4)) >> 3); // binding index = bank - 3 + } + const GfxShader *vs = vmemo[vi].vs; + uint32_t banks = vmemo[vi].banks; + if (!banks || !(banks & ~(1u << 4))) { + MO.no_out++; + return; + } + // a position without the scene camera (c4) has a view of its own (c8 c9 c10 [c13]: its c8 is at a new address every + // frame, last frame's is never found): drawn again it would get this frame's view and no camera motion + // (TOTK_MV_CHECK while turning: 6.2 px end-point error against the camera vectors' 1.7) + static int noc4 = -1; + if (noc4 < 0) noc4 = rt_knob("MVOBJ_NOC4", "0"); // TOTK_MVOBJ_NOC4=1 (test): drawn again all the same + if (!(banks >> 4 & 1) && !noc4) { + MO.no_out++; + return; + } + // only positions that read an object's own blocks: bones (c5), place (c7) or c6. The rest (terrain and grass: c14 + // c15 c16, with or without c9) measured no better than the camera's motion (TOTK_MV_CHECK, turning, those draws + // alone: object warp error 0.112 against the camera's 0.113, the object part 0.4 of what the picture shows) and + // were half the draws checked and listed. TOTK_MVOBJ_ALL=1 (test) takes them again. + static int all = -1; + if (all < 0) all = rt_knob("MVOBJ_ALL", "0"); + if (!all && !(banks & (1u << 5 | 1u << 6 | 1u << 7))) { + MO.no_out++; + return; + } + // TOTK_MVOBJ_ONLY=<hex bank mask> (test): only draws whose position reads no bank outside the mask (bit n: cn) + static long only = -2; + if (only == -2) { + const char *e = cfg_get("TOTK_MVOBJ_ONLY"); + only = e && *e ? strtol(e, NULL, 16) : -1; + } + if (only >= 0 && banks & ~(uint32_t)only & ~(1u << 4)) return; + static long exact = -2; // TOTK_MVOBJ_EXACT=<hex bank mask> (test): only draws with exactly these position banks + if (exact == -2) { + const char *e = cfg_get("TOTK_MVOBJ_EXACT"); + exact = e && *e ? strtol(e, NULL, 16) : -1; + } + if (exact >= 0 && banks != (uint32_t)exact) return; + mo_mask_note(banks, B.st.vs); + // last frame's camera and blocks; held still when none of the position's blocks changed (most of the scenery) + VkDescriptorBufferInfo cam, prevb[17]; + float jit[2]; + if (!vk_dlaa_prev_camera(&cam, jit)) { + MO.nodata++, MO.why[0]++; + return; + } + static struct { uint64_t frame, key; int moving; VkDescriptorBufferInfo b[17]; uint32_t have; } memo[8192]; + uint64_t key = banks; + for (int b = 3; b <= 16; b++) + if (banks >> b & 1 && b != 4) key = (key ^ ex->ubo[NVN_STAGE_VS][b - 3].addr) * 0x100000001b3ull; + uint32_t mi = (uint32_t)(key * 0xff51afd7ed558ccdull >> 51); + uint32_t have = 0; + if (memo[mi].frame == frame && memo[mi].key == key) { + if (!memo[mi].moving) { + if (t0) MO.ns_still += kern_now_ns() - tp; + return (void)MO.still++; + } + memcpy(prevb, memo[mi].b, sizeof prevb), have = memo[mi].have; + } else { + MO.misses++; + int moving = 0; + for (int b = 3; b <= 16; b++) { + if (!(banks >> b & 1) || b == 4) continue; + const p_bind_buf *u = &ex->ubo[NVN_STAGE_VS][b - 3]; + int changed; + if (u->addr && u->size && mo_prev_cached(b - 3, u, &prevb[b], &changed)) + have |= 1u << b, moving |= changed << b; // the banks whose block changed + } + memo[mi].frame = frame, memo[mi].key = key, memo[mi].moving = moving; + memcpy(memo[mi].b, prevb, sizeof prevb), memo[mi].have = have; + if (!moving) { + if (t0) MO.ns_still += kern_now_ns() - tp; + return (void)MO.still++; + } + } + if (t0) MO.ns_part[1] += kern_now_ns() - tp, MO.n_part[1]++, tp = kern_now_ns(); + GfxShaderIo io[32]; + uint32_t extra = gfx_shader_outputs(vs, io) & ~gfx_shader_outputs(B.st.vs, io); + const GfxShaderRes *r; + if (gfx_shader_resources(vs, GFX_RES_IMG, &r)) { + MO.storage++; + return; + } + const GfxShader *fs = mo_fs(31 - __builtin_clz(extra)); + if (!fs) { + MO.no_fs++; + return; + } + VkPipeState ms = B.st; + ms.vs = vs, ms.fs = fs, ms.feedback = 0; + memset(ms.color_fmt, 0, sizeof ms.color_fmt); + ms.color_fmt[0] = NVN_FORMAT_RG16F; + // blending off (blend_en below); the factors stay the game's, valid NVN values + ms.color.blend_en = 0, ms.color.logic_op = 0; + ms.chmask.mask = 0x3; // r and g of target 0 + ms.ms.a2c = 0; + // the game's own test with its equal case (LESS -> LEQUAL, GREATER -> GEQUAL): the surface the draw left in the + // scene depth passes, anything nearer hides it (TOTK_MVOBJ_EQUAL=1: EQUAL, for comparison) + static int eq = -1; + if (eq < 0) eq = rt_knob("MVOBJ_EQUAL", "0"); + int df = B.st.ds.df; + df = eq ? NVN_DEPTH_FUNC_EQUAL : df == NVN_DEPTH_FUNC_LESS ? NVN_DEPTH_FUNC_LEQUAL + : df == NVN_DEPTH_FUNC_GREATER ? NVN_DEPTH_FUNC_GEQUAL : df == NVN_DEPTH_FUNC_LEQUAL || df == NVN_DEPTH_FUNC_GEQUAL ? df + : NVN_DEPTH_FUNC_EQUAL; + ms.ds.dt = 1, ms.ds.dw = 0, ms.ds.df = (uint64_t)df, ms.ds.st = 0; + // the pipeline per state, kept: the lookup hashes and locks the whole state + static struct { VkPipeState st; VkPipeline p; const VkPipeInfo *info; int used; } pmemo[64]; + static int plast = -1; // the entry of the last draw: a model's parts in a row share the state, no hash then + uint32_t pi; + if (plast >= 0 && pmemo[plast].used && !memcmp(&pmemo[plast].st, &ms, sizeof ms)) { + pi = (uint32_t)plast; + } else { + uint64_t ph = 0xcbf29ce484222325ull; + for (size_t i = 0; i < sizeof ms; i += 8) { + uint64_t w8; + memcpy(&w8, (const uint8_t *)&ms + i, sizeof ms - i < 8 ? sizeof ms - i : 8); + ph = (ph ^ w8) * 0x100000001b3ull; + } + pi = (uint32_t)(ph >> 58); + } + const VkPipeInfo *info = NULL; + VkPipeline pipe; + int pending = 0; + if (pmemo[pi].used && !memcmp(&pmemo[pi].st, &ms, sizeof ms)) { + info = pmemo[pi].info; + pipe = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE); // its optimised build once swapped in + if (!pipe) return; + } else { + pipe = vk_pipeline_graphics_async(&ms, &info, &pending, 0); + if (pending || !pipe || !info) { + MO.building += pending != 0; + return; + } + pmemo[pi].st = ms, pmemo[pi].p = pipe, pmemo[pi].info = info, pmemo[pi].used = 1; + } + plast = (int)pi; + uint64_t t1 = kern_now_ns(); + if (t0) MO.ns_part[2] += t1 - tp, MO.n_part[2]++; + if (t0) MO.ns_check -= t1 - t0; // counted as recording below + // into the frame's list, past the primary's binding caches (mo_desc); mo_emit records it + MQ.d = mo_grow(MQ.d, &MQ.cap, MQ.n + 1, sizeof *MQ.d); + MoDraw *q = &MQ.d[MQ.n]; + float par_in[4] = {(float)z->width, (float)z->height, jit[0], jit[1]}; + if (!MQ.par.buffer || memcmp(par_in, MQ.par_in, sizeof par_in)) { + VkBuffer pbuf; + VkDeviceSize poff; + memcpy(scratch(ex, 16, &pbuf, &poff), par_in, sizeof par_in); + MQ.par = (VkDescriptorBufferInfo){pbuf, poff, 16}; + memcpy(MQ.par_in, par_in, sizeof par_in); + } + memset(mo_ovr, 0, sizeof mo_ovr); + for (int bk = 3; bk <= 16; bk++) // last frame's block, or this frame's where none was found + mo_ovr[NVN_STAGE_VS][bk + 15] = bk == 4 ? cam : have >> bk & 1 ? prevb[bk] : ubo_info(ex, NVN_STAGE_VS, bk); + mo_ovr[NVN_STAGE_FS][3] = MQ.par; + mo_ovr_on = 1; + int bound = mo_desc(ex, info, q); + mo_ovr_on = 0; + if (!bound) { + MO.storage++; + return; + } + q->pipe = pipe, q->info = info; + if (!MQ.nst || memcmp(&MQ.st[MQ.nst - 1], &ms, sizeof ms)) { + MQ.st = mo_grow(MQ.st, &MQ.cst, MQ.nst + 1, sizeof *MQ.st); + MQ.st[MQ.nst++] = ms; + } + q->st = (uint32_t)MQ.nst - 1; + q->v = draw_viewport(ex), q->sc = vk_exec_scissor(ex); + q->bias[0] = ex->poly_offset.units, q->bias[1] = ex->poly_offset.clamp, q->bias[2] = ex->poly_offset.factor; + q->vb = mo_vertex_buffers(ex); + q->ib = ib, q->ib_off = ib_off; + q->it = d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32; + q->count = (uint32_t)d->count, q->instances = d->instances > 0 ? (uint32_t)d->instances : 1; + q->base_vertex = d->base_vertex, q->base_instance = (uint32_t)d->base_instance; + q->cam = ex->ubo[NVN_STAGE_VS][1].addr; + MQ.n++; + MO.draws++; + for (int i = 0; i < 16; i++) // the frame's draws per position banks and changed banks (vk_mvobj_frame_masks) + if (!MF[i].n || (MF[i].mask == banks && MF[i].chg == (uint32_t)memo[mi].moving)) { + MF[i].mask = banks, MF[i].chg = (uint32_t)memo[mi].moving, MF[i].n++; + break; + } + if (t0) MO.ns_rec += kern_now_ns() - t1, MO.n_rec++; +} + +int vk_mvobj_execute(VkExec *ex, uint64_t cam, VkImageView mv, VkImageView depth, VkFormat depth_fmt, uint32_t w, + uint32_t h) { + static uint64_t frames; + if (++frames % 60 == 0) + rt_log("vk_exec: per-object motion: %.2f ms a frame checking, %.2f ms listing (%.1f us a draw), %.2f ms recording", + MO.ns_check / 1e6 / (frames ? frames : 1), MO.ns_rec / 1e6 / (frames ? frames : 1), + MO.n_rec ? MO.ns_rec / 1e3 / MO.n_rec : 0.0, MO.ns_emit / 1e6 / (frames ? frames : 1)); + if (frames % 600 == 0) { + char l[1400]; + int o = 0; + for (int i = 0; i < 32 && MM[i].n && o < 1300; i++) + o += snprintf(l + o, sizeof l - o, " %#x:%llu (u%d s%d t%d)", MM[i].mask, (unsigned long long)MM[i].n, MM[i].ubo, + MM[i].ssbo, MM[i].tex); + if (o) rt_log("vk_exec: per-object motion position banks (bit n: cn) and draws:%s", l); + o = 0; + for (int i = 0; i < 14 && o < 1300; i++) + if (MO.lk[i][0] + MO.lk[i][1] + MO.lk[i][2] + MO.lk[i][3]) + o += snprintf(l + o, sizeof l - o, " c%d %llu/%llu/%llu/%llu/%llu", i + 3, (unsigned long long)MO.lk[i][0], + (unsigned long long)MO.lk[i][1], (unsigned long long)MO.lk[i][2], + (unsigned long long)MO.lk[i][3], (unsigned long long)MO.lk[i][4]); + if (o) rt_log("vk_exec: per-object motion last-frame lookups (none/self/side/nearer/refused):%s", l); + } + if (frames % 60 == 0 && MO.ran) + rt_log("vk_exec: per-object motion recorded a frame: %.0f draws, %.0f pipeline binds, %.0f dynamic states, %.0f " + "pushes of %.1f bindings, %.0f set binds, %.0f vertex buffer binds; sets written %.0f, reused %.0f", + (double)MO.e_draws / MO.ran, (double)MO.e_pipe / MO.ran, (double)MO.e_dyn / MO.ran, (double)MO.e_push / MO.ran, + MO.e_push ? (double)MO.e_pushw / MO.e_push : 0.0, (double)MO.e_sets / MO.ran, (double)MO.e_vb / MO.ran, + (double)MO.e_setw / MO.ran, (double)MO.e_setreuse / MO.ran); + if (frames % 60 == 0) + rt_log("vk_exec: per-object motion checks: filter %.2f ms (%.0f a frame), data %.2f ms (%.0f), variant and " + "pipeline %.2f ms (%.0f); %.0f objects looked up; still draws %.2f ms", MO.ns_part[0] / 1e6 / frames, + (double)MO.n_part[0] / frames, + MO.ns_part[1] / 1e6 / frames, (double)MO.n_part[1] / frames, MO.ns_part[2] / 1e6 / frames, + (double)MO.n_part[2] / frames, (double)MO.misses / frames, MO.ns_still / 1e6 / frames); + if (frames % 60 == 0 && MO.ncyc) + rt_log("vk_exec: per-object motion lookups: %llu cycles finding last frame's, %llu the copies, %llu comparing, " + "a lookup; %llu bytes a lookup", (unsigned long long)(MO.cyc[0] / MO.ncyc), + (unsigned long long)(MO.cyc[1] / MO.ncyc), (unsigned long long)(MO.cyc[2] / MO.ncyc), + (unsigned long long)(MO.big / MO.ncyc)); + if (frames % 60 == 0) + rt_log("vk_exec: per-object motion: %llu draws read place or bones, %llu of them still, %llu drawn again, %llu without last frame's data, %llu " + "waiting for a shader or pipeline; %llu frames ran, %llu dropped (no data: camera %llu, bones %llu, place %llu, " + "bones copy %llu, place copy %llu; uniform alignment %llu; variant without its output %llu, with storage %llu, " + "no fragment shader %llu; another camera %llu)", + (unsigned long long)MO.cand, (unsigned long long)MO.still, (unsigned long long)MO.draws, (unsigned long long)MO.nodata, + (unsigned long long)MO.building, + (unsigned long long)MO.ran, (unsigned long long)MO.dropped, (unsigned long long)MO.why[0], + (unsigned long long)MO.why[1], (unsigned long long)MO.why[2], (unsigned long long)MO.why[3], + (unsigned long long)MO.why[4], (unsigned long long)LIM.ubo_align, (unsigned long long)MO.no_out, + (unsigned long long)MO.storage, (unsigned long long)MO.no_fs, (unsigned long long)MO.other_cam); + if (!MQ.n) return 0; + static int exec = -1; + if (exec < 0) exec = rt_knob("MVOBJ_EXEC", "1"); // TOTK_MVOBJ_EXEC=0: listed, never recorded (test) + if (!exec) { + mo_drop(); + return 0; + } + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (MQ.frame != frame || MQ.fence != ex->fence || MQ.depth_fmt != depth_fmt) { + mo_drop(); + return 0; + } + int fo = 0; + for (int i = 0; i < 16 && MF[i].n && fo < (int)sizeof fm_last - 40; i++) + fo += snprintf(fm_last + fo, sizeof fm_last - fo, " %#x/%#x:%u", MF[i].mask, MF[i].chg, MF[i].n); + if (!fo) fm_last[0] = 0; + // only draws with the scene camera block (vk_dlaa.c mv_pass's): last frame's camera they get is that block's, and + // a draw of another view would move by the difference between the two views + int kept = 0; + for (int i = 0; i < MQ.n; i++) + if (MQ.d[i].cam == cam) MQ.d[kept++] = MQ.d[i]; + else MO.other_cam++; + MQ.n = kept; + if (!MQ.n) { + mo_drop(); + return 0; + } + uint64_t t0 = kern_now_ns(); + VkCommandBuffer cb = ex->cb; + VkRenderingAttachmentInfo ca = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .imageView = mv, + .imageLayout = VK_IMAGE_LAYOUT_GENERAL, .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE}; + VkRenderingAttachmentInfo da = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .imageView = depth, + .imageLayout = VK_IMAGE_LAYOUT_GENERAL, .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, + .storeOp = VK_ATTACHMENT_STORE_OP_STORE}; + VkRenderingInfo ri = {.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {w, h}}, .layerCount = 1, + .colorAttachmentCount = 1, .pColorAttachments = &ca, .pDepthAttachment = &da}; + vkCmdBeginRendering(cb, &ri); // through the recorder (vk_rec.h), in order with the draws and the end + mo_emit(cb); + vkCmdEndRendering(cb); + // the draws left their pipeline, descriptors, buffers and dynamic state bound: the next game draw sets everything + vk_exec_draw_new_cb(); + ex->dirty = VK_EXEC_DIRTY_ALL; + ex->recorded += MQ.n; + MQ.n = MQ.nst = MQ.nvb = 0, MQ.ndesc = 0, MQ.frame = 0; + MO.ran++; + MO.ns_emit += kern_now_ns() - t0; + return 1; +} + static void bind_index(VkExec *ex, const void *data, uint64_t bytes, VkIndexType type) { VkBuffer b; VkDeviceSize o; @@ -2213,6 +3065,7 @@ static void op_draw_idx(VkExec *ex, const p_draw_idx *d) { vkCmdDrawIndexed(ex->cb, (uint32_t)d->count, d->instances > 0 ? (uint32_t)d->instances : 1, 0, d->base_vertex, (uint32_t)d->base_instance); ex->recorded++; + if (d->itype != NVN_INDEX_TYPE_UNSIGNED_BYTE) mvobj_draw(ex, d, sp.buffer, sp.offset); } // DrawElementsIndirect: the arguments are read by the GPU from the pool buffer (they are often written by an earlier @@ -2244,6 +3097,7 @@ static void op_draw_idx_ind(VkExec *ex, const p_draw_idx_ind *d) { static void op_dispatch(VkExec *ex, const p_dispatch *d) { if (d->x <= 0 || d->y <= 0 || d->z <= 0) return; if (!ex->prog) return warn_once(W_PROGRAM, "a dispatch without a bound program is skipped"); + vk_contact_flush(ex); cb_res(ex); dummies_init(); int present; @@ -2258,7 +3112,9 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) { twin_dispatch = 1; bind_resources(ex, info); twin_dispatch = 0; + VkPassMark *m = dispatch_mark(ex, cs, (uint32_t)d->x, (uint32_t)d->y, (uint32_t)d->z); vkCmdDispatch(ex->cb, (uint32_t)d->x, (uint32_t)d->y, (uint32_t)d->z); + vk_pass_mark_end(ex->cb, m); ex->recorded++; if (vk_dump_armed) vk_dump_dispatch(ex, cs, p, info, d); } @@ -2293,7 +3149,9 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { vk_exec_barrier(ex); B.c0_valid[2] = 0; // the range no longer holds B.c0[2] } + VkPassMark *m = dispatch_mark(ex, cs, 0, 0, 0); vkCmdDispatchIndirect(ex->cb, ip.buffer, ip.offset); + vk_pass_mark_end(ex->cb, m); ex->recorded++; vk_mem_count(ip.cls, VK_MUSE_IND, sizeof(VkDispatchIndirectCommand)); VK_MEM_BOUND(ip); @@ -2415,3 +3273,6 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { default: rt_fatal("vk: op %s is not a draw op", nvn_op_name(op)); } } + +// The last executed list's draws as " <position banks>/<changed banks>:<draws>" (bit n: cn), for TOTK_MV_CHECK's lines. +const char *vk_mvobj_frame_masks(void) { return fm_last; } diff --git a/runtime/vk_exec_probe.c b/runtime/vk_exec_probe.c index 504ce3d5..cbd7f2f4 100644 --- a/runtime/vk_exec_probe.c +++ b/runtime/vk_exec_probe.c @@ -3,7 +3,8 @@ // the completion thread; a guest-memory word a callback writes is visible once fence_wait returns; finish drains every // submission; a present of a cleared vk_texture.c image (layout GENERAL) reads back exactly through the swapchain // capture. Clear part: a hand-built op stream (SET_RT to an RGBA8 and a D32 texture, whole-target clears, -// then scissored clears) through submit reads back exactly with read_texels. +// then scissored clears) through submit reads back exactly with read_texels. Pass marks part (test_marks): the GPU's +// buffer marks and the ring records name the last pass of a submission, and the device-lost report text says so. #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -175,7 +176,7 @@ static void init_tex(tex_t *t, pool_t *pool, int64_t off, int format) { t->d.target = NVN_TEXTURE_TARGET_2D, t->d.format = format, t->d.width = TEX_W, t->d.height = TEX_H; t->d.depth = 1, t->d.levels = 1; nvn_tex_layout(&t->d, &t->L); - t->pool = pool, t->off = off, t->mem = pool->mem + off, t->serial = 100 + (uint64_t)off; + t->pool = pool, t->pool_flags = (uint16_t)pool->flags, t->off = off, t->mem = pool->mem + off, t->serial = 100 + (uint64_t)off; } // Pixel of the RGBA8 target after the stream: the scissored second clear inside the rectangle, the first elsewhere. @@ -386,6 +387,74 @@ static uint8_t *draw_prefix(uint8_t *q, prog_t *prog, const pool_t *pool, tex_t return q; } +// ---- pass marks part (vk_crumbs.c): one submission with a pass of 2 draws into the draw part's target, a dispatch and +// a pass of 3 draws into a second target. Once its fence is done, the buffer marks must name the last pass as both +// started and finished, the ring records must hold what the three were, and vk_pass_marks_format (the text +// vk_device_fault_report logs) must name it. Without VK_AMD_buffer_marker or with TOTK_PASS_CRUMBS=0 it is skipped: +// NVIDIA's checkpoints can only be read once the device is lost. +static char marks_text[2048]; +static int marks_skipped; + +static int test_marks(const GfxBackend *b, pool_t *pool, tex_t *color, prog_t *gfx, prog_t *cs) { + uint32_t s0, f0; + if (!vk_pass_marks_read(&s0, &f0)) { + marks_skipped = 1; + return 0; + } + static tex_t second; + init_tex(&second, pool, 0x40000, NVN_FORMAT_RGBA8); + CHECK(0x40000 + second.L.size <= 0x80000, "pass-marks target exceeds its pool slot"); + static _Alignas(8) uint8_t stream[8192]; + uint8_t *q = draw_prefix(stream, gfx, pool, color); + const p_draw d = {NVN_DRAW_PRIMITIVE_TRIANGLES, 0, 3, 0, 0}; + for (int i = 0; i < 2; i++) q = put(q, OP_DRAW, &d, sizeof d); + const p_bind_program bc = {cs, 1 << 5}; + q = put(q, OP_BIND_PROGRAM, &bc, sizeof bc); + const p_dispatch dp = {2, 1, 1, 0}; + q = put(q, OP_DISPATCH, &dp, sizeof dp); + const p_bind_program bg = {gfx, 3}; + q = put(q, OP_BIND_PROGRAM, &bg, sizeof bg); + p_set_rt rt; + memset(&rt, 0, sizeof rt); + rt.n = 1, rt.color[0] = &second; + q = put(q, OP_SET_RT, &rt, sizeof rt); + for (int i = 0; i < 3; i++) q = put(q, OP_DRAW, &d, sizeof d); + q = put(q, OP_END, NULL, 0); + CHECK(q <= stream + sizeof stream, "pass-marks op stream overflow"); + uint64_t handle = (uint64_t)(uintptr_t)stream; + uint64_t f = b->submit(&handle, 1); + CHECK(b->fence_wait(f, 20000000000ull), "fence_wait of the pass-marks submission timed out"); + uint32_t started = 0, finished = 0; + vk_pass_marks_read(&started, &finished); + CHECK(started && started == finished, "after the fence the GPU last started mark %u and finished mark %u (before " + "the submission: %u and %u)", started, finished, s0, f0); + const VkPassMark *p1 = vk_pass_mark_record(finished), *dm = vk_pass_mark_record(finished - 1), + *p0 = vk_pass_mark_record(finished - 2); + CHECK(p1 && p1->kind == VK_MARK_PASS && p1->fence == f && p1->step == 1, + "the last finished mark %u is not pass 1 of submission %llu", finished, (unsigned long long)f); + CHECK(p1->w == TEX_W && p1->h == TEX_H && p1->d == 1 && p1->ncolor == 1 && + p1->color[0] == VK_FORMAT_R8G8B8A8_UNORM && p1->depth == VK_FORMAT_UNDEFINED, + "pass 1 has %ux%u, %u layers, %u colour targets, format %d, depth %d", p1->w, p1->h, p1->d, p1->ncolor, + (int)p1->color[0], (int)p1->depth); + CHECK(p1->draws == 3 && p1->first[0] && p1->first[1] && p1->first[0] == p1->last[0] && p1->first[1] == p1->last[1], + "pass 1 has %u draws (3 expected) and shaders %p %p ... %p %p", p1->draws, (const void *)p1->first[0], + (const void *)p1->first[1], (const void *)p1->last[0], (const void *)p1->last[1]); + CHECK(dm && dm->kind == VK_MARK_DISPATCH && dm->fence == f && dm->step == 0 && dm->w == 2 && dm->h == 1 && + dm->d == 1 && dm->first[0], + "the mark before pass 1 is not dispatch 0 of 2x1x1 groups of submission %llu", (unsigned long long)f); + CHECK(p0 && p0->kind == VK_MARK_PASS && p0->fence == f && p0->step == 0 && p0->draws == 2 && + p0->first[0] == p1->first[0], + "the mark before the dispatch is not pass 0 of submission %llu with 2 draws", (unsigned long long)f); + vk_pass_marks_format("VK_AMD_buffer_marker", started, finished, marks_text, sizeof marks_text); + char want[128]; + snprintf(want, sizeof want, "last finished %u: pass 1 of submission %llu: %ux%u, 1 layer, colour formats %d, ", + finished, (unsigned long long)f, TEX_W, TEX_H, (int)VK_FORMAT_R8G8B8A8_UNORM); + CHECK(strstr(marks_text, want) && strstr(marks_text, ", 3 draws; first vs "), + "the report text lacks \"%s\" or the draw count:\n%s", want, marks_text); + b->texture_release(&second); + return 0; +} + static int test_draw(const GfxBackend *b, uint8_t *guest, int *n_drawn, int *n_tries, int *n_cs) { char root[PLAT_PATH_MAX * 2], path[PLAT_PATH_MAX * 2 + 64]; CHECK(probe_census_dir(root, sizeof root), "no build/v%d/shaders/jobs.json above the exe (run tools/shader_census.py)", @@ -503,10 +572,12 @@ static int test_draw(const GfxBackend *b, uint8_t *guest, int *n_drawn, int *n_t // Compute: a few census compute shaders, one dispatch each, resources unbound (zero buffers and stand-in images). int dispatched = 0; + prog_t *cs_prog = NULL; for (int t = 0; t < 4 && t < nc; t++) { const DrawEnt *one[1] = {&e[cs[(long long)t * nc / (nc < 4 ? nc : 4)]]}; prog_t *prog = dr_prog(root, one, 1); CHECK(prog, "census code file of %.16s is missing", one[0]->key); + cs_prog = prog; uint8_t *q = stream; q = put(q, OP_BEGIN, NULL, 0); const p_bind_program bp = {prog, 1 << 5}; @@ -530,6 +601,7 @@ static int test_draw(const GfxBackend *b, uint8_t *guest, int *n_drawn, int *n_t dispatched++; ind_dispatches++; } + if (test_marks(b, &pool, &color, hit, cs_prog)) return 1; b->texture_release(&color); b->pool_fini(&pool); free(pairs), free(cs), free(e), free(jobs); @@ -845,7 +917,33 @@ static int test_mirror(const GfxBackend *b, uint8_t *guest, int *bytes) { return 0; } +// A pool of zero bytes (TotK makes some now and then; MacNativeGames hit one on 1.2.1) initialises as an empty pool: no +// Vulkan buffer or memory (a buffer or import of 0 bytes is invalid usage) and no pool index entry, so a lookup at its +// address still finds the pool that starts there, before and after the empty pool's Finalize. +static int test_empty_pool(const GfxBackend *b, uint8_t *guest) { + pool_t pool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = guest + 0xE00000, .size = 0x100000, + .game_mem = guest + 0xE00000}; + pool_t empty = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = guest + 0xE00000, .size = 0, + .game_mem = guest + 0xE00000}; + CHECK(b->pool_init(&pool), "pool_init of a CPU-visible pool for the empty-pool part"); + long errors = vk_validation_errors_total(); + CHECK(b->pool_init(&empty), "pool_init of a zero-byte pool"); + uint64_t a = (uint64_t)(uintptr_t)pool.mem; + VkMemSpan whole, at, after; + CHECK(vk_mem_resolve(a + 16, 16, &whole) && whole.buffer, "the pool beside an empty pool is not resolved"); + CHECK(vk_mem_resolve(a, 0, &at) && at.buffer == whole.buffer && at.offset == whole.offset - 16, + "a lookup at the start of a pool finds the empty pool there"); + CHECK(vk_validation_errors_total() == errors, "the zero-byte pool raised %ld validation errors", + vk_validation_errors_total() - errors); + b->pool_fini(&empty); + CHECK(vk_mem_resolve(a + 16, 16, &after) && after.buffer == whole.buffer, + "the empty pool's Finalize took the pool beside it out of the index"); + b->pool_fini(&pool); + return 0; +} + int probe_vkexec(void) { + vk_hdr_off(); // the present checks read 8-bit swapchain pixels layouts_equal = 0; main_tid = plat_thread_id(); const GfxBackend *b = &gfx_vulkan_backend; @@ -865,6 +963,7 @@ int probe_vkexec(void) { int nvnx_bytes = 0, mirror_bytes = 0; if (!bad) bad = test_nvnx(b, guest, &nvnx_bytes); if (!bad) bad = test_mirror(b, guest, &mirror_bytes); + if (!bad) bad = test_empty_pool(b, guest); b->finish(); vk_pipeline_shutdown(); // the pipelines and the draw part's stand-ins go before the device vk_exec_draw_shutdown(); @@ -909,5 +1008,12 @@ int probe_vkexec(void) { "between submissions mirrored, CPU bytes beside a fill kept, %d bytes compared)\n", mirror_bytes); else printf("probe-vkexec: mirror part skipped (memory mode import)\n"); + if (marks_skipped) + printf("probe-vkexec: pass marks part skipped (no VK_AMD_buffer_marker, or TOTK_PASS_CRUMBS=0)\n"); + else + printf("probe-vkexec: pass marks part passed (pass, dispatch, pass in one submission: the GPU's last started and " + "finished mark is the last pass, ring records exact); the report reads:\n%s\n", marks_text); + printf("probe-vkexec: empty pool part passed (a zero-byte pool at the start of another: no Vulkan objects, lookups " + "there find the other pool before and after its Finalize)\n"); return 0; } diff --git a/runtime/vk_exec_xfer.c b/runtime/vk_exec_xfer.c index e09a62ca..4c9db592 100644 --- a/runtime/vk_exec_xfer.c +++ b/runtime/vk_exec_xfer.c @@ -356,10 +356,11 @@ static int sub_resolve(const tex_t *t, const view_t *v, int has_v, const NVNcopy s->level = has_v && v->set_levels ? v->base_level : 0; s->layer0 = has_v && v->set_layers ? v->min_layer : 0; uint32_t lw = max1(img->width >> s->level), lh = max1(img->height >> s->level); - s->x = r->xoffset, s->y = r->yoffset, s->z = r->zoffset; + int k = img->scale > 1 ? img->scale : 1; // a larger shadow map: the game's region in its texels + s->x = r->xoffset * k, s->y = r->yoffset * k, s->z = r->zoffset; s->nz = r->depth > 0 ? (uint32_t)r->depth : 1; - s->w = r->width > 0 ? (uint32_t)r->width : defaults_to_level ? (s->x < (int32_t)lw ? lw - (uint32_t)s->x : 0) : 0; - s->h = r->height > 0 ? (uint32_t)r->height : defaults_to_level ? (s->y < (int32_t)lh ? lh - (uint32_t)s->y : 0) : 0; + s->w = r->width > 0 ? (uint32_t)r->width * k : defaults_to_level ? (s->x < (int32_t)lw ? lw - (uint32_t)s->x : 0) : 0; + s->h = r->height > 0 ? (uint32_t)r->height * k : defaults_to_level ? (s->y < (int32_t)lh ? lh - (uint32_t)s->y : 0) : 0; int is3d = img->type == VK_IMAGE_TYPE_3D; uint32_t layers = is3d ? 1 : (uint32_t)s->z + s->nz, first = is3d ? 0 : s->layer0; if (s->level >= img->levels || s->x < 0 || s->y < 0 || s->z < 0 || !s->w || !s->h || @@ -415,6 +416,10 @@ static int buffer_side(const VkExec *ex, const Sub *s, uint64_t addr, uint64_t * static void copy_buffer_image(VkExec *ex, const p_copy_b2t *p, int to_image) { Sub s; if (!sub_resolve(p->tex, &p->view, p->has_view, &p->r, 1, &s)) return; + if (s.img->scale > 1) { // the buffer holds the game's texel count, the image more + warn_once(W_REGION, "buffer copy of a shadow map enlarged by TOTK_SHADOW_SCALE: skipped"); + return; + } uint64_t size; VkBufferImageCopy c; VkMemSpan m; @@ -591,6 +596,7 @@ static void clear_tex(VkExec *ex, const p_clear_tex *p) { } void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload) { + if (op != OP_RENDER_ENABLE && op != OP_SIGNAL_EVENT) vk_contact_flush(ex); switch (op) { case OP_COPY_B2B: copy_b2b(ex, payload); break; case OP_COPY_B2T: copy_buffer_image(ex, payload, 1); break; diff --git a/runtime/vk_hdr.c b/runtime/vk_hdr.c new file mode 100644 index 00000000..e8480bb2 --- /dev/null +++ b/runtime/vk_hdr.c @@ -0,0 +1,659 @@ +// HDR output (TOTK_HDR=1): an HDR10 swapchain (Rec. 2020 primaries, PQ) and a game picture with highlights above the +// SDR white. Settings: TOTK_HDR (read at start), TOTK_HDR_PEAK_NITS (the display's peak), TOTK_HDR_GAME_NITS (the +// picture's white, "paper white") and TOTK_HDR_UI_NITS (the HUD's and this menu's white), the three read when the +// settings change. With TOTK_HDR=0 nothing here runs and the present is what it was. +// +// The game's picture (docs/architecture.md, "HDR"). The game tonemaps its scene into an r11g11b10f target, upscales it +// and draws the HUD into a second one (float, nothing in these passes clamps), and one last draw copies that into the +// 8-bit sRGB window texture it presents. So: +// 1. vk_hdr_shader_swap: the tonemap's fragment shader becomes shaders/hdr_tonemap.frag, which keeps values above 1 +// (relative to the HUD's white) and rolls off at the peak. It is recognised by its constants and resources, not by +// a hash, so another game version or translator still finds it; its SPIR-V is replaced in memory only. The game's +// FSR 1 upscale after it would clamp the picture to 1 again; fsr_unclamp drops that clamp in its SPIR-V. +// 2. vk_hdr_draw / vk_hdr_present_source: a draw into a presented window texture that samples one float texture of +// its size is that copy; the present then reads the float texture instead of the window texture. Any other last +// draw into it leaves the window texture, decoded from sRGB: menus and movies stay SDR at the HUD's white. +// The present is queued before the next frame's work (vk_backend.c), so the float target is read before the game +// draws into it again. +// 3. vk_hdr_encode: on the present, the crop rectangle goes to PQ code values in an RGBA16F image (shaders/hdr_encode.comp) +// that FSR 1 or the blit scale into the swapchain. vk_hdr_ui_*: the in-game menu draws into an RGBA8 image and +// shaders/hdr_ui.frag blends it onto the swapchain at the HUD's white. +// The tonemap's approach is RenoDX's TotK mod by souperman9 and Musa on ShortFuse's RenoDX (github.com/clshortfuse/renodx, +// src/games/totk, MIT); the code is ours. +#include "vk.h" + +#include <math.h> +#include <string.h> + +#include "cfg.h" +#include "gfx.h" +#include "hdr_encode_spv.h" +#include "hdr_fullscreen_spv.h" +#include "hdr_tonemap_spv.h" +#include "hdr_ui_spv.h" +#include "host.h" +#include "nvn_api.h" +#include "vk_exec.h" +#include "platform/platform.h" + +#define SLOTS 8 + +// ---- settings + +static int g_wanted = -1; + +int vk_hdr_wanted(void) { + if (g_wanted < 0) g_wanted = cfg_bool("TOTK_HDR"); + return g_wanted; +} + +void vk_hdr_off(void) { g_wanted = 0; } + +// The three nits, read again when the settings change. The replay (every draw's support buffer) and the present +// thread read them: an update fills the snapshot not in use and then publishes it. +typedef struct { float peak, game, ui, hue; } Nits; +static struct { unsigned seen; int cur; Nits snap[2]; PlatMutex lock; } N = {.seen = ~0u, .lock = PLAT_MUTEX_INIT}; + +static Nits nits(void) { + unsigned v = cfg_version(); + if (__atomic_load_n(&N.seen, __ATOMIC_ACQUIRE) != v) { + plat_mutex_lock(&N.lock); + if (N.seen != v) { + Nits *n = &N.snap[!N.cur]; + n->peak = fmaxf((float)cfg_double("TOTK_HDR_PEAK_NITS"), 80.0f); + n->game = fmaxf((float)cfg_double("TOTK_HDR_GAME_NITS"), 80.0f); + n->ui = fmaxf((float)cfg_double("TOTK_HDR_UI_NITS"), 80.0f); + n->hue = fminf(fmaxf((float)cfg_double("TOTK_HDR_HUE"), 0.0f), 100.0f) / 100.0f; + __atomic_store_n(&N.cur, !N.cur, __ATOMIC_RELEASE); + __atomic_store_n(&N.seen, v, __ATOMIC_RELEASE); + } + plat_mutex_unlock(&N.lock); + } + return N.snap[__atomic_load_n(&N.cur, __ATOMIC_ACQUIRE)]; +} + +void vk_hdr_support(float out[4]) { + Nits n = nits(); + out[0] = n.game / n.ui; // the picture's white against the HUD's + out[1] = n.peak > n.game ? n.peak / n.game : 1.0f; // the peak against the picture's white + out[2] = n.hue; // hue correction of highlights (hdr_tonemap.frag) + out[3] = 0; +} + +// ---- 1. the tonemap shader + +enum { SPV_OP_NAME = 5, SPV_OP_TYPE_FLOAT = 22, SPV_OP_CONSTANT = 43, SPV_OP_DECORATE = 71, SPV_DEC_BINDING = 33, + SPV_DEC_SET = 34 }; + +// The variables of the game's shader, in the order of hdr_tonemap.frag's bindings 0..4. +static const char *const tm_names[5] = {"support_buffer", "fp_t_tcb_8", "fp_t_tcb_A", "fp_c1", "fp_c3"}; + +static int name_is(const uint32_t *a, uint32_t len, const char *s) { // OpName operands after the id + size_t n = strlen(s); + return (len - 2) * 4 >= n + 1 && !memcmp(a + 1, s, n + 1); +} + +// The game's FSR 1 pass after the tonemap (some frames take it in place of the plain pass, at 1:1 as well as when it +// scales up; what picks it is not known yet) ends with clamp(colour, 0.0, 1.0) per channel, which cuts the HDR +// highlights back to the HUD's white for as long as the game takes it. Its own 0..1 clamps of the filter weights each +// feed a float bit trick (OpBitcast), so only the clamps whose result is stored to the shader's output change, to +// max(x, 0.0): one word shorter, so the words after it move down and the module shrinks. Recognised by FSR's two +// reciprocal constants (0x7ef07ebb, 0x7ef19fff) and its one texture fp_t_tcb_8. Returns the clamps changed, -1 for +// that shader with no output clamp found (a translator that stores the colour another way), 0 for any other shader. +enum { SPV_OP_EXT_INST_IMPORT = 11, SPV_OP_EXT_INST = 12, SPV_OP_VARIABLE = 59, SPV_OP_STORE = 62, + SPV_OP_ACCESS_CHAIN = 65, SPV_SC_OUTPUT = 3, GLSL_FMAX = 40, GLSL_FCLAMP = 43 }; + +static int fsr_unclamp(uint32_t *w, uint64_t *words) { + uint64_t n = *words; + uint32_t bound = w[3], glsl = 0, float_type = 0, zero = 0, one = 0; + int magic = 0, tex = 0; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) return 0; + const uint32_t *a = w + i + 1; + if (op == SPV_OP_EXT_INST_IMPORT && len >= 3 && name_is(a, len, "GLSL.std.450")) glsl = a[0]; + else if (op == SPV_OP_TYPE_FLOAT && len == 3 && a[1] == 32) float_type = a[0]; + else if (op == SPV_OP_CONSTANT && len == 4) { + magic |= (a[2] == 0x7ef07ebbu) | (a[2] == 0x7ef19fffu) << 1; + if (a[0] == float_type && float_type && a[2] == 0) zero = a[1]; + if (a[0] == float_type && float_type && a[2] == 0x3f800000u) one = a[1]; + } else if (op == SPV_OP_NAME && len >= 3 && name_is(a, len, "fp_t_tcb_8")) tex = 1; + i += len; + } + if (magic != 3 || !tex) return 0; + if (!glsl || !zero || !one || !bound) return -1; + uint8_t *out = calloc(bound, 1); // id -> 1: a pointer into an Output variable, 2: a value stored through one + if (!out) return 0; + for (uint64_t i = 5; i < n; i += w[i] >> 16) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + const uint32_t *a = w + i + 1; + if (op == SPV_OP_VARIABLE && len >= 4 && a[2] == SPV_SC_OUTPUT && a[1] < bound) out[a[1]] = 1; + else if (op == SPV_OP_ACCESS_CHAIN && len >= 4 && a[2] < bound && out[a[2]] == 1 && a[1] < bound) out[a[1]] = 1; + else if (op == SPV_OP_STORE && len >= 3 && a[0] < bound && out[a[0]] == 1 && a[1] < bound) out[a[1]] = 2; + } + int changed = 0; + uint64_t k = 5; // the write position: never past the read position, as instructions only shrink + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + const uint32_t *a = w + i + 1; // result type, result, set, instruction, x, min, max + if (op == SPV_OP_EXT_INST && len == 8 && a[2] == glsl && a[3] == GLSL_FCLAMP && a[1] < bound && out[a[1]] == 2 && + a[5] == zero && a[6] == one) { + uint32_t ins[7] = {7u << 16 | SPV_OP_EXT_INST, a[0], a[1], a[2], GLSL_FMAX, a[4], zero}; + memmove(w + k, ins, sizeof ins); + k += 7, changed++; + } else { + memmove(w + k, w + i, len * sizeof *w); + k += len; + } + i += len; + } + free(out); + *words = k; + return changed ? changed : -1; +} + +void vk_hdr_shader_swap(uint32_t **spirv, uint64_t *words) { + if (!vk_hdr_wanted() || !*spirv || *words < 5) return; + int unclamped = fsr_unclamp(*spirv, words); + if (unclamped > 0) { + static volatile int32_t fsrs; + rt_log("hdr: the game's FSR pass keeps values above 1 (%d output clamps; %d so far)", unclamped, + plat_inc32(&fsrs)); + return; + } + if (unclamped < 0) { + static volatile int32_t missed; + if (plat_inc32(&missed) == 1) + rt_log("hdr: the game's FSR pass was found but no clamp of its output; it still clips HDR highlights"); + return; + } + const uint32_t *w = *spirv; + uint64_t n = *words; + // the game's constants: the BT.601 red weight, -1/ln 2 (e^-x as exp2), 2/3 (the grade's red weight), the bloom clamp + static const float want[4] = {0.298900008f, -1.44269502f, 0.666666687f, 10000.0f}; + uint32_t float_type = 0, found = 0, ids[5] = {0}, bind[5], set[5]; + int has_bind[5] = {0}; + for (uint64_t i = 5; i < n;) { + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + if (!len || i + len > n) return; + const uint32_t *a = w + i + 1; + if (op == SPV_OP_TYPE_FLOAT && len == 3 && a[1] == 32) float_type = a[0]; + else if (op == SPV_OP_CONSTANT && len == 4 && a[0] == float_type && float_type) + for (int k = 0; k < 4; k++) { + float f; + memcpy(&f, &a[2], 4); + if (f == want[k]) found |= 1u << k; + } + else if (op == SPV_OP_NAME && len >= 3) + for (int k = 0; k < 5; k++) + if (name_is(a, len, tm_names[k])) ids[k] = a[0]; + i += len; + } + if (found != 15) return; + for (int k = 0; k < 5; k++) + if (!ids[k]) return; + for (uint64_t i = 5; i < n;) { // the bindings and sets of those variables + uint32_t op = w[i] & 0xffff, len = w[i] >> 16; + const uint32_t *a = w + i + 1; + if (op == SPV_OP_DECORATE && len == 4) + for (int k = 0; k < 5; k++) + if (a[0] == ids[k]) { + if (a[1] == SPV_DEC_BINDING) bind[k] = a[2], has_bind[k] |= 1; + else if (a[1] == SPV_DEC_SET) set[k] = a[2], has_bind[k] |= 2; + } + i += len; + } + for (int k = 0; k < 5; k++) + if (has_bind[k] != 3) return; + uint64_t m = sizeof hdr_tonemap_spv / 4; + uint32_t *out = malloc(sizeof hdr_tonemap_spv); + if (!out) return; + memcpy(out, hdr_tonemap_spv, sizeof hdr_tonemap_spv); + // ours declares binding k for tm_names[k] in set 0: each gets the game's binding and set + uint32_t bound = out[3], *ours = calloc(bound ? bound : 1, sizeof *ours); // id -> our binding + 1 + if (!ours) { + free(out); + return; + } + for (uint64_t i = 5; i < m;) { + uint32_t op = out[i] & 0xffff, len = out[i] >> 16; + uint32_t *a = out + i + 1; + if (op == SPV_OP_DECORATE && len == 4 && a[1] == SPV_DEC_BINDING && a[2] < 5) { + if (a[0] < bound) ours[a[0]] = a[2] + 1; + a[2] = bind[a[2]]; + } + i += len; + } + for (uint64_t i = 5; i < m;) { + uint32_t op = out[i] & 0xffff, len = out[i] >> 16; + uint32_t *a = out + i + 1; + if (op == SPV_OP_DECORATE && len == 4 && a[1] == SPV_DEC_SET && a[0] < bound && ours[a[0]]) a[2] = set[ours[a[0]] - 1]; + i += len; + } + free(ours); + free(*spirv); + *spirv = out; + *words = m; + static volatile int32_t swaps; + rt_log("hdr: the game's tonemap shader runs as shaders/hdr_tonemap.frag (%d so far)", plat_inc32(&swaps)); +} + +// ---- 2. the picture the window texture is copied from + +int vk_hdr_track; // vk_exec_draw.c calls vk_hdr_draw: set at the first present with TOTK_HDR=1 +static volatile int32_t g_active; // the swapchain is HDR10 (gfx_present.c vk_hdr_set_active) +static PlatMutex rec_lock = PLAT_MUTEX_INIT; +static struct { VkImage win, src; VkFormat fmt; } recs[4]; // per presented window image: what the last draw copied +static int nrecs; + +void vk_hdr_set_active(int on) { plat_xchg32(&g_active, on); } +int vk_hdr_active(void) { return plat_cas32(&g_active, 0, 0); } + +static int is_float(VkFormat f) { + return f == VK_FORMAT_B10G11R11_UFLOAT_PACK32 || f == VK_FORMAT_R16G16B16A16_SFLOAT || + f == VK_FORMAT_R32G32B32A32_SFLOAT; +} + +void vk_hdr_draw(VkExec *ex, const VkPipeInfo *info) { + const VkExecTarget *t = &ex->color[0]; + if (!t->image) return; + int known = 0; + for (int i = 0; i < 4; i++) // unlocked first look: most draws go elsewhere (a stale slot only costs the lock below) + known |= __atomic_load_n(&recs[i].win, __ATOMIC_RELAXED) == t->image; + if (!known) return; + VkImage src = VK_NULL_HANDLE; + VkFormat fmt = VK_FORMAT_UNDEFINED; + int ntex = 0, slot = -1; + for (uint32_t i = 0; i < info->n_bindings; i++) { + const VkPipeBinding *bd = &info->bindings[i]; + if (bd->kind != GFX_RES_TEX || bd->stage != GFX_STAGE_FS || !bd->res) continue; + ntex += bd->count ? bd->count : 1; + if ((bd->res->type & 0x7ff) == 3 && !bd->res->separate) slot = bd->res->slot; // a plain 2D texture + } + if (ntex == 1 && slot >= 0 && slot < 128 && ex->tex[1][slot]) { + const VkTexImage *s = vk_texture_by_id(NVN_HANDLE_TEX(ex->tex[1][slot]), NULL, NULL); + if (s && is_float(s->format) && s->width == t->width && s->height == t->height && s->samples == 1) + src = s->image, fmt = s->format; + } + plat_mutex_lock(&rec_lock); + for (int i = 0; i < nrecs; i++) + if (recs[i].win == t->image) recs[i].src = src, recs[i].fmt = fmt; + plat_mutex_unlock(&rec_lock); +} + +void vk_hdr_present_source(const VkTexImage *win, VkImage *image, VkFormat *format) { + if (!vk_hdr_wanted()) return; + vk_hdr_track = 1; + plat_mutex_lock(&rec_lock); + int r = -1; + for (int i = 0; i < nrecs; i++) + if (recs[i].win == win->image) r = i; + if (r < 0 && nrecs < 4) r = nrecs++, recs[r].win = win->image, recs[r].src = VK_NULL_HANDLE; + // kept until the next draw into the window texture: a present again without a new copy shows the same picture + if (r >= 0 && recs[r].src && vk_hdr_active()) *image = recs[r].src, *format = recs[r].fmt; + plat_mutex_unlock(&rec_lock); + static int logged; + if (!logged && *image != win->image) logged = 1, rt_log("hdr: the present reads the game's float picture"); +} + +void vk_hdr_image_gone(VkImage image) { // a texture image went away: forget it as window or source + if (!vk_hdr_track) return; // no records before the first HDR present (vk_hdr_present_source): no lock per image + plat_mutex_lock(&rec_lock); + for (int i = 0; i < nrecs; i++) { + if (recs[i].src == image) recs[i].src = VK_NULL_HANDLE; + if (recs[i].win == image) recs[i] = recs[--nrecs], recs[nrecs] = (typeof(recs[0])){0}, i--; + } + plat_mutex_unlock(&rec_lock); +} + +// ---- swapchain + +// With TOTK_HDR=1, the HDR10 format among the surface's (10-bit UNORM, ST 2084): 1 with *out set, else 0 and the +// reason logged once. +int vk_hdr_surface_format(const VkSurfaceFormatKHR *f, uint32_t n, VkSurfaceFormatKHR *out) { + if (!vk_hdr_wanted()) return 0; + static int logged; + if (!logged) { + char b[1024]; + int o = 0; + for (uint32_t i = 0; i < n && o < (int)sizeof b - 32; i++) + o += snprintf(b + o, sizeof b - (size_t)o, " %d/%d", (int)f[i].format, (int)f[i].colorSpace); + rt_log("hdr: surface formats (VkFormat/VkColorSpaceKHR):%s", b); + } + for (int pass = 0; pass < 2; pass++) + for (uint32_t i = 0; i < n; i++) + if (f[i].colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT && + f[i].format == (pass ? VK_FORMAT_A2R10G10B10_UNORM_PACK32 : VK_FORMAT_A2B10G10R10_UNORM_PACK32)) { + *out = f[i]; + if (!logged) rt_log("hdr: HDR10 swapchain (format %d)", (int)f[i].format); + logged = 1; + return 1; + } + if (!logged) + rt_log("hdr: the display offers no HDR10 format, so the picture stays SDR (Windows: turn on \"Use HDR\" in the " + "display settings; Linux: a Wayland compositor with colour management and HDR on)"); + logged = 1; + return 0; +} + +// The display's peak and the picture's white for sc; again when the settings change (gfx_present.c calls it on every +// present to an HDR10 swapchain, with new = 1 for a new swapchain). +void vk_hdr_metadata(VkSwapchainKHR sc, int new_sc) { + static unsigned sent = ~0u; + VkCtx *c = vk_ctx(); + if (!c->has_hdr_metadata || (!new_sc && cfg_version() == sent)) return; + sent = cfg_version(); + PFN_vkSetHdrMetadataEXT set = (PFN_vkSetHdrMetadataEXT)vkGetDeviceProcAddr(c->device, "vkSetHdrMetadataEXT"); + if (!set) return; + Nits n = nits(); + VkHdrMetadataEXT m = {.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT, + .displayPrimaryRed = {0.708f, 0.292f}, .displayPrimaryGreen = {0.170f, 0.797f}, + .displayPrimaryBlue = {0.131f, 0.046f}, .whitePoint = {0.3127f, 0.3290f}, + .maxLuminance = n.peak, .minLuminance = 0.0f, .maxContentLightLevel = n.peak, + .maxFrameAverageLightLevel = n.game}; + set(c->device, 1, &sc, &m); +} + +// ---- 3. present passes + +static void barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, VkImageLayout to, VkPipelineStageFlags2 ss, + VkAccessFlags2 sa, VkPipelineStageFlags2 ds, VkAccessFlags2 da) { + VkImageMemoryBarrier2 b = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .srcStageMask = ss, .srcAccessMask = sa, + .dstStageMask = ds, .dstAccessMask = da, .oldLayout = from, .newLayout = to, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + VkDependencyInfo d = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, + .pImageMemoryBarriers = &b}; + vkCmdPipelineBarrier2(cb, &d); +} + +typedef struct { VkImage img; VmaAllocation alloc; VkImageView view; uint32_t w, h; } Img; + +static void img_free(VkCtx *c, Img *m) { + if (m->view) vkDestroyImageView(c->device, m->view, NULL); + if (m->img) vmaDestroyImage(c->vma, m->img, m->alloc); + *m = (Img){0}; +} + +// m at w x h in fmt; another size waits for the device first (earlier presents may still read the old image). +static int img_ensure(VkCtx *c, Img *m, uint32_t w, uint32_t h, VkFormat fmt, VkImageUsageFlags usage) { + if (m->img && m->w == w && m->h == h) return 1; + if (m->img) { + vk_queue_lock(); + vkDeviceWaitIdle(c->device); + vk_queue_unlock(); + img_free(c, m); + } + VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = fmt, + .extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + if (vmaCreateImage(c->vma, &ii, &ai, &m->img, &m->alloc, NULL) != VK_SUCCESS) { + m->img = VK_NULL_HANDLE; + return 0; + } + VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = m->img, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + if (vkCreateImageView(c->device, &vi, NULL, &m->view) != VK_SUCCESS) { + m->view = VK_NULL_HANDLE; + img_free(c, m); + return 0; + } + m->w = w, m->h = h; + return 1; +} + +static struct { + int tried, ok; + PFN_vkCmdPushDescriptorSetKHR push; + VkSampler sampler; + VkDescriptorSetLayout dsl_enc, dsl_ui; + VkPipelineLayout enc_layout, ui_layout; + VkPipeline enc; + VkPipeline ui; // for ui_fmt + VkFormat ui_fmt; + Img out; // PQ code values of the crop, RGBA16F + Img menu; // the in-game menu, RGBA8 + VkImageView src_view[SLOTS]; +} H; + +static VkShaderModule module(VkCtx *c, const uint32_t *code, size_t size) { + VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = size, .pCode = code}; + VkShaderModule m = VK_NULL_HANDLE; + return vkCreateShaderModule(c->device, &mi, NULL, &m) == VK_SUCCESS ? m : VK_NULL_HANDLE; +} + +static VkDescriptorSetLayout set_layout(VkCtx *c, const VkDescriptorSetLayoutBinding *b, uint32_t n) { + VkDescriptorSetLayoutCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR, + .bindingCount = n, .pBindings = b}; + VkDescriptorSetLayout l = VK_NULL_HANDLE; + return vkCreateDescriptorSetLayout(c->device, &ci, NULL, &l) == VK_SUCCESS ? l : VK_NULL_HANDLE; +} + +static VkPipelineLayout pipe_layout(VkCtx *c, VkDescriptorSetLayout *dsl, VkShaderStageFlags st, uint32_t pc) { + VkPushConstantRange pr = {st, 0, pc}; + VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1, + .pSetLayouts = dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr}; + VkPipelineLayout l = VK_NULL_HANDLE; + return vkCreatePipelineLayout(c->device, &li, NULL, &l) == VK_SUCCESS ? l : VK_NULL_HANDLE; +} + +static int init(VkCtx *c) { + H.tried = 1; + H.push = (PFN_vkCmdPushDescriptorSetKHR)vkGetDeviceProcAddr(c->device, "vkCmdPushDescriptorSetKHR"); + VkDescriptorSetLayoutBinding enc[2] = { + {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}, + {.binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT}}; + VkDescriptorSetLayoutBinding ui = {.binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT}; + VkSamplerCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .magFilter = VK_FILTER_NEAREST, + .minFilter = VK_FILTER_NEAREST, .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .maxLod = 0}; + if (!H.push || vkCreateSampler(c->device, &si, NULL, &H.sampler) != VK_SUCCESS || + !(H.dsl_enc = set_layout(c, enc, 2)) || !(H.dsl_ui = set_layout(c, &ui, 1)) || + !(H.enc_layout = pipe_layout(c, &H.dsl_enc, VK_SHADER_STAGE_COMPUTE_BIT, 32)) || + !(H.ui_layout = pipe_layout(c, &H.dsl_ui, VK_SHADER_STAGE_FRAGMENT_BIT, 16))) { + rt_log("hdr: the present passes could not be created; the picture goes out unconverted"); + return 0; + } + VkShaderModule m = module(c, hdr_encode_spv, sizeof hdr_encode_spv); + VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"}, + .layout = H.enc_layout}; + VkResult r = m ? vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &H.enc) : VK_ERROR_UNKNOWN; + if (m) vkDestroyShaderModule(c->device, m, NULL); + if (r != VK_SUCCESS) { + rt_log("hdr: the present passes could not be created; the picture goes out unconverted"); + return 0; + } + return H.ok = 1; +} + +VkImage vk_hdr_encode(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, const int crop[4]) { + VkCtx *c = vk_ctx(); + if (!H.tried) init(c); + if (!H.ok || !img_ensure(c, &H.out, (uint32_t)crop[2], (uint32_t)crop[3], VK_FORMAT_R16G16B16A16_SFLOAT, + VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) + return VK_NULL_HANDLE; + slot %= SLOTS; + if (H.src_view[slot]) vkDestroyImageView(c->device, H.src_view[slot], NULL); + VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = src, + .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = fmt, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; + if (vkCreateImageView(c->device, &vi, NULL, &H.src_view[slot]) != VK_SUCCESS) { + H.src_view[slot] = VK_NULL_HANDLE; + return VK_NULL_HANDLE; + } + Nits n = nits(); + // an sRGB format decodes itself through the view; 8-bit UNORM holds sRGB codes the shader decodes + int decode = fmt == VK_FORMAT_R8G8B8A8_UNORM || fmt == VK_FORMAT_B8G8R8A8_UNORM; + const VkPipelineStageFlags2 CS = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, ALL = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT; + barrier(cb, src, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, ALL, VK_ACCESS_2_MEMORY_WRITE_BIT, CS, + VK_ACCESS_2_SHADER_SAMPLED_READ_BIT); + barrier(cb, H.out.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, ALL, 0, CS, + VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT); + VkDescriptorImageInfo ii = {H.sampler, H.src_view[slot], VK_IMAGE_LAYOUT_GENERAL}, + oi = {VK_NULL_HANDLE, H.out.view, VK_IMAGE_LAYOUT_GENERAL}; + VkWriteDescriptorSet w[2] = { + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii}, + {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 1, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .pImageInfo = &oi}}; + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, H.enc); + rec_push(H.push, cb, VK_PIPELINE_BIND_POINT_COMPUTE, H.enc_layout, 0, 2, w); + struct { int32_t offset[2], size[2]; float p[4]; } pc = {{crop[0], crop[1]}, {crop[2], crop[3]}, + {n.ui, n.peak, decode ? 1.0f : 0.0f, 0}}; + vkCmdPushConstants(cb, H.enc_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, &pc); + vkCmdDispatch(cb, ((uint32_t)crop[2] + 7) / 8, ((uint32_t)crop[3] + 7) / 8, 1); + barrier(cb, src, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, CS, 0, ALL, 0); + barrier(cb, H.out.img, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, CS, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, ALL, + VK_ACCESS_2_MEMORY_READ_BIT); + return H.out.img; +} + +// The in-game menu's target: an RGBA8 image of the swapchain's size in COLOR_ATTACHMENT_OPTIMAL (its old content +// dropped); the caller renders the menu into the view with a clear to 0. +VkImageView vk_hdr_ui_target(VkCommandBuffer cb, uint32_t w, uint32_t h) { + VkCtx *c = vk_ctx(); + if (!H.tried) init(c); + if (!H.ok || !img_ensure(c, &H.menu, w, h, VK_FORMAT_R8G8B8A8_UNORM, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)) + return VK_NULL_HANDLE; + barrier(cb, H.menu.img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, 0, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, + VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT); + return H.menu.view; +} + +static int ui_pipeline(VkCtx *c, VkFormat fmt) { + if (H.ui && H.ui_fmt == fmt) return 1; + if (H.ui) { // another swapchain format: earlier presents may still use the old pipeline + vk_queue_lock(); + vkDeviceWaitIdle(c->device); + vk_queue_unlock(); + vkDestroyPipeline(c->device, H.ui, NULL); + H.ui = VK_NULL_HANDLE; + } + VkShaderModule vs = module(c, hdr_fullscreen_spv, sizeof hdr_fullscreen_spv), + fs = module(c, hdr_ui_spv, sizeof hdr_ui_spv); + VkPipelineShaderStageCreateInfo st[2] = { + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vs, + .pName = "main"}, + {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fs, + .pName = "main"}}; + VkPipelineVertexInputStateCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO}; + VkPipelineInputAssemblyStateCreateInfo ia = {.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST}; + VkPipelineViewportStateCreateInfo vp = {.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, .scissorCount = 1}; + VkPipelineRasterizationStateCreateInfo rs = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .polygonMode = VK_POLYGON_MODE_FILL, .cullMode = VK_CULL_MODE_NONE, + .lineWidth = 1.0f}; + VkPipelineMultisampleStateCreateInfo ms = {.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT}; + VkPipelineColorBlendAttachmentState ba = { + .blendEnable = VK_TRUE, .srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA, + .dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, .colorBlendOp = VK_BLEND_OP_ADD, + .srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE, .dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + .alphaBlendOp = VK_BLEND_OP_ADD, .colorWriteMask = 0xf}; + VkPipelineColorBlendStateCreateInfo cb = {.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .attachmentCount = 1, .pAttachments = &ba}; + VkDynamicState dyn[2] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; + VkPipelineDynamicStateCreateInfo ds = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = 2, .pDynamicStates = dyn}; + VkPipelineRenderingCreateInfo ri = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, .pColorAttachmentFormats = &fmt}; + VkGraphicsPipelineCreateInfo gi = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = &ri, + .stageCount = 2, .pStages = st, .pVertexInputState = &vi, + .pInputAssemblyState = &ia, .pViewportState = &vp, .pRasterizationState = &rs, + .pMultisampleState = &ms, .pColorBlendState = &cb, .pDynamicState = &ds, + .layout = H.ui_layout}; + VkResult r = vs && fs ? vkCreateGraphicsPipelines(c->device, VK_NULL_HANDLE, 1, &gi, NULL, &H.ui) : VK_ERROR_UNKNOWN; + if (vs) vkDestroyShaderModule(c->device, vs, NULL); + if (fs) vkDestroyShaderModule(c->device, fs, NULL); + if (r != VK_SUCCESS) { + H.ui = VK_NULL_HANDLE; + rt_log("hdr: the menu pass could not be created; the menu is not shown"); + return 0; + } + H.ui_fmt = fmt; + return 1; +} + +// The menu image onto dst (a swapchain image in COLOR_ATTACHMENT_OPTIMAL, view dst_view of format fmt). +void vk_hdr_ui_compose(VkCommandBuffer cb, VkImageView dst_view, VkFormat fmt, uint32_t w, uint32_t h) { + VkCtx *c = vk_ctx(); + if (!H.ok || !H.menu.img || !ui_pipeline(c, fmt)) return; + barrier(cb, H.menu.img, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT, VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT, + VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT, VK_ACCESS_2_SHADER_SAMPLED_READ_BIT); + VkRenderingAttachmentInfo ca = {.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, .imageView = dst_view, + .imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD, .storeOp = VK_ATTACHMENT_STORE_OP_STORE}; + VkRenderingInfo ri = {.sType = VK_STRUCTURE_TYPE_RENDERING_INFO, .renderArea = {{0, 0}, {w, h}}, .layerCount = 1, + .colorAttachmentCount = 1, .pColorAttachments = &ca}; + vkCmdBeginRendering(cb, &ri); + vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, H.ui); + VkViewport v = {0, 0, (float)w, (float)h, 0, 1}; + VkRect2D s = {{0, 0}, {w, h}}; + vkCmdSetViewport(cb, 0, 1, &v); + vkCmdSetScissor(cb, 0, 1, &s); + VkDescriptorImageInfo ii = {H.sampler, H.menu.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL}; + VkWriteDescriptorSet wr = {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = 0, .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, .pImageInfo = &ii}; + rec_push(H.push, cb, VK_PIPELINE_BIND_POINT_GRAPHICS, H.ui_layout, 0, 1, &wr); + float pc[4] = {nits().ui, 0, 0, 0}; + vkCmdPushConstants(cb, H.ui_layout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof pc, pc); + vkCmdDraw(cb, 3, 1, 0, 0); + vkCmdEndRendering(cb); +} + +void vk_hdr_release(void) { + VkCtx *c = vk_ctx(); + for (int i = 0; i < SLOTS; i++) + if (H.src_view[i]) vkDestroyImageView(c->device, H.src_view[i], NULL), H.src_view[i] = VK_NULL_HANDLE; + img_free(c, &H.out); + img_free(c, &H.menu); + if (H.enc) vkDestroyPipeline(c->device, H.enc, NULL); + if (H.ui) vkDestroyPipeline(c->device, H.ui, NULL); + if (H.enc_layout) vkDestroyPipelineLayout(c->device, H.enc_layout, NULL); + if (H.ui_layout) vkDestroyPipelineLayout(c->device, H.ui_layout, NULL); + if (H.dsl_enc) vkDestroyDescriptorSetLayout(c->device, H.dsl_enc, NULL); + if (H.dsl_ui) vkDestroyDescriptorSetLayout(c->device, H.dsl_ui, NULL); + if (H.sampler) vkDestroySampler(c->device, H.sampler, NULL); + memset(&H, 0, sizeof H); +} + +// PQ code (0..1023) of a 10-bit channel to the sRGB byte of the same colour at the HUD's white (menu test shots and +// flight thumbnails of an HDR10 swapchain). +static float pq_nits(float e) { + const float m1 = 0.1593017578125f, m2 = 78.84375f, c1 = 0.8359375f, c2 = 18.8515625f, c3 = 18.6875f; + float p = powf(e, 1.0f / m2); + return 10000.0f * powf(fmaxf(p - c1, 0.0f) / (c2 - c3 * p), 1.0f / m1); +} + +void vk_hdr_to_srgb8(const uint8_t *in, uint8_t *out, size_t px, int a2r) { + float ui = nits().ui; + // Rec. 2020 to Rec. 709 (the inverse of ITU-R BT.2087's matrix) + static const float m[9] = {1.6605f, -0.5876f, -0.0728f, -0.1246f, 1.1329f, -0.0083f, -0.0182f, -0.1006f, 1.1187f}; + for (size_t i = 0; i < px; i++) { + uint32_t v; + memcpy(&v, in + 4 * i, 4); + float c[3] = {(float)(v & 1023), (float)(v >> 10 & 1023), (float)(v >> 20 & 1023)}; // A2B10G10R10: R low + if (a2r) { float t = c[0]; c[0] = c[2]; c[2] = t; } + for (int k = 0; k < 3; k++) c[k] = pq_nits(c[k] / 1023.0f) / ui; + for (int k = 0; k < 3; k++) { + float l = fminf(fmaxf(m[3 * k] * c[0] + m[3 * k + 1] * c[1] + m[3 * k + 2] * c[2], 0.0f), 1.0f); + float s = l <= 0.0031308f ? 12.92f * l : 1.055f * powf(l, 1.0f / 2.4f) - 0.055f; + out[4 * i + k] = (uint8_t)(s * 255.0f + 0.5f); + } + out[4 * i + 3] = 255; + } +} diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index fba5677a..d17a23e4 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -20,6 +20,7 @@ #include "nvn_api.h" #include "nvn_pool_index.h" #include "vk.h" +#include "vk_exec.h" #include "platform/platform.h" // Timestamps for the tracking cost, in ns; qpc_ms converts. @@ -245,6 +246,11 @@ int vk_pool_init(pool_t *p) { // virtual pools that map it. Either gets device-local memory: 1.4.3's CPU never writes its 0x18c pool's storage // (unchanged over 3600 frames of the intro), so the guest storage and the GPU copy need no sync. if ((p->flags & NVN_MEMORY_POOL_FLAGS_GPU_NO_ACCESS) && !(p->flags & NVN_MEMORY_POOL_FLAGS_PHYSICAL)) return 1; + // A pool of zero bytes (TotK makes some now and then) holds no address the GPU can use: it gets no record. A record + // would import 0 bytes (invalid usage; the driver refuses the memory) and put an empty range into the index at the + // pool's address, where a lookup of the pool that starts there found the empty one first. vk_pool_fini finds no + // record for it and does nothing. + if (!p->size) return 1; PoolRec *r = calloc(1, sizeof *r); if (!r) rt_fatal("vk: out of memory for a pool record"); r->key = p; @@ -827,7 +833,7 @@ static int query_stamp(uint64_t lo, uint64_t hi, uint32_t g) { // - GPU -> guest: the replay notes each GPU write into CPU-visible memory at record time (vk_mem_gpu_pending) and // reports it at the fence (vk_gpu_written), which copies those bytes back into guest memory. Until then the CPU // copies skip them: a page the CPU and the GPU both write keeps the GPU's bytes. -// TOTK_SHADOW=1/0 forces the mode on or off; the default is on for Linux. +// TOTK_SHADOW=1/0 forces the mode on or off; the default is on for Linux and Wine. typedef struct { uint64_t lo, hi; } SRange; static PlatMutex shadow_lock = PLAT_MUTEX_INIT; // after pool_lock; before gen_lock static PoolRec **stouch; // shadow_lock: pools resolved since the last sync @@ -1187,7 +1193,7 @@ static void mirror_mode(int discrete) { if (freeze_idle < 0) freeze_idle = 0; const char *sh = cfg_get("TOTK_SHADOW"); #ifdef _WIN32 - shadow_on = sh && *sh && strcmp(sh, "0"); + shadow_on = sh && *sh ? strcmp(sh, "0") != 0 : plat_is_wine(); #else shadow_on = !sh || !*sh || strcmp(sh, "0"); #endif @@ -1195,6 +1201,11 @@ static void mirror_mode(int discrete) { sh && *sh ? " (TOTK_SHADOW)" : ""); rt_log("mem: mode %s (%s): %s", mode == 2 ? "verify" : mode ? "mirror" : "import", why, mode ? "pools used as buffers get a device-local mirror" : "every CPU-visible pool is imported host memory"); + // Import is the integrated-GPU mode. On a discrete card every draw reads the pools over PCIe (the comment above + // this section has the measurements), which is the difference between playable and 5 fps when it is not intended. + if (!mode && discrete) + rt_log("mem: warning: import on a discrete GPU: the GPU reads the game's pools over PCIe on every draw, far " + "slower than mirror (the auto default on a discrete GPU); use memory = auto unless testing"); } static void range_add(MRange **a, int *n, int *cap, PoolRec *r, uint64_t off, uint64_t size) { @@ -1801,6 +1812,8 @@ static void collect_locked(VkCtx *c) { for (int i = 0; i < ndead; i++) { Dead *d = &dead[i]; if (d->seq > done_seq || d->tl > tl) { dead[k++] = *d; continue; } + rec_check_handle(d->kind == DEAD_VIEW ? REC_VIEW : d->kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, d->handle, 1, + d->tl, tl); switch (d->kind) { case DEAD_IMAGE: vkDestroyImage(c->device, (VkImage)(uintptr_t)d->handle, NULL); break; case DEAD_IMAGE_VMA: vmaDestroyImage(c->vma, (VkImage)(uintptr_t)d->handle, d->alloc); break; @@ -1932,8 +1945,21 @@ static void defer(int kind, uint64_t handle, VmaAllocation alloc) { plat_once(&cmd_once, cmd_init, NULL); plat_rmutex_lock(&cmd_lock); if (ndead == cap_dead) grow((void **)&dead, &cap_dead, sizeof *dead); - // The open batch may record into the object: it counts as the newest user. - dead[ndead++] = (Dead){kind, handle, alloc, open_active ? open_b.seq : next_seq, tl_noted}; + // The open batch may record into the object: it counts as the newest user. The GPU must also be past every + // submission that names it. tl_noted covers the submitted ones; an object the replay lets go of while it records + // a submission (vk_exec_recording_fence) may be named by that one too, not yet submitted: vk_texture.c + // slot_resolve hands a draw a released entry, and entry_of destroys released entries at that address later in + // the same submission. Stamped with the last submitted value, the object went as soon as the GPU reached it, + // before the submission that names it ran, and with the recorder thread (vk_rec.c) behind, before the recorder + // made the call that names it. LAS Recomp and Savage Wind Recomp stamp every deferral with the submission being + // recorded (runtime/nvn/vulkan/textures.c defer: vk_pending_seq). TOTK_DEFER_STAMP=0 brings back the old stamp. + static int stamp = -1; + if (stamp < 0) stamp = rt_knob("DEFER_STAMP", "1") != 0; + uint64_t tl = tl_noted, rf = stamp ? vk_exec_recording_fence() : 0; + if (rf > tl) tl = rf; + dead[ndead++] = (Dead){kind, handle, alloc, open_active ? open_b.seq : next_seq, tl}; + rec_check_handle(kind == DEAD_VIEW ? REC_VIEW : kind == DEAD_BUFFER_VMA ? REC_BUFFER : REC_IMAGE, handle, 0, tl, + vk_sync_fence_done()); plat_rmutex_unlock(&cmd_lock); } diff --git a/runtime/vk_mvcheck.c b/runtime/vk_mvcheck.c new file mode 100644 index 00000000..16660b24 --- /dev/null +++ b/runtime/vk_mvcheck.c @@ -0,0 +1,474 @@ +// TOTK_MV_CHECK=<frames> (test): measures how well DLSS's motion vectors explain the picture, without screenshots or +// judging by eye. Every <frames> DLAA frames, vk_dlaa.c mv_pass copies to host memory the scene light target of that +// frame and of the frame before (before DLSS, so jittered), and the motion image twice: after the camera pass (camera +// only) and after the per-object pass (what DLSS gets). Once the GPU is done, the completion thread compares them: +// warp error: the frame before, sampled bilinearly at p + mv + d, against this frame at p, as a relative luminance +// difference |a - b| / (a + b) averaged over pixels; d = last frame's jitter minus this frame's (the vectors are +// between unjittered positions, the pictures are jittered: content at x is drawn at x + jitter). Lower is better, +// and the correct vectors give the lowest. Over every pixel: zero motion, the camera's and the final vectors, and +// the final ones with d subtracted or left out (the jitter convention). Over the object pixels (final and camera +// differ by more than a quarter pixel): the camera's, the final ones and variants of the object part (final - +// camera) scaled by k = -1, 0.5, 1.5, 2 or with one axis negated. The variant with the lowest error names the +// correction the object vectors need, if any ("best"). +// flow: on up to FLOW_MAX object pixels with some texture, the offset whose 5x5 luminance patch of the frame before +// matches best, searched over a window around both the camera and the final vector and refined to a sub-pixel by +// a parabola: the measured motion. The end-point error of the camera and final vectors (+ d) against it, median in +// render pixels, and how often the final vector is the nearer. +// output (DLSS's picture, which the vectors are for): on about OUT_SAMPLES pixels picked independently of the vectors, +// the picture's own motion is measured with a wide search (12 px around the camera vector); where it differs from +// the camera's by over 1.5 px the pixel moves on its own. At each sample, DLSS's output (at the same unjittered +// place) against this frame's input, as a relative luminance difference: on the moving samples it grows where +// DLSS drags old colour along wrong vectors (ghosting, smearing), on the static ones it is the plain difference +// anti-aliasing and upscaling make. Their ratio compares runs with the per-object pass on and off (it does not +// depend on the pass's own vectors) and modes. +// One "mv_check:" line per check. Disocclusion and changing light add error to every candidate alike, so the comparison +// between candidates counts, not the absolute values. Copies and analysis cost a few ms on check frames only. +#include <math.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "cfg.h" +#include "host.h" +#include "nvn_api.h" +#include "vk.h" +#include "vk_exec.h" +#include "vk_dlaa.h" + +#define FLOW_MAX 20000 +#define OBJ_MIN 0.25f // final and camera vectors differ by more than this (render pixels): an object pixel + +#define OUT_SAMPLES 4000 +enum { PREV, CUR, MV_CAM, MV_FIN, OUT, NBUF }; +static struct { + int period; // 0 off, -1 not read yet + VkBuffer buf[NBUF]; + VmaAllocation mem[NBUF]; + uint8_t *map[NBUF]; + uint64_t cap[NBUF]; + uint32_t w, h, ow, oh; // ow, oh: DLSS's output in OUT, 0 none this check + int want_out; // the check frame's output is to be copied (vk_mvcheck_output) + char masks[400]; // the check frame's motion draws (vk_mvobj_frame_masks) + VkFormat cfmt; + uint64_t n; // camera-stage calls (DLAA frames with motion) + uint64_t prev_frame; // gfx frame the PREV colour is of, 0 none + float prev_u[2], u[2], uprev[2]; + int check; // this frame is a check frame (camera stage copied MV_CAM) + int busy; // analysis pending: the buffers are not touched + uint64_t done; +} K = {.period = -1}; + +static int on(void) { + if (K.period < 0) { + const char *v = cfg_get("TOTK_MV_CHECK"); + K.period = v && *v ? atoi(v) : 0; + if (K.period < 0) K.period = 0; + if (K.period && K.period < 4) K.period = 4; + if (K.period) rt_log("mv_check: motion vectors measured every %d frames", K.period); + } + return K.period > 0; +} + +static int cbytes(VkFormat f) { + return f == VK_FORMAT_B10G11R11_UFLOAT_PACK32 ? 4 : f == VK_FORMAT_R16G16B16A16_SFLOAT ? 8 : 0; +} + +// Host buffers of at least the size asked for; a larger size gets new ones and leaves the old (test only). +static int ensure(int i, uint64_t bytes) { + if (K.cap[i] >= bytes) return 1; + VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = bytes, + .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO, + .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT}; + VmaAllocationInfo info; + if (vmaCreateBuffer(vk_ctx()->vma, &bi, &ai, &K.buf[i], &K.mem[i], &info) != VK_SUCCESS || !info.pMappedData) { + rt_log("mv_check: host buffer of %llu bytes failed: off", (unsigned long long)bytes); + K.period = 0; + return 0; + } + K.map[i] = info.pMappedData, K.cap[i] = bytes; + return 1; +} + +static void barrier(VkCommandBuffer cb) { + VkMemoryBarrier2 m = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, + .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT | VK_ACCESS_2_MEMORY_READ_BIT, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_2_HOST_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT | VK_ACCESS_2_MEMORY_READ_BIT | + VK_ACCESS_2_HOST_READ_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &m}; + vkCmdPipelineBarrier2(cb, &di); +} + +static void copy(VkCommandBuffer cb, int i, VkImage img, uint32_t level, uint32_t layer, uint32_t w, uint32_t h) { + barrier(cb); + VkBufferImageCopy r = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}, .imageExtent = {w, h, 1}}; + vkCmdCopyImageToBuffer(cb, img, VK_IMAGE_LAYOUT_GENERAL, K.buf[i], 1, &r); + barrier(cb); +} + +// ---- decoding + +static float half(uint16_t v) { + uint32_t s = (uint32_t)(v >> 15) << 31, e = v >> 10 & 31, m = v & 1023, f; + if (!e) return (s ? -1.0f : 1.0f) * ldexpf((float)m, -24); + f = e == 31 ? s | 0x7f800000u | m << 13 : s | (e + 112) << 23 | m << 13; + float r; + memcpy(&r, &f, 4); + return r; +} + +static float ufloat(uint32_t v, int mbits) { // unsigned 5-bit exponent mini-float + uint32_t e = v >> mbits, m = v & ((1u << mbits) - 1); + if (e == 31) return 65504.0f; + return e ? ldexpf(1.0f + (float)m / (float)(1u << mbits), (int)e - 15) : ldexpf((float)m / (float)(1u << mbits), -14); +} + +static void luminance(const uint8_t *src, VkFormat f, uint32_t n, float *out) { + for (uint32_t i = 0; i < n; i++) { + float r, g, b; + if (f == VK_FORMAT_B10G11R11_UFLOAT_PACK32) { + uint32_t v; + memcpy(&v, src + 4 * (size_t)i, 4); + r = ufloat(v & 0x7ff, 6), g = ufloat(v >> 11 & 0x7ff, 6), b = ufloat(v >> 22, 5); + } else { + uint16_t c[4]; + memcpy(c, src + 8 * (size_t)i, 8); + r = half(c[0]), g = half(c[1]), b = half(c[2]); + } + float y = 0.2126f * r + 0.7152f * g + 0.0722f * b; + out[i] = isfinite(y) && y > 0 ? y : 0; + } +} + +// ---- analysis + +typedef struct { const float *cur, *prev; uint32_t w, h; } Pic; + +static int sample(const Pic *p, float x, float y, float *out) { // bilinear, pixel index coordinates + if (!(x >= 0 && y >= 0 && x <= (float)(p->w - 1) && y <= (float)(p->h - 1))) return 0; + uint32_t x0 = (uint32_t)x, y0 = (uint32_t)y, x1 = x0 + 1 < p->w ? x0 + 1 : x0, y1 = y0 + 1 < p->h ? y0 + 1 : y0; + float fx = x - (float)x0, fy = y - (float)y0; + const float *a = p->prev + (size_t)y0 * p->w, *b = p->prev + (size_t)y1 * p->w; + *out = (a[x0] * (1 - fx) + a[x1] * fx) * (1 - fy) + (b[x0] * (1 - fx) + b[x1] * fx) * fy; + return 1; +} + +typedef struct { double sum; uint64_t n; } Acc; +static void acc(Acc *a, const Pic *p, uint32_t x, uint32_t y, float mx, float my) { + float b; + if (!sample(p, (float)x + mx, (float)y + my, &b)) return; + float c = p->cur[(size_t)y * p->w + x]; + a->sum += fabsf(c - b) / (c + b + 1e-4f), a->n++; +} +static double mean(const Acc *a) { return a->n ? a->sum / (double)a->n : NAN; } + +static float sad5(const Pic *p, uint32_t x, uint32_t y, int dx, int dy) { + float s = 0; + for (int j = -2; j <= 2; j++) + for (int i = -2; i <= 2; i++) { + int px = (int)x + i + dx, py = (int)y + j + dy; + if (px < 0 || py < 0 || px >= (int)p->w || py >= (int)p->h) return INFINITY; + float c = p->cur[(size_t)(y + j) * p->w + x + i], b = p->prev[(size_t)py * p->w + px]; + s += fabsf(c - b) / (c + b + 1e-4f); + } + return s; +} + +static float parabola(float l, float c, float r) { // offset of the minimum of three samples at -1, 0, 1 + float den = l - 2 * c + r; + if (!(den > 1e-9f) || !isfinite(l) || !isfinite(r)) return 0; + float o = 0.5f * (l - r) / den; + return o < -0.5f ? -0.5f : o > 0.5f ? 0.5f : o; // a flat curve says nothing past the neighbours +} + +static int by_float(const void *a, const void *b) { + float x = *(const float *)a, y = *(const float *)b; + return x < y ? -1 : x > y; +} + +// The object pixels' measured motion (sub-pixel), searched over the box around both vectors with a margin of 3; +// 0 when the patch is flat or the match is not clearly better than the box's average. +static int flow(const Pic *p, uint32_t x, uint32_t y, float ax, float ay, float bx, float by, float out[2]) { + float mu = 0, var = 0; + for (int j = -2; j <= 2; j++) + for (int i = -2; i <= 2; i++) mu += p->cur[(size_t)(y + j) * p->w + x + i]; + mu /= 25; + for (int j = -2; j <= 2; j++) + for (int i = -2; i <= 2; i++) { + float d = p->cur[(size_t)(y + j) * p->w + x + i] - mu; + var += d * d; + } + if (mu <= 0 || sqrtf(var / 25) < 0.05f * mu) return 0; + int x0 = (int)floorf(fminf(ax, bx)) - 3, x1 = (int)ceilf(fmaxf(ax, bx)) + 3; + int y0 = (int)floorf(fminf(ay, by)) - 3, y1 = (int)ceilf(fmaxf(ay, by)) + 3; + if (x1 - x0 > 40 || y1 - y0 > 40) return 0; + float best = INFINITY, total = 0; + int bdx = 0, bdy = 0, n = 0; + for (int dy = y0; dy <= y1; dy++) + for (int dx = x0; dx <= x1; dx++) { + float s = sad5(p, x, y, dx, dy); + if (!isfinite(s)) continue; + total += s, n++; + if (s < best) best = s, bdx = dx, bdy = dy; + } + if (!n || !isfinite(best) || best > 0.5f * total / (float)n) return 0; + out[0] = (float)bdx + parabola(sad5(p, x, y, bdx - 1, bdy), best, sad5(p, x, y, bdx + 1, bdy)); + out[1] = (float)bdy + parabola(sad5(p, x, y, bdx, bdy - 1), best, sad5(p, x, y, bdx, bdy + 1)); + return 1; +} + +// Totals over every check (logged every TOTAL_EVERY checks): end-point error histograms in HBIN px bins of the object +// samples (camera, final) and of the static samples (the measurement's own noise floor), and the object warp errors. +#define HBIN 0.05f +#define HN 640 +#define TOTAL_EVERY 20 +#define KN 120 // histogram of per-sample scale ratios, -2 to 4 in steps of 0.05 +enum { H_CAM, H_FIN, H_STATIC, NH }; +static struct { uint64_t h[NH][HN], n[NH], checks, wrong, kh[KN], kn; double obj[2], objn, knum, kden, out[2], outn[2]; uint64_t cal[3]; } T; // cal: checks where k=0.5, k=1 fit the static pixels best; checks + +static void hist_add(int k, float e) { + int b = e >= 0 && e < HBIN * HN ? (int)(e / HBIN) : HN - 1; // NaN and huge errors in the last bin + T.h[k][b < HN ? b : HN - 1]++, T.n[k]++; +} +static float hist_q(int k, double q) { // quantile q of histogram k, bin centre + uint64_t want = (uint64_t)(q * (double)T.n[k]), s = 0; + for (int b = 0; b < HN; b++) + if ((s += T.h[k][b]) > want) return ((float)b + 0.5f) * HBIN; + return NAN; +} + +static float kmedian(void) { + uint64_t s = 0; + for (int b = 0; b < KN; b++) + if ((s += T.kh[b]) * 2 > T.kn) return -2 + ((float)b + 0.5f) * 0.05f; + return NAN; +} + +static uint32_t mix(uint32_t x, uint32_t y) { // a fixed pseudo-random pick of pixels, spread over the picture + uint32_t v = x * 0x9E3779B1u ^ y * 0x85EBCA77u; + v ^= v >> 15, v *= 0x2C1B3C6Du, v ^= v >> 12; + return v; +} + +static void analyse(void *arg) { + (void)arg; + VmaAllocator vma = vk_ctx()->vma; + for (int i = 0; i < NBUF; i++) vmaInvalidateAllocation(vma, K.mem[i], 0, VK_WHOLE_SIZE); + uint32_t w = K.w, h = K.h, n = w * h; + float *cur = malloc(sizeof(float) * n), *prev = malloc(sizeof(float) * n), *e_cam = NULL, *e_fin = NULL, + *e_st = NULL; + if (!cur || !prev) goto out; + luminance(K.map[CUR], K.cfmt, n, cur); + luminance(K.map[PREV], K.cfmt, n, prev); + Pic p = {cur, prev, w, h}; + const uint16_t *A = (const uint16_t *)K.map[MV_CAM], *B = (const uint16_t *)K.map[MV_FIN]; + float dx = K.uprev[0] - K.u[0], dy = K.uprev[1] - K.u[1]; + enum { ZERO, CAM, FIN, FIN_NEG_D, FIN_NO_D, NALL }; + enum { O_CAM, O_FIN, O_KM1, O_K05, O_K15, O_K2, O_NX, O_NY, NOBJ }; + static const char *const oname[NOBJ] = {"camera", "final", "k=-1", "k=0.5", "k=1.5", "k=2", "-x", "-y"}; + Acc all[NALL] = {0}, obj[NOBJ] = {0}, cal[3] = {0}; + // the object pixels first, for the sampling rates: about FLOW_MAX object and FLOW_MAX / 4 static samples + uint64_t nobj = 0, huge = 0; // huge: camera vectors over 256 px or not finite + double cam_len = 0; + for (uint32_t y = 2; y + 2 < h; y++) + for (uint32_t x = 2; x + 2 < w; x++) { + size_t i = (size_t)y * w + x; + float ax = half(A[2 * i]), ay = half(A[2 * i + 1]), bx = half(B[2 * i]), by = half(B[2 * i + 1]); + if (isfinite(ax) && isfinite(ay)) cam_len += hypotf(ax, ay); + huge += !isfinite(ax) || !isfinite(ay) || hypotf(ax, ay) > 256; + nobj += fabsf(bx - ax) > OBJ_MIN || fabsf(by - ay) > OBJ_MIN; + } + uint32_t rate_obj = nobj > FLOW_MAX ? (uint32_t)(0xffffffffull * FLOW_MAX / nobj) : 0xffffffffu; + uint64_t nst = (uint64_t)n - nobj; + uint32_t rate_st = nst > FLOW_MAX / 4 ? (uint32_t)(0xffffffffull * (FLOW_MAX / 4) / nst) : 0xffffffffu; + e_cam = malloc(sizeof(float) * FLOW_MAX * 2), e_fin = malloc(sizeof(float) * FLOW_MAX * 2), + e_st = malloc(sizeof(float) * FLOW_MAX); + if (!e_cam || !e_fin || !e_st) goto out; + int nflow = 0, nearer = 0, tried = 0, nst_f = 0; + double knum = 0, kden = 0; + // object samples the final vector misses by more than 1.5 px where the camera's fits better: where and how + struct { int n; uint32_t x0, y0, x1, y1; double ox, oy, mx, my; } bad = {0, UINT32_MAX, UINT32_MAX, 0, 0, 0, 0, 0, 0}; + for (uint32_t y = 2; y + 2 < h; y++) + for (uint32_t x = 2; x + 2 < w; x++) { + size_t i = (size_t)y * w + x; + float ax = half(A[2 * i]), ay = half(A[2 * i + 1]), bx = half(B[2 * i]), by = half(B[2 * i + 1]); + if (!isfinite(ax) || !isfinite(ay) || !isfinite(bx) || !isfinite(by)) continue; + acc(&all[ZERO], &p, x, y, dx, dy); + acc(&all[CAM], &p, x, y, ax + dx, ay + dy); + acc(&all[FIN], &p, x, y, bx + dx, by + dy); + acc(&all[FIN_NEG_D], &p, x, y, bx - dx, by - dy); + acc(&all[FIN_NO_D], &p, x, y, bx, by); + float ox = bx - ax, oy = by - ay, f[2]; + if (fabsf(ox) <= OBJ_MIN && fabsf(oy) <= OBJ_MIN) { + // calibration: static pixels with a small camera vector (known right), at k = 0.5, 1, 1.5 of it + float al = hypotf(ax, ay); + if (al > 0.25f && al < 2) { + acc(&cal[0], &p, x, y, 0.5f * ax + dx, 0.5f * ay + dy), acc(&cal[1], &p, x, y, ax + dx, ay + dy); + acc(&cal[2], &p, x, y, 1.5f * ax + dx, 1.5f * ay + dy); + } + if (mix(x, y) > rate_st || nst_f >= FLOW_MAX) continue; + if (flow(&p, x, y, ax + dx, ay + dy, ax + dx, ay + dy, f)) e_st[nst_f++] = hypotf(ax + dx - f[0], ay + dy - f[1]); + continue; + } + acc(&obj[O_CAM], &p, x, y, ax + dx, ay + dy); + acc(&obj[O_FIN], &p, x, y, bx + dx, by + dy); + acc(&obj[O_KM1], &p, x, y, ax - ox + dx, ay - oy + dy); + acc(&obj[O_K05], &p, x, y, ax + 0.5f * ox + dx, ay + 0.5f * oy + dy); + acc(&obj[O_K15], &p, x, y, ax + 1.5f * ox + dx, ay + 1.5f * oy + dy); + acc(&obj[O_K2], &p, x, y, ax + 2 * ox + dx, ay + 2 * oy + dy); + acc(&obj[O_NX], &p, x, y, ax - ox + dx, by + dy); + acc(&obj[O_NY], &p, x, y, bx + dx, ay - oy + dy); + if (mix(x, y) > rate_obj || nflow >= 2 * FLOW_MAX) continue; + tried++; + if (!flow(&p, x, y, ax + dx, ay + dy, bx + dx, by + dy, f)) continue; + float ec = hypotf(ax + dx - f[0], ay + dy - f[1]), ef = hypotf(bx + dx - f[0], by + dy - f[1]); + e_cam[nflow] = ec, e_fin[nflow] = ef, nflow++; + nearer += ef < ec; + if (fabsf(ox) > 0.5f || fabsf(oy) > 0.5f) { // the scale of the object part the measured motion shows + float mx = f[0] - ax - dx, my = f[1] - ay - dy, oo = ox * ox + oy * oy, kr = (mx * ox + my * oy) / oo; + knum += mx * ox + my * oy, kden += oo; + int kb = (int)floorf((kr + 2) / 0.05f); + T.kh[kb < 0 ? 0 : kb >= KN ? KN - 1 : kb]++, T.kn++; + } + if (ef > 1.5f && ef > ec) { + bad.n++, bad.ox += ox, bad.oy += oy, bad.mx += f[0] - ax - dx, bad.my += f[1] - ay - dy; + if (x < bad.x0) bad.x0 = x; + if (y < bad.y0) bad.y0 = y; + if (x > bad.x1) bad.x1 = x; + if (y > bad.y1) bad.y1 = y; + } + } + int best = 0; + for (int k = 1; k < NOBJ; k++) + if (obj[k].n && (!obj[best].n || mean(&obj[k]) < mean(&obj[best]))) best = k; + char line[512]; + int o = snprintf(line, sizeof line, "object pixels %.1f%% (%llu):", 100.0 * (double)nobj / (double)n, + (unsigned long long)nobj); + for (int k = 0; k < NOBJ && o < (int)sizeof line; k++) o += snprintf(line + o, sizeof line - o, " %s %.4f", oname[k], mean(&obj[k])); + if (o < (int)sizeof line) snprintf(line + o, sizeof line - o, "; best %s", nobj ? oname[best] : "-"); + // DLSS's output against this frame's input, on samples moving on their own and static ones (the picture's motion) + char outl[200] = ""; + if (K.ow && K.oh) { + float *ol = malloc(sizeof(float) * (size_t)K.ow * K.oh); + if (ol) { + luminance(K.map[OUT], VK_FORMAT_R16G16B16A16_SFLOAT, K.ow * K.oh, ol); + Pic op = {NULL, ol, K.ow, K.oh}; // sample() reads prev + float sx = (float)K.ow / (float)w, sy = (float)K.oh / (float)h; + uint32_t rate = (uint32_t)(0xffffffffull * OUT_SAMPLES / n); + Acc mov = {0}, sta = {0}; + for (uint32_t y = 2; y + 2 < h; y++) + for (uint32_t x = 2; x + 2 < w; x++) { + if ((mix(x, y) ^ 0x5bd1e995u) * 0x2C1B3C6Du > rate) continue; + size_t i = (size_t)y * w + x; + float ax = half(A[2 * i]) + dx, ay = half(A[2 * i + 1]) + dy, f[2], ov; + if (!isfinite(ax) || !isfinite(ay) || !flow(&p, x, y, ax - 12, ay - 12, ax + 12, ay + 12, f)) continue; + if (!sample(&op, ((float)x + 0.5f - K.u[0]) * sx - 0.5f, ((float)y + 0.5f - K.u[1]) * sy - 0.5f, &ov)) + continue; + float c = cur[i], e = fabsf(ov - c) / (ov + c + 1e-4f); + Acc *a = hypotf(f[0] - ax, f[1] - ay) > 1.5f ? &mov : &sta; + a->sum += e, a->n++; + } + snprintf(outl, sizeof outl, "; output against input: moving on their own %.4f (%llu), static %.4f (%llu), " + "ratio %.3f", mean(&mov), (unsigned long long)mov.n, mean(&sta), (unsigned long long)sta.n, + mean(&mov) / mean(&sta)); + if (mov.n) T.out[0] += mov.sum, T.outn[0] += (double)mov.n; + if (sta.n) T.out[1] += sta.sum, T.outn[1] += (double)sta.n; + free(ol); + } + } + float mc = NAN, mf = NAN, ms = NAN; + for (int k = 0; k < nflow; k++) hist_add(H_CAM, e_cam[k]), hist_add(H_FIN, e_fin[k]); + for (int k = 0; k < nst_f; k++) hist_add(H_STATIC, e_st[k]); + if (nflow) { + qsort(e_cam, (size_t)nflow, sizeof *e_cam, by_float), qsort(e_fin, (size_t)nflow, sizeof *e_fin, by_float); + mc = e_cam[nflow / 2], mf = e_fin[nflow / 2]; + } + if (nst_f) qsort(e_st, (size_t)nst_f, sizeof *e_st, by_float), ms = e_st[nst_f / 2]; + char badl[200] = ""; + if (bad.n) + snprintf(badl, sizeof badl, "; %d missed (over 1.5 px, camera nearer) in x %u-%u y %u-%u, object part (%.2f %.2f), " + "measured (%.2f %.2f)", bad.n, bad.x0, bad.x1, bad.y0, bad.y1, bad.ox / bad.n, bad.oy / bad.n, + bad.mx / bad.n, bad.my / bad.n); + rt_log("mv_check: #%llu %ux%u, jitter (%.2f %.2f) after (%.2f %.2f), camera motion %.2f px; warp error, every pixel: " + "zero %.4f camera %.4f final %.4f (jitter subtracted %.4f, left out %.4f); camera vectors over 256 px %llu; %s; " + "flow on %d of %d object " + "samples: median error camera %.2f px, final %.2f px, final nearer %.0f%%; static %.2f px (%d); scale %.2f; calibration, static pixels under 2 px of camera motion (%llu): k=0.5 %.4f k=1 %.4f " + "k=1.5 %.4f%s%s; motion draws (position banks/changed banks:draws)%s", + (unsigned long long)++K.done, w, h, K.u[0], K.u[1], K.uprev[0], K.uprev[1], cam_len / (double)n, + mean(&all[ZERO]), mean(&all[CAM]), mean(&all[FIN]), mean(&all[FIN_NEG_D]), mean(&all[FIN_NO_D]), (unsigned long long)huge, line, + nflow, tried, mc, mf, nflow ? 100.0 * nearer / nflow : 0.0, ms, nst_f, kden > 0 ? knum / kden : NAN, (unsigned long long)cal[1].n, mean(&cal[0]), mean(&cal[1]), mean(&cal[2]), badl, outl, K.masks); + if (cal[1].n > 1000) T.cal[0] += mean(&cal[0]) < mean(&cal[1]) && mean(&cal[0]) < mean(&cal[2]), T.cal[1] += mean(&cal[1]) <= mean(&cal[0]) && mean(&cal[1]) <= mean(&cal[2]), T.cal[2]++; + T.knum += knum, T.kden += kden; + T.checks++; + if (obj[O_FIN].n) { + T.obj[0] += obj[O_CAM].sum, T.obj[1] += obj[O_FIN].sum, T.objn += (double)obj[O_FIN].n; + T.wrong += nobj * 200 > n && best != O_FIN; // over 0.5 % of the picture and the vectors are not the best fit + } + if (T.checks % TOTAL_EVERY == 0) + rt_log("mv_check total: %llu checks, %llu with over 0.5%% object pixels fit better by a variant; object warp error " + "camera %.4f final %.4f; end-point error (median / 90%% / 99%%) object samples camera %.2f / %.2f / %.2f " + "px, final %.2f / %.2f / %.2f px (%llu); static samples %.2f / %.2f / %.2f px (%llu); object scale %.3f (median %.2f of %llu); calibration: k=0.5 best in %llu, k=1 in %llu of %llu checks; " + "output against input: moving on their own %.4f (%.0f), static %.4f (%.0f), ratio %.3f", + (unsigned long long)T.checks, (unsigned long long)T.wrong, T.objn ? T.obj[0] / T.objn : NAN, + T.objn ? T.obj[1] / T.objn : NAN, hist_q(H_CAM, 0.5), hist_q(H_CAM, 0.9), hist_q(H_CAM, 0.99), + hist_q(H_FIN, 0.5), hist_q(H_FIN, 0.9), hist_q(H_FIN, 0.99), (unsigned long long)T.n[H_FIN], + hist_q(H_STATIC, 0.5), hist_q(H_STATIC, 0.9), hist_q(H_STATIC, 0.99), (unsigned long long)T.n[H_STATIC], + T.kden > 0 ? T.knum / T.kden : NAN, kmedian(), (unsigned long long)T.kn, (unsigned long long)T.cal[0], (unsigned long long)T.cal[1], + (unsigned long long)T.cal[2], T.outn[0] ? T.out[0] / T.outn[0] : NAN, T.outn[0], + T.outn[1] ? T.out[1] / T.outn[1] : NAN, T.outn[1], + T.outn[0] && T.outn[1] ? (T.out[0] / T.outn[0]) / (T.out[1] / T.outn[1]) : NAN); +out: + free(cur), free(prev), free(e_cam), free(e_fin), free(e_st); + __atomic_store_n(&K.busy, 0, __ATOMIC_RELEASE); +} + +// ---- capture (vk_dlaa.c mv_pass) + +void vk_mvcheck_camera(VkCommandBuffer cb, VkImage mv, uint32_t w, uint32_t h, float jx, float jy) { + K.check = 0; + if (!on()) return; + K.n++; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (K.n % (uint64_t)K.period || __atomic_load_n(&K.busy, __ATOMIC_ACQUIRE) || K.prev_frame + 1 != frame || + K.w != w || K.h != h) + return; + if (!ensure(MV_CAM, (uint64_t)w * h * 4) || !ensure(MV_FIN, (uint64_t)w * h * 4)) return; + copy(cb, MV_CAM, mv, 0, 0, w, h); + K.check = 1, K.u[0] = jx, K.u[1] = jy, K.uprev[0] = K.prev_u[0], K.uprev[1] = K.prev_u[1]; +} + +void vk_mvcheck_final(VkCommandBuffer cb, uint64_t fence, VkImage mv, VkImage cimg, VkFormat cfmt, uint32_t level, + uint32_t layer, uint32_t w, uint32_t h, float jx, float jy) { + if (!on()) return; + uint64_t frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + int b = cbytes(cfmt); + int want_prev = (K.n + 1) % (uint64_t)K.period == 0 && !__atomic_load_n(&K.busy, __ATOMIC_ACQUIRE); + if (!b) return (void)(K.check = 0, K.prev_frame = 0); + if (K.check) { + if (!ensure(CUR, (uint64_t)w * h * (uint64_t)b)) return; + copy(cb, MV_FIN, mv, 0, 0, w, h); + copy(cb, CUR, cimg, level, layer, w, h); + K.check = 0, K.prev_frame = 0, K.ow = K.oh = 0, K.want_out = 1; + snprintf(K.masks, sizeof K.masks, "%s", vk_mvobj_frame_masks()); + __atomic_store_n(&K.busy, 1, __ATOMIC_RELEASE); + vk_completion_add(fence, analyse, NULL); + return; + } + if (!want_prev) return; + if (!ensure(PREV, (uint64_t)w * h * (uint64_t)b)) return; + copy(cb, PREV, cimg, level, layer, w, h); + K.prev_frame = frame, K.prev_u[0] = jx, K.prev_u[1] = jy, K.w = w, K.h = h, K.cfmt = cfmt; +} + +// After DLSS ran on a check frame (vk_dlaa.c evaluate): its output (RGBA16F, ow x oh) for the analysis, which runs once +// the submission is done. +void vk_mvcheck_output(VkCommandBuffer cb, VkImage out, uint32_t ow, uint32_t oh) { + if (!K.want_out) return; + K.want_out = 0; + if (!ensure(OUT, (uint64_t)ow * oh * 8)) return; + copy(cb, OUT, out, 0, 0, ow, oh); + K.ow = ow, K.oh = oh; +} diff --git a/runtime/vk_mvobj_fs.h b/runtime/vk_mvobj_fs.h new file mode 100644 index 00000000..19b1a16b --- /dev/null +++ b/runtime/vk_mvobj_fs.h @@ -0,0 +1,39 @@ +// Generated by tools/make_mvobj_fs.py: the per-object motion fragment shader (vk_mvobj.c). Do not edit. +#define MVOBJ_FS_LOCATION_WORD 24 // the input Location literal, patched to the variant's +static const uint32_t mvobj_fs_spv[] = { + 0x07230203, 0x00010000, 0x00000000, 0x00000034, 0x00000000, 0x00020011, 0x00000001, 0x0003000e, + 0x00000000, 0x00000001, 0x0008000f, 0x00000004, 0x00000001, 0x6e69616d, 0x00000000, 0x0000000e, + 0x0000000f, 0x00000010, 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x0000000e, 0x0000001e, + 0x00000000, 0x00040047, 0x0000000f, 0x0000000b, 0x0000000f, 0x00040047, 0x00000010, 0x0000001e, + 0x00000000, 0x00030047, 0x0000000b, 0x00000002, 0x00050048, 0x0000000b, 0x00000000, 0x00000023, + 0x00000000, 0x00040047, 0x00000011, 0x00000022, 0x00000000, 0x00040047, 0x00000011, 0x00000021, + 0x00000001, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000004, + 0x00000020, 0x00040017, 0x00000005, 0x00000004, 0x00000002, 0x00040017, 0x00000006, 0x00000004, + 0x00000004, 0x00040015, 0x00000007, 0x00000020, 0x00000000, 0x00020014, 0x00000008, 0x00040020, + 0x00000009, 0x00000001, 0x00000006, 0x00040020, 0x0000000a, 0x00000003, 0x00000006, 0x0003001e, + 0x0000000b, 0x00000006, 0x00040020, 0x0000000c, 0x00000002, 0x0000000b, 0x00040020, 0x00000017, + 0x00000002, 0x00000006, 0x0004002b, 0x00000004, 0x00000012, 0x3f000000, 0x0004002b, 0x00000004, + 0x00000013, 0x00000000, 0x0004002b, 0x00000004, 0x00000014, 0x358637bd, 0x0004002b, 0x00000007, + 0x00000015, 0x00000000, 0x0007002c, 0x00000006, 0x00000016, 0x00000013, 0x00000013, 0x00000013, + 0x00000013, 0x0004003b, 0x00000009, 0x0000000e, 0x00000001, 0x0004003b, 0x00000009, 0x0000000f, + 0x00000001, 0x0004003b, 0x0000000a, 0x00000010, 0x00000003, 0x0004003b, 0x0000000c, 0x00000011, + 0x00000002, 0x00050036, 0x00000002, 0x00000001, 0x00000000, 0x00000003, 0x000200f8, 0x00000018, + 0x00050041, 0x00000017, 0x00000019, 0x00000011, 0x00000015, 0x0004003d, 0x00000006, 0x0000001a, + 0x00000019, 0x0004003d, 0x00000006, 0x0000001b, 0x0000000e, 0x0004003d, 0x00000006, 0x0000001c, + 0x0000000f, 0x0007004f, 0x00000005, 0x0000001d, 0x0000001c, 0x0000001c, 0x00000000, 0x00000001, + 0x0007004f, 0x00000005, 0x0000001e, 0x0000001a, 0x0000001a, 0x00000002, 0x00000003, 0x00050083, + 0x00000005, 0x0000001f, 0x0000001d, 0x0000001e, 0x00050051, 0x00000004, 0x00000020, 0x0000001b, + 0x00000000, 0x00050051, 0x00000004, 0x00000021, 0x0000001b, 0x00000001, 0x00050051, 0x00000004, + 0x00000022, 0x0000001b, 0x00000003, 0x00050088, 0x00000004, 0x00000023, 0x00000020, 0x00000022, + 0x00050088, 0x00000004, 0x00000024, 0x00000021, 0x00000022, 0x00050051, 0x00000004, 0x00000025, + 0x0000001a, 0x00000000, 0x00050051, 0x00000004, 0x00000026, 0x0000001a, 0x00000001, 0x00050085, + 0x00000004, 0x00000027, 0x00000023, 0x00000012, 0x00050081, 0x00000004, 0x00000028, 0x00000027, + 0x00000012, 0x00050085, 0x00000004, 0x00000029, 0x00000028, 0x00000025, 0x00050085, 0x00000004, + 0x0000002a, 0x00000024, 0x00000012, 0x00050083, 0x00000004, 0x0000002b, 0x00000012, 0x0000002a, + 0x00050085, 0x00000004, 0x0000002c, 0x0000002b, 0x00000026, 0x00050051, 0x00000004, 0x0000002d, + 0x0000001f, 0x00000000, 0x00050051, 0x00000004, 0x0000002e, 0x0000001f, 0x00000001, 0x00050083, + 0x00000004, 0x0000002f, 0x00000029, 0x0000002d, 0x00050083, 0x00000004, 0x00000030, 0x0000002c, + 0x0000002e, 0x00070050, 0x00000006, 0x00000031, 0x0000002f, 0x00000030, 0x00000013, 0x00000013, + 0x000500ba, 0x00000008, 0x00000032, 0x00000022, 0x00000014, 0x000600a9, 0x00000006, 0x00000033, + 0x00000032, 0x00000031, 0x00000016, 0x0003003e, 0x00000010, 0x00000033, 0x000100fd, 0x00010038 +}; diff --git a/runtime/vk_mvprev.c b/runtime/vk_mvprev.c new file mode 100644 index 00000000..386c48c9 --- /dev/null +++ b/runtime/vk_mvprev.c @@ -0,0 +1,219 @@ +// Last frame's object and bone blocks, for per-object motion vectors (TOTK_DLAA_OBJMV, on with DLAA). The banks kept +// start as c5, c7 and c14 and grow with what the vertex shader variants read (vk_mvprev_want); c7 and c5 are described +// here, the others follow the same two-copy rule. +// +// The scene's vertex shaders read an object's place from bank c7 (gsys_shape: one 3x4 matrix at the start of a +// 0x100 block) and a skinned object's bones from c5 (gsys_skeleton: a run of 3x4 matrices), of this frame only. The +// game writes both into two copies that take turns, the second right after the first: an object's block is at addr in +// one frame and at addr +- step in the next, step its size rounded up to 256 bytes (vk_mvsurvey.c measured it: every +// c7 pair 0x100 apart, c5 pairs their rounded size apart, the pair's contents the same object moved a little). The +// copy this frame does not write still holds last frame's when the frame is submitted, so the uniform snapshot +// (vk_usnap.c) also copies both neighbours of these blocks, and vk_mvprev_find picks last frame's: +// self the block was bound at the same address last frame too (an object the game did not write again): itself +// side one neighbour was bound in that bank last frame: that one +// nearer both were (the copies of adjacent objects interleave): the one with more of its first 16 words equal to +// this frame's +// none neither: a new object, no motion of its own +// The pick is checked against what was snapshotted of that block last frame (intact: the same first CHECK bytes), and +// a summary goes to the log every SUMMARY frames: +// mv_prev: c<n>: <blocks> blocks: self a %, side b %, nearer c %, none d %; intact e %; moved: still f %, under +// one unit g %, more h % +#include <math.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "cfg.h" +#include "host.h" +#include "vk.h" +#include "vk_exec.h" +#include "vk_dlaa.h" + +#define CHECK 128u +#define TABLE (1u << 14) +#define SUMMARY 600 + +enum { BONES = 2, PLACE = 4, CLOCK = 11, BANKS = 14 }; // NVN binding index: banks c5, c7 and c14; c3..c16 + +// 16 bytes, so a table of TABLE entries stays in the cache: every scene draw probes it (the bytes of TOTK_MVPREV_CHECK +// live in a table of their own, by entry index, allocated only for the check) +typedef struct { + uint64_t addr; // 0: empty + int index; + uint32_t have; +} Seen; + +typedef struct { uint64_t blocks, self, side, nearer, none, checked, intact, still, small, big; } Stat; + +static struct { + int on; // 1 knob on and DLAA on this frame + int knob; // -1 not read + Seen *t[2]; // this frame's bound blocks, last frame's + uint8_t (*bytes[2])[CHECK]; // TOTK_MVPREV_CHECK: the first CHECK bytes of each entry of t[k], NULL without the check + int timed; // TOTK_MVOBJ_TIMES: the summary's times + uint32_t frames; + Stat st[BANKS]; // per binding index + uint64_t last[BANKS]; // the block the last scene draw bound per binding index + uint32_t mask; // binding indices kept (vk_mvprev_want): bones, place and clock from the start + uint64_t ns_draw, ns_frame; // time in vk_mvprev_draw and vk_mvprev_frame since the last summary +} P = {.knob = -1, .mask = 1u << BONES | 1u << PLACE | 1u << CLOCK}; + +static uint32_t slot_of(uint64_t addr, int index) { + uint64_t h = (addr ^ (uint64_t)index * 0x9E3779B97F4A7C15ull) * 0xff51afd7ed558ccdull; + return (uint32_t)(h >> 40) & (TABLE - 1); +} + +static Seen *find(Seen *t, uint64_t addr, int index, int add) { + for (uint32_t i = slot_of(addr, index), k = 0; k < TABLE; i = (i + 1) & (TABLE - 1), k++) { + if (t[i].addr == addr && t[i].index == index) return &t[i]; + if (!t[i].addr) { + if (!add) return NULL; + t[i].addr = addr, t[i].index = index; + return &t[i]; + } + } + return NULL; +} + +int vk_mvprev_on(void) { return P.on > 0; } + +void vk_mvprev_want(uint32_t index_mask) { + static int fixed = -1; + if (fixed < 0) fixed = !rt_knob("MVPREV_GROW", "1"); // TOTK_MVPREV_GROW=0: c5, c7 and c14 only (test) + if (!fixed) P.mask |= index_mask & ((1u << BANKS) - 1); +} + +uint64_t vk_mvprev_step(int stage, int index, uint64_t size) { + if (P.on <= 0 || stage != 0 || index < 0 || index >= BANKS || !(P.mask >> index & 1)) return 0; + return (size + 255) & ~(uint64_t)255; +} + +// Translation distance between the first matrices of two blocks (rows' fourth floats). +static float moved(const uint8_t *a, const uint8_t *b) { + const float *x = (const float *)a, *y = (const float *)b; + float dx = x[3] - y[3], dy = x[7] - y[7], dz = x[11] - y[11]; + return sqrtf(dx * dx + dy * dy + dz * dz); +} + +int vk_mvprev_find(int index, uint64_t addr, uint64_t size, uint64_t *prev, int *how) { + *how = MVPREV_NONE; + if (P.on <= 0 || !P.t[1]) return 0; + if (find(P.t[1], addr, index, 0)) { + *prev = addr, *how = MVPREV_SELF; + return 1; + } + uint64_t step = (size + 255) & ~(uint64_t)255, cand[2]; + int n = 0; + if (find(P.t[1], addr + step, index, 0)) cand[n++] = addr + step; + if (addr >= step && find(P.t[1], addr - step, index, 0)) cand[n++] = addr - step; + if (!n) return 0; + if (n == 2) { // the copies of adjacent objects interleave: the one with more words equal to this frame's + const uint32_t *c = (const uint32_t *)vk_usnap_bytes(addr, 64), *a = (const uint32_t *)vk_usnap_bytes(cand[0], 64), + *b = (const uint32_t *)vk_usnap_bytes(cand[1], 64); + if (!c || !a || !b) return 0; + int na = 0, nb = 0; + for (int w = 0; w < 16; w++) na += c[w] == a[w], nb += c[w] == b[w]; + if (nb > na) cand[0] = cand[1]; + *how = MVPREV_NEARER; + } else { + *how = MVPREV_SIDE; + } + *prev = cand[0]; + return 1; +} + +static void mvprev_draw_(VkExec *ex); +void vk_mvprev_draw(VkExec *ex) { + if (P.on <= 0) return; + if (!P.timed) return mvprev_draw_(ex); // the clock reads were 1 % of the render thread + uint64_t t0 = kern_now_ns(); + mvprev_draw_(ex); + P.ns_draw += kern_now_ns() - t0; +} + +static void mvprev_draw_(VkExec *ex) { + const VkExecTarget *z = &ex->depth; + if (!z->image || z->width < DLAA_SCENE_MIN_W || z->format != VK_FORMAT_D32_SFLOAT) return; + int check = P.bytes[0] != NULL; // TOTK_MVPREV_CHECK=1: the summary's checks (3.6 ms a frame) + for (int index = 0; index < BANKS; index++) { + if (!(P.mask >> index & 1)) continue; + const p_bind_buf *u = &ex->ubo[0][index]; + if (!u->addr || !u->size || u->addr == P.last[index]) continue; // the next draws keep the block: noted already + P.last[index] = u->addr; + Seen *k = find(P.t[0], u->addr, index, 1); + if (!k || k->have || !check) continue; // else only the address is noted + int w = index; + uint32_t want = u->size < CHECK ? (uint32_t)u->size : CHECK; + const uint8_t *cur = vk_usnap_bytes(u->addr, want); + if (!cur) continue; + memcpy(P.bytes[0][k - P.t[0]], cur, want), k->have = want; + Stat *s = &P.st[w]; + s->blocks++; + uint64_t prev; + int how; + if (!vk_mvprev_find(index, u->addr, u->size, &prev, &how)) { + s->none++; + continue; + } + if (how == MVPREV_SELF) s->self++; + else if (how == MVPREV_SIDE) s->side++; + else s->nearer++; + const Seen *last = find(P.t[1], prev, index, 0); + const uint8_t *now = vk_usnap_bytes(prev, want); + if (last && now && last->have >= want && want >= 48) { + s->checked++; + if (!memcmp(now, P.bytes[1][last - P.t[1]], want)) s->intact++; + float d = moved(cur, now); + if (d < 0.01f) s->still++; + else if (d < 1.0f) s->small++; + else s->big++; + } + } +} + +void vk_mvprev_frame(void) { + // without DLSS (no NGX, no RTX GPU, dlaa = 0) nothing reads last frame's blocks: P.on stays 0, no tables + if (!vk_dlaa_available()) return; + if (P.knob < 0) { + P.knob = rt_knob("DLAA_OBJMV", "1"); + if (P.knob) { + P.t[0] = calloc(TABLE, sizeof(Seen)), P.t[1] = calloc(TABLE, sizeof(Seen)); + if (!P.t[0] || !P.t[1]) P.knob = 0; + if (P.knob && rt_knob("MVPREV_CHECK", "0")) { + P.bytes[0] = calloc(TABLE, CHECK), P.bytes[1] = calloc(TABLE, CHECK); + if (!P.bytes[0] || !P.bytes[1]) free(P.bytes[0]), free(P.bytes[1]), P.bytes[0] = P.bytes[1] = NULL; + } + P.timed = rt_knob("MVOBJ_TIMES", "0"); + } + } + int on = P.knob && cfg_bool("TOTK_DLAA"); + if (on != (P.on > 0)) { + P.on = on ? 1 : -1; + if (P.t[0]) memset(P.t[0], 0, TABLE * sizeof(Seen)), memset(P.t[1], 0, TABLE * sizeof(Seen)); + return; + } + if (P.on <= 0) return; + uint64_t t0 = P.timed ? kern_now_ns() : 0; + Seen *t = P.t[1]; + P.t[1] = P.t[0], P.t[0] = t; + uint8_t (*b)[CHECK] = P.bytes[1]; + P.bytes[1] = P.bytes[0], P.bytes[0] = b; + memset(P.t[0], 0, TABLE * sizeof(Seen)); + memset(P.last, 0, sizeof P.last); + if (P.timed) P.ns_frame += kern_now_ns() - t0; + if (++P.frames % SUMMARY) return; + if (P.timed) rt_log("mv_prev: %.3f ms a frame in the draws, %.3f ms at the present", P.ns_draw / 1e6 / SUMMARY, + P.ns_frame / 1e6 / SUMMARY); + P.ns_draw = P.ns_frame = 0; + for (int w = 0; w < BANKS; w++) { + Stat *s = &P.st[w]; + if (!s->blocks) continue; + double b = (double)s->blocks, c = s->checked ? (double)s->checked : 1; + rt_log("mv_prev: c%d: %llu blocks: self %.1f %%, side %.1f %%, nearer %.1f %%, none %.1f %%; intact %.1f %% of " + "%llu; moved: still %.1f %%, under one unit %.1f %%, more %.1f %%", + w + 3, (unsigned long long)s->blocks, 100 * s->self / b, 100 * s->side / b, 100 * s->nearer / b, + 100 * s->none / b, 100 * s->intact / c, (unsigned long long)s->checked, 100 * s->still / c, + 100 * s->small / c, 100 * s->big / c); + } + memset(P.st, 0, sizeof P.st); +} diff --git a/runtime/vk_mvsurvey.c b/runtime/vk_mvsurvey.c new file mode 100644 index 00000000..0e782c21 --- /dev/null +++ b/runtime/vk_mvsurvey.c @@ -0,0 +1,258 @@ +// Motion-vector survey (TOTK_MV_SURVEY=1, off by default): measures whether per-object motion vectors can be made from +// the uniform blocks the scene's draws bind. Per-object vectors need each object's matrices of the previous frame; the +// game keeps none (its shaders read gsys_shape, gsys_skeleton and gsys_projection of this frame only), so they would +// have to be kept from the previous frame's uniform snapshot (vk_usnap.c). That works when a block's guest address +// stands for the same object from one frame to the next, which this survey measures. +// +// For every scene draw (vertex stage, scene depth bound: D32 at least DLAA_SCENE_MIN_W wide, as vk_dlaa_draw), each user bank +// (c3 + index) with a binding is noted: address, size and its first SNAP bytes as of the submit (the snapshot's copy). +// At each frame the bank's blocks are compared with the previous frame's and the one before: +// repeat1 / repeat2 blocks whose address was bound in that bank one / two frames ago +// same / near / far of the blocks repeated from last frame: bytes unchanged / every float within 5 % or 1.0 of +// last frame's (the same object, moved a little) / some float further (another object, likely) +// The game writes its per-object blocks into two copies that take turns, so a moving object's block is at one address +// in even frames and another in odd ones. To find the rule that pairs them, a bank's blocks bound two frames ago but +// not last frame (this frame's copy) are sorted by address, and so are last frame's blocks not bound this frame (the +// other copy); with equal counts they are paired in order: +// pairs, delta the paired blocks and the commonest address differences (this frame's minus last frame's) +// psame / pnear / pfar the pairs' contents, as same / near / far above (near: the same object, moved) +// unequal frames whose two lists had different lengths (no pairing that frame) +// What the bank holds: each block's leading 3x4 matrices (rows of a rotation times a scale: three rows of the same +// length, at right angles, finite), counted over the whole block as of the submit: +// mats 0 / 1 / 2-7 / 8+ blocks by that count (one: an object's place, gsys_shape; many: bones, gsys_skeleton) +// Every SURVEY_FRAMES frames the log gets two lines per bank with the window's totals: +// mv_survey: c<n>: <draws> draws, <blocks> blocks (<per frame>), sizes <top three>; repeat1 a %, repeat2 b %; +// same c %, near d %, far e % +// mv_survey: c<n>: pairs <n> (<unequal> frames unequal), delta <top three>; psame a %, pnear b %, pfar c %; +// mats 0 x<n> 1 x<n> 2-7 x<n> 8+ x<n>, at most <m> +#include <math.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "host.h" +#include "vk.h" +#include "vk_exec.h" +#include "vk_dlaa.h" + +#define BANKS 18 +#define SNAP 256u +#define SURVEY_FRAMES 300 +#define TABLE (1u << 15) // blocks per frame, all banks; a town frame binds about 10,000 uniform ranges + +typedef struct { + uint64_t addr; // 0: empty slot + uint32_t size, n; + int bank; + uint8_t bytes[SNAP]; + uint32_t have; // bytes of `bytes` that were in the snapshot + uint32_t mats; // leading 3x4 matrices of the whole block, 0 when it was not in the snapshot +} Block; + +typedef struct { + uint64_t draws, blocks, rep1, rep2, same, near, far, unread; + uint32_t size[4]; + uint64_t size_n[4]; + uint64_t pairs, psame, pnear, pfar, unequal, mats[4]; + uint32_t mats_max; + int64_t delta[4]; + uint64_t delta_n[4]; +} Stat; + +static struct { + int on; + Block *t[3]; // this frame, last frame, the one before + const Block **x, **y; // pairing: this frame's copy, last frame's + uint32_t frames; + Stat st[BANKS]; +} S; + +static uint32_t slot_of(uint64_t addr, int bank) { + uint64_t h = (addr ^ (uint64_t)bank * 0x9E3779B97F4A7C15ull) * 0xff51afd7ed558ccdull; + return (uint32_t)(h >> 40) & (TABLE - 1); +} + +static Block *find(Block *t, uint64_t addr, int bank, int add) { + for (uint32_t i = slot_of(addr, bank), k = 0; k < TABLE; i = (i + 1) & (TABLE - 1), k++) { + if (t[i].addr == addr && t[i].bank == bank) return &t[i]; + if (!t[i].addr) { + if (!add) return NULL; + t[i].addr = addr, t[i].bank = bank; + return &t[i]; + } + } + return NULL; // full: the block goes uncounted +} + +static void note_size(Stat *s, uint32_t size) { + for (int i = 0; i < 4; i++) + if (s->size[i] == size || !s->size_n[i]) { + s->size[i] = size, s->size_n[i]++; + return; + } + s->size_n[3]++; // a fifth size: counted with the last +} + +static void note_delta(Stat *s, int64_t d) { + for (int i = 0; i < 4; i++) + if (s->delta[i] == d || !s->delta_n[i]) { + s->delta[i] = d, s->delta_n[i]++; + return; + } + s->delta_n[3]++; +} + +// Leading 3x4 matrices: three rows of about the same length (a uniform scale), at right angles, all finite. +static uint32_t lead_mats(const float *f, uint32_t nf) { + uint32_t k = 0; + for (; (k + 1) * 12 <= nf && k < 4096; k++) { + const float *m = f + k * 12; + float l[3]; + for (int r = 0; r < 3; r++) { + const float *v = m + r * 4; + if (!isfinite(v[0]) || !isfinite(v[1]) || !isfinite(v[2]) || !isfinite(v[3])) return k; + l[r] = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); + if (l[r] < 1e-3f || l[r] > 1e3f) return k; + } + if (fabsf(l[1] - l[0]) > 0.02f * l[0] || fabsf(l[2] - l[0]) > 0.02f * l[0]) return k; + for (int a = 0; a < 3; a++) + for (int b = a + 1; b < 3; b++) { + const float *u = m + a * 4, *v = m + b * 4; + if (fabsf(u[0] * v[0] + u[1] * v[1] + u[2] * v[2]) > 0.02f * l[a] * l[b]) return k; + } + } + return k; +} + +static int by_bank_addr(const void *a, const void *b) { + const Block *x = *(const Block *const *)a, *y = *(const Block *const *)b; + if (x->bank != y->bank) return x->bank < y->bank ? -1 : 1; + return x->addr < y->addr ? -1 : x->addr > y->addr; +} + +static void survey_init(void) { + S.on = rt_knob("MV_SURVEY", "0") ? 1 : -1; + if (S.on < 0) return; + for (int i = 0; i < 3; i++) S.t[i] = calloc(TABLE, sizeof(Block)); + S.x = malloc(TABLE * sizeof *S.x), S.y = malloc(TABLE * sizeof *S.y); + if (!S.t[0] || !S.t[1] || !S.t[2] || !S.x || !S.y) { + S.on = -1; + return; + } + rt_log("mv_survey: on (scene draws' uniform blocks, a summary every %d frames)", SURVEY_FRAMES); +} + +void vk_mvsurvey_draw(VkExec *ex) { + if (!S.on) survey_init(); + if (S.on < 0) return; + const VkExecTarget *z = &ex->depth; + if (!z->image || z->width < DLAA_SCENE_MIN_W || z->format != VK_FORMAT_D32_SFLOAT) return; + for (int b = 0; b < BANKS; b++) { + const p_bind_buf *u = &ex->ubo[0][b]; + if (!u->addr || !u->size) continue; + S.st[b].draws++; + Block *k = find(S.t[0], u->addr, b, 1); + if (!k) continue; + if (k->n++) continue; + k->size = (uint32_t)u->size; + uint32_t want = u->size < SNAP ? (uint32_t)u->size : SNAP; + VkDescriptorBufferInfo bi; + const uint8_t *src = vk_usnap_find(u->addr, want, 1, &bi) ? vk_usnap_map() + bi.offset : NULL; + k->have = src ? want : 0; + if (src) memcpy(k->bytes, src, want); + const uint8_t *all = vk_usnap_find(u->addr, u->size, 1, &bi) ? vk_usnap_map() + bi.offset : NULL; + k->mats = all ? lead_mats((const float *)all, (uint32_t)(u->size / 4)) : 0; + if (all) { + Stat *s = &S.st[b]; + s->mats[k->mats == 0 ? 0 : k->mats == 1 ? 1 : k->mats < 8 ? 2 : 3]++; + if (k->mats > s->mats_max) s->mats_max = k->mats; + } + } +} + +// Same object moved a little: every float within 5 % or 1.0 of last frame's. +static int compare(const Block *a, const Block *b) { + uint32_t n = a->have < b->have ? a->have : b->have; + if (!n) return -1; + if (!memcmp(a->bytes, b->bytes, n)) return 0; + const float *x = (const float *)a->bytes, *y = (const float *)b->bytes; + for (uint32_t i = 0; i < n / 4; i++) { + float d = fabsf(x[i] - y[i]); + if (isnan(d) || (d > 1.0f && d > 0.05f * fabsf(y[i]))) return 2; + } + return 1; +} + +void vk_mvsurvey_frame(void) { + if (S.on <= 0) return; + for (uint32_t i = 0; i < TABLE; i++) { + const Block *k = &S.t[0][i]; + if (!k->addr) continue; + Stat *s = &S.st[k->bank]; + s->blocks++; + note_size(s, k->size); + const Block *p = find(S.t[1], k->addr, k->bank, 0); + if (p) { + s->rep1++; + int c = compare(k, p); + if (c == 0) s->same++; + else if (c == 1) s->near++; + else if (c == 2) s->far++; + else s->unread++; + } + if (find(S.t[2], k->addr, k->bank, 0)) s->rep2++; + } + // pairing of the two copies, bank by bank + uint32_t nx = 0, ny = 0; + for (uint32_t i = 0; i < TABLE; i++) { + const Block *k = &S.t[0][i]; + if (k->addr && !find(S.t[1], k->addr, k->bank, 0) && find(S.t[2], k->addr, k->bank, 0)) S.x[nx++] = k; + k = &S.t[1][i]; + if (k->addr && !find(S.t[0], k->addr, k->bank, 0)) S.y[ny++] = k; + } + qsort(S.x, nx, sizeof *S.x, by_bank_addr); + qsort(S.y, ny, sizeof *S.y, by_bank_addr); + for (uint32_t i = 0, j = 0; i < nx;) { + int b = S.x[i]->bank; + uint32_t i1 = i, j0, j1; + while (i1 < nx && S.x[i1]->bank == b) i1++; + while (j < ny && S.y[j]->bank < b) j++; + for (j0 = j1 = j; j1 < ny && S.y[j1]->bank == b;) j1++; + Stat *s = &S.st[b]; + if (i1 - i != j1 - j0) s->unequal++; + else + for (uint32_t a = i, c = j0; a < i1; a++, c++) { + s->pairs++; + note_delta(s, (int64_t)(S.x[a]->addr - S.y[c]->addr)); + int r = compare(S.x[a], S.y[c]); + if (r == 0) s->psame++; + else if (r == 1) s->pnear++; + else if (r == 2) s->pfar++; + } + i = i1, j = j1; + } + Block *old = S.t[2]; + S.t[2] = S.t[1], S.t[1] = S.t[0], S.t[0] = old; + memset(S.t[0], 0, TABLE * sizeof(Block)); + if (++S.frames % SURVEY_FRAMES) return; + for (int b = 0; b < BANKS; b++) { + Stat *s = &S.st[b]; + if (!s->blocks) continue; + double bl = (double)s->blocks, r1 = s->rep1 ? (double)s->rep1 : 1; + rt_log("mv_survey: c%d: %llu draws, %llu blocks (%.0f a frame), sizes %#x x%llu %#x x%llu %#x x%llu; repeat1 %.1f %%, " + "repeat2 %.1f %%; same %.1f %%, near %.1f %%, far %.1f %%, unread %.1f %%", + b + 3, (unsigned long long)s->draws, (unsigned long long)s->blocks, bl / SURVEY_FRAMES, s->size[0], + (unsigned long long)s->size_n[0], s->size[1], (unsigned long long)s->size_n[1], s->size[2], + (unsigned long long)s->size_n[2], 100 * s->rep1 / bl, 100 * s->rep2 / bl, 100 * s->same / r1, + 100 * s->near / r1, 100 * s->far / r1, 100 * s->unread / r1); + double pr = s->pairs ? (double)s->pairs : 1; + rt_log("mv_survey: c%d: pairs %llu (%llu frames unequal), delta %#llx x%llu %#llx x%llu %#llx x%llu; psame %.1f %%, " + "pnear %.1f %%, pfar %.1f %%; mats 0 x%llu 1 x%llu 2-7 x%llu 8+ x%llu, at most %u", + b + 3, (unsigned long long)s->pairs, (unsigned long long)s->unequal, (unsigned long long)s->delta[0], + (unsigned long long)s->delta_n[0], (unsigned long long)s->delta[1], (unsigned long long)s->delta_n[1], + (unsigned long long)s->delta[2], (unsigned long long)s->delta_n[2], 100 * s->psame / pr, + 100 * s->pnear / pr, 100 * s->pfar / pr, (unsigned long long)s->mats[0], (unsigned long long)s->mats[1], + (unsigned long long)s->mats[2], (unsigned long long)s->mats[3], s->mats_max); + } + memset(S.st, 0, sizeof S.st); +} diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index feba5764..8c288eec 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -1913,6 +1913,8 @@ static void rec_fill(KeyRec *r, const VkPipeState *s) { // A pipeline was built for a draw: queue its record for the file. Caller holds P.lock. static void keys_note(const VkPipeState *s) { if (!A.on || !K.path[0]) return; + static const uint8_t none[32]; + if (s->fs && !memcmp(gfx_shader_input_id(s->fs), none, 32)) return; // the runtime's own shader (gfx_shader_own) KeyRec r; rec_fill(&r, s); if (!seen_add(rec_hash(&r))) return; @@ -2569,7 +2571,8 @@ static struct { uint8_t clamp, poly, minus_w, samples; // library state (GKey fields) double t0, ms[3]; // ms: build time per kind (vertex, fragment, compute) // warmed: driver cache only; nolib: vertex and fragment variants skipped with the graphics pipeline library off - uint64_t built[3], have[3], fail[3], warmed[3], noshader, nolib, done, cs_draw; + // skipped: variants of the skip list (SK below) + uint64_t built[3], have[3], fail[3], warmed[3], noshader, nolib, skipped, done, cs_draw; double warm_ms[3]; volatile int32_t kept; // libraries entered in libs[] so far int32_t keep_n; @@ -2577,6 +2580,134 @@ static struct { PlatMutex stat; } LB = {.stat = PLAT_MUTEX_INIT}; +// ---- preparation that outlives a driver crash. A bug in the driver's shader compiler can kill the process inside +// vkCreate*Pipelines: NVIDIA 615.78.08 on an RTX 5080 crashed on the same vertex library at the same point of every +// preparation, natively and under Wine, so preparation never finished (issue #30 and a report on PR #60). +// While lb_warm_prepared builds, slot k of <dir>/shader-prepare-journal.bin holds the variant its worker k has handed to +// the driver (input id and stage; zeros between variants). The writes go straight to the OS, so they outlast the +// process, and a run that finishes deletes the file. The next preparation or boot (skip_init) moves the variants a +// leftover journal names into <dir>/shader-prepare-skip.txt with this GPU and driver version, and preparation and the +// boot prebuild (lb_worker) skip what that file lists for them. The variants the other workers were building at the +// crash are skipped along with the culprit; like it, they build at their first draw, where the driver can still crash. +// Another driver version builds them again, and so does deleting the file. tools/prepare_shaders.py runs the build +// again (--warm-shaders) after a run that left a journal. +#define JR_SLOT 64 // bytes per journal slot: input id, stage, zeros +#define JR_SLOTS 64 // lb_warm_prepared's thread cap + +static struct { + PlatFile *journal; // while lb_warm_prepared builds + uint8_t (*iid)[32]; // the skip list's variants for this GPU and driver + size_t n; + int loaded; +} SK; + +static int cache_dir(char *dir, size_t n) { // the folder of pipeline-cache.bin and shader-prepared.bin + snprintf(dir, n, "%s", P.cache_path); + char *cut = plat_last_sep(dir); + if (!cut) return 0; + *cut = 0; + return 1; +} + +static void gpu_key(char *out, size_t n) { // vendor:device:driver version, as the skip list names a GPU and driver + const VkPhysicalDeviceProperties *p = &vk_ctx()->props; + snprintf(out, n, "%04x:%04x:%08x", p->vendorID, p->deviceID, p->driverVersion); +} + +static const char *stage_name(int st) { + return st == GFX_STAGE_VS ? "vertex" : st == GFX_STAGE_FS ? "fragment" : st == GFX_STAGE_CS ? "compute" : "other"; +} + +static int hex_digit(int c) { + return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1; +} + +// A journal a crashed run left into the skip list, then the skip list's variants for this GPU and driver into SK. +static void skip_init(const char *dir) { + if (SK.loaded) return; + SK.loaded = 1; + char jp[PLAT_PATH_MAX * 2 + 48], sp[PLAT_PATH_MAX * 2 + 48], key[32]; + snprintf(jp, sizeof jp, "%s/shader-prepare-journal.bin", dir); + snprintf(sp, sizeof sp, "%s/shader-prepare-skip.txt", dir); + gpu_key(key, sizeof key); + uint64_t jn = 0, sn = 0; + uint8_t *j = plat_file_read(jp, &jn); + char *s = (char *)plat_file_read(sp, &sn); + int keep_journal = 0; + if (j) { + static const char head[] = + "# Shader variants the graphics driver was building when the process died during preparation\n" + "# (runtime/vk_pipeline.c skip_init): input id, stage, GPU vendor:device:driver version. Preparation skips\n" + "# them on that GPU and driver. Delete this file to build them again.\n"; + size_t cap = (s ? sn : sizeof head) + (jn / JR_SLOT + 1) * 128, len = 0; + char *out = malloc(cap); + int added = 0; + if (out) { + len = s ? sn : sizeof head - 1; + memcpy(out, s ? s : head, len); + if (len && out[len - 1] != '\n') out[len++] = '\n'; + for (uint64_t o = 0; o + 33 <= jn; o += JR_SLOT) { + int set = 0; + for (int b = 0; b < 32; b++) set |= j[o + b]; + if (!set) continue; + char hex[65]; + for (int b = 0; b < 32; b++) snprintf(hex + 2 * b, 3, "%02x", j[o + b]); + len += (size_t)snprintf(out + len, cap - len, "%s %s %s\n", hex, stage_name(j[o + 32]), key); + added++; + } + out[len] = 0; // parsed below as the list's text + } + if (added && plat_file_write_atomic(sp, out, len)) { + rt_log("vk_pipeline: the last shader preparation died while the driver built %d variants; %s now lists them " + "and preparation skips them on this GPU and driver (%s)", added, sp, key); + free(s), s = out, sn = len, out = NULL; + } else if (added) { + rt_log("vk_pipeline: cannot write %s; %s stays for the next run", sp, jp); + keep_journal = 1; + } + free(out); + free(j); + } + if (!keep_journal) plat_file_delete(jp); + for (char *line = s, *next; line && *line; line = next) { + next = strchr(line, '\n'); + if (next) *next++ = 0; + char hex[65], st[16], k[32]; + uint8_t iid[32]; + int ok = line[0] != '#' && sscanf(line, "%64s %15s %31s", hex, st, k) == 3 && strlen(hex) == 64 && + !strcmp(k, key); + for (int b = 0; ok && b < 32; b++) { + int hi = hex_digit(hex[2 * b]), lo = hex_digit(hex[2 * b + 1]); + if (hi < 0 || lo < 0) ok = 0; + else iid[b] = (uint8_t)(hi << 4 | lo); + } + uint8_t (*grown)[32] = ok ? realloc(SK.iid, (SK.n + 1) * sizeof *SK.iid) : NULL; + if (grown) memcpy(grown[SK.n++], iid, 32), SK.iid = grown; + } + free(s); + if (SK.n) rt_log("vk_pipeline: %zu shader variants skipped ahead of play on this GPU and driver (%s)", SK.n, sp); +} + +static int skip_listed(const uint8_t iid[32]) { + for (size_t i = 0; i < SK.n; i++) + if (!memcmp(SK.iid[i], iid, 32)) return 1; + return 0; +} + +static void skip_count(void) { + plat_mutex_lock(&LB.stat); + LB.skipped++; + plat_mutex_unlock(&LB.stat); +} + +// Journal slot `slot`: the variant e its worker hands to the driver next, or zeros (e NULL) when the build returned. +static void journal_mark(int slot, const GfxWarmEnt *e) { + if (!SK.journal || slot < 0 || slot >= JR_SLOTS) return; + uint8_t rec[JR_SLOT] = {0}; + if (e) memcpy(rec, e->iid, 32), rec[32] = e->stage; + plat_file_pwrite(SK.journal, rec, sizeof rec, (uint64_t)slot * JR_SLOT); +} + VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info) { if (info) *info = NULL; if (!cs || !ensure_init()) return VK_NULL_HANDLE; @@ -2749,7 +2880,8 @@ static void lb_report(const char *how) { rt_log("vk_pipeline: stage library prebuild %s: %llu of %zu variants in %.0f ms; kept: vertex %llu built (avg %.1f ms), " "%llu there; fragment %llu built (avg %.1f ms), %llu there; compute %llu built (avg %.1f ms), %llu there; driver " "cache only: vertex %llu (avg %.1f ms), fragment %llu (avg %.1f ms), compute %llu (avg %.1f ms); %llu failed, %llu not in the shader cache, " - "%llu vertex/fragment skipped (graphics pipeline library off); process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, + "%llu vertex/fragment skipped (graphics pipeline library off), %llu skipped (the driver crashed on them before, " + "shader-prepare-skip.txt); process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, now_ms() - LB.t0, (unsigned long long)LB.built[0], LB.built[0] ? LB.ms[0] / LB.built[0] : 0.0, (unsigned long long)LB.have[0], (unsigned long long)LB.built[1], LB.built[1] ? LB.ms[1] / LB.built[1] : 0.0, (unsigned long long)LB.have[1], (unsigned long long)LB.built[2], LB.built[2] ? LB.ms[2] / LB.built[2] : 0.0, @@ -2757,7 +2889,7 @@ static void lb_report(const char *how) { (unsigned long long)LB.warmed[1], LB.warmed[1] ? LB.warm_ms[1] / LB.warmed[1] : 0.0, (unsigned long long)LB.warmed[2], LB.warmed[2] ? LB.warm_ms[2] / LB.warmed[2] : 0.0, (unsigned long long)(LB.fail[0] + LB.fail[1] + LB.fail[2]), - (unsigned long long)LB.noshader, (unsigned long long)LB.nolib, (double)mem_proc); + (unsigned long long)LB.noshader, (unsigned long long)LB.nolib, (unsigned long long)LB.skipped, (double)mem_proc); plat_mutex_unlock_shared(&LB.stat); } @@ -2768,6 +2900,13 @@ static void lb_worker(void *arg) { for (;;) { size_t i = (size_t)plat_inc64(&LB.next) - 1; if (i >= LB.n || LB.stop) break; + if (skip_listed(LB.e[i].iid)) { + skip_count(); + plat_mutex_lock(&LB.stat); + LB.done++; + plat_mutex_unlock(&LB.stat); + continue; + } plat_thread_priority(PLAT_PRIO_NORMAL); // gfx_shader's cache lock, disk reads const GfxShader *sh = gfx_shader_load_input(LB.e[i].iid, 0); plat_thread_priority(PLAT_PRIO_IDLE); @@ -2805,6 +2944,8 @@ static void lb_start(const uint32_t pr_hist[16], const uint32_t fs_hist[4]) { const char *e = cfg_value("TOTK_LIB_PREBUILD"), *ke = cfg_get("TOTK_LIB_PREBUILD_KEEP"); const char *te = cfg_get("TOTK_LIB_PREBUILD_THREADS"); // no fixed default: a quarter of the CPUs when unset if (!A.on || !cfg_bool("TOTK_LIB_PREBUILD")) return; + char dir[PLAT_PATH_MAX * 2]; + if (cache_dir(dir, sizeof dir)) skip_init(dir); GfxWarmEnt *all = NULL; size_t total = gfx_shader_warm_list(&all), n = 0, n_cs = 0; LB.keep_n = ke && *ke ? atoi(ke) : 0; @@ -2847,16 +2988,20 @@ static void lb_stop(void) { LB.threads = 0; } -// Preparation: one prepared variant at a time into the driver's disk cache (private shader copy, nothing kept). +// Preparation: one prepared variant at a time into the driver's disk cache (private shader copy, nothing kept). arg is +// the worker's journal slot. static void lb_warm_worker(void *arg) { - (void)arg; + int slot = (int)(intptr_t)arg; for (;;) { size_t i = (size_t)plat_inc64(&LB.next) - 1; if (i >= LB.n) return; - int owned = 0; - const GfxShader *sh = gfx_shader_load_private(LB.e[i].iid, &owned); + int owned = 0, skip = skip_listed(LB.e[i].iid); + const GfxShader *sh = skip ? NULL : gfx_shader_load_private(LB.e[i].iid, &owned); int st = sh ? gfx_shader_stage(sh) : -1; - if (!sh) { + if (sh) journal_mark(slot, &LB.e[i]); // from here to its clearing below, a crash names this variant + if (skip) { + skip_count(); + } else if (!sh) { plat_mutex_lock(&LB.stat); LB.noshader++; plat_mutex_unlock(&LB.stat); @@ -2874,6 +3019,7 @@ static void lb_warm_worker(void *arg) { if (P.gpl) lb_library(sh, st == GFX_STAGE_VS, 0); else lb_skip_lib(); } + if (sh) journal_mark(slot, NULL); if (owned) gfx_shader_release_private(sh); plat_mutex_lock(&LB.stat); uint64_t d = ++LB.done; @@ -2894,23 +3040,36 @@ static void lb_warm_prepared(const char *dir) { memcpy(LB.e[i].iid, f + 16 + 36 * i + 4, 32), LB.e[i].stage = f[16 + 36 * i], LB.e[i].prio = f[16 + 36 * i + 1]; free(f); if (!LB.e) return; + skip_init(dir); + char jp[PLAT_PATH_MAX * 2 + 48]; + snprintf(jp, sizeof jp, "%s/shader-prepare-journal.bin", dir); + SK.journal = plat_fs_create(jp, NULL); // skip_init removed a leftover one + if (!SK.journal) rt_log("vk_pipeline: cannot create %s: a driver crash in this run will not be recorded", jp); LB.n = n, LB.idle = 0, LB.t0 = now_ms(); static const uint32_t no_pr[16], no_fs[4]; lb_state(no_pr, no_fs); int cpus = (int)plat_cpu_usable(), k = 0; - PlatThread *t[64]; - for (int i = 0; i < cpus && i < 64; i++) - if ((t[k] = plat_thread_new(lb_warm_worker, NULL, "lib warm"))) k++; - if (!k) lb_warm_worker(NULL); + PlatThread *t[JR_SLOTS]; + for (int i = 0; i < cpus && i < JR_SLOTS; i++) + if ((t[k] = plat_thread_new(lb_warm_worker, (void *)(intptr_t)k, "lib warm"))) k++; + if (!k) lb_warm_worker((void *)(intptr_t)0); for (int i = 0; i < k; i++) plat_thread_join(t[i]); + if (SK.journal) plat_file_close(SK.journal), SK.journal = NULL, plat_file_delete(jp); // every build returned lb_report("preparation done"); // with gpl off only compute variants are built: say so, rather than count the skipped ones as built + unsigned long long skipped = LB.skipped; + size_t tried = n - (size_t)skipped; if (LB.nolib) printf("prepare-shaders: %zu variants on %d threads in %.0f ms, %llu vertex/fragment variants skipped (graphics " - "pipeline library off, they build at their first draw)\n", n, k, now_ms() - LB.t0, (unsigned long long)LB.nolib); + "pipeline library off, they build at their first draw)\n", tried, k, now_ms() - LB.t0, + (unsigned long long)LB.nolib); else - printf("prepare-shaders: %zu variants built into the driver cache on %d threads in %.0f ms\n", n, k, now_ms() - LB.t0); - free(LB.e), LB.e = NULL, LB.n = 0, LB.next = 0, LB.done = 0; + printf("prepare-shaders: %zu variants built into the driver cache on %d threads in %.0f ms\n", tried, k, + now_ms() - LB.t0); + if (skipped) // installer/steps.py Prepare reads this line + printf("prepare-shaders: %llu variants skipped: the graphics driver crashed while building them in an earlier " + "run (shader-prepare-skip.txt); they build at their first draw\n", skipped); + free(LB.e), LB.e = NULL, LB.n = 0, LB.next = 0, LB.done = 0, LB.skipped = 0; } // totk.exe --prepare-shaders (tools/prepare_shaders.py): the device sets the translator's host answers (they are in every @@ -2922,10 +3081,7 @@ int vk_pipeline_prepare_shaders(void) { return 1; } char dir[PLAT_PATH_MAX * 2]; - snprintf(dir, sizeof dir, "%s", P.cache_path); - char *cut = plat_last_sep(dir); - if (!cut) return 1; - *cut = 0; + if (!cache_dir(dir, sizeof dir)) return 1; int cpus = (int)plat_cpu_usable(); const char *pe = cfg_get("TOTK_PREWARM_XLATE"); // no fixed default: half the CPUs, at most 16, when unset int procs = pe && *pe ? atoi(pe) : cpus / 2 > 16 ? 16 : cpus / 2; @@ -2944,10 +3100,7 @@ int vk_pipeline_warm_shaders(void) { return 1; } char dir[PLAT_PATH_MAX * 2]; - snprintf(dir, sizeof dir, "%s", P.cache_path); - char *cut = plat_last_sep(dir); - if (!cut) return 1; - *cut = 0; + if (!cache_dir(dir, sizeof dir)) return 1; char prp[PLAT_PATH_MAX * 2 + 32]; snprintf(prp, sizeof prp, "%s/shader-prepared.bin", dir); if (!plat_file_exists(prp)) { diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index eb898e33..ffa2c72e 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -26,6 +26,10 @@ static VkCommandBuffer rec_cb; // the part being recorded (set while the static int rec_live; // producer: rec_begin started the recorder for rec_cb static PlatThread *rec_thread; static int rec_prio; // the submitting thread's priority, which the recorder takes +static int rec_chk; // TOTK_REC_CHECK (rec_check_handle) +static uint64_t rec_delay_ns; // TOTK_REC_DELAY_US (rec_main) +static volatile uint64_t part_h0; // put_h at rec_begin: where the packets of the part being recorded start +static volatile uint64_t part_fence; // the value of the part being recorded (rec_begin) // Producer statistics, logged by rec_stats_log. static struct { uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on; } RS_; @@ -34,12 +38,13 @@ enum { R_PAD, R_BeginQuery, R_BeginRendering, R_BindDescriptorSets, R_BindIndexBuffer, R_BindPipeline, R_BindVertexBuffers2, R_BlitImage, R_ClearAttachments, R_ClearColorImage, R_ClearDepthStencilImage, R_CopyBuffer, R_CopyBufferToImage, R_CopyImage, R_CopyImageToBuffer, R_Dispatch, R_DispatchIndirect, R_Draw, R_DrawIndexed, R_DrawIndexedIndirect, - R_EndQuery, R_EndRendering, R_FillBuffer, R_PipelineBarrier2, R_PushConstants, R_ResetQueryPool, R_ResolveImage, + R_EndQuery, R_EndRendering, R_ExecuteCommands, R_FillBuffer, R_PipelineBarrier2, R_PushConstants, R_ResetQueryPool, R_ResolveImage, R_SetBlendConstants, R_SetCullMode, R_SetDepthBias, R_SetDepthBiasEnable, R_SetDepthCompareOp, R_SetDepthTestEnable, R_SetDepthWriteEnable, R_SetFrontFace, R_SetPrimitiveRestartEnable, R_SetPrimitiveTopology, R_SetRasterizerDiscardEnable, R_SetScissorWithCount, R_SetStencilCompareMask, R_SetStencilOp, R_SetStencilReference, R_SetStencilTestEnable, R_SetStencilWriteMask, R_SetViewportWithCount, R_WriteTimestamp, R_WriteTimestamp2, R_PushTpl, R_Push, R_VertexInput, R_Bool, R_PolygonMode, R_BlendEnable, R_BlendEquation, R_WriteMask, R_EndCb, + R_PassMark, }; static uint64_t now_ns(void) { return plat_now_ns(); } @@ -64,6 +69,9 @@ static void rec_main(void *arg) { continue; } } + // TOTK_REC_DELAY_US (debug): the recorder falls this far behind each batch it finds, as on a slow machine, + // so objects the replay lets go of meet packets it has not made yet (TOTK_REC_CHECK). + if (rec_delay_ns) plat_sleep_ns(rec_delay_ns); for (int k = 0; t != h; k++) { uint64_t w = ring[t % RING_WORDS]; uint32_t op = (uint32_t)(w & 0xffff), n = (uint32_t)(w >> 32); @@ -103,6 +111,9 @@ static void *put(uint32_t op, size_t bytes) { } wait_space(n); ring[pos] = (uint64_t)op | n << 32; + // Packers reserve room for optional arrays they may leave unwritten (BeginRendering's depth and stencil); with + // TOTK_REC_CHECK those words would still hold handles from the ring's previous lap. + if (rec_chk) memset(&ring[pos + 1], 0, (n - 1) * 8); put_h += n; RS_.packets++, RS_.words += n; return &ring[pos + 1]; @@ -124,16 +135,25 @@ void rec_drain(void) { RS_.drains++, RS_.drain_ns += now_ns() - t0; } -void rec_begin(VkCommandBuffer cb) { +void rec_begin(VkCommandBuffer cb, uint64_t fence) { static int mode = -1; if (mode < 0) { - // Default on for every GPU. AMD drivers once lost the device with the recorder (issues #5 and #40); after - // vk_mem_note_recording kept objects alive until their submission completed and the recorder began ending - // its own parts (rec_end_cb), RDNA 1 and 2 cards ran with it on and off without a loss or a white scene. - mode = rt_knob("VK_RECORDER", "1"); + // Default off on AMD GPUs. With the recorder those drivers lost the device or hung the GPU at startup on + // RDNA 2, 3 and 4, under AMD's Windows driver and under RADV (issues #5, #40, #277 and #283: a black screen, + // or VK_ERROR_DEVICE_LOST with a breadcrumb naming a part of a replay submission, and vk_recorder = 0 gets + // past it). Keeping objects until their submission completed and ending a part from the recorder + // (rec_end_cb) fixed the white scene on an RX 6700 XT, but not this: 0.2.8 turned the recorder on for every + // GPU and the AMD reports came back the same day. TOTK_VK_RECORDER=1 turns it on for an A/B run. + const char *set = cfg_get("TOTK_VK_RECORDER"); + mode = rt_knob("VK_RECORDER", vk_caps.amd ? "0" : "1"); + if (!mode && vk_caps.amd && !(set && *set)) + rt_log("vk_rec: no recorder thread on AMD GPUs (TOTK_VK_RECORDER=1 turns it on)"); if (mode) { plat_event_init(&wake_ev, 0, 0); rec_prio = plat_thread_priority_get(); + rec_chk = rt_knob("REC_CHECK", "0"); // debug switches, read before the recorder starts + const char *d = cfg_get("TOTK_REC_DELAY_US"); + rec_delay_ns = d && *d && atoi(d) > 0 ? (uint64_t)atoi(d) * 1000 : 0; rec_thread = plat_thread_new(rec_main, NULL, "vk recorder"); if (!rec_thread) { rt_log("vk_rec: no recorder thread: the replay makes its Vulkan calls itself"); @@ -141,6 +161,9 @@ void rec_begin(VkCommandBuffer cb) { } else { rt_log("vk_rec: the replay's Vulkan commands are made by a recorder thread%s", mode == 2 ? " in the B blocks (TOTK_AB)" : ""); + if (rec_chk) rt_log("vk_rec: TOTK_REC_CHECK: deferred and destroyed objects are searched in the ring"); + if (rec_delay_ns) rt_log("vk_rec: TOTK_REC_DELAY_US: the recorder waits %llu us before each batch", + (unsigned long long)(rec_delay_ns / 1000)); } } } @@ -148,6 +171,8 @@ void rec_begin(VkCommandBuffer cb) { if (!RT_KNOB_ON(mode)) return; RS_.parts_on++; rec_cb = cb; // the recorder is idle: rec_end drained it + __atomic_store_n(&part_h0, put_h, __ATOMIC_RELEASE); + __atomic_store_n(&part_fence, fence, __ATOMIC_RELEASE); rec_live = 1; } @@ -162,11 +187,14 @@ void rec_end(void) { // recorded and the submitting thread ended broke when a submission was split (vk_sync.c vk_sync_split): the GPU hung // or the scene turned green, then white. The handoff in rec_drain orders the two threads, so Vulkan allows the old // split; the driver did not cope with it. TOTK_REC_END=0 brings the old split back. +// The line is written only when a part was recorded by the recorder: with the recorder off (the default on AMD) the +// switch changes nothing, and reports read an unconditional line as proof that the recorder was running (#315). typedef struct { int kind; uint64_t n; } A_EndCb; int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n) { - static int mode = -1; - if (mode < 0) { - mode = rt_knob("REC_END", "1"); + static int mode = -1, said; + if (mode < 0) mode = rt_knob("REC_END", "1"); + if (rec_live && cb == rec_cb && !said) { + said = 1; rt_log("vk_rec: a part the recorder records is ended by %s (TOTK_REC_END=%d)", mode ? "the recorder" : "the submitting thread", mode); } @@ -193,6 +221,64 @@ void rec_stats_log(void) { memset(&RS_, 0, sizeof RS_); } +// TOTK_REC_CHECK=1 (debug, recorder on): the packets of the part being recorded are searched word by word for an object +// vk_memory.c lets go of. put() zeroes each packet's room, so only words a packer wrote can match. A hit is a hazard +// - at a deferral (destroy 0) when the stamp tl is below the part's value: vk_memory.c collect_locked may destroy the +// object once the GPU reaches tl, before the part ran and, with the recorder behind, before it made the call; +// - at a destruction (destroy 1) always: a packet not made yet hands the driver a dead handle, a made one leaves it +// in a command buffer not yet submitted. +// "Made" means before the tail the recorder published, which trails it by up to 64 packets. Hazards are logged (the +// first 64) and counted by kind, packet and case for rec_check_log; named deferrals that wait long enough are only +// counted. Reads race the producer only in words below head, which stay put until the ring +// wraps (16 MiB later). +static const char *const kind_name[REC_KINDS] = {"image", "view", "buffer"}; +static volatile int32_t chk_seen[2], chk_named, chk_logged, chk_hz[2][2][REC_KINDS][64]; // [destroy][made][kind][op] +void rec_check_handle(int kind, uint64_t handle, int destroy, uint64_t tl, uint64_t gpu) { + if (!rec_chk || !handle || kind < 0 || kind >= REC_KINDS || !__atomic_load_n(&rec_live, __ATOMIC_RELAXED)) return; + uint64_t h = __atomic_load_n(&head, __ATOMIC_ACQUIRE), p = __atomic_load_n(&part_h0, __ATOMIC_ACQUIRE); + uint64_t fence = __atomic_load_n(&part_fence, __ATOMIC_ACQUIRE); + if (p > h || h - p > RING_WORDS / 2) return; // a part this large is not searched + plat_inc32(&chk_seen[destroy != 0]); + while (p < h) { + uint64_t w = ring[p % RING_WORDS]; + uint32_t op = (uint32_t)(w & 0xffff), n = (uint32_t)(w >> 32); + if (!n) return; // overwritten meanwhile + if (op != R_PAD) + for (uint32_t i = 1; i < n; i++) { + if (ring[(p + i) % RING_WORDS] != handle) continue; + int made = p < __atomic_load_n(&tail, __ATOMIC_ACQUIRE); + if (!destroy && tl >= fence) { plat_inc32(&chk_named); return; } + plat_inc32(&chk_hz[destroy][made][kind][op & 63]); + if (plat_inc32(&chk_logged) <= 64) + rt_log("vk_rec: check: HAZARD: %s %#llx %s while packet op %u (byte %u of its arguments) of the " + "part being recorded names it, %s; deferral stamp %llu, part value %llu, GPU at %llu", + kind_name[kind], (unsigned long long)handle, destroy ? "destroyed" : "deferred", op, + 8 * (i - 1), made ? "made" : "not known made", (unsigned long long)tl, + (unsigned long long)fence, (unsigned long long)gpu); + return; + } + p += n; + } +} + +void rec_check_log(void) { + static volatile int32_t said; + if (!rec_chk || plat_xchg32(&said, 1)) return; + int any = 0; + for (int d = 0; d < 2; d++) + for (int m = 0; m < 2; m++) + for (int k = 0; k < REC_KINDS; k++) + for (int op = 0; op < 64; op++) + if (chk_hz[d][m][k][op]) { + any = 1; + rt_log("vk_rec: check summary: %d hazards: %s %s, packet op %d %s", (int)chk_hz[d][m][k][op], + kind_name[k], d ? "destroyed" : "deferred", op, m ? "made" : "not known made"); + } + rt_log("vk_rec: check summary: %s; searched for %d objects deferred and %d destroyed while a part was recorded; %d " + "deferred ones the part named waited for its value", any ? "hazards above" : "no hazards", (int)chk_seen[0], + (int)chk_seen[1], (int)chk_named); +} + #define OWNED(cb) (rec_live && (cb) == rec_cb) // A call the ring takes no packet for: everything queued first, then the call here. #define FALLBACK() (rec_drain(), RS_.fallbacks++) @@ -405,6 +491,15 @@ void rc_vkCmdClearDepthStencilImage(VkCommandBuffer cb, VkImage i, VkImageLayout pub(); } +typedef struct { uint32_t n; const VkCommandBuffer *c; } A_ExecuteCommands; +void rc_vkCmdExecuteCommands(VkCommandBuffer cb, uint32_t n, const VkCommandBuffer *c) { + if (!OWNED(cb)) { vkCmdExecuteCommands(cb, n, c); return; } + A_ExecuteCommands *a = put(R_ExecuteCommands, sizeof *a + sizeof *c * n); + uint8_t *at = (uint8_t *)(a + 1); + a->n = n, a->c = tail_copy(&at, c, sizeof *c * n); + pub(); +} + typedef struct { VkPipelineLayout l; VkShaderStageFlags st; uint32_t o, n; const void *v; } A_PushConstants; void rc_vkCmdPushConstants(VkCommandBuffer cb, VkPipelineLayout l, VkShaderStageFlags st, uint32_t o, uint32_t n, const void *v) { @@ -522,6 +617,14 @@ void rec_polygon_mode(PFN_vkCmdSetPolygonModeEXT fn, VkCommandBuffer cb, VkPolyg pub(); } +typedef struct { uint32_t id; int end; } A_PassMark; +void rec_pass_mark(VkCommandBuffer cb, uint32_t id, int end) { + if (!OWNED(cb)) { vk_pass_mark_cmd(cb, id, end); return; } + A_PassMark *a = put(R_PassMark, sizeof *a); + *a = (A_PassMark){id, end}; + pub(); +} + typedef struct { void *fn; uint32_t first, n; const void *v; } A_Targets; #define TARGET_CMD(fname, R, PFN, T) \ void fname(PFN fn, VkCommandBuffer cb, uint32_t first, uint32_t n, const T *v) { \ @@ -583,6 +686,7 @@ static void exec_packet(uint32_t op, void *p) { break; } F(PushConstants, a->l, a->st, a->o, a->n, a->v) + F(ExecuteCommands, a->n, a->c) #undef F case R_EndRendering: vkCmdEndRendering(cb); break; case R_BlitImage: { A_Img *a = p; vkCmdBlitImage(cb, a->s, a->sl, a->d, a->dl, a->n, a->r, a->f); break; } @@ -609,6 +713,7 @@ static void exec_packet(uint32_t op, void *p) { case R_BlendEnable: { A_Targets *a = p; ((PFN_vkCmdSetColorBlendEnableEXT)a->fn)(cb, a->first, a->n, a->v); break; } case R_BlendEquation: { A_Targets *a = p; ((PFN_vkCmdSetColorBlendEquationEXT)a->fn)(cb, a->first, a->n, a->v); break; } case R_WriteMask: { A_Targets *a = p; ((PFN_vkCmdSetColorWriteMaskEXT)a->fn)(cb, a->first, a->n, a->v); break; } + case R_PassMark: { A_PassMark *a = p; vk_pass_mark_cmd(cb, a->id, a->end); break; } case R_EndCb: { A_EndCb *a = p; if (a->kind) vk_crumb(cb, a->kind, a->n); diff --git a/runtime/vk_rec.h b/runtime/vk_rec.h index d4054e1e..feddc707 100644 --- a/runtime/vk_rec.h +++ b/runtime/vk_rec.h @@ -15,13 +15,19 @@ // MIT, Copyright (c) 2026 Peiikai): the driver's work behind the commands depends on nothing the replay decides next. #pragma once -void rec_begin(VkCommandBuffer cb); // the submitting thread, sub_lock held +void rec_begin(VkCommandBuffer cb, uint64_t fence); // the submitting thread, sub_lock held; fence: the part's value void rec_end(void); // waits for the recorder; no-op when rec_begin did not start it // The end of part cb: the breadcrumb crumb (0: none, vk_crumbs.c), then vkEndCommandBuffer, both made by the recorder // when it records cb, and the wait for it (1); otherwise rec_end and the breadcrumb, and the caller ends cb (0). int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n); void rec_drain(void); // waits until the recorder made every call queued so far void rec_stats_log(void); // TOTK_TRACE=perf: packets, bytes, drains and waits since the last call +// TOTK_REC_CHECK=1 (debug, recorder on): vk_memory.c reports each object it defers (destroy 0, stamped tl) or destroys +// (destroy 1) with the value the GPU reached (gpu); a packet of the part being recorded that still names it is logged +// as a hazard when the object can go before the part ran. rec_check_log: the counts by kind and packet, at exit. +enum { REC_IMAGE, REC_VIEW, REC_BUFFER, REC_KINDS }; +void rec_check_handle(int kind, uint64_t handle, int destroy, uint64_t tl, uint64_t gpu); +void rec_check_log(void); // Extension commands called through device function pointers. void rec_push_tpl(PFN_vkCmdPushDescriptorSetWithTemplateKHR fn, VkCommandBuffer cb, VkDescriptorUpdateTemplate tpl, @@ -38,6 +44,8 @@ void rec_blend_equation(PFN_vkCmdSetColorBlendEquationEXT fn, VkCommandBuffer cb const VkColorBlendEquationEXT *v); void rec_write_mask(PFN_vkCmdSetColorWriteMaskEXT fn, VkCommandBuffer cb, uint32_t first, uint32_t n, const VkColorComponentFlags *v); +// A pass mark (vk_crumbs.c vk_pass_mark_cmd: a buffer marker and a checkpoint), in order with the commands around it. +void rec_pass_mark(VkCommandBuffer cb, uint32_t id, int end); #define REC_CMDS(X) \ X(BeginQuery, (VkCommandBuffer cb, VkQueryPool p, uint32_t q, VkQueryControlFlags f), (cb, p, q, f)) \ @@ -72,6 +80,7 @@ void rec_write_mask(PFN_vkCmdSetColorWriteMaskEXT fn, VkCommandBuffer cb, uint32 X(EndRendering, (VkCommandBuffer cb), (cb)) \ X(FillBuffer, (VkCommandBuffer cb, VkBuffer b, VkDeviceSize o, VkDeviceSize n, uint32_t v), (cb, b, o, n, v)) \ X(PipelineBarrier2, (VkCommandBuffer cb, const VkDependencyInfo *di), (cb, di)) \ + X(ExecuteCommands, (VkCommandBuffer cb, uint32_t n, const VkCommandBuffer *c), (cb, n, c)) \ X(PushConstants, (VkCommandBuffer cb, VkPipelineLayout l, VkShaderStageFlags st, uint32_t o, uint32_t n, \ const void *v), (cb, l, st, o, n, v)) \ X(ResetQueryPool, (VkCommandBuffer cb, VkQueryPool p, uint32_t f, uint32_t n), (cb, p, f, n)) \ @@ -111,6 +120,7 @@ REC_CMDS(REC_DECL) #define vkCmdBindPipeline rc_vkCmdBindPipeline #define vkCmdBindVertexBuffers2 rc_vkCmdBindVertexBuffers2 #define vkCmdBlitImage rc_vkCmdBlitImage +#define vkCmdExecuteCommands rc_vkCmdExecuteCommands #define vkCmdClearAttachments rc_vkCmdClearAttachments #define vkCmdClearColorImage rc_vkCmdClearColorImage #define vkCmdClearDepthStencilImage rc_vkCmdClearDepthStencilImage diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 8f16f154..ae9ee33b 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -61,6 +61,14 @@ static volatile uint8_t ts_busy[TS_PAIRS]; // written until the completion threa static int ts_cur = -1; // the pair of the part being recorded, -1 untimed static uint64_t ts_last_end; // completion thread: end of the latest part read (ticks) static volatile int64_t ts_busy_ns, ts_parts, ts_missed; +// GPU idle (with calibrated timestamps): the gap before each timed part, split by whether the part was submitted after +// the previous one had ended (the GPU waited for the CPU: starved) or before (it waited for untimed work or a wait). +static uint64_t ts_sub_ns[TS_PAIRS]; // CPU time the part was handed to vkQueueSubmit2 +static int ts_unsent[4], ts_n_unsent; // pairs closed and not yet submitted (sub_lock) +static uint64_t ts_last_end_ns; // completion thread: ts_last_end on the CPU clock +static volatile int64_t ts_idle_ns, ts_starved_ns, ts_gaps, ts_max_gap_ns, ts_queued_ns; +static int64_t cal_tick, cal_ns, cal_at; // completion thread: a calibrated (GPU tick, CPU ns) pair and when it was taken +static PFN_vkGetCalibratedTimestampsKHR cal_fn; // On the first submission that wants timing (perf_gpu_timing: TOTK_TRACE=perf or the full performance overlay). static void ts_setup(VkCtx *c) { @@ -91,6 +99,49 @@ static void ts_setup(VkCtx *c) { typedef struct { uint32_t pair; } TsRead; +// Completion thread: refresh the GPU tick / CPU clock pair every 250 ms; 0 when there is no calibration. The host +// domain is the clock plat_now_ns reads (QPC on Windows, CLOCK_MONOTONIC elsewhere), plat_counter_hz ticks a second. +static int cal_refresh(VkCtx *c) { + if (!c->has_calibrated) return 0; + if (!cal_fn) { + cal_fn = (PFN_vkGetCalibratedTimestampsKHR)vkGetDeviceProcAddr(c->device, c->has_calibrated == 2 ? "vkGetCalibratedTimestampsKHR" + : "vkGetCalibratedTimestampsEXT"); + if (!cal_fn) { c->has_calibrated = 0; return 0; } + } + int64_t now = (int64_t)plat_now_ns(); + if (cal_at && now - cal_at < 250000000) return 1; + VkCalibratedTimestampInfoKHR ci[2] = {{.sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_KHR, .timeDomain = VK_TIME_DOMAIN_DEVICE_KHR}, +#ifdef _WIN32 + {.sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_KHR, .timeDomain = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_KHR}}; +#else + {.sType = VK_STRUCTURE_TYPE_CALIBRATED_TIMESTAMP_INFO_KHR, .timeDomain = VK_TIME_DOMAIN_CLOCK_MONOTONIC_KHR}}; +#endif + uint64_t v[2], dev; + if (cal_fn(c->device, 2, ci, v, &dev) != VK_SUCCESS) { + if (!cal_at) { // the domain pair is not supported: no split + rt_log("vk: calibrated timestamps failed: the perf line shows no GPU idle split"); + c->has_calibrated = 0; + } + return cal_at != 0; + } + cal_tick = (int64_t)(v[0] & ts_mask), cal_ns = (int64_t)((double)v[1] * 1e9 / (double)plat_counter_hz()), cal_at = now; + return 1; +} + +static int64_t tick_ns(uint64_t t) { return cal_ns + (int64_t)((double)((int64_t)t - cal_tick) * ts_period); } + +static void max64(volatile int64_t *m, int64_t v) { + int64_t o = __atomic_load_n(m, __ATOMIC_RELAXED); + while (v > o && !__atomic_compare_exchange_n(m, &o, v, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {} +} + +// Just before vkQueueSubmit2 (sub_lock held): the parts closed since the last submit leave the CPU now. +static void ts_submitted(void) { + uint64_t now = plat_now_ns(); + for (int i = 0; i < ts_n_unsent; i++) ts_sub_ns[ts_unsent[i]] = now; + ts_n_unsent = 0; +} + static void ts_read_cb(void *arg) { // completion thread, in submission order TsRead *t = arg; VkCtx *c = vk_ctx(); @@ -98,6 +149,18 @@ static void ts_read_cb(void *arg) { // completion thread, in submission order if (vkGetQueryPoolResults(c->device, ts_pool, 2 * t->pair, 2, sizeof v, v, sizeof v[0], VK_QUERY_RESULT_64_BIT) == VK_SUCCESS) { uint64_t s = v[0] & ts_mask, e = v[1] & ts_mask; if (e >= s) { + if (cal_refresh(c)) { + int64_t sn = tick_ns(s), en = tick_ns(e), sub = (int64_t)ts_sub_ns[t->pair]; + if (ts_last_end_ns && sn > ts_last_end_ns) { + int64_t gap = sn - ts_last_end_ns, late = sub - ts_last_end_ns; + plat_add64(&ts_idle_ns, gap); + if (late > 0) plat_add64(&ts_starved_ns, late < gap ? late : gap); + if (gap > 500000) plat_inc64(&ts_gaps); + max64(&ts_max_gap_ns, gap); + } + if (sub && sn > sub) plat_add64(&ts_queued_ns, sn - sub); + if (en > ts_last_end_ns) ts_last_end_ns = en; + } uint64_t from = s > ts_last_end ? s : ts_last_end; if (e > from) { int64_t ns = (int64_t)((double)(e - from) * ts_period); @@ -132,6 +195,8 @@ static int ts_open(VkCommandBuffer cb) { static void ts_close(VkCommandBuffer cb, int pair, uint64_t fence) { if (pair < 0) return; vkCmdWriteTimestamp(cb, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, ts_pool, 2 * (uint32_t)pair + 1); + ts_sub_ns[pair] = 0; + if (ts_n_unsent < 4) ts_unsent[ts_n_unsent++] = pair; TsRead *t = malloc(sizeof *t); if (!t) rt_fatal("vk: out of memory"); t->pair = (uint32_t)pair; @@ -159,6 +224,12 @@ static void gpu_stats(PerfGpu *g) { g->gpu_parts = (uint64_t)plat_xchg64(&ts_parts, 0); g->gpu_missed = (uint64_t)plat_xchg64(&ts_missed, 0); g->have_vram = vk_mem_vram(&g->vram_used, &g->vram_budget, &g->vram_exact); + g->have_idle = vk_ctx()->has_calibrated != 0 && cal_at != 0; + g->idle_ns = (uint64_t)plat_xchg64(&ts_idle_ns, 0); + g->starved_ns = (uint64_t)plat_xchg64(&ts_starved_ns, 0); + g->queued_ns = (uint64_t)plat_xchg64(&ts_queued_ns, 0); + g->idle_gaps = (uint64_t)plat_xchg64(&ts_gaps, 0); + g->max_gap_ns = (uint64_t)plat_xchg64(&ts_max_gap_ns, 0); } // Weak stand-in for vk_exec.c's replay: only empty recordings ({OP_BEGIN, OP_END}) can be submitted. @@ -303,6 +374,7 @@ VkCommandBuffer vk_sync_split(void) { {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = cb}}; VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = pro ? 2 : 1, .pCommandBufferInfos = pro ? cbi : cbi + 1}; + ts_submitted(); vk_queue_lock(); VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE); vk_queue_unlock(); @@ -317,7 +389,7 @@ VkCommandBuffer vk_sync_split(void) { vkBeginCommandBuffer(s->cb, &bi); vk_crumb(s->cb, VK_CRUMB_SUBMIT, cur_fence); ts_begin(s->cb); - rec_begin(s->cb); + rec_begin(s->cb, cur_fence); return s->cb; } @@ -346,7 +418,7 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { vkBeginCommandBuffer(s->cb, &bi); vk_crumb(s->cb, VK_CRUMB_SUBMIT, fence); ts_begin(s->cb); - rec_begin(s->cb); + rec_begin(s->cb, fence); int recorded = vk_exec_record(s->cb, handles, n, fence); s = &slots[cur_slot]; // the last part (vk_sync_split) if (recorded) ts_end(s->cb, fence); @@ -366,6 +438,7 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { .commandBufferInfoCount = (pro ? 1u : 0u) + (recorded ? 1u : 0u), .pCommandBufferInfos = pro ? cbi : cbi + 1, .signalSemaphoreInfoCount = 1, .pSignalSemaphoreInfos = &sig}; + ts_submitted(); vk_queue_lock(); VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE); vk_queue_unlock(); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index b97d3c18..0a63a3bb 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -26,6 +26,7 @@ #include "nvn_tex.h" #include "tex.h" #include "vk.h" +#include "vk_dlaa.h" #include "vk_exec.h" #include "platform/platform.h" @@ -166,8 +167,7 @@ static int same_but_swizzle(const TexKey *a, const TexKey *b) { static int tex_mirrored(const tex_t *t) { if (!t->mem || nvn_gpu_only((uintptr_t)t->mem)) return 0; if (t->has_layout) return 1; - const pool_t *p = t->pool; - if (!p || !(p->flags & (NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED | NVN_MEMORY_POOL_FLAGS_CPU_CACHED))) return 0; + if (!(t->pool_flags & (NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED | NVN_MEMORY_POOL_FLAGS_CPU_CACHED))) return 0; if (t->d.flags & (NVN_TEXTURE_FLAGS_DISPLAY | NVN_TEXTURE_FLAGS_COMPRESSIBLE | NVN_TEXTURE_FLAGS_ADAPTIVE_ZCULL | NVN_TEXTURE_FLAGS_DEPTH16_PREFER_FAST_CLEAR | NVN_TEXTURE_FLAGS_ZCULL_SUPPORT_STENCIL)) return 0; @@ -292,6 +292,33 @@ static void init_image(Tex *e, VkCommandBuffer cb) { } } +// TOTK_SHADOW_SCALE (1 to 4): the game's shadow maps are made this many times larger on each side. TotK renders its +// sun cascades into a 960x960 DEPTH16 2D array of 4 layers and its static shadows into 1440x1440 and 720x720 arrays of 2, +// whatever the output resolution, and shrinks the cascades under load (872, 848, 800, 480 and so on). Those are made +// larger here: render passes into them scale the game's viewports and scissors (vk_exec.c resolve_rt, pass_scale), +// copies scale their regions (vk_exec_xfer.c sub_resolve), and shaders sample them with normalised coordinates, so the +// game sees nothing of it. A map whose storage the CPU writes (mirrored) or that aliases pool memory keeps its size, +// as its texels must match the guest layout. +static int shadow_scale(void) { + static int scale = -1; + if (scale < 0) { + int s = cfg_int("TOTK_SHADOW_SCALE"); + s = s < 1 ? 1 : s > 4 ? 4 : s; + rt_log("vk: shadow map scale %d", s); + scale = s; + } + return scale; +} + +static int shadow_scale_of(const nvn_texdesc *d, const TexVkFormat *f, uint32_t samples, int mirrored) { + int scale = shadow_scale(); + if (scale == 1 || mirrored || samples > 1 || d->format != NVN_FORMAT_DEPTH16 || !f->depth) return 1; + if (d->target != NVN_TEXTURE_TARGET_2D_ARRAY || d->width != d->height || d->width < 256 || d->depth < 2 || d->depth > 4) + return 1; + uint32_t most = vk_ctx()->props.limits.maxImageDimension2D; + return d->width * (uint32_t)scale <= most ? scale : 1; +} + // defer_init: the caller records the image's first transition (init_image or alias_copy). static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { static volatile int32_t n_unmapped, n_unsupported, n_failed, n_unbound, n_thick; @@ -319,6 +346,8 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { case NVN_TEXTURE_TARGET_CUBEMAP: case NVN_TEXTURE_TARGET_CUBEMAP_ARRAY: layers = L.layers; break; default: break; } + int scale = shadow_scale_of(d, &f, d->samples > 1 ? (uint32_t)d->samples : 1, tex_mirrored(t)); + w *= (uint32_t)scale, h *= (uint32_t)scale; uint32_t levels = umax(d->levels, 1), most = 1; for (uint32_t s = umax(umax(w, h), depth); s > 1; s >>= 1) most++; if (levels > most) levels = most; @@ -331,6 +360,10 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { if (!ds && (ff & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; if ((d->flags & NVN_TEXTURE_FLAGS_IMAGE) && (ff & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) && samples == 1) usage |= VK_IMAGE_USAGE_STORAGE_BIT; + // contact shadows (vk_contact.c) darken the game's B10G11R11 scene colour in place from a compute shader + if (f.vk == VK_FORMAT_B10G11R11_UFLOAT_PACK32 && (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) && samples == 1 && + (ff & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) && w >= 640 && vk_contact_strength()) + usage |= VK_IMAGE_USAGE_STORAGE_BIT; VkImageCreateFlags flags = ds ? 0 : VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; // format-reinterpreting views // Cube compatible only for cube targets: the replay makes cube views of nothing else (vk_exec_draw.c get_view checks // the image's own view type). The flag on every square array of 6+ layers changed how AMD's Windows driver lays such @@ -388,7 +421,7 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { e->f = f; e->pub.addr = k->addr; VkImage img = VK_NULL_HANDLE; - if (nvn_gpu_only(k->addr)) { // storage in a physical or virtual pool: alias the pool memory (section 3.2) + if (scale == 1 && nvn_gpu_only(k->addr)) { // storage in a physical or virtual pool: alias the pool memory (section 3.2) if (vkCreateImage(c->device, &ci, NULL, &img) == VK_SUCCESS && !bind_pool_memory(c, e, img, L.size)) { vkDestroyImage(c->device, img, NULL); img = VK_NULL_HANDLE; @@ -430,6 +463,7 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { p->bound = e->bound_to != NULL; p->block_views = (uint8_t)block_views; p->fb_usage = (uint8_t)fb_usage; + p->scale = (uint8_t)scale; if (!defer_init) { init_image(e, vk_upload_begin()); @@ -622,9 +656,14 @@ static void astc_check(const Tex *e, const uint8_t *raw, const VkAstcRegion *ar, (unsigned long long)n_texels, (unsigned long long)n_diff_tex); } +// Staging bytes of one part of a texture upload decoded or copied on the CPU (see upload). With TOTK_ASTC=cpu a +// large ASTC array decodes to RGBA8 in one go: a staging buffer of 676682336 bytes failed (-2) on an Intel UHD with +// 8 GB of shared memory and the game closed before the title screen. +#define UPLOAD_PART (32ull << 20) // fits the staging pool (vk_staging takes up to half of its 64 MiB block) + // Copies the whole storage into the image (every level and layer the image has). static void upload(Tex *e) { - static volatile int32_t n_bad, n_undecoded; + static volatile int32_t n_bad, n_undecoded, n_split; const tex_t *t = &e->t; const nvn_texdesc *d = &t->d; int bw, bh, bpb = nvn_fmt_bpb(d->format, &bw, &bh); @@ -648,16 +687,27 @@ static void upload(Tex *e) { total += (out + 15) & ~15ull; decoded += texels * 4; } - VkCommandBuffer cb = vk_upload_begin(); VkBuffer sb, rbuf = VK_NULL_HANDLE; VmaAllocation ralloc = NULL; - uint8_t *stg = vk_staging(total, &sb, NULL), *rmap = acheck ? vk_staging(decoded, &rbuf, &ralloc) : NULL; - k = 0; - for (uint32_t lv = 0; lv < levels; lv++) - for (uint32_t ly = 0; ly < layers; ly++, k++) { + uint8_t *rmap = NULL, *raw_copy = NULL; + int checked = 0, parts = 1; + // A CPU decode or copy stages at most UPLOAD_PART bytes per staging buffer (a region larger than that alone) and + // records one copy per buffer; every part stays in the same upload batch, submitted as before. The GPU decoder + // takes the raw blocks of every region from one buffer. + for (uint32_t k0 = 0, k1; k0 < n; k0 = k1) { + VkDeviceSize base = rg[k0].bufferOffset, size; + for (k1 = k0 + 1; k1 < n && (agpu || (k1 + 1 < n ? rg[k1 + 1].bufferOffset : total) - base <= UPLOAD_PART);) k1++; + size = (k1 < n ? rg[k1].bufferOffset : total) - base; + if (k0) parts++; + VkCommandBuffer cb = vk_upload_begin(); + uint8_t *stg = vk_staging(size, &sb, NULL); + if (acheck) rmap = vk_staging(decoded, &rbuf, &ralloc); + for (k = k0; k < k1; k++) { + uint32_t lv = rg[k].imageSubresource.mipLevel, ly = rg[k].imageSubresource.baseArrayLayer; uint32_t w = rg[k].imageExtent.width, h = rg[k].imageExtent.height, sl = rg[k].imageExtent.depth; uint32_t rb = (w + bw - 1) / bw * (uint32_t)bpb, rows = (h + bh - 1) / bh; size_t raw = (size_t)rb * rows * sl; + rg[k].bufferOffset -= base; uint8_t *dst = stg + rg[k].bufferOffset, *tmp = (astc && !agpu) || rgb ? malloc(raw) : NULL; if (nvn_deswizzle(d, lv, ly, t->mem, tmp ? tmp : dst, rb)) { log_limited(&n_bad, "texture %#llx fmt %#x target %d: level %u layer %u has no CPU layout; uploaded as zeros", @@ -681,23 +731,29 @@ static void upload(Tex *e) { } free(tmp); } - uint8_t *raw_copy = acheck ? malloc(total) : NULL; - if (raw_copy) memcpy(raw_copy, stg, total); - barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); - int checked = 0; - if (agpu) { - uint64_t a0 = perf_begin(); - checked = vk_astc_record(cb, sb, total, ar, (int)n, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, e->f.astc_bc, e->pub.image, - e->pub.aspect, rbuf); - perf_end(PERF_ASTC, a0); - if (!checked) - log_limited(&n_undecoded, "ASTC texture %#llx (fmt %#x %ux%u) not decoded on the GPU: left as it was", - (unsigned long long)e->pub.addr, d->format, d->width, d->height); - } else { - vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, n, rg); + if (acheck) { + raw_copy = malloc(size); + memcpy(raw_copy, stg, size); + } + barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); + if (agpu) { + uint64_t a0 = perf_begin(); + checked = vk_astc_record(cb, sb, size, ar, (int)n, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb, e->f.astc_bc, + e->pub.image, e->pub.aspect, rbuf); + perf_end(PERF_ASTC, a0); + if (!checked) + log_limited(&n_undecoded, "ASTC texture %#llx (fmt %#x %ux%u) not decoded on the GPU: left as it was", + (unsigned long long)e->pub.addr, d->format, d->width, d->height); + } else { + vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, k1 - k0, rg + k0); + } + barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW); + vk_upload_end(); } - barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW); - vk_upload_end(); + if (parts > 1) + log_limited(&n_split, "texture %#llx fmt %#x %ux%u, %u levels, %u layers: %.1f MiB staged in %d parts", + (unsigned long long)e->pub.addr, d->format, d->width, d->height, levels, layers, total / 1048576.0, + parts); if (acheck) { // waits for the GPU vk_upload_flush(1); if (checked) { @@ -751,7 +807,7 @@ static int storage_rewritten(const Tex *e, uint32_t since, uint32_t *now) { uint64_t lo = (e->pub.addr + 0xfff) & ~0xfffull, hi = (e->pub.addr + e->t.L.size) & ~0xfffull; if (hi <= lo) lo = e->pub.addr, hi = e->pub.addr + e->t.L.size; int ch = vk_mem_changed(lo, hi - lo, since, now); - if (ch != 1 || !since || !e->snap || memcmp(e->snap, (const void *)(uintptr_t)e->pub.addr, e->t.L.size)) return ch; + if (ch != 1 || !since || !e->snap || memcmp(e->snap, (const void *)(uintptr_t)e->pub.addr, e->t.L.size)) return ch == 1; log_limited(&n_kept, "write-back of %#llx kept: fmt %#x %ux%u, %llu bytes; a neighbour wrote its page", (unsigned long long)e->pub.addr, e->t.d.format, e->t.d.width, e->t.d.height, (unsigned long long)e->t.L.size); @@ -1644,6 +1700,10 @@ static VkSampler make_sampler(VkCtx *c, const samp_par *d) { if (d->cmp_mode) { si.compareEnable = VK_TRUE; si.compareOp = compare_op(d->cmp_func); + // A depth-compare sampler set to nearest returns 0 or 1 per tap, and the game's shaders smooth shadow edges by + // a few taps spaced for its own shadow map texels. With TOTK_SHADOW_SCALE those taps cover a fraction of the + // area and the edges turn hard and stepped; linear filtering makes every tap a 2x2 PCF instead. + if (shadow_scale() > 1) si.magFilter = si.minFilter = VK_FILTER_LINEAR; } VkSamplerCustomBorderColorCreateInfoEXT cb = {.sType = VK_STRUCTURE_TYPE_SAMPLER_CUSTOM_BORDER_COLOR_CREATE_INFO_EXT}; VkSamplerReductionModeCreateInfo rm = {.sType = VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO}; @@ -1688,11 +1748,32 @@ void vk_sampler_register(int id, const samp_par *s) { unlock(); } +// DLSS upscaling (vk_sampler_dlss_bias): the scene's material textures, the filtered mipmapped ones that repeat, are +// sampled for the output's resolution; clamped ones (post effects, LUTs, the UI) keep the game's bias. +static float dlss_bias; + +static int dlss_biased(const samp_par *k) { + int rep_s = k->ws == NVN_WRAP_MODE_REPEAT || k->ws == NVN_WRAP_MODE_MIRRORED_REPEAT; + int rep_t = k->wt == NVN_WRAP_MODE_REPEAT || k->wt == NVN_WRAP_MODE_MIRRORED_REPEAT; + return !k->cmp_mode && k->minf == NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR && (rep_s || rep_t); +} + +void vk_sampler_dlss_bias(float b) { + lock(); + if (b != dlss_bias) { + dlss_bias = b; + for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = VK_NULL_HANDLE; // looked up again (the key moves) + samp_gen++; + } + unlock(); +} + // Caller holds the lock. static VkSampler sampler_for(VkCtx *c, const samp_par *s) { samp_par k; memset(&k, 0, sizeof k); k = *s; + if (dlss_bias != 0 && dlss_biased(&k)) k.lod_bias += dlss_bias; // part of the key: samplers live until shutdown VkSampler v = (VkSampler)nvn_hm_get(&samplers, &k); if (!v) { v = make_sampler(c, &k); diff --git a/runtime/vk_usnap.c b/runtime/vk_usnap.c index 056fdf80..4e9744b9 100644 --- a/runtime/vk_usnap.c +++ b/runtime/vk_usnap.c @@ -22,6 +22,7 @@ #include "host.h" #include "nvn_api.h" #include "vk.h" +#include "vk_dlaa.h" #include "platform/platform.h" #define ALIGN 256u @@ -34,6 +35,9 @@ typedef struct Arena { VkBuffer buf; VmaAllocation alloc; uint8_t *map; + uint8_t *cpu; // with DLSS (vk_dlaa_available), the same bytes in host memory for the CPU's reads (vk_usnap_bytes): + // map is often device memory through the BAR, written fast but read back at about a microsecond a + // cache line. Without DLSS nothing reads them back, and the bytes go into map in one copy. uint64_t size; } Arena; @@ -81,7 +85,8 @@ static Arena *arena_get(uint64_t need) { if (!a) rt_fatal("vk_usnap: out of memory"); a->size = need < ARENA_MIN ? ARENA_MIN : (need + (1u << 20) - 1) & ~(uint64_t)((1u << 20) - 1); VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = a->size, - .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT}; + // transfer source: DLAA copies the camera block out (vk_dlaa.c mv_pass) + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT}; // Device-local through the BAR when the device has it (the GPU reads constants on every draw), else host memory. VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | @@ -120,9 +125,35 @@ static void collect(const op_hdr *h, int depth) { if (h->op == OP_BIND_UBO) { const p_bind_buf *b = a; if (b->addr && b->size) { - if (U.ncol == U.ccol) U.col = grow(U.col, &U.ccol, sizeof *U.col); uint64_t n = b->size < MAX_RANGE ? b->size : MAX_RANGE; - U.col[U.ncol++] = (Rng){b->addr & ~(uint64_t)(ALIGN - 1), b->addr + n}; + // the vertex stage's object and bone blocks: also the game's other copy on either side, which still + // holds last frame's (vk_mvprev.c) + uint64_t step = vk_mvprev_step(b->stage, b->index, b->size); + // which neighbours lie in an imported pool, per block and vk_mem_gen: the same blocks are bound by + // many draws, and two resolves for each binding were a quarter of the snapshot's time + static struct { uint64_t addr, size, gen; int ok; } nc[1024]; + uint32_t ci = (uint32_t)((b->addr ^ n << 48) * 0x9E3779B97F4A7C15ull >> 54); + uint64_t gen = vk_mem_gen(); + int known = step && nc[ci].addr == b->addr && nc[ci].size == n && nc[ci].gen == gen, ok = 0; + for (int k = step ? -1 : 0; k <= (step ? 1 : 0); k++) { + uint64_t at = b->addr + (uint64_t)((int64_t)k * (int64_t)step); + if (k < 0 && b->addr < step) continue; + // a neighbour past its pool would make the whole merged region fail the pool check below, and + // the bindings in it read live memory again + VkMemSpan sp; + uint64_t lo = at & ~(uint64_t)(ALIGN - 1); + if (k && known) { + if (!(nc[ci].ok >> (k + 1) & 1)) continue; + } else if (k) { + if (!vk_mem_resolve(lo, at + n - lo, &sp) || sp.alloc || !sp.pool || sp.avail < at + n - lo || + (sp.cls != VK_MCLS_HOST_CACHED && sp.cls != VK_MCLS_HOST_UNC)) + continue; + ok |= 1 << (k + 1); + } + if (U.ncol == U.ccol) U.col = grow(U.col, &U.ccol, sizeof *U.col); + U.col[U.ncol++] = (Rng){at & ~(uint64_t)(ALIGN - 1), at + n}; + } + if (step && !known) nc[ci].addr = b->addr, nc[ci].size = n, nc[ci].gen = gen, nc[ci].ok = ok; } } h = nvn_op_next(h); @@ -253,8 +284,14 @@ void vk_usnap_begin(const uint64_t *handles, int n, uint64_t fence) { } if (!U.nreg) return; if (!(U.arena = arena_get(total))) return; + uint8_t *dst = U.arena->map; + if (vk_dlaa_available()) { + if (!U.arena->cpu && !(U.arena->cpu = malloc(U.arena->size))) rt_fatal("vk_usnap: out of memory"); + dst = U.arena->cpu; + } for (int i = 0; i < U.nreg; i++) - memcpy(U.arena->map + U.reg[i].off, (const void *)(uintptr_t)U.reg[i].lo, U.reg[i].hi - U.reg[i].lo); + memcpy(dst + U.reg[i].off, (const void *)(uintptr_t)U.reg[i].lo, U.reg[i].hi - U.reg[i].lo); + if (dst != U.arena->map) memcpy(U.arena->map, dst, total); // one sequential write into the device copy vmaFlushAllocation(vk_ctx()->vma, U.arena->alloc, 0, total); // a no-op on coherent memory U.on = 1; U.subs++, U.regions += (uint64_t)U.nreg, U.bytes += total, U.copy_ns += kern_now_ns() - t0; @@ -300,10 +337,20 @@ void vk_usnap_drop(uint64_t addr, uint64_t n) { uint64_t vk_usnap_gen(void) { return U.gen; } +// the host copy where there is one (DLSS), else the device copy +static uint8_t *host_copy(void) { return U.arena->cpu ? U.arena->cpu : U.arena->map; } + +const uint8_t *vk_usnap_map(void) { return U.arena ? host_copy() : NULL; } + +const uint8_t *vk_usnap_bytes(uint64_t addr, uint64_t n) { + VkDescriptorBufferInfo bi; + return vk_usnap_find(addr, n, 1, &bi) ? host_copy() + bi.offset : NULL; +} + // The arenas back on the free list (vk_exec_draw_shutdown: the device is idle, every completion ran). void vk_usnap_shutdown(void) { plat_mutex_lock(&free_lock); - for (Arena *a = free_arenas, *next; a; a = next) next = a->next, vmaDestroyBuffer(vk_ctx()->vma, a->buf, a->alloc), free(a); + for (Arena *a = free_arenas, *next; a; a = next) next = a->next, vmaDestroyBuffer(vk_ctx()->vma, a->buf, a->alloc), free(a->cpu), free(a); free_arenas = NULL; plat_mutex_unlock(&free_lock); } diff --git a/tests/diff/common.py b/tests/diff/common.py index 648e280c..c2f796df 100644 --- a/tests/diff/common.py +++ b/tests/diff/common.py @@ -213,7 +213,8 @@ def build_harness(work, bodies, jobs, cflags): f.write(''.join(f'void t{k}(Cpu *c);\n' for k in range(len(bodies)))) f.write('GuestFn tests[] = {' + ','.join(f't{k}' for k in range(len(bodies))) + '};\n') f.write(f'int ntests = {len(bodies)};\n') - srcs += [work / 'table.c', ROOT / 'tests' / 'diff' / 'harness.c'] + # fma.c: a --cflags=-march=x86-64-v2 run must exercise the software fma the v2 build links, not the C runtime's. + srcs += [work / 'table.c', ROOT / 'tests' / 'diff' / 'harness.c', ROOT / 'runtime' / 'fma.c'] objs, procs = [], [] for s in srcs: o = work / (s.stem + '.o') diff --git a/tests/diff/func.py b/tests/diff/func.py index 25a5d0bc..62c34068 100644 --- a/tests/diff/func.py +++ b/tests/diff/func.py @@ -204,7 +204,8 @@ def build(work, emitted, jobs, cflags): f'int ntests = {len(emitted)};\n' + 'void rt_stub_call(Cpu *c, uint64_t target);\n' + ''.join(f'void {gen.fname(t)}(Cpu *c) {{ rt_stub_call(c, {hex(t)}ull); }}\n' for t in sorted(called))) - srcs += [table, ROOT / 'tests' / 'diff' / 'harness.c'] + # fma.c: a --cflags=-march=x86-64-v2 run must exercise the software fma the v2 build links, not the C runtime's. + srcs += [table, ROOT / 'tests' / 'diff' / 'harness.c', ROOT / 'runtime' / 'fma.c'] objs, procs = [], [] for s in srcs: o = work / (s.stem + '.o') diff --git a/tests/installer_gui_test.py b/tests/installer_gui_test.py index 4eb9c228..93fe5dd3 100644 --- a/tests/installer_gui_test.py +++ b/tests/installer_gui_test.py @@ -30,15 +30,16 @@ from installer import layout as layouts # noqa: E402 from installer import state as statefile # noqa: E402 from installer.events import Reporter # noqa: E402 from installer.runner import Context # noqa: E402 -from installer.steps import all_steps # noqa: E402 -from installer.guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, Catalog, Form, Model, Process, # noqa: E402 - amount, describe_event, parse_line, windows_ui_language) +from installer.steps import Fonts, all_steps # noqa: E402 +from installer.guilogic import (DONE, FAILED, PENDING, RUNNING, SKIPPED, UI_LANGUAGE_LABELS, # noqa: E402 + UI_LANGUAGES, Catalog, Form, Model, Process, amount, default_game_language, + describe_event, parse_line, ui_language_for_langid, windows_ui_language) INSTALLED = ROOT / 'build' / 'install-test' FIXTURE = ROOT / 'build' / 'inst-gui' / 'e2e-install' COPIED = ('totk.ini', statefile.NAME) # written by a run, so never linked START = {'type': 'start', 'release': 'r1', 'mode': 'install', 'dest': 'D:\\T', 'log': 'D:\\T.log', - 'steps': ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']} + 'steps': ['check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'cleanup']} def fold(*events): @@ -66,7 +67,7 @@ class ModelTest(unittest.TestCase): m = fold(START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'lift': 'current', 'compile': 'current'}}, {'type': 'step', 'step': 'check', 'state': 'started'}) self.assertEqual([m.states[s] for s in m.steps], - [RUNNING, PENDING, SKIPPED, SKIPPED, PENDING, PENDING]) + [RUNNING, PENDING, PENDING, SKIPPED, SKIPPED, PENDING, PENDING]) self.assertEqual(m.current, 'check') m.apply({'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.0}) self.assertEqual(m.states['check'], DONE) @@ -137,10 +138,12 @@ class ModelTest(unittest.TestCase): class RunStatesTest(unittest.TestCase): - OK = [START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'lift': 'current', 'compile': 'current', - 'prepare': 'current', 'cleanup': 'not_needed'}}, + OK = [START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'fonts': 'current', 'lift': 'current', + 'compile': 'current', 'prepare': 'current', + 'cleanup': 'not_needed'}}, {'type': 'step', 'step': 'check', 'state': 'started'}, {'type': 'step', 'step': 'check', 'state': 'done'}, {'type': 'step', 'step': 'extract', 'state': 'started'}, {'type': 'step', 'step': 'extract', 'state': 'done'}, + {'type': 'step', 'step': 'fonts', 'state': 'skipped'}, {'type': 'step', 'step': 'lift', 'state': 'skipped'}, {'type': 'step', 'step': 'compile', 'state': 'skipped'}, {'type': 'step', 'step': 'prepare', 'state': 'skipped'}, {'type': 'step', 'step': 'cleanup', 'state': 'done'}, {'type': 'end', 'result': 'ok', 'seconds': 1}] @@ -200,11 +203,20 @@ class FormTest(unittest.TestCase): def test_unknown_language(self): self.assertEqual(self.form(lang='xx').problems(), ['ui.need_lang']) + def test_firmware_folder(self): + firmware = self.dir / 'firmware' + firmware.mkdir() + self.assertEqual(self.form(firmware=str(firmware)).problems(), []) + self.assertEqual(self.form(firmware=str(self.dir / 'no-firmware')).problems(), ['ui.missing_firmware']) + def test_command(self): - cmd = self.form().command('py.exe', root=ROOT, temp=self.dir / 't', verify=True) + firmware = self.dir / 'firmware' + firmware.mkdir() + cmd = self.form(firmware=str(firmware)).command('py.exe', root=ROOT, temp=self.dir / 't', verify_hashes=True) self.assertEqual(cmd[:3], ['py.exe', '-m', 'installer']) - for flag in ('--base', '--update', '--keys', '--lang', '--dest', '--root', '--temp', '--verify'): + for flag in ('--base', '--update', '--keys', '--firmware', '--lang', '--dest', '--root', '--temp', '--verify'): self.assertIn(flag, cmd) + self.assertEqual(cmd[cmd.index('--firmware') + 1], str(firmware)) self.assertEqual(cmd[cmd.index('--lang') + 1], 'en-US') def test_installed_folder_needs_only_itself(self): @@ -233,25 +245,41 @@ class FormTest(unittest.TestCase): class CatalogTest(unittest.TestCase): - def test_russian_covers_english_with_the_same_placeholders(self): - en, ru = Catalog('en').english, Catalog('ru').texts - self.assertEqual(sorted(set(en) - set(ru)), []) - self.assertEqual(sorted(set(ru) - set(en)), []) - holes = [k for k in en if sorted(re.findall(r'\{\w+\}', en[k])) != sorted(re.findall(r'\{\w+\}', ru[k]))] - self.assertEqual(holes, []) + def test_every_language_covers_english_with_the_same_placeholders(self): + en = Catalog('en').english + for lang in UI_LANGUAGES[1:]: + with self.subTest(lang=lang): + other = Catalog(lang).texts + self.assertEqual(sorted(set(en) - set(other)), []) + self.assertEqual(sorted(set(other) - set(en)), []) + holes = [k for k in en + if sorted(re.findall(r'\{\w+\}', en[k])) != sorted(re.findall(r'\{\w+\}', other[k]))] + self.assertEqual(holes, []) + + def test_language_names_tell_the_codes_apart(self): + # gui.App.selected_lang maps the shown name back to its code across every window language + names = {} + for lang in UI_LANGUAGES: + cat = Catalog(lang) + for id in cat.english: + if id.startswith('lang.'): + names.setdefault(cat(id), set()).add(id) + self.assertEqual({n: ids for n, ids in names.items() if len(ids) > 1}, {}) + self.assertEqual(sorted(UI_LANGUAGE_LABELS), sorted(UI_LANGUAGES)) def test_every_id_the_window_uses_exists(self): cat = Catalog('en') ids = ['ui.' + n for n in ('title', 'start', 'cancel', 'open_folder', 'notice', 'space_one', 'space_two', 'process_died', 'result_ok', 'result_failed', 'result_cancelled')] - ids += [f'step.{s}' for s in ('check', 'extract', 'lift', 'compile', 'prepare', 'cleanup')] + ids += [f'step.{s}' for s in ('check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'cleanup')] ids += ['ui.state_' + n for n in ('started', 'done', 'skipped', 'pending', 'running', 'failed')] - for lang in ('en', 'ru'): + for lang in UI_LANGUAGES: cat.set_language(lang) + self.assertEqual(cat.lang, lang) self.assertEqual([i for i in ids if not cat.has(i)], []) def test_codes_become_names(self): - for lang, korean in (('en', 'Korean'), ('ru', 'Корейский')): + for lang, korean in (('en', 'Korean'), ('ru', 'Корейский'), ('pt-BR', 'Coreano'), ('zh-Hans', '韩语')): cat = Catalog(lang) self.assertIn(korean, cat('warn.lang_needs_fonts', lang='ko')) self.assertNotIn(' ko ', cat('warn.lang_needs_fonts', lang='ko')) @@ -280,7 +308,17 @@ class CatalogTest(unittest.TestCase): self.assertEqual(describe_event(cat, {'type': 'start'}), '') def test_windows_language_is_a_known_value(self): - self.assertIn(windows_ui_language(), ('en', 'ru')) + self.assertIn(windows_ui_language(), UI_LANGUAGES) + + def test_windows_langid_picks_the_window_language(self): + for langid, lang in ((0x0419, 'ru'), (0x0819, 'ru'), (0x0416, 'pt-BR'), (0x0816, 'en'), + (0x0804, 'zh-Hans'), (0x1004, 'zh-Hans'), + (0x0404, 'en'), (0x0c04, 'en'), (0x1404, 'en'), (0x0409, 'en'), (0x0411, 'en')): + self.assertEqual(ui_language_for_langid(langid), lang, hex(langid)) + + def test_chinese_window_starts_with_a_game_language_that_needs_no_shared_fonts(self): + self.assertEqual(default_game_language('zh-Hans'), 'en-US') + self.assertEqual(default_game_language('ru'), 'ru') BREAK_CHILD = 'import time\nprint("ready", flush=True)\ntime.sleep(60)\n' @@ -334,6 +372,10 @@ def make_fixture(): ctx = Context(layout, FIXTURE, ROOT / 'build' / 'inst-gui' / 'e2e-temp', Reporter(out=io.StringIO()), options, state=state) ctx.steps = all_steps() ctx.plan() + # An install made before the fonts step has no record for it: record it as current when its output is (the + # languages that need the fonts have them in shared-fonts/). + if Fonts().current(ctx, {}): + state['steps'].setdefault('fonts', {'release': state.get('release', ''), 'key': ctx.keys['fonts']}) for step_id, record in state['steps'].items(): record['key'] = ctx.keys[step_id] statefile.save(FIXTURE, state) @@ -346,7 +388,7 @@ class EndToEndTest(unittest.TestCase): def run_cli(form, temp, tries=1): """Runs the CLI in --verify mode through Process, up to `tries` times while the RomFS hash reads wrong.""" for attempt in range(tries): - p = Process(form.command(sys.executable, root=ROOT, temp=temp, verify=True), cwd=ROOT) + p = Process(form.command(sys.executable, root=ROOT, temp=temp, verify_hashes=True), cwd=ROOT) model, stray = Model(), [] t0, code = time.monotonic(), None while code is None and time.monotonic() - t0 < 600: @@ -384,7 +426,7 @@ class EndToEndTest(unittest.TestCase): def test_cancel_during_the_check(self): cmd = Form(dest=str(make_fixture())).command(sys.executable, root=ROOT, temp=ROOT / 'build' / 'inst-gui' / 'e2e-temp', - verify=True) + verify_hashes=True) p = Process(cmd, cwd=ROOT) model, t0, code, cancelled = Model(), time.monotonic(), None, False while code is None and time.monotonic() - t0 < 120: diff --git a/tests/installer_test.py b/tests/installer_test.py index 036d897a..0e1d67ff 100644 --- a/tests/installer_test.py +++ b/tests/installer_test.py @@ -3,6 +3,7 @@ with a newer release, totk.ini and the event stream. No game files, no tools; st Run: python tests/installer_test.py """ +import http.client import io import json import re @@ -11,6 +12,7 @@ import tempfile import unittest from pathlib import Path from types import SimpleNamespace +from unittest import mock ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) @@ -197,6 +199,122 @@ class RunnerTest(unittest.TestCase): self.assertEqual(e.exception.id, 'state.newer_format') +class ReleaseFilesTest(unittest.TestCase): + def test_release_ships_every_tool_the_steps_run(self): + sys.path.insert(0, str(ROOT / 'tools')) + import make_release + text = (ROOT / 'installer' / 'steps.py').read_text(encoding='utf-8') + used = set(re.findall(r"layout\.tools / '(\w+\.py)'", text)) + for names in re.findall(r"layout\.tools / n for n in \((.*?)\)", text, re.S): + used |= set(re.findall(r"'(\w+\.py)'", names)) + self.assertIn('cheats.py', used) + self.assertEqual(used - set(make_release.TOOLS_FILES), set()) + + +class RenamedVersionTest(unittest.TestCase): + """An install the setup before 0.3.1 recorded as 1.1.1 is the 1.1.0 update (#332).""" + + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + root = Path(__file__).resolve().parents[1] + layout = Layout(root=root, dev=True, release='9.9', python=Path('python'), tools=root / 'tools', + recomp=root / 'recomp', llvm=Path('llvm'), deps=Path('deps'), runtime=Path('rt'), + runtime_lib=Path('lib'), ninja=Path('ninja'), hactool=Path('hactool'), xlate=Path('xlate')) + state = {'format': 1, 'release': '0.2.8', 'version': '1.1.1', 'inputs': {}, + 'steps': {'extract': {'key': 'recorded by 0.2.8', 'release': '0.2.8'}}} + out = io.StringIO() + self.ctx = runner.Context(layout, Path(tmp.name), Path(tmp.name), Reporter(out=out), SimpleNamespace(), + state=state) + + def test_old_name_is_read_as_1_1_0(self): + steps.migrate_state(self.ctx) + self.assertEqual(steps.game_version(self.ctx), '1.1.0') + self.assertEqual(steps.version_tag(steps.game_version(self.ctx)), 'v110') + self.assertEqual(steps.expected_update(self.ctx)['display_version'], '1.1.0') + self.assertTrue(self.ctx.layout.patches(steps.game_version(self.ctx)).is_file()) + + def test_extraction_is_kept_under_the_new_name(self): + steps.migrate_state(self.ctx) + self.assertEqual(self.ctx.state['steps']['extract']['key'], steps.Extract().key(self.ctx)) + self.assertEqual(self.ctx.state['steps']['extract']['release'], '0.2.8') # full check of kept output runs + + def test_other_versions_are_untouched(self): + self.ctx.state['version'] = '1.4.3' + steps.migrate_state(self.ctx) + self.assertEqual(self.ctx.state['version'], '1.4.3') + self.assertEqual(self.ctx.state['steps']['extract']['key'], 'recorded by 0.2.8') + + +class LiftKeyTest(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.dest = Path(tmp.name) + layout = SimpleNamespace(recomp=self.dest / 'no-recomp', tools=self.dest / 'no-tools', + patches=lambda version: self.dest / 'no-patches') + self.ctx = SimpleNamespace(dest=self.dest, layout=layout, keys={'extract': 'x'}, state={'version': '1.4.3'}) + + def key(self): + return steps.Lift().key(self.ctx) + + def add_cheats(self, name, text, mod='Cheats', where='cheats'): + folder = self.dest / 'mods' / mod / where + folder.mkdir(parents=True, exist_ok=True) + (folder / name).write_text(text) + + def test_extracting_main_keeps_the_key(self): + before = self.key() + (self.dest / 'exefs').mkdir() + (self.dest / 'exefs' / 'main').write_bytes(bytes(0x48)) + self.assertEqual(self.key(), before) + + def test_cheat_files_change_the_key(self): + before = self.key() + self.add_cheats('277178B7DBA1B6D4.txt', '[a]') + added = self.key() + self.assertNotEqual(added, before) + self.add_cheats('277178B7DBA1B6D4.txt', '[b]') + edited = self.key() + self.assertNotEqual(edited, added) + self.add_cheats('277178B7DBA1B6D4.txt', '[c]', mod='Other', where='atmosphere/contents/0100F2C0115B6000/cheats') + self.assertNotEqual(self.key(), edited) + + def test_exefs_patch_files_change_the_key(self): + before = self.key() + self.add_cheats('dynamic-fps.pchtxt', '@nsobid-277178B7DBA1B6D4', mod='Fps', where='exefs') + added = self.key() + self.assertNotEqual(added, before) + self.add_cheats('277178B7DBA1B6D4.ips', 'PATCHEOF', mod='Ips', where='atmosphere/exefs_patches/x') + both = self.key() + self.assertNotEqual(both, added) + self.add_cheats('main.npdm', 'not a patch', mod='Fps', where='exefs') + self.assertEqual(self.key(), both) + + def test_renaming_a_mod_folder_changes_the_key(self): + # tools/cheats.py and tools/exefs_patches.py name a patch set after "<mod folder>/<cheat or file>": a renamed + # folder needs a lift even though its files are the same + self.add_cheats('277178B7DBA1B6D4.txt', '[a]', mod='MyNXCheats') + self.add_cheats('dynamic-fps.pchtxt', '@nsobid-277178B7DBA1B6D4', mod='Fps', where='exefs') + before = self.key() + mods = self.dest / 'mods' + (mods / 'MyNXCheats').rename(mods / 'MyNXCheats 1.4.3') + cheats_renamed = self.key() + self.assertNotEqual(cheats_renamed, before) + (mods / 'Fps').rename(mods / 'Fps 2') + self.assertNotEqual(self.key(), cheats_renamed) + + def test_status_and_other_files_keep_the_key(self): + self.add_cheats('277178B7DBA1B6D4.txt', '[a]') + before = self.key() + (self.dest / 'build-cache').mkdir() + (self.dest / 'build-cache' / 'cheats.status').write_text('written by lift') + self.add_cheats('readme.md', 'not a cheat file') + (self.dest / 'mods' / 'Cheats' / 'romfs').mkdir() + (self.dest / 'mods' / 'Cheats' / 'romfs' / 'a.txt').write_text('a RomFS file') + self.assertEqual(self.key(), before) + + class IniTest(unittest.TestCase): EXAMPLE = '[display]\n# vk_present = mailbox\n\n[language]\n# locale = en-US\n# lang = en-US\n# x = 1\n' @@ -258,7 +376,8 @@ class CleanupTest(unittest.TestCase): self.expected = {'romfs': {'size': len(image), 'sha256': digest}} self.ctx = SimpleNamespace(reporter=Reporter(out=io.StringIO(), log_path=base / 'x.log'), dest=self.dest, temp=temp, lang='en-US', state={}, - layout=SimpleNamespace(ini_example=ROOT / 'totk.ini.example')) + layout=SimpleNamespace(ini_example=ROOT / 'totk.ini.example', dev=False, + python=base / 'python' / 'python.exe', root=base / 'release')) self.saved = steps.expected_update steps.expected_update = lambda ctx: self.expected @@ -277,6 +396,16 @@ class CleanupTest(unittest.TestCase): def test_intact_romfs_passes(self): self.assertEqual(self.run_cleanup(), 'ok') + def test_keeps_build_and_writes_rebuild_command(self): + for name in ('gen', 'build', 'check'): + (self.ctx.temp / name).mkdir() + self.assertEqual(self.run_cleanup(), 'ok') + self.assertTrue((self.ctx.temp / 'gen').is_dir() and (self.ctx.temp / 'build').is_dir()) + self.assertFalse((self.ctx.temp / 'check').exists()) + lines = (self.dest / 'build-cache' / 'rebuild.txt').read_text(encoding='utf-8').splitlines() + self.assertEqual(lines[1:], ['-c', steps.REBUILD_BOOT, str(self.ctx.layout.root), '--root', + str(self.ctx.layout.root), '--rebuild', str(self.dest)]) + def test_emptied_romfs_fails(self): for p in (self.dest / 'romfs').iterdir(): p.unlink() @@ -291,6 +420,133 @@ class CleanupTest(unittest.TestCase): self.assertEqual(self.run_cleanup(), 'cleanup.romfs_unreadable') +class BaseGameCheckTest(unittest.TestCase): + """Issue #261: an update NSP chosen as the base game was reported as "not Tears of the Kingdom".""" + BASE, UPDATE = '0100F2C0115B6000', '0100F2C0115B6800' + + @staticmethod + def nca(content_type, title_id): + return ({'name': 'x.nca', 'nca_offset': 0, 'size': 0xc00, 'content_type': content_type, 'title_id': title_id}, b'') + + def test_update_package_names_the_update(self): + # An update NSP keeps the base title id on its Program NCA; only its Meta NCA carries the update id. + records = [self.nca(0, self.BASE), self.nca(1, self.UPDATE)] + with self.assertRaises(ValueError): + steps.program_nca(records) + self.assertEqual(steps.base_misuse(records), 'check.base_is_update') + + def test_combined_package_is_named(self): + records = [self.nca(0, self.BASE), self.nca(1, self.BASE), self.nca(1, self.UPDATE)] + self.assertEqual(steps.base_misuse(records), 'check.base_combined') + + def test_plain_base_game_passes(self): + records = [self.nca(0, self.BASE), self.nca(1, self.BASE), self.nca(3, self.BASE)] + self.assertIsNone(steps.base_misuse(records)) + self.assertEqual(steps.program_nca(records)[0]['title_id'], self.BASE) + + +class FontsTest(unittest.TestCase): + """The fonts step: keyed by language and firmware, a no-op without firmware, writes dest/shared-fonts.""" + + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + base = Path(self.tmp.name) + self.dest, temp, self.firmware = base / 'dest', base / 'temp', base / 'firmware' + temp.mkdir() + self.firmware.mkdir() + (self.firmware / 'a.nca').write_bytes(b'nca') + self.keys = base / 'prod.keys' + self.keys.write_text('header_key = 00\n') + self.ctx = SimpleNamespace(dest=self.dest, temp=temp, lang='zh-Hans', state={}, + layout=SimpleNamespace(tools=ROOT / 'tools', root=ROOT), + reporter=Reporter(out=io.StringIO()), + options=SimpleNamespace(lang='zh-Hans', firmware=self.firmware, keys=str(self.keys))) + self.saved_run_tool = steps.run_tool + + def tearDown(self): + steps.run_tool = self.saved_run_tool + self.ctx.reporter.close() + self.tmp.cleanup() + + def fake_tool(self, files=None, code=0): + """Stand-in for extract_shared_fonts.py: writes the requested files where --output points.""" + def run(ctx, step, script, args): + out = Path(args[args.index('--output') + 1]) + out.mkdir(parents=True) + for name in steps.SHARED_FONT_FILES if files is None else files: + (out / name).write_bytes(b'bfttf') + (out / 'manifest.json').write_text('{}') + log = ctx.temp / 'fonts.log' + log.write_text('error line\n') + return code, log + steps.run_tool = run + + def test_all_steps_order(self): + self.assertEqual([s.id for s in steps.all_steps()], + ['check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'dlss', 'cleanup']) + + def test_extracts_into_the_install_folder(self): + self.fake_tool() + self.assertEqual(steps.Fonts().run(self.ctx), {'files': 6}) + self.assertTrue(steps.shared_fonts_ready(self.dest)) + self.assertEqual(sorted(p.name for p in (self.dest / 'shared-fonts').iterdir()), + sorted((*steps.SHARED_FONT_FILES, 'manifest.json'))) + + def test_noop_without_firmware(self): + self.ctx.options.firmware = None + self.fake_tool() + self.assertEqual(steps.Fonts().run(self.ctx), {}) + self.assertFalse((self.dest / 'shared-fonts').exists()) + + def test_noop_for_other_languages(self): + self.ctx.lang, self.ctx.options.lang = 'en-US', 'en-US' + self.fake_tool() + self.assertEqual(steps.Fonts().run(self.ctx), {}) + self.assertFalse((self.dest / 'shared-fonts').exists()) + + def test_failed_extraction_keeps_the_old_folder(self): + self.fake_tool() + steps.Fonts().run(self.ctx) + self.fake_tool(code=1) + with self.assertRaises(steps.InstallError) as e: + steps.Fonts().run(self.ctx) + self.assertEqual(e.exception.id, 'fonts.failed') + self.assertTrue(steps.shared_fonts_ready(self.dest)) + + def test_incomplete_output_is_an_error(self): + self.fake_tool(files=steps.SHARED_FONT_FILES[:1]) + with self.assertRaises(steps.InstallError) as e: + steps.Fonts().run(self.ctx) + self.assertEqual(e.exception.id, 'fonts.incomplete') + + def test_current_tracks_language_and_output(self): + fonts = steps.Fonts() + self.assertFalse(fonts.current(self.ctx, {})) + self.fake_tool() + fonts.run(self.ctx) + self.assertTrue(fonts.current(self.ctx, {})) + self.ctx.options.lang = 'en-US' + self.assertTrue(fonts.current(self.ctx, {})) + self.ctx.options.lang = 'zh-Hans' + (self.dest / 'shared-fonts' / '0-standard.bfttf').unlink() + self.assertFalse(fonts.current(self.ctx, {})) + + def test_key_changes_with_language_and_firmware(self): + fonts = steps.Fonts() + zh = fonts.key(self.ctx) + self.ctx.options.lang = 'en-US' + self.assertNotEqual(zh, fonts.key(self.ctx)) + self.ctx.options.lang = 'zh-Hans' + (self.firmware / 'b.nca').write_bytes(b'other') + self.assertNotEqual(zh, fonts.key(self.ctx)) + + def test_registered_child_is_used(self): + nested = self.firmware / 'registered' + nested.mkdir() + (nested / 'c.nca').write_bytes(b'nca') + self.assertEqual(steps.firmware_dir(self.firmware), nested) + + class EventsTest(unittest.TestCase): def test_lines_are_json_and_the_log_is_english(self): with tempfile.TemporaryDirectory() as d: @@ -314,11 +570,86 @@ class EventsTest(unittest.TestCase): catalog = json.loads((ROOT / 'installer' / 'text' / 'en.json').read_text(encoding='utf-8')) used = set() for path in (ROOT / 'installer').glob('*.py'): - used |= set(re.findall(r"InstallError\('([a-z_.]+)'", path.read_text())) - used |= set(re.findall(r"warning\('([a-z_.]+)'", path.read_text())) - used |= {f'phase.{s}.{p}' for s, p in re.findall(r"progress\('(\w+)', '(\w+)'[,)]", path.read_text())} + text = path.read_text(encoding='utf-8') + used |= set(re.findall(r"InstallError\('([a-z_.]+)'", text)) + used |= set(re.findall(r"warning\('([a-z_.]+)'", text)) + used |= {f'phase.{s}.{p}' for s, p in re.findall(r"progress\('(\w+)', '(\w+)'[,)]", text)} self.assertEqual(sorted(i for i in used if i not in catalog), []) +@unittest.skipUnless(steps.os.name == 'nt', 'DLSS is Windows-only') +class DlssTest(unittest.TestCase): + """The DLSS step: off without --dlss, needs an RTX GPU, keeps a player's library, verifies the download.""" + + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + base = Path(self.tmp.name) + self.dest, temp = base / 'dest', base / 'temp' + self.dest.mkdir() + temp.mkdir() + self.src = base / 'nvngx_dlss.dll' + self.src.write_bytes(b'dlss' * 1000) + self.saved = steps.DLSS_URL, steps.DLSS_SIZE, steps.DLSS_SHA256 + steps.DLSS_URL = self.src.as_uri() # urllib reads file: URLs as it reads https: ones + steps.DLSS_SIZE, steps.DLSS_SHA256 = 4000, steps.file_sha256(self.src) + rtx = {'devices': [{'name': 'NVIDIA GeForce RTX 4080 SUPER', 'api': [1, 4, 0], 'type': 2}]} + self.ctx = SimpleNamespace(dest=self.dest, temp=temp, host=rtx, reporter=Reporter(out=io.StringIO()), + options=SimpleNamespace(dlss=True)) + + def tearDown(self): + steps.DLSS_URL, steps.DLSS_SIZE, steps.DLSS_SHA256 = self.saved + self.ctx.reporter.close() + self.tmp.cleanup() + + def warnings(self): + return [json.loads(line)['id'] for line in self.ctx.reporter.out.getvalue().splitlines() + if json.loads(line)['type'] == 'warning'] + + def test_off_without_the_option(self): + self.ctx.options.dlss = False + self.assertEqual(steps.Dlss().run(self.ctx), {}) + self.assertFalse((self.dest / 'nvngx_dlss.dll').exists()) + self.assertNotEqual(steps.Dlss().key(self.ctx), steps.Dlss().key(SimpleNamespace(options=SimpleNamespace(dlss=True)))) + + def test_downloads_and_checks_the_library(self): + rec = steps.Dlss().run(self.ctx) + self.assertEqual(rec, {'installed': steps.DLSS_VERSION}) + self.assertEqual((self.dest / 'nvngx_dlss.dll').read_bytes(), self.src.read_bytes()) + self.assertFalse(list(self.ctx.temp.iterdir())) + self.assertTrue(steps.Dlss().current(self.ctx, rec)) + (self.dest / 'nvngx_dlss.dll').unlink() + self.assertFalse(steps.Dlss().current(self.ctx, rec)) + + def test_a_wrong_file_is_not_installed_and_tried_again(self): + steps.DLSS_SHA256 = '0' * 64 + rec = steps.Dlss().run(self.ctx) + self.assertEqual(rec, {'failed': True}) + self.assertFalse((self.dest / 'nvngx_dlss.dll').exists()) + self.assertEqual(self.warnings(), ['warn.dlss_download_failed']) + self.assertFalse(steps.Dlss().current(self.ctx, rec)) + + def test_a_cut_connection_warns(self): + class Cut(io.BytesIO): # a response whose connection drops mid-file + def read(self, n=-1): + raise http.client.IncompleteRead(b'dl', 3998) + + with mock.patch.object(steps.urllib.request, 'urlopen', return_value=Cut()): + self.assertEqual(steps.Dlss().run(self.ctx), {'failed': True}) + self.assertFalse((self.dest / 'nvngx_dlss.dll').exists()) + self.assertFalse(list(self.ctx.temp.iterdir())) + self.assertEqual(self.warnings(), ['warn.dlss_download_failed']) + + def test_keeps_the_players_library(self): + (self.dest / 'nvngx_dlss.dll').write_bytes(b'newer') + self.assertEqual(steps.Dlss().run(self.ctx), {'kept': True}) + self.assertEqual((self.dest / 'nvngx_dlss.dll').read_bytes(), b'newer') + self.assertEqual(self.warnings(), ['warn.dlss_kept']) + + def test_skipped_without_an_rtx_gpu(self): + self.ctx.host = {'devices': [{'name': 'AMD Radeon RX 7800 XT', 'api': [1, 3, 0], 'type': 2}]} + self.assertEqual(steps.Dlss().run(self.ctx), {'skipped': 'no_rtx'}) + self.assertEqual(self.warnings(), []) + + if __name__ == '__main__': unittest.main() diff --git a/tests/mods_romfs_check.py b/tests/mods_romfs_check.py new file mode 100644 index 00000000..c3eb5b1b --- /dev/null +++ b/tests/mods_romfs_check.py @@ -0,0 +1,178 @@ +"""The mods folder served as RomFS overlays (runtime/romfs.c, runtime/mods.c): load order, added files, case, Unicode. + + .venv\\Scripts\\python tests\\mods_romfs_check.py --exe build\\v121-<tag>\\totk.exe --version v121 + +Builds a mods folder in a temp directory with files of its own bytes at real RomFS paths (from +build/extracted/<version>/romfs/index.json), runs `totk.exe --probe-fs` on it and reads the `mods:` lines of the log. +--probe-fs compares the bytes it reads through the RomFS with every overlay file, so a wrong winner fails it too. +A second folder checks the merged resource size table the start writes to the profile (runtime/romfs.c +restbl_overlay): a grown file's entry, an added file's, a shipped table's and another version's table's. +Needs the extracted RomFS of the version. +""" +import argparse +import json +import os +import struct +import subprocess +import sys +import tempfile +import zlib +from pathlib import Path + +import zstandard + +ROOT = Path(__file__).resolve().parents[1] +UNI = 'Ж мод' + + +def pick_paths(version): + index = json.loads((ROOT / 'build/extracted' / version / 'romfs/index.json').read_text()) + paths = [f['path'] for f in index['files'] if 1024 <= f['size'] <= 65536] + have = set(f['path'] for f in index['files']) + p1 = next(p for p in paths if any(c.isalpha() for c in p.rsplit('/', 1)[-1])) + p2 = next(p for p in paths if p != p1) + i = next(k for k, c in enumerate(p1) if c.isalpha() and k > p1.rfind('/')) + p1case = p1[:i] + p1[i].swapcase() + p1[i + 1:] + assert p1case not in have + return p1, p2, p1case + + +def put(root, mod, rel, data, part='romfs', times=64): + f = Path(root) / mod / part / rel + f.parent.mkdir(parents=True, exist_ok=True) + f.write_bytes(data * times) + + +def run(exe, mods, order, disabled=''): + """--probe-fs with a profile of its own beside mods (the merged resource size table is written there).""" + env = dict(os.environ, TOTK_MODS_DIR=str(mods), TOTK_MODS_ORDER=order, TOTK_MODS_DISABLED=disabled) + profile = Path(mods).parent / 'profile' + r = subprocess.run([str(exe), '--profile', str(profile), '--probe-fs'], env=env, capture_output=True, timeout=600) + log = (r.stdout + r.stderr).decode('utf-8', 'replace') + return r.returncode, log + + +# ---- the resource size table (RESTBL: "RESTBL", u32 version, u32 name length, u32 hashed count, u32 named count, +# {u32 crc32, u32 size} sorted, then {char[name length], u32 size}) +def restbl_read(data): + raw = zstandard.ZstdDecompressor().decompress(data) + assert raw[:6] == b'RESTBL' + _, name_len, hashed, named = struct.unpack_from('<4I', raw, 6) + out = dict(struct.unpack_from('<II', raw, 22 + i * 8) for i in range(hashed)) + hashes = [struct.unpack_from('<I', raw, 22 + i * 8)[0] for i in range(hashed)] + assert hashes == sorted(hashes), 'hashed entries out of order' + at = 22 + hashed * 8 + for _ in range(named): + out[raw[at:at + name_len].split(b'\0')[0].decode()] = struct.unpack_from('<I', raw, at + name_len)[0] + at += name_len + 4 + return out, raw + + +def restbl_pack(entries, raw): + """raw (a table) with the sizes of its hashed entries set from entries (crc32 -> size, existing hashes only).""" + out = bytearray(raw) + hashed = struct.unpack_from('<I', raw, 14)[0] + for i in range(hashed): + h = struct.unpack_from('<I', raw, 22 + i * 8)[0] + if h in entries: + struct.pack_into('<I', out, 26 + i * 8, entries[h]) + return zstandard.ZstdCompressor(write_content_size=True).compress(bytes(out)) + + +def key(path): + return zlib.crc32((path[:-3] if path.endswith(('.zs', '.mc')) else path).encode()) + + +def restbl_case(exe, version, check, tmp): + romfs = ROOT / 'build/extracted' / version / 'romfs' + index = json.loads((romfs / 'index.json').read_text()) + files = {f['path']: f for f in index['files']} + table_path = next(p for p in files if p.startswith('System/Resource/ResourceSizeTable.') and + p.endswith('.rsizetable.zs')) + with (romfs / 'romfs.bin').open('rb') as image: + def read(path, n=None): + image.seek(files[path]['offset']) + return image.read(n or files[path]['size']) + game, game_raw = restbl_read(read(table_path)) + packs = [p for p in files if p.startswith('Pack/Actor/') and p.endswith('.pack.zs') and key(p) in game] + grown, shipped_key, old_key, old_other = packs[0], packs[1], packs[2], packs[3] + was = zstandard.get_frame_parameters(read(grown, 18)).content_size + zs = zstandard.ZstdCompressor(write_content_size=True) + mods = Path(tmp) / 'restbl' / 'mods' + put(mods, 'Grow', grown, zs.compress(bytes(was + 100000)), times=1) + put(mods, 'Grow', 'Pack/Actor/ModTest_Added.pack.zs', zs.compress(bytes(50000)), times=1) + put(mods, 'Ships', table_path, restbl_pack({key(shipped_key): game[key(shipped_key)] + 12345}, game_raw), times=1) + put(mods, 'Old', old_key, b'old', times=1) # smaller than the original: only its table raises it + put(mods, 'Old', table_path.replace('.Product.', '.Product.999', 1) if '.Product.' in table_path else + 'System/Resource/ResourceSizeTable.Product.999.Nin_NX_NVN.rsizetable.zs', + restbl_pack({key(old_key): game[key(old_key)] + 7777, key(old_other): game[key(old_other)] + 5555}, + game_raw), times=1) + code, log = run(exe, mods, 'Grow|Ships|Old') + check(code == 0, 'probe-fs passes with the resource size table mods', log) + merged_file = mods.parent / 'profile' / 'mods' / table_path.rsplit('/', 1)[1] + check(merged_file.is_file(), f'the merged table is written to the profile ({merged_file.name})', log) + if not merged_file.is_file(): + return + merged, _ = restbl_read(merged_file.read_bytes()) + g = game[key(grown)] + check(merged[key(grown)] >= g + 100000, f'a file grown by 100000 bytes: entry {g} -> {merged[key(grown)]}') + added = merged.get(key('Pack/Actor/ModTest_Added.pack.zs'), 0) + check(added >= 50000 + 256, f'an added 50000-byte pack gets an entry ({added})') + check(merged[key(shipped_key)] == game[key(shipped_key)] + 12345, 'a shipped table\'s larger entry is merged') + check(merged[key(old_key)] == game[key(old_key)] + 7777, 'another version\'s table raises its own mod\'s file') + check(merged[key(old_other)] == game[key(old_other)], 'another version\'s table leaves other files alone') + lower = [h for h, s in game.items() if merged.get(h, 0) < s] + check(not lower, f'no entry of the game\'s table is lowered ({len(lower)})') + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('--exe', type=Path, required=True) + ap.add_argument('--version', choices=('v121', 'v143'), required=True) + args = ap.parse_args() + p1, p2, p1case = pick_paths(args.version) + fails = 0 + + def check(ok, what, log=None): + nonlocal fails + print(('PASS ' if ok else 'FAIL ') + what) + if not ok: + fails += 1 + if log: + print('\n'.join(l for l in log.splitlines() if 'mods' in l or 'overlay' in l.lower())[-3000:]) + + with tempfile.TemporaryDirectory() as tmp: + mods = Path(tmp) / 'mods' + put(mods, 'Top', p1, b'mod-A') + put(mods, 'Low', p1, b'mod-B') + put(mods, 'Low', p2, b'mod-C') + put(mods, 'Top', 'New/added.bin', b'mod-D') + put(mods, 'Low', 'New/added.bin', b'mod-E') + put(mods, 'Case', p1case, b'mod-F') + put(mods, UNI, p2, b'mod-G') + # a file of another game version's (no RomFS has a Product.999 table): counted, never served + put(mods, 'Low', 'System/Resource/ResourceSizeTable.Product.999.rsizetable.zs', b'mod-H') + put(mods, 'Lite', 'Lite/added.bin', b'mod-I', part='romfslite') # TKMM's romfslite folder + + code, log = run(args.exe, mods, f'Top|Low|{UNI}|Case') + check(code == 0, 'probe-fs passes with five mods (served bytes equal the winners\' files)', log) + for line in ('mods: Top: 2 files served (1 replaced, 1 added), 0 overridden', + 'mods: Low: 1 files served (1 replaced, 0 added), 2 overridden by Top, 1 for another game version', + 'mods: Case: 0 files served (0 replaced, 0 added), 1 overridden by Top', + f'mods: {UNI}: 0 files served (0 replaced, 0 added), 1 overridden by Low', + 'mods: Lite: 1 files served (0 replaced, 1 added), 0 overridden'): + check(line in log, line, log) + check(p1case not in log, 'the case-flipped path is served as the RomFS path, not added', log) + + code, log = run(args.exe, mods, f'Top|Low|{UNI}|Case', disabled='Top') + check(code == 0, 'probe-fs passes with Top switched off', log) + check('mods: Low: 3 files served (2 replaced, 1 added), 0 overridden' in log, 'Low serves P1 and the added file', log) + check('mods: Top: ' not in log.replace('mods: Top: off', ''), 'a switched-off mod serves nothing', log) + + restbl_case(args.exe, args.version, check, tmp) + print('mods_romfs_check:', 'FAIL' if fails else 'ok') + return 1 if fails else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/ncz_test.py b/tests/ncz_test.py new file mode 100644 index 00000000..106f1a0d --- /dev/null +++ b/tests/ncz_test.py @@ -0,0 +1,173 @@ +"""tools/ncz.py and the installer's unpacking of NSZ input (installer/steps.py, Check.unpack) on small made-up files: +an NCA with an AES-CTR section and a plain one, stored as an NCZ with one zstd stream or as NCZBLOCK blocks. + +Run: .venv-py314\\Scripts\\python tests/ncz_test.py +""" +import hashlib +import io +import os +import struct +import sys +import tempfile +import unittest +from compression import zstd +from pathlib import Path +from types import SimpleNamespace + +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT)) +sys.path.insert(0, str(ROOT / 'tools')) +import ncz # noqa: E402 +from installer import steps # noqa: E402 +from installer.events import Reporter # noqa: E402 + +KEY, COUNTER = bytes(range(16)), bytes(range(16, 32)) +# (offset, size, crypto type): an AES-CTR section, then a plain one +SECTIONS = ((0x4000, 0x9000, 3), (0xD000, 0x3200, 1)) + + +def pfs0(files): + """An NSP of (name, data) pairs.""" + strings = b''.join(name.encode() + b'\0' for name, _ in files) + strings += bytes(-(16 + 0x18 * len(files) + len(strings)) % 0x20) + head = b'PFS0' + struct.pack('<III', len(files), len(strings), 0) + offset = name_at = 0 + for name, data in files: + head += struct.pack('<QQII', offset, len(data), name_at, 0) + offset += len(data) + name_at += len(name) + 1 + return head + strings + b''.join(data for _, data in files) + + +def made_up_nca(): + """(the NCA as stored on the console, its body after the header with the sections decrypted).""" + rand = os.urandom + plain = rand(0x2000) + bytes(0x7000) # half random, half compressible + rest = bytes(0x1200) + rand(0x2000) + counter = COUNTER[:8] + struct.pack('>Q', 0x4000 >> 4) + secret = Cipher(algorithms.AES(KEY), modes.CTR(counter)).encryptor().update(plain) + header = rand(0x4000) + return header + secret + rest, plain + rest + + +def ncz_file(nca, body, blocks=False): + out = nca[:0x4000] + b'NCZSECTN' + struct.pack('<Q', len(SECTIONS)) + for offset, size, crypto in SECTIONS: + out += struct.pack('<QQQQ', offset, size, crypto, 0) + KEY + COUNTER + if not blocks: + return out + zstd.compress(body) + exponent = 14 + pieces = [body[i:i + (1 << exponent)] for i in range(0, len(body), 1 << exponent)] + stored = [zstd.compress(p) if p.count(0) > len(p) // 2 else p for p in pieces] # random blocks stay as they are + out += b'NCZBLOCK' + bytes([2, 1, 0, exponent]) + struct.pack('<IQ', len(pieces), len(body)) + out += struct.pack(f'<{len(stored)}I', *map(len, stored)) + return out + b''.join(stored) + + +class UnpackTest(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.dir = Path(self.tmp.name) + self.nca, self.body = made_up_nca() + self.name = hashlib.sha256(self.nca).hexdigest()[:32] + self.tik = b'ticket' * 40 + + def tearDown(self): + self.tmp.cleanup() + + def nsz(self, blocks=False, damage=None): + data = ncz_file(self.nca, self.body, blocks) + if damage: + data = damage(bytearray(data)) + path = self.dir / 'game.nsz' + path.write_bytes(pfs0([(self.name + '.ncz', bytes(data)), ('a.tik', self.tik)])) + return path + + def expected(self): + return pfs0([(self.name + '.nca', self.nca), ('a.tik', self.tik)]) + + def test_stream_comes_back_byte_for_byte(self): + src = self.nsz() + self.assertEqual(ncz.compressed(src)[0], 'nsz') + self.assertEqual(ncz.unpacked_size(src), len(self.expected())) + seen = [] + self.assertEqual(ncz.unpack(src, self.dir / 'out.nsp', lambda d, t: seen.append((d, t))), 'nsz') + self.assertEqual((self.dir / 'out.nsp').read_bytes(), self.expected()) + self.assertEqual(seen[-1], (len(self.expected()), len(self.expected()))) + self.assertFalse((self.dir / 'out.nsp.part').exists()) + + def test_blocks_come_back_byte_for_byte(self): + ncz.unpack(self.nsz(blocks=True), self.dir / 'out.nsp') + self.assertEqual((self.dir / 'out.nsp').read_bytes(), self.expected()) + + def test_plain_nsp_is_not_compressed(self): + path = self.dir / 'plain.nsp' + path.write_bytes(self.expected()) + self.assertIsNone(ncz.compressed(path)) + + def test_changed_byte_fails_the_hash(self): + def flip(data): + data[0x4000 + 16 + 0x40 * len(SECTIONS) + 0x100] ^= 1 # inside a stored (random) block + return data + with self.assertRaises(ncz.DamagedFile) as e: + ncz.unpack(self.nsz(blocks=True, damage=flip), self.dir / 'out.nsp') + self.assertIn('hash mismatch', e.exception.detail) + + def test_cut_stream_is_damaged(self): + with self.assertRaises(ValueError): + ncz.unpack(self.nsz(damage=lambda d: d[:-40]), self.dir / 'out.nsp') + self.assertFalse((self.dir / 'out.nsp').exists()) + + +class CheckUnpackTest(unittest.TestCase): + """Check.unpack: an NSZ base becomes <temp>/unpacked/base.nsp, a plain NSP update is read in place, a second run + reuses the NSP.""" + + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + base = Path(self.tmp.name) + nca, body = made_up_nca() + name = hashlib.sha256(nca).hexdigest()[:32] + self.base, self.update = base / 'base.nsz', base / 'update.nsp' + self.base.write_bytes(pfs0([(name + '.ncz', ncz_file(nca, body))])) + self.update.write_bytes(pfs0([('a.tik', b'x' * 64)])) + self.expected = pfs0([(name + '.nca', nca)]) + self.out = io.StringIO() + self.ctx = SimpleNamespace(options=SimpleNamespace(base=str(self.base), update=str(self.update)), + temp=base / 'temp', reporter=Reporter(out=self.out, log_path=base / 'x.log')) + + def tearDown(self): + self.ctx.reporter.close() + self.tmp.cleanup() + + def test_unpacks_the_base_and_reuses_it(self): + steps.Check().unpack(self.ctx) + target = self.ctx.temp / 'unpacked' / 'base.nsp' + self.assertEqual(target.read_bytes(), self.expected) + self.assertEqual(steps.game_file(self.ctx, 'base'), target) + self.assertEqual(steps.game_file(self.ctx, 'update'), self.update) + self.assertIn('"phase": "unpack_base"', self.out.getvalue()) + mtime = target.stat().st_mtime_ns + steps.Check().unpack(self.ctx) + self.assertEqual(target.stat().st_mtime_ns, mtime) + + def test_content_decides_not_the_name(self): + renamed = self.base.with_name('base.nsp') + self.base.rename(renamed) + self.ctx.options.base = str(renamed) + steps.Check().unpack(self.ctx) + self.assertEqual((self.ctx.temp / 'unpacked' / 'base.nsp').read_bytes(), self.expected) + + def test_damaged_base_names_the_chosen_file(self): + data = bytearray(self.base.read_bytes()) + data[-100] ^= 0xFF + self.base.write_bytes(bytes(data)) + with self.assertRaises(steps.InstallError) as e: + steps.Check().unpack(self.ctx) + self.assertIn(e.exception.id, ('check.input_damaged', 'check.unpack_failed')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/romfs_prompts_test.py b/tests/romfs_prompts_test.py new file mode 100644 index 00000000..dca0d054 --- /dev/null +++ b/tests/romfs_prompts_test.py @@ -0,0 +1,539 @@ +"""tools/make_romfs_patches.py --prompts and tools/button_icons.py on a RomFS made here, with no game files: each prompt +names the label of the Xbox button that the pad layout presses, the shoulder buttons, triggers, Plus, Minus and the +d-pad are drawn as on an Xbox controller, the resource size table grows with the fonts, every other file and member +stays byte for byte, the same input gives the same output, and a RomFS the tool does not recognise fails before it +writes anything. Both game versions: 1.2.1 (Common.Product.110, 72x72 button guide images) and 1.4.3 +(Common.Product.140, 108x108). + +Run: .venv-py314\\Scripts\\python tests\\romfs_prompts_test.py +""" +import compression.zstd as zstd +import hashlib +import importlib.util +import io +import json +import random +import struct +import subprocess +import sys +import tempfile +import unittest +import zlib +from pathlib import Path + +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.recordingPen import RecordingPen +from fontTools.pens.t2CharStringPen import T2CharStringPen +from fontTools.ttLib import TTFont + +ROOT = Path(__file__).resolve().parent.parent +TOOL = ROOT / 'tools' / 'make_romfs_patches.py' +spec = importlib.util.spec_from_file_location('button_icons', ROOT / 'tools' / 'button_icons.py') +icons = importlib.util.module_from_spec(spec) +spec.loader.exec_module(icons) + +# The face button each Switch button is in a pad layout (docs/controls.md, runtime/input_map.c), the label an Xbox pad +# prints on it, and the layout each overlay is made for (runtime/input_map.c input_prompts_folder). +POSITION = {'nintendo': {'A': 'east', 'B': 'south', 'X': 'north', 'Y': 'west'}, + 'xbox': {'A': 'south', 'B': 'east', 'X': 'west', 'Y': 'north'}} +XBOX_LABEL = {'south': 'A', 'east': 'B', 'west': 'X', 'north': 'Y'} +STYLE_LAYOUT = {'xbox': 'nintendo', 'xbox-label': 'xbox'} +FACE = {'A': 0xE040, 'B': 0xE041, 'X': 0xE042, 'Y': 0xE043} +# The glyphs drawn as Xbox icons, with their advance: LB RB LT RT, Menu and View, the d-pad. +DRAWN = {0xE044: 820, 0xE045: 820, 0xE046: 760, 0xE047: 760, 0xE04B: 640, 0xE04C: 640, 0xE049: 640, + **{cp: 640 for cp in range(0xE060, 0xE066)}} + +# restbl: the resource size table's name after its version, in both forms the tool takes. +VERSIONS = {'v121': dict(common='110', size=72, newest=121, restbl='Nin_NX_NVN.rsizetable.zs'), + 'v143': dict(common='140', size=108, newest=143, restbl='rsizetable.zs')} +DEFAULT_FONT, REGIONAL_FONT = 'Font/Font.Nin_NX_NVN.bfarc.zs', 'Font/Font_KRko.Nin_NX_NVN.bfarc.zs' +PANES = {'T_A_00': 'A', 'T_B_00': 'B', 'T_X_00': 'X', 'T_Y_00': 'Y', 'T_Name_00': 'A'} +MESSAGES = {**PANES, 'Greeting': 'Hello'} +R8_UNORM, BC1 = 0x201, 0x1a01 +IMAGE = 16384 # an R8 square up to 128 rows, two GOBs wide, block height 16: the size of the game's key images + + +# ---- the formats, written here from their layout so the tools' readers are checked against an independent writer +def sarc(members, align=0x80): + names, sfnt, name_at = list(members), b'', {} + for name in names: + name_at[name] = len(sfnt) // 4 + sfnt += name.encode() + b'\0' + sfnt += bytes(-len(sfnt) % 4) + head = 0x14 + 12 + 16 * len(names) + 8 + len(sfnt) + start = head + -head % align + data, nodes = b'', b'' + for name in names: + data += bytes(-len(data) % align) + nodes += struct.pack('<4I', zlib.crc32(name.encode()), 0x01000000 | name_at[name], len(data), len(data) + len(members[name])) + data += members[name] + out = (b'SARC' + struct.pack('<HHIIHH', 0x14, 0xFEFF, start + len(data), start, 0x100, 0) + + b'SFAT' + struct.pack('<HHI', 12, len(names), 0x65) + nodes + b'SFNT' + struct.pack('<HH', 8, 0) + sfnt) + return out + bytes(start - len(out)) + data + + +def sarc_files(data): + header, start = struct.unpack_from('<H', data, 4)[0], struct.unpack_from('<I', data, 12)[0] + count, out = struct.unpack_from('<H', data, header + 6)[0], {} + names = header + 12 + count * 16 + 8 + for i in range(count): + _, attribute, lo, hi = struct.unpack_from('<4I', data, header + 12 + i * 16) + at = names + (attribute & 0xffffff) * 4 + out[data[at:data.index(0, at)].decode()] = data[start + lo:start + hi] + return out + + +def bflan(panes): + """A FLAN whose pai1 section steps each pane's visibility from 0 at frame 0 to 1 at frame 1.""" + entries = [name.encode().ljust(28, b'\0') + bytes([1, 0, 0, 0]) + struct.pack('<I', 36) + + b'FLVI' + bytes([1, 0, 0, 0]) + struct.pack('<I', 12) + + bytes([0, 0, 1, 0]) + struct.pack('<HHI', 2, 0, 12) + + struct.pack('<fHxx', 0.0, 0) + struct.pack('<fHxx', 1.0, 1) for name in panes] + table, body = 20, b'' + offsets = [] + for e in entries: + offsets.append(table + 4 * len(entries) + len(body)) + body += e + section = struct.pack('<HBxHHI', 0, 0, 0, len(entries), table) + struct.pack(f'<{len(entries)}I', *offsets) + body + section = b'pai1' + struct.pack('<I', 8 + len(section)) + section + return b'FLAN' + struct.pack('<HHIIHH', 0xFEFF, 0x14, 0x08000000, 0x14 + len(section), 1, 0) + section + + +def visibility(data): + """pane -> [(frame, value)] of a FLAN made by bflan().""" + pos, out = 0x14, {} + count, table = struct.unpack_from('<H', data, pos + 14)[0], pos + struct.unpack_from('<I', data, pos + 16)[0] + for e in range(count): + eo = pos + struct.unpack_from('<I', data, table + 4 * e)[0] + keys = eo + 36 + 12 + 12 + out[data[eo:eo + 28].split(b'\0')[0].decode()] = [struct.unpack_from('<fH', data, keys + 8 * k) for k in range(2)] + return out + + +def bntx(textures): + """A BNTX of single-mip square textures: name -> (format, size, image).""" + out = bytearray(0x30) + out[0:4], out[0x20:0x24] = b'BNTX', b'NX ' + table = len(out) + out += bytes(8 * len(textures)) + struct.pack_into('<IQ', out, 0x24, len(textures), table) + for k, (name, (fmt, size, image)) in enumerate(textures.items()): + info = len(out) + out += bytes(0x80) + out[info:info + 4] = b'BRTI' + struct.pack_into('<Q', out, table + 8 * k, info) + struct.pack_into('<H', out, info + 0x16, 1) + struct.pack_into('<I', out, info + 0x1c, fmt) + struct.pack_into('<II', out, info + 0x24, size, size) + struct.pack_into('<I', out, info + 0x34, 4) # block height 1 << 4 GOBs + struct.pack_into('<I', out, info + 0x50, len(image)) + struct.pack_into('<Q', out, info + 0x60, len(out)) + out += struct.pack('<H', len(name)) + name.encode() + b'\0' + out += bytes(-len(out) % 8) + mips = len(out) + struct.pack_into('<Q', out, info + 0x70, mips) + out += bytes(8) + out += bytes(-len(out) % 0x200) + struct.pack_into('<Q', out, mips, len(out)) + out += image + return bytes(out) + + +def bntx_images(data): + """name without the ^ suffix -> (format, size, image) of a BNTX made by bntx().""" + count, table = struct.unpack_from('<IQ', data, 0x24) + out = {} + for k in range(count): + info = struct.unpack_from('<Q', data, table + 8 * k)[0] + at = struct.unpack_from('<Q', data, info + 0x60)[0] + name = data[at + 2:at + 2 + struct.unpack_from('<H', data, at)[0]].decode().split('^')[0] + start = struct.unpack_from('<Q', data, struct.unpack_from('<Q', data, info + 0x70)[0])[0] + out[name] = (struct.unpack_from('<I', data, info + 0x1c)[0], struct.unpack_from('<I', data, info + 0x24)[0], + data[start:start + struct.unpack_from('<I', data, info + 0x50)[0]]) + return out + + +def bflyt(panes): + """A FLYT with one txt1 section per pane (name -> text) after a pan1 section.""" + sections = b'pan1' + struct.pack('<I', 0x4c) + bytes(0x44) + for name, text in panes.items(): + s = bytearray(0x80) + s[0:4] = b'txt1' + struct.pack_into('<I', s, 4, len(s)) + s[12:12 + len(name)] = name.encode() + struct.pack_into('<H', s, 0x56, 2 * len(text) + 2) + struct.pack_into('<I', s, 0x64, 0x70) + s[0x70:0x70 + 2 * len(text) + 2] = text.encode('utf-16-le') + b'\0\0' + sections += s + return b'FLYT' + struct.pack('<HHIIHH', 0xFEFF, 0x14, 0x08000000, 0x14 + len(sections), len(panes) + 1, 0) + sections + + +def pane_texts(data): + pos, out = struct.unpack_from('<H', data, 6)[0], {} + while pos + 8 <= len(data): + magic, size = data[pos:pos + 4], struct.unpack_from('<I', data, pos + 4)[0] + if magic == b'txt1': + length, at = struct.unpack_from('<H', data, pos + 0x56)[0], pos + struct.unpack_from('<I', data, pos + 0x64)[0] + out[data[pos + 12:pos + 36].split(b'\0')[0].decode()] = data[at:at + length - 2].decode('utf-16-le') + pos += size + return out + + +def msbt(messages): + """A UTF-16 MSBT: one LBL1 group with every label, then TXT2 (label -> text).""" + labels = b''.join(bytes([len(name)]) + name.encode() + struct.pack('<I', i) for i, name in enumerate(messages)) + lbl1 = struct.pack('<III', 1, len(messages), 12) + labels + texts = [text.encode('utf-16-le') + b'\0\0' for text in messages.values()] + offsets, body = [], b'' + for text in texts: + offsets.append(4 + 4 * len(texts) + len(body)) + body += text + txt2 = struct.pack(f'<I{len(texts)}I', len(texts), *offsets) + body + + def section(magic, content): + s = magic + struct.pack('<I', len(content)) + bytes(8) + content + return s + bytes(-len(s) % 16) + sections = section(b'LBL1', lbl1) + section(b'TXT2', txt2) + return b'MsgStdBn' + struct.pack('<HHBBHHI', 0xFEFF, 0, 1, 3, 2, 0, 0x20 + len(sections)) + bytes(10) + sections + + +def message_texts(data): + sections, pos = {}, 0x20 + for _ in range(struct.unpack_from('<H', data, 0x0e)[0]): + size = struct.unpack_from('<I', data, pos + 4)[0] + sections[data[pos:pos + 4]] = (pos + 16, size) + pos = (pos + 16 + size + 15) & ~15 + (base, _), labels = sections[b'LBL1'], {} + for g in range(struct.unpack_from('<I', data, base)[0]): + count, at = struct.unpack_from('<II', data, base + 4 + 8 * g) + at += base + for _ in range(count): + n = data[at] + labels[struct.unpack_from('<I', data, at + 1 + n)[0]] = data[at + 1:at + 1 + n].decode() + at += 1 + n + 4 + base, size = sections[b'TXT2'] + count = struct.unpack_from('<I', data, base)[0] + offsets = list(struct.unpack_from(f'<{count}I', data, base + 4)) + [size] + return {labels[i]: data[base + offsets[i]:base + offsets[i + 1]].decode('utf-16-le').rstrip('\0') for i in range(count)} + + +def restbl(entries): + """A RESTBL keyed by the CRC32 of each path without .zs (path -> size).""" + crcs = sorted((zlib.crc32(path.removesuffix('.zs').encode()), size) for path, size in entries.items()) + return b'RESTBL' + struct.pack('<IIII', 1, 160, len(crcs), 0) + b''.join(struct.pack('<II', *c) for c in crcs) + + +def restbl_sizes(table): + count = struct.unpack_from('<I', table, 14)[0] + return dict(struct.unpack_from('<II', table, 22 + 8 * i) for i in range(count)) + + +MAGIC = 0x18029a7f # MacNativeGames make_button_icons.py: a .bfotf's first word is its key XOR this + + +def bfotf(otf, key): + """A .bfotf as MacNativeGames make_button_icons.py bfotf_encode writes it: 32-bit words XORed with the key.""" + size = len(otf) + out = struct.pack('<II', MAGIC ^ key, struct.unpack('<I', size.to_bytes(4, 'big'))[0] ^ key) + out += b''.join(struct.pack('<I', struct.unpack_from('<I', otf, i)[0] ^ key) for i in range(0, size - size % 4, 4)) + return out + otf[size - size % 4:] + + +def unbfotf(blob): + key = struct.unpack_from('<I', blob, 0)[0] ^ MAGIC + size = int.from_bytes(struct.pack('<I', struct.unpack_from('<I', blob, 4)[0] ^ key), 'big') + words = b''.join(struct.pack('<I', struct.unpack_from('<I', blob, 8 + i)[0] ^ key) for i in range(0, size - size % 4, 4)) + return words + blob[8 + size - size % 4:8 + size] + + +def rect(pen, x0, y0, x1, y1, hole=False): + """A rectangle, counter-clockwise (filled, the CFF way) or clockwise (a counter).""" + points = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] + points = points[::-1] if hole else points + pen.moveTo(points[0]) + for p in points[1:]: + pen.lineTo(p) + pen.closePath() + + +def polygon(*points): + def draw(pen): + pen.moveTo(points[0]) + for p in points[1:]: + pen.lineTo(p) + pen.closePath() + return draw + + +def cff_font(name, glyphs): + """A CFF OpenType font with a 1000-unit em: code point -> (advance, draw(pen)).""" + names = {cp: f'uni{cp:04X}' for cp in glyphs} + fb = FontBuilder(1000, isTTF=False) + fb.setupGlyphOrder(['.notdef'] + list(names.values())) + fb.setupCharacterMap(names) + charstrings, metrics = {}, {} + for gname, (advance, draw) in [('.notdef', (500, lambda pen: rect(pen, 50, 0, 450, 700)))] + \ + [(names[cp], glyphs[cp]) for cp in glyphs]: + pen = T2CharStringPen(advance, None) + draw(pen) + charstrings[gname], metrics[gname] = pen.getCharString(), (advance, 0) + fb.setupCFF(name, {'FullName': name}, charstrings, {}) + fb.setupHorizontalMetrics(metrics) + fb.setupHorizontalHeader(ascent=800, descent=-200) + fb.setupNameTable({'familyName': name, 'styleName': 'Regular'}) + fb.setupOS2(sTypoAscender=800, usWinAscent=800, usWinDescent=200) + fb.setupPost() + out = io.BytesIO() + fb.save(out) + return out.getvalue() + + +# A distinct rectangle and advance for every button glyph, so a glyph that moved is found by its outline. +BUTTONS = {cp: (600 + 4 * (cp - 0xE040), lambda pen, i=cp - 0xE040: rect(pen, 20 + 5 * i, 20, 300 + 5 * i, 400 + 3 * i)) + for cp in range(0xE040, 0xE069)} +LETTERS_1 = {ord('L'): (560, polygon((60, 0), (480, 0), (480, 100), (160, 100), (160, 700), (60, 700))), + ord('B'): (560, lambda pen: [rect(pen, 60, 0, 460, 700), rect(pen, 160, 100, 360, 300, True), + rect(pen, 160, 400, 360, 600, True)]), + ord('R'): (560, lambda pen: [rect(pen, 60, 0, 460, 700), rect(pen, 160, 400, 360, 600, True)])} +LETTERS_2 = {ord('T'): (560, polygon((230, 0), (330, 0), (330, 600), (520, 600), (520, 700), (40, 700), (40, 600), (230, 600)))} + + +class Romfs: + """A RomFS (romfs.bin, index.json) holding the files make_romfs_patches.py reads, the originals kept to compare.""" + + def __init__(self, folder, version, text_fonts=True, restbl_skips=(), l_format=R8_UNORM): + v, rnd = VERSIONS[version], random.Random(version) + words = [b'Layout', b'Pane', b'Font', b'Msg', b'Texture', b'Anim', b'Group', b'Text', b'Base', b'Key'] + samples = [b' '.join(rnd.choice(words) + str(rnd.randrange(999)).encode() for _ in range(rnd.randrange(20, 80))) + for _ in range(400)] + self.dictionary = zstd.train_dict(samples, 4096) + self.common = f'UI/LayoutArchive/Common.Product.{v["common"]}.Nin_NX_NVN.blarc.zs' + self.mals = [f'Mals/EUen.Product.{v["newest"]}.sarc.zs', f'Mals/USen.Product.{v["newest"]}.sarc.zs'] + self.old_mals = 'Mals/USen.Product.110.sarc.zs' + self.restbl = f'System/Resource/ResourceSizeTable.Product.{v["newest"]}.{v["restbl"]}' + self.old_restbl = f'System/Resource/ResourceSizeTable.Product.100.{v["restbl"]}' # an older version's: left alone + size = v['size'] + images = {f'Nt_KeyTex{b}_00': (R8_UNORM, size, rnd.randbytes(IMAGE)) for b in 'ABXY'} + images.update({'Nt_KeyTexL_00^z': (l_format, size, rnd.randbytes(IMAGE)), + 'Nt_KeyTexZL_00': (R8_UNORM, size, rnd.randbytes(IMAGE)), + 'Nt_KeyTexLSt_00': (R8_UNORM, size, rnd.randbytes(IMAGE))}) + self.text_fonts = [bfotf(cff_font('Text1', LETTERS_1), 0x01020304), bfotf(cff_font('Text2', LETTERS_2), 0x0a0b0c0d)] + default = {'scft/External.bfotf': bfotf(cff_font('External', BUTTONS), 0x5a17c3e9), 'fnt/rest.bin': rnd.randbytes(700)} + if text_fonts: + default.update(zip(icons.TEXT_FONTS, self.text_fonts)) + mals = sarc({'LayoutMsg/DoCommand_00.msbt': msbt(MESSAGES), 'LayoutMsg/Other_00.msbt': msbt({'T_A_00': 'A'})}) + self.contents = { # RomFS path -> contents before compression + self.common: sarc({'anim/ShortCutBG_00_In.bflan': bflan(('C_CaptureBG_00', 'C_CaptureClearBG_00', + 'P_CaptureClearBG_00', 'N_Other_00')), + 'anim/AutoBuilder_00_In.bflan': bflan(('C_CaptureBG_00',)), + 'blyt/DoCommand_00.bflyt': bflyt(PANES), + 'blyt/Title_00.bflyt': bflyt({'N_Title_00': 'Z'}), + 'timg/__Combined.bntx': bntx(images)}), + DEFAULT_FONT: sarc(default), + # The same font under another key: drawn once, stored under each archive's own key. + REGIONAL_FONT: sarc({'scft/External.bfotf': bfotf(cff_font('External', BUTTONS), 0x77777777), + 'fnt/rest.bin': rnd.randbytes(500)}), + **{path: mals for path in self.mals + [self.old_mals]}, + } + sizes = {path: len(data) + 256 for path, data in self.contents.items() if path not in restbl_skips} + self.contents[self.restbl] = restbl({**sizes, **{f'Other/{i}.bin': 1000 + i for i in range(20)}}) + self.contents[self.old_restbl] = restbl({DEFAULT_FONT: 1}) + plain = (REGIONAL_FONT, self.restbl, self.old_restbl) # stored without a dictionary + self.files = {'Pack/ZsDic.pack.zs': zstd.compress(sarc({'zs.zsdic': self.dictionary.dict_content}))} + self.files.update({path: zstd.compress(data, zstd_dict=None if path in plain else self.dictionary) + for path, data in self.contents.items()}) + self.files['Other/file.bin'] = rnd.randbytes(100) + image, entries = bytearray(), [] + for path, data in self.files.items(): + image += bytes(-len(image) % 16) + entries.append(dict(path=path, offset=len(image), size=len(data))) + image += data + folder.mkdir(parents=True) + (folder / 'romfs.bin').write_bytes(image) + (folder / 'index.json').write_text(json.dumps(dict(status='pass', files=entries))) + self.folder, self.version = folder, version + + def run(self, out, *flags): + return subprocess.run([sys.executable, '-I', str(TOOL), '--version', self.version, '--romfs', str(self.folder), + '--out', str(out), *flags], capture_output=True, text=True, encoding='utf-8') + + def read(self, out, path): + """The overlay's file, decompressed.""" + raw = (out / path).read_bytes() + return zstd.decompress(raw, zstd_dict=self.dictionary if zstd.get_frame_info(raw).dictionary_id else None) + + +def font(blob): + return TTFont(io.BytesIO(unbfotf(blob))) + + +def glyph(f, cp): + """(outline, advance) of the glyph of code point cp.""" + name = f.getBestCmap()[cp] + pen = RecordingPen() + f.getGlyphSet()[name].draw(pen) + return pen.value, f['hmtx'][name][0] + + +def manifest(out): + return {line.split('\t')[0]: line.split('\t')[1:] for line in (out / 'manifest.tsv').read_text().splitlines() + if not line.startswith('#')} + + +class PromptsTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory(prefix='totk-prompts-') + cls.base = Path(cls.tmp.name) + cls.romfs = {v: Romfs(cls.base / v / 'romfs', v) for v in VERSIONS} + cls.outs = {} + for v, romfs in cls.romfs.items(): + for style in ('plain', 'xbox', 'xbox-label'): + out = cls.base / v / style + proc = romfs.run(out, *(() if style == 'plain' else ('--prompts', style))) + assert proc.returncode == 0, proc.stderr + cls.outs[v, style] = (out, json.loads(proc.stdout.strip().splitlines()[-1])) + + @classmethod + def tearDownClass(cls): + cls.tmp.cleanup() + + def each(self, styles=('xbox', 'xbox-label')): + """(RomFS, style, overlay folder, summary) of every run of these styles.""" + return [(self.romfs[v], style, out, summary) for (v, style), (out, summary) in self.outs.items() + if style in styles] + + def test_plain_overlay_has_only_the_frame_rate_patch(self): + for romfs, _, out, summary in self.each(('plain',)): + with self.subTest(version=romfs.version, style="plain"): + self.assertEqual(set(manifest(out)), {romfs.common}) + self.assertIsNone(summary['prompts']) + keys = visibility(sarc_files(romfs.read(out, romfs.common))['anim/ShortCutBG_00_In.bflan']) + self.assertEqual(keys['C_CaptureBG_00'], [(0.0, 0), (0.0, 1)]) + self.assertEqual(keys['N_Other_00'], [(0.0, 0), (1.0, 1)]) + + def test_face_buttons_name_the_xbox_label_of_the_button_pressed(self): + for romfs, style, out, _ in self.each(): + with self.subTest(version=romfs.version, style=style): + layout = STYLE_LAYOUT[style] + label = {b: XBOX_LABEL[POSITION[layout][b]] for b in FACE} # Switch button -> the label of its button + before, after = sarc_files(romfs.contents[romfs.common]), sarc_files(romfs.read(out, romfs.common)) + old, new = bntx_images(before['timg/__Combined.bntx']), bntx_images(after['timg/__Combined.bntx']) + panes, messages = pane_texts(after['blyt/DoCommand_00.bflyt']), {} + for path in romfs.mals if layout == 'nintendo' else []: + messages[path] = message_texts(sarc_files(romfs.read(out, path))['LayoutMsg/DoCommand_00.msbt']) + original = font(sarc_files(romfs.contents[DEFAULT_FONT])['scft/External.bfotf']) + for path in (DEFAULT_FONT, REGIONAL_FONT): + drawn = font(sarc_files(romfs.read(out, path))['scft/External.bfotf']) + for b, cp in FACE.items(): + self.assertEqual(glyph(drawn, cp), glyph(original, FACE[label[b]]), f'{path}: glyph of {b}') + for b in FACE: + self.assertEqual(new[f'Nt_KeyTex{b}_00'], old[f'Nt_KeyTex{label[b]}_00'], f'action prompt image of {b}') + self.assertEqual(panes[f'T_{b}_00'], label[b], f'diamond letter of {b}') + for path, texts in messages.items(): + self.assertEqual(texts[f'T_{b}_00'], label[b], f'{path}: diamond message of {b}') + # Text that only looks like a letter pane, and every other message, stays. + self.assertEqual((panes['T_Name_00'], pane_texts(after['blyt/Title_00.bflyt'])), ('A', {'N_Title_00': 'Z'})) + for path, texts in messages.items(): + self.assertEqual((texts['T_Name_00'], texts['Greeting']), ('A', 'Hello')) + self.assertEqual(sarc_files(romfs.read(out, path))['LayoutMsg/Other_00.msbt'], msbt({'T_A_00': 'A'})) + + def test_other_buttons_are_drawn_as_xbox_icons(self): + for romfs, style, out, _ in self.each(): + with self.subTest(version=romfs.version, style=style): + original = font(sarc_files(romfs.contents[DEFAULT_FONT])['scft/External.bfotf']) + default = sarc_files(romfs.read(out, DEFAULT_FONT)) + drawn = font(default['scft/External.bfotf']) + for cp in range(0xE040, 0xE069): + outline, advance = glyph(drawn, cp) + if cp in DRAWN: + self.assertNotEqual(outline, glyph(original, cp)[0], f'U+{cp:04X} drawn') + self.assertEqual(advance, DRAWN[cp], f'U+{cp:04X} advance') + elif cp not in FACE.values(): + self.assertEqual((outline, advance), glyph(original, cp), f'U+{cp:04X} kept') + # The letters are holes in the key: LB is the key, L, and B with its two counters. + moves = sum(op == 'moveTo' for op, _ in glyph(drawn, 0xE044)[0]) + self.assertEqual(moves, 5) + self.assertEqual(sum(op == 'moveTo' for op, _ in glyph(drawn, 0xE047)[0]), 1 + 1 + 1 + 1) # RT: key, R + counter, T + self.assertEqual(sum(op == 'moveTo' for op, _ in glyph(drawn, 0xE060)[0]), 2) # d-pad up: cross, arrow + # The text fonts and the archives' other members stay byte for byte; both archives hold the same font. + before = sarc_files(romfs.contents[DEFAULT_FONT]) + self.assertEqual({k: v for k, v in default.items() if k != 'scft/External.bfotf'}, + {k: v for k, v in before.items() if k != 'scft/External.bfotf'}) + regional = sarc_files(romfs.read(out, REGIONAL_FONT)) + self.assertEqual(regional['fnt/rest.bin'], sarc_files(romfs.contents[REGIONAL_FONT])['fnt/rest.bin']) + self.assertEqual(unbfotf(regional['scft/External.bfotf']), unbfotf(default['scft/External.bfotf'])) + self.assertEqual(struct.unpack_from('<I', regional['scft/External.bfotf'])[0] ^ MAGIC, 0x77777777) + + def test_button_guide_images_show_the_same_icons(self): + for romfs, style, out, _ in self.each(): + with self.subTest(version=romfs.version, style=style): + size = VERSIONS[romfs.version]['size'] + before = sarc_files(romfs.contents[romfs.common]) + after = sarc_files(romfs.read(out, romfs.common)) + self.assertEqual(len(after['timg/__Combined.bntx']), len(before['timg/__Combined.bntx'])) + old, new = bntx_images(before['timg/__Combined.bntx']), bntx_images(after['timg/__Combined.bntx']) + glyphs = icons.xbox_icons(icons.text_glyphs(romfs.text_fonts)) + for name, cp in icons.HUD_TEXTURES.items(): + fmt, width, image = new[name] + pixels = bytes(image[icons.gob_offset(x, y, (size + 63) // 64, 16)] for y in range(size) for x in range(size)) + self.assertEqual(pixels, icons.rasterize(glyphs[cp][1], size), name) + self.assertEqual({pixels[0], pixels[-1], max(pixels)}, {0, 255}, name) # a margin around a white key + self.assertEqual(new['Nt_KeyTexLSt_00'], old['Nt_KeyTexLSt_00']) + for member in before: # the 60 fps patch and the prompts touch only these + if member not in ('timg/__Combined.bntx', 'blyt/DoCommand_00.bflyt') and not member.startswith('anim/'): + self.assertEqual(after[member], before[member], member) + + def test_resource_size_table_grows_with_the_fonts(self): + for romfs, style, out, summary in self.each(): + with self.subTest(version=romfs.version, style=style): + table = restbl_sizes(romfs.read(out, romfs.restbl)) + original = restbl_sizes(romfs.contents[romfs.restbl]) + grown = {} + for path in manifest(out): + if path != romfs.restbl: + grown[path] = len(romfs.read(out, path)) - len(romfs.contents[path]) + self.assertTrue(grown[DEFAULT_FONT] > 0 and grown[REGIONAL_FONT] > 0, grown) + self.assertEqual(summary['prompts']['grown'], {p: g for p, g in grown.items() if g > 0}) + for crc, entry in original.items(): + paths = [p for p in grown if zlib.crc32(p.removesuffix('.zs').encode()) == crc and grown[p] > 0] + self.assertEqual(table[crc], entry + (grown[paths[0]] if paths else 0)) + self.assertEqual(set(table), set(original)) + + def test_manifest_lists_what_changed_with_both_hashes(self): + for romfs, style, out, _ in self.each(('plain', 'xbox', 'xbox-label')): + with self.subTest(version=romfs.version, style=style): + expect = {romfs.common} + if style != 'plain': + expect |= {DEFAULT_FONT, REGIONAL_FONT, romfs.restbl} | (set(romfs.mals) if style == 'xbox' else set()) + lines = manifest(out) + self.assertEqual(set(lines), expect) + for path, (size, sha, original) in lines.items(): + data = (out / path).read_bytes() + self.assertEqual((int(size), sha), (len(data), hashlib.sha256(data).hexdigest()), path) + self.assertEqual(original, hashlib.sha256(romfs.files[path]).hexdigest(), path) + + def test_same_input_same_output_and_the_older_flag(self): + romfs = self.romfs['v121'] + for flags in (('--prompts', 'xbox'), ('--xbox-prompts',)): + out = self.base / 'again' / '-'.join(flags) + proc = romfs.run(out, *flags) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual((out / 'manifest.tsv').read_bytes(), (self.outs['v121', 'xbox'][0] / 'manifest.tsv').read_bytes()) + + def test_unrecognised_romfs_fails_before_writing(self): + cases = {'no text fonts': (dict(text_fonts=False), icons.TEXT_FONTS[0]), + 'no size entry': (dict(restbl_skips=(REGIONAL_FONT,)), 'has no entry in the resource size table'), + 'compressed L image': (dict(l_format=BC1), 'Nt_KeyTexL_00: format')} + for i, (case, (options, message)) in enumerate(cases.items()): + with self.subTest(case): + romfs = Romfs(self.base / 'bad' / str(i) / 'romfs', 'v121', **options) + out = self.base / 'bad' / str(i) / 'out' + proc = romfs.run(out, '--prompts', 'xbox-label') + self.assertNotEqual(proc.returncode, 0) + self.assertIn(message, proc.stderr) + self.assertFalse(out.exists(), 'a failed run writes nothing') + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/build_game.py b/tools/build_game.py index 65a2062f..4b9b0419 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -17,9 +17,11 @@ first (rpath $ORIGIN). docs/building.md lists the packages. Every runtime/*.c is compiled. build/<version>/modules.c carries the layout, the linked function tables, the expected SHA-256 of each ExeFS file (tools/data/expected-<display version>.json) and the default game paths -(--exefs, --romfs, --profile), which totk.exe uses unless its own command line names others. tools/romfs_index.py -writes build/<version>/romfs-index.bin and romfs_config.h from the RomFS directory's index.json (runtime/romfs.c reads -the index next to totk.exe). +(--exefs, --romfs, --profile), which totk.exe uses unless its own command line names others. --exefs and --romfs +default to extracted/<version> in the work folder: the [work] dir of config.local.toml, where tools/extract.py writes +them, else build/. tools/romfs_index.py writes build/<version>/romfs-index.bin and romfs_config.h from the RomFS +directory's index.json (runtime/romfs.c reads the index next to totk.exe). Without that index.json the build stops: +the exe would stop at start. --no-romfs builds it anyway. Private builds reuse the main build's objects. When --out differs from build/<version>, the main build's generated objects are up to date (checked with a dry run of its build.ninja) and every header they include from runtime/ is @@ -66,6 +68,7 @@ import time import urllib.request from pathlib import Path +import check_isa import romfs_index from build_shaders import stale as stale_shaders from check_content import GENERATED_MARKER @@ -92,16 +95,27 @@ NINJA = UCRT / 'ninja.exe' if WIN else on_path('ninja') SDL3_DLLS = [UCRT / 'SDL3.dll', UCRT / 'libiconv-2.dll', UCRT / 'libopus-0.dll'] if WIN else [] # Flags for generated code as LAS Recomp builds it (Makefile GENFLAGS, Windows), plus -march for the FP/SIMD lowering. +# --cpu v2 replaces the level for CPUs without AVX2/FMA; runtime/fma.c then supplies the correctly rounded fma the +# lowering needs (runtime/simd.h). GEN_FLAGS = ['-O1', '-fno-strict-aliasing', '-Wno-return-type', '-Wno-unused-label', '-fvisibility=hidden', '-march=x86-64-v3'] RT_FLAGS = ['-O2', '-g', '-Wall', '-fno-strict-aliasing', '-march=x86-64-v3'] + + +def set_cpu(cpu): + """Target x86-64-v3 (the default) or x86-64-v2. RT_CPU_LEVEL lets runtime/main.c's self-test check the same + instruction set the build was compiled for.""" + march = f'-march=x86-64-{cpu}' + GEN_FLAGS[GEN_FLAGS.index('-march=x86-64-v3')] = march + RT_FLAGS[RT_FLAGS.index('-march=x86-64-v3')] = march + RT_FLAGS.append(f'-DRT_CPU_LEVEL={3 if cpu == "v3" else 2}') if WIN: # _WIN32_WINNT: Windows 10, the port's minimum. MSYS2's headers default to it, llvm-mingw's (the release toolchain) # to Windows 7, which hides the per-monitor DPI functions gfx_window_win.c and dpi_probe_win.c call. RT_FLAGS.append('-D_WIN32_WINNT=0x0A00') # -Xlinker passes the map path as one argument; -Wl, would split it at any comma in the build dir path LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '-Xlinker', '"--Map=$builddir/totk.map"'] # the map feeds tools/prof_report.py - LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx static (movies); libzstd static (resolution.c) + LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx static (movies); libzstd static (resolution.c) VMA_INCLUDE = UCRT.parent / 'include' / 'vma' # vk_mem_alloc.h; MSYS2 package vulkan-memory-allocator else: # -no-pie: the lifted modules' function tables (hundreds of thousands of pointers) need no load-time relocation. @@ -137,7 +151,7 @@ def vma_include(): # --toolchain: an llvm-mingw tree. The libraries, headers and DLLs MSYS2 supplies come from the deps directory instead. -LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lvulkan-1', '-lSDL3', '-lopus', +LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lvulkan-1', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw' # Runtime sources compiled per install, not in the library: romfs.c includes romfs_config.h, patches.c and ov_zstd.c @@ -150,7 +164,7 @@ WINDRES = UCRT / 'windres.exe' # runtime/totk.rc: the manifest of totk.exe TOOLCHAIN_BIN = None # llvm-mingw bin directory, set by main() for --toolchain DEPS = None # the deps directory of an llvm-mingw build # tools/data/expected-<display version>.json for each build version name. -DISPLAY_VERSIONS = {'v0': '1.0.0', 'v111': '1.1.1', 'v112': '1.1.2', 'v121': '1.2.1', 'v143': '1.4.3'} +DISPLAY_VERSIONS = {'v0': '1.0.0', 'v110': '1.1.0', 'v112': '1.1.2', 'v121': '1.2.1', 'v143': '1.4.3'} GiB = 1 << 30 # Peak commit of one compile, 1.2.1 at -O1 (2026-10-20): 120-180 MiB per ~3 MiB chunk, 568 MiB for main_table.c @@ -227,9 +241,10 @@ def write_if_changed(path, text): def copy_romfs_patches(version, out): """totk.exe loads romfs-patches beside romfs-index.bin (runtime/romfs.c overlay_init). tools/make_romfs_patches.py writes them into build/<version> only, so a build in another directory ran without the 60 fps overlay (the quick - menus' captured background turned black). Copies them when their manifest differs; romfs-patches-xbox (the same with - the A/B and X/Y prompts swapped, pad_layout = xbox_prompts) the same way when it was made.""" - for name in ('romfs-patches', 'romfs-patches-xbox'): + menus' captured background turned black). Copies them when their manifest differs; romfs-patches-xbox and + romfs-patches-xbox-label (the same with Xbox button prompts, make_romfs_patches.py --prompts) the same way when they + were made.""" + for name in ('romfs-patches', 'romfs-patches-xbox', 'romfs-patches-xbox-label'): src = ROOT / 'build' / version / name dst = out / name if src.resolve() == dst.resolve() or not (src / 'manifest.tsv').is_file(): @@ -241,6 +256,16 @@ def copy_romfs_patches(version, out): print(f'romfs patches: copied {name} from {src}') +def work_dir(): + """The work folder: the [work] dir of config.local.toml (relative to the checkout), where tools/extract.py writes + extracted/; build/ without the file or the key.""" + cfg = ROOT / 'config.local.toml' + if not cfg.is_file(): + return ROOT / 'build' + import tomllib + return ROOT / tomllib.loads(cfg.read_text(encoding='utf-8')).get('work', {}).get('dir', 'build') + + def c_string(s): return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"' @@ -691,6 +716,16 @@ def elf_sections(path): return out, hi - (lo or 0) +def objdump_tool(): + """The disassembler for tools/check_isa.py: llvm-objdump of --toolchain, else MSYS2's or the one on PATH.""" + suffix = '.exe' if WIN else '' + for d in (TOOLCHAIN_BIN, UCRT if WIN else None): + for name in ('llvm-objdump', 'objdump'): + if d and (d / (name + suffix)).is_file(): + return str(d / (name + suffix)) + return check_isa.find_objdump() + + def mib(n): return f'{n / (1 << 20):,.1f} MiB' @@ -698,6 +733,8 @@ def mib(n): def main(): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument('--version', default='v121', help='game version (default v121)') + ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3', + help='target CPU level: v3 (AVX2/FMA, default) or v2 (SSE4.2, for older CPUs; slower)') ap.add_argument('--gen', type=Path, help='generated C directory (default generated/<version>)') ap.add_argument('--out', type=Path, help='build directory (default build/<version>)') ap.add_argument('--modules', help='comma-separated subset of the layout modules (default all)') @@ -710,9 +747,12 @@ def main(): help='comma-separated runtime/*.c names to leave out (the link needs another definition of what they ' 'define)') ap.add_argument('--exefs', type=Path, - help="totk.exe's default ExeFS directory (default build/extracted/<version>/exefs)") + help="totk.exe's default ExeFS directory (default <work dir>/extracted/<version>/exefs, the work " + "dir being the [work] dir of config.local.toml, else build)") ap.add_argument('--romfs', type=Path, help="totk.exe's default RomFS directory with romfs.bin and index.json " - '(default build/extracted/<version>/romfs)') + '(default <work dir>/extracted/<version>/romfs)') + ap.add_argument('--no-romfs', action='store_true', + help='build even when the RomFS directory has no index.json (the exe then stops at start)') ap.add_argument('--profile', type=Path, help="totk.exe's default profile directory (default build/profile for v121, " "build/profile-<version> otherwise, so trying another version leaves " "the main profile alone; saves load on 1.2.1 and 1.4.3 alike)") @@ -732,14 +772,20 @@ def main(): ap.add_argument('--expected', type=Path, help='expected hashes (default tools/data/expected-<display version>.json)') args = ap.parse_args() + set_cpu(args.cpu) gen = (args.gen or ROOT / 'generated' / args.version).resolve() out = (args.out or ROOT / 'build' / args.version).resolve() - extracted = ROOT / 'build' / 'extracted' / args.version + extracted = None if args.exefs and args.romfs else work_dir() / 'extracted' / args.version paths = {'exefs': (args.exefs or extracted / 'exefs').resolve(), 'romfs': (args.romfs or extracted / 'romfs').resolve(), 'profile': (args.profile or ROOT / 'build' / ('profile' if args.version == 'v121' else f'profile-{args.version}')).resolve()} + has_romfs = (paths['romfs'] / 'index.json').is_file() + if not (has_romfs or args.no_romfs or args.make_runtime_lib or args.compile_only): + sys.exit(f'{paths["romfs"] / "index.json"} not found: without the RomFS the game stops at start. Run ' + 'tools/extract.py (it writes extracted/<version> into the [work] dir of config.local.toml), pass ' + '--romfs DIR, or --no-romfs to build without it') expected = args.expected if not expected: if args.version not in DISPLAY_VERSIONS: @@ -786,7 +832,8 @@ def main(): if unknown: sys.exit(f'unknown modules {unknown}; the layout has {names}') linked = [m for m in names if m in wanted] - mods = {m: chunk_files(gen, m) for m in linked} + # --make-runtime-lib builds runtime/ only, so it never reads the generated chunks + mods = {} if args.make_runtime_lib else {m: chunk_files(gen, m) for m in linked} cpus = os.cpu_count() or 1 budget = min(MEM_BUDGET, int(total_ram() * 0.8)) @@ -795,7 +842,7 @@ def main(): if missing: sys.exit(f'{expected}: no exefs_sha256 for {missing}') write_if_changed(out / 'modules.c', module_glue(layout, linked, hashes, paths, args.version)) - if (paths['romfs'] / 'index.json').is_file(): + if has_romfs: count, size, dt = romfs_index.write(paths['romfs'], out) print(f'romfs index: {count} files, {mib(size)}, {dt:.1f} s') else: @@ -850,6 +897,12 @@ def main(): size = sum(o.stat().st_size for o in files) print(f' {mod:8} {len(files):5} objects {mib(size):>14}') verify_objects(out, objs, pairs is not None, main_out, pairs, base, env) + if args.cpu == 'v2': + # Inline assembly, target attributes and intrinsics pass -march=x86-64-v2 by; one vmovdqa of a YMM register + # stops a CPU without AVX (tools/check_isa.py) + fail = check_isa.check_objects(objs['runtime'], objdump_tool(), out / 'obj' / 'runtime' / 'isa-v2.json') + if fail: + sys.exit(f'build_game: {fail}') if args.compile_only: return if args.make_runtime_lib: diff --git a/tools/build_shaders.py b/tools/build_shaders.py index 3ce6300e..c64afb94 100644 --- a/tools/build_shaders.py +++ b/tools/build_shaders.py @@ -1,6 +1,6 @@ -"""Compile the runtime's own GLSL shaders (runtime/shaders/*.comp) into C headers of SPIR-V words. +"""Compile the runtime's own GLSL shaders (runtime/shaders/*.comp, *.vert, *.frag) into C headers of SPIR-V words. -Each runtime/shaders/<name>.comp becomes runtime/<name>_spv.h, which declares `static const uint32_t <name>_spv[]` +Each runtime/shaders/<name>.comp (or .vert, .frag: one name per shader) becomes runtime/<name>_spv.h, which declares `static const uint32_t <name>_spv[]` and records the SHA-256 of the source it came from. The headers are committed, so building totk.exe needs no shader compiler; tools/build_game.py refuses to build when a header no longer matches its source. Run this after editing a shader. Needs glslc and spirv-val from MSYS2 UCRT64 (package mingw-w64-ucrt-x86_64-shaderc, spirv-tools). @@ -20,6 +20,7 @@ ROOT = Path(__file__).resolve().parent.parent BIN = Path(r'C:\msys64\ucrt64\bin') # Windows; elsewhere glslc and spirv-val come from PATH WIN = os.name == 'nt' TARGET = 'vulkan1.1' +STAGES = {'.comp': 'compute', '.vert': 'vertex', '.frag': 'fragment'} def source_hash(src): @@ -43,10 +44,14 @@ def header_path(src): return src.parent.parent / f'{src.stem}_spv.h' +def sources(runtime): + return sorted(p for p in (runtime / 'shaders').iterdir() if p.suffix in STAGES) + + def stale(runtime): """Shader sources under runtime/shaders whose header is missing or records another source hash.""" out = [] - for src in sorted((runtime / 'shaders').glob('*.comp')): + for src in sources(runtime): h = header_path(src) if not h.exists() or f'// source sha256 {source_hash(src)}' not in h.read_text(encoding='utf-8'): out.append(src) @@ -56,7 +61,7 @@ def stale(runtime): def compile_one(src): with tempfile.TemporaryDirectory() as tmp: spv = Path(tmp) / 'out.spv' - subprocess.run([str(BIN / 'glslc.exe') if WIN else 'glslc', '-O', f'--target-env={TARGET}', '-fshader-stage=compute', + subprocess.run([str(BIN / 'glslc.exe') if WIN else 'glslc', '-O', f'--target-env={TARGET}', f'-fshader-stage={STAGES[src.suffix]}', '-o', str(spv), str(src)], check=True) subprocess.run([str(BIN / 'spirv-val.exe') if WIN else 'spirv-val', f'--target-env', TARGET, str(spv)], check=True) data = spv.read_bytes() @@ -75,7 +80,7 @@ def main(): for s in bad: print(f'{header_path(s)}: stale, run tools/build_shaders.py') return 1 if bad else 0 - for src in sorted((args.runtime / 'shaders').glob('*.comp')): + for src in sources(args.runtime): compile_one(src) return 0 diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 7c8b0412..210c1acf 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -1,7 +1,7 @@ """Build the shader translator: build/xlate/totk_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan). Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays -byte-identical; 0001-0004 are MNG's, 0005-0009 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build +byte-identical; 0001-0004 are MNG's, 0005-0014 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build tools and vswhere.exe (docs/building.md). Run with .venv. On Linux it builds build/xlate/totk_xlate for linux-x64, which NativeAOT links with the system's clang (no MSVC); NativeAOT builds for the OS it runs on only. @@ -99,8 +99,8 @@ def prepare_sources() -> Path: for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")): shutil.rmtree(junk) patches = sorted((XLATE / "patches").glob("*.patch")) - if len(patches) != 9: - sys.exit(f"expected 9 patches, found {len(patches)}") + if len(patches) != 15: + sys.exit(f"expected 15 patches, found {len(patches)}") for patch in patches: # git apply works outside a repository when given a directory; --check first so a failure names the patch. for extra in (["--check"], []): diff --git a/tools/button_icons.py b/tools/button_icons.py new file mode 100644 index 00000000..c062080e --- /dev/null +++ b/tools/button_icons.py @@ -0,0 +1,319 @@ +# Adapted from MacNativeGames games/totk/scripts/make_button_icons.py @ 933b6a0 (MIT, Copyright (c) 2026 +# MacNativeGames contributors). +"""Xbox button icons for the game's prompts, drawn on the player's machine for tools/make_romfs_patches.py --prompts. +The outlines are our own shapes with letters cut out of them in the game's own text fonts: nothing of the game is in +the repository. + +Every prompt except the button guide in play is text in the button font, scft/External.bfotf of each +Font/Font*.Nin_NX_NVN.bfarc.zs: private-use code points from U+E040 (A, B, X, Y, then L, R, ZL, ZR, the d-pad, Plus and +Minus, the sticks). A .bfotf is an OpenType (CFF) font after an 8-byte header; from offset 4 on, every byte is XORed +with a 4-byte key, which the size in the header's second word gives away (bfotf_decode). redraw_font draws the shoulder +buttons, the triggers, Plus, Minus and the d-pad as an Xbox controller names them (LB RB, LT RT, Menu and View, a +cross), can swap the glyphs of the face buttons (A with B, X with Y), and has fontTools write the font again. The +archive then grows by a kilobyte or two, more than the game's resource size table allows it: grow_restbl raises those +entries (without it the game stops at start, MacNativeGames found). + +The button guide in play (bottom right) draws L and ZL from images of the common layout archive instead +(timg/__Combined.bntx, Nt_KeyTexL_00 and Nt_KeyTexZL_00): bntx_redraw paints the same icons into them, the size +unchanged. The sticks keep the Switch icons, and so do prompts drawn as fixed images (some tutorial animations). + +Needs fontTools (tools/requirements-installer.txt, tools/requirements-py314.txt). +""" +import io +import struct +import zlib + +from fontTools.pens.basePen import BasePen +from fontTools.pens.boundsPen import BoundsPen +from fontTools.pens.recordingPen import RecordingPen +from fontTools.pens.reverseContourPen import ReverseContourPen +from fontTools.pens.t2CharStringPen import T2CharStringPen +from fontTools.pens.transformPen import TransformPen +from fontTools.ttLib import TTFont + +FONT = 'scft/External.bfotf' +# The letters on the keys come from the text fonts of the default archive; the regional archives lack them. +TEXT_ARCHIVE = 'Font/Font.Nin_NX_NVN.bfarc.zs' +TEXT_FONTS = ('scft/ninP_RodinNTLG-B_DH_003_subset.bfotf', 'scft/nintendo_NTLG-DB_DH_002.bfotf') +# The button guide's images and the glyph each one shows. +HUD_TEXTURES = {'Nt_KeyTexL_00': 0xE044, 'Nt_KeyTexZL_00': 0xE046} + + +def xor(data, key): + """data with byte i XORed with key[i % 4].""" + pad = (key * (len(data) // 4 + 1))[:len(data)] + return (int.from_bytes(data, 'little') ^ int.from_bytes(pad, 'little')).to_bytes(len(data), 'little') + + +def bfotf_decode(blob): + """(OpenType font, key) of a .bfotf (the module docstring). The font fills the rest of the file.""" + key = xor(blob[4:8], struct.pack('>I', len(blob) - 8)) + otf = xor(blob[8:], key) + assert otf[:4] == b'OTTO', 'not a CFF OpenType font' + return otf, key + + +def bfotf_encode(otf, key, head): + """The .bfotf of otf: head (the original's first word) and the size, the rest XORed with key as bfotf_decode reads + it. The size field holds the whole font, so its length has to be a multiple of 4 (fontTools pads its tables).""" + assert len(otf) % 4 == 0, len(otf) + return head[:4] + xor(struct.pack('>I', len(otf)) + otf, key) + + +# ---- drawing, in units of the font's 1000-unit em; the round A/B/X/Y icons fill y 20..585 +Y0, Y1 = 20, 585 +CY, R = (Y0 + Y1) / 2, (Y1 - Y0) / 2 +K = 0.5523 # control point distance of a quarter circle drawn with one cubic curve, per unit of radius + + +class Glyphs: + """Outlines of letters from the game's own text fonts, drawn as holes in a shape.""" + def __init__(self, fonts): + self.fonts = fonts + + def outline(self, ch): + for font in self.fonts: + cmap = font.getBestCmap() + if ord(ch) in cmap: + glyphs = font.getGlyphSet(); g = glyphs[cmap[ord(ch)]] + rec = RecordingPen(); g.draw(rec); box = BoundsPen(glyphs); g.draw(box) + return rec, box.bounds, font['head'].unitsPerEm, g.width + raise KeyError(f'no text font has {ch!r}') + + def hole(self, pen, text, cx, cy, cap, max_width): + """text centred on (cx, cy), at most cap high and max_width wide, its contours reversed so they cut holes.""" + parts, x, ink = [], 0.0, [1e9, 1e9, -1e9, -1e9] + for ch in text: + rec, b, upm, advance = self.outline(ch); k = 1000 / upm + parts.append((rec, k, x)) + ink = [min(ink[0], x + b[0] * k), min(ink[1], b[1] * k), max(ink[2], x + b[2] * k), max(ink[3], b[3] * k)] + x += advance * k * 0.96 + width, height = ink[2] - ink[0], ink[3] - ink[1] + scale = min(cap / height, max_width / width) + ox, oy = cx - (ink[0] + width / 2) * scale, cy - (ink[1] + height / 2) * scale + reverse = ReverseContourPen(pen) + for rec, k, x in parts: + rec.replay(TransformPen(reverse, (scale * k, 0, 0, scale * k, ox + x * scale, oy))) + + +def text_glyphs(blobs): + """Glyphs over the text fonts in blobs (.bfotf files), the first that has a letter giving it.""" + return Glyphs([TTFont(io.BytesIO(bfotf_decode(blob)[0])) for blob in blobs]) + + +def circle(pen, cx, cy, r): + k = K * r + pen.moveTo((cx + r, cy)) + pen.curveTo((cx + r, cy + k), (cx + k, cy + r), (cx, cy + r)) + pen.curveTo((cx - k, cy + r), (cx - r, cy + k), (cx - r, cy)) + pen.curveTo((cx - r, cy - k), (cx - k, cy - r), (cx, cy - r)) + pen.curveTo((cx + k, cy - r), (cx + r, cy - k), (cx + r, cy)) + pen.closePath() + + +def rounded(pen, x0, y0, x1, y1, bottom, top=None): + """Counter-clockwise rounded rectangle (bottom and top corner radii).""" + top = bottom if top is None else top + kb, kt = K * bottom, K * top + pen.moveTo((x0 + bottom, y0)); pen.lineTo((x1 - bottom, y0)) + pen.curveTo((x1 - bottom + kb, y0), (x1, y0 + bottom - kb), (x1, y0 + bottom)); pen.lineTo((x1, y1 - top)) + pen.curveTo((x1, y1 - top + kt), (x1 - top + kt, y1), (x1 - top, y1)); pen.lineTo((x0 + top, y1)) + pen.curveTo((x0 + top - kt, y1), (x0, y1 - top + kt), (x0, y1 - top)); pen.lineTo((x0, y0 + bottom)) + pen.curveTo((x0, y0 + bottom - kb), (x0 + bottom - kb, y0), (x0 + bottom, y0)); pen.closePath() + + +def polygon(pen, points): + pen.moveTo(points[0]) + for p in points[1:]: + pen.lineTo(p) + pen.closePath() + + +def xbox_icons(g): + """Code point -> (advance, draw(pen)) for the buttons an Xbox controller names differently from a Switch: L R = LB + RB, ZL ZR = LT RT, Plus and Minus = Menu and View, the d-pad as a cross. g: Glyphs for the letters. The face + buttons keep the game's own glyphs (redraw_font swaps them for the Nintendo layout).""" + def bumper(label, advance=820): + def draw(pen): + rounded(pen, 40, Y0 + 70, advance - 40, Y1 - 70, 70, 150); g.hole(pen, label, advance / 2, CY, 230, advance - 200) + return advance, draw + + def trigger(label, advance=760): + def draw(pen): + rounded(pen, 60, Y0, advance - 60, Y1, 60, 230); g.hole(pen, label, advance / 2, CY - 20, 230, advance - 220) + return advance, draw + + def menu(advance=640): # three bars, as on the Menu button + def draw(pen): + circle(pen, advance / 2, CY, R); reverse = ReverseContourPen(pen) + for dy in (-115, 0, 115): + rounded(reverse, advance / 2 - 150, CY + dy - 38, advance / 2 + 150, CY + dy + 38, 38) + return advance, draw + + def view(advance=640): # two overlapping windows, as on the View button + def draw(pen): + circle(pen, advance / 2, CY, R); reverse = ReverseContourPen(pen); s = 150 + rounded(reverse, advance / 2 - s - 15, CY + 15, advance / 2 - 15, CY + s + 15, 16) + rounded(reverse, advance / 2 + 15, CY - s - 15, advance / 2 + s + 15, CY - 15, 16) + return advance, draw + + def dpad(*lit, advance=640): # a cross with an arrow cut into each direction the prompt means + def draw(pen): + cx, length, arm = advance / 2, R, R * 0.40 + polygon(pen, [(cx + length, CY - arm), (cx + length, CY + arm), (cx + arm, CY + arm), (cx + arm, CY + length), + (cx - arm, CY + length), (cx - arm, CY + arm), (cx - length, CY + arm), (cx - length, CY - arm), + (cx - arm, CY - arm), (cx - arm, CY - length), (cx + arm, CY - length), (cx + arm, CY - arm)]) + m, t, h = (length + arm) / 2, arm * 0.72, arm * 0.80 # arm centre, half base and height of the arrow + heads = {'up': [(cx - t, CY + m - h / 2), (cx, CY + m + h / 2), (cx + t, CY + m - h / 2)], + 'down': [(cx + t, CY - m + h / 2), (cx, CY - m - h / 2), (cx - t, CY - m + h / 2)], + 'left': [(cx - m + h / 2, CY - t), (cx - m - h / 2, CY), (cx - m + h / 2, CY + t)], + 'right': [(cx + m - h / 2, CY + t), (cx + m + h / 2, CY), (cx + m - h / 2, CY - t)]} + for d in lit: + polygon(pen, heads[d]) # already clockwise: a hole + return advance, draw + + return { + 0xE044: bumper('LB'), 0xE045: bumper('RB'), 0xE046: trigger('LT'), 0xE047: trigger('RT'), + 0xE04B: menu(), 0xE04C: view(), + 0xE049: dpad('up', 'down', 'left', 'right'), 0xE060: dpad('up'), 0xE061: dpad('down'), + 0xE062: dpad('left'), 0xE063: dpad('right'), 0xE064: dpad('up', 'down'), 0xE065: dpad('left', 'right'), + } + + +def redraw_font(otf, icons, swaps=()): + """The OpenType font otf with the glyphs of each (code point, code point) pair of swaps trading places (outlines and + horizontal metrics), then every code point of icons (code point -> (advance, draw)) drawn anew, written again by + fontTools. The timestamps stay, so the same input gives the same bytes.""" + font = TTFont(io.BytesIO(otf), recalcTimestamp=False) + assert font['head'].unitsPerEm == 1000, f'em of {font["head"].unitsPerEm} units, the drawings assume 1000' + cff = font['CFF '].cff + top = cff.topDictIndex[0] + assert not hasattr(top, 'ROS'), 'CID-keyed CFF' # its glyphs can use different private dictionaries + charstrings, private, hmtx, cmap = top.CharStrings, top.Private, font['hmtx'], font.getBestCmap() + nominal = getattr(private, 'nominalWidthX', 0) + for a, b in swaps: + ga, gb = cmap[a], cmap[b] + outline_a, outline_b = charstrings[ga], charstrings[gb] + charstrings[ga], charstrings[gb] = outline_b, outline_a + hmtx[ga], hmtx[gb] = hmtx[gb], hmtx[ga] + for cp, (advance, draw) in icons.items(): + pen = T2CharStringPen(advance - nominal, None); draw(pen) # the width operand is relative to nominalWidthX + charstrings[cmap[cp]] = pen.getCharString(private=private, globalSubrs=cff.GlobalSubrs) + glyphs = font.getGlyphSet() + for cp, (advance, _) in icons.items(): + box = BoundsPen(glyphs); glyphs[cmap[cp]].draw(box) + hmtx[cmap[cp]] = (advance, int(round(box.bounds[0]))) + out = io.BytesIO(); font.save(out) + return out.getvalue() + + +# ---- the button guide's images: square R8_UNORM masks (white key, black letter), one mip, block-linear, 72x72 up to +# 1.2.1 and 108x108 in 1.4.3. Each is drawn from the icon of the same Switch button, fitted into the square. +R8_UNORM, HUD_MARGIN, SUPERSAMPLE = 0x201, 4, 4 + + +def gob_offset(x, y, width_gobs, block_height): + """Byte offset of (x, y) in a Tegra block-linear surface of 1 byte per pixel (GOBs of 64 bytes x 8 rows).""" + return ((y // (8 * block_height)) * 512 * block_height * width_gobs + (x // 64) * 512 * block_height + + ((y % (8 * block_height)) // 8) * 512 + ((x % 64) // 32) * 256 + ((y % 8) // 2) * 64 + + ((x % 32) // 16) * 32 + (y % 2) * 16 + (x % 16)) + + +class PolygonPen(BasePen): + """Outlines flattened to closed polygons.""" + def __init__(self): + super().__init__(None); self.polygons = [] + + def _moveTo(self, pt): + self.polygons.append([pt]) + + def _lineTo(self, pt): + self.polygons[-1].append(pt) + + def _curveToOne(self, pt1, pt2, pt3): + p0 = self.polygons[-1][-1] + for i in range(1, 17): + t = i / 16; u = 1 - t + self.polygons[-1].append(tuple(u ** 3 * p0[k] + 3 * u * u * t * pt1[k] + 3 * u * t * t * pt2[k] + t ** 3 * pt3[k] + for k in (0, 1))) + + def _qCurveToOne(self, pt1, pt2): + p0 = self.polygons[-1][-1] + for i in range(1, 17): + t = i / 16; u = 1 - t + self.polygons[-1].append(tuple(u * u * p0[k] + 2 * u * t * pt1[k] + t * t * pt2[k] for k in (0, 1))) + + +def rasterize(draw, size): + """A size x size coverage mask (0..255, rows top down) of draw's outline, fitted into the square, nonzero winding.""" + rec = RecordingPen(); draw(rec) + box = BoundsPen(None); rec.replay(box); x0, y0, x1, y1 = box.bounds + inner = size - 2 * HUD_MARGIN; scale = inner / max(x1 - x0, y1 - y0) + ox = HUD_MARGIN + (inner - (x1 - x0) * scale) / 2 - x0 * scale + oy = HUD_MARGIN + (inner - (y1 - y0) * scale) / 2 - y0 * scale + flat = PolygonPen(); rec.replay(TransformPen(flat, (scale, 0, 0, -scale, ox, size - oy))) # font y up -> rows down + edges = [(a, b) for poly in flat.polygons for a, b in zip(poly, poly[1:] + poly[:1]) if a[1] != b[1]] + n = size * SUPERSAMPLE; cover = [0] * (size * size) + for row in range(n): + y = (row + 0.5) / SUPERSAMPLE + crossings = sorted((a[0] + (y - a[1]) * (b[0] - a[0]) / (b[1] - a[1]), 1 if b[1] > a[1] else -1) + for a, b in edges if min(a[1], b[1]) <= y < max(a[1], b[1])) + winding, start = 0, None + for x, d in crossings: + if winding == 0: + start = x + winding += d + if winding == 0: + for col in range(max(0, round(start * SUPERSAMPLE)), min(n, round(x * SUPERSAMPLE))): + cover[(row // SUPERSAMPLE) * size + col // SUPERSAMPLE] += 1 + return bytes(min(255, c * 255 // (SUPERSAMPLE * SUPERSAMPLE)) for c in cover) + + +def bntx_redraw(data, base, textures, icons): + """Paint the icon of each texture of textures (name without the ^ suffix -> code point; icons: code point -> + (advance, draw)) over its image in the BNTX at data[base:] (a bytearray), in place. Returns the names painted.""" + assert data[base:base + 4] == b'BNTX' and data[base + 0x20:base + 0x24] == b'NX ' + count, table = struct.unpack_from('<IQ', data, base + 0x24) + done = [] + for k in range(count): + info = base + struct.unpack_from('<Q', data, base + table + 8 * k)[0] + assert data[info:info + 4] == b'BRTI' + at = base + struct.unpack_from('<Q', data, info + 0x60)[0] + name = data[at + 2:at + 2 + struct.unpack_from('<H', data, at)[0]].decode().split('^')[0] + if name not in textures: + continue + mips, fmt = struct.unpack_from('<H', data, info + 0x16)[0], struct.unpack_from('<I', data, info + 0x1c)[0] + (w, h), layout = struct.unpack_from('<II', data, info + 0x24), struct.unpack_from('<I', data, info + 0x34)[0] + assert fmt == R8_UNORM and w == h and mips == 1, f'{name}: format {fmt:#x}, {w}x{h}, {mips} mips' + start = base + struct.unpack_from('<Q', data, base + struct.unpack_from('<Q', data, info + 0x70)[0])[0] + size, block = struct.unpack_from('<I', data, info + 0x50)[0], 1 << (layout & 7) + mask = rasterize(icons[textures[name]][1], w) + for y in range(h): + for x in range(w): + at = gob_offset(x, y, (w + 63) // 64, block) + assert at < size, f'{name}: pixel ({x}, {y}) past the image' + data[start + at] = mask[y * w + x] + done.append(name) + missing = sorted(set(textures) - set(done)) + assert not missing, f'not in the texture list: {", ".join(missing)}' + return done + + +# ---- the resource size table: System/Resource/ResourceSizeTable.Product.<n>.Nin_NX_NVN.rsizetable.zs, a RESTBL whose +# entries are keyed by the CRC32 of the RomFS path without .zs. The game sizes the buffer it loads a file into from its +# entry, so an archive that grew needs its entry raised by as much. +def restbl_offsets(table): + """CRC32 of a path -> offset of its size in the RESTBL table.""" + assert table[:6] == b'RESTBL', 'not a RESTBL' + count = struct.unpack_from('<I', table, 14)[0] + return {struct.unpack_from('<I', table, 22 + i * 8)[0]: 22 + i * 8 + 4 for i in range(count)} + + +def grow_restbl(table, growth): + """table with the entry of each RomFS path of growth (path -> bytes it grew) raised by that much.""" + at, out = restbl_offsets(table), bytearray(table) + for path, extra in growth.items(): + h = zlib.crc32(path.removesuffix('.zs').encode()) + assert h in at, f'{path} has no entry in the resource size table' + struct.pack_into('<I', out, at[h], struct.unpack_from('<I', out, at[h])[0] + extra) + return bytes(out) diff --git a/tools/cheats.py b/tools/cheats.py new file mode 100644 index 00000000..5406fe4d --- /dev/null +++ b/tools/cheats.py @@ -0,0 +1,374 @@ +"""Cheat files to code patch sets: the code cheats of the mods' Atmosphere cheat files become patch sets of the lifted +game. + +Lifted code cannot be written to while the game runs, so a cheat that writes instructions into main's .text is turned +into `site` lines (recomp/patches.py) when the game is lifted; the runtime (runtime/cheats.c) switches each cheat's set +from the in-game menu. A cheat that only writes data (heap, main's data, pointer chains, button conditions) needs no +patch: runtime/cheats.c runs it in its cheat VM. + + python tools/cheats.py --exefs <dir> --mods <mods folder> --patches <patch file> --out <patch file> --status <file> + +The cheat files are <mod>/cheats/<build id>.txt in each folder of --mods (or the Atmosphere places of that folder, +as runtime/mods.c finds them; the name's case does not matter), the build id the first 8 bytes of main's. Every mod's +file is built, switched on or off: the runtime decides which run. --out gets the --patches file followed by one set +per convertible cheat (`cheat_<hash>`, switch TOTK_CHEAT_<HASH>, default off) and its sites. --status gets one line +per cheat, mods by name, cheats in file order, tab separated: + + <name hash> <content hash> <kind> <buttons> <note> + +name hash: FNV-1a 32 of "<mod folder name>/<cheat name>" in UTF-8 (a name repeated in one file: name + "#2", "#3" ...), +8 upper-case hex digits, so two mods' cheats of one name are apart; content +hash: the same over the cheat's words, so the runtime sees an edited cheat that was not rebuilt. kind: + code instruction writes, a patch set + key instruction writes while buttons are held (8 mask / writes / 21 else / the original words / 20): a patch + set the runtime switches on while the cheat is on and <buttons> (hex HidNpadButton mask) are held + master a {master} cheat with instruction writes: a set that is always on + restore writes only the original instructions (switching a cheat off restores them): nothing to do + heading no opcodes (a title or a group label) + data no instruction writes: the runtime's cheat VM + unsupported <note> says why +A code cheat that calls or jumps into a code cave it writes (words where main has zeros) gets the cave's body inlined +at the call site; a conditional skip of one store in the body (cbz wN, +8; str wN, ...) is dropped. + +Standard library and recomp/ only. +""" +import argparse +import struct +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'recomp')) +import flags # noqa: E402 +from lifter import Lifter # noqa: E402 +from nso import Nso # noqa: E402 + +BASE = 0x7000000000 # main's base in the layout (recomp/layout.py); only pc-relative decoding sees it +NOP = 0xD503201F +RET = 0xD65F03C0 + + +def fnv(data): + h = 0x811C9DC5 + for b in data: + h = ((h ^ b) * 0x01000193) & 0xFFFFFFFF + return f'{h:08X}' + + +def build_id(main): + """The first 8 bytes of the NSO build id, as cheat files name themselves.""" + return Path(main).read_bytes()[0x40:0x48].hex().upper() + + +# ---- parsing + +class Cheat: + def __init__(self, name, master, line): + self.name, self.master, self.line, self.words = name, master, line, [] + self.key = self.kind = self.note = '' + self.buttons = 0 + self.sites = {} # {offset: [words]} + + +def parse(text, mod): + """Cheats in file order, keyed under the mod folder name; words that are not 8 hex digits are skipped.""" + out = [] + for no, raw in enumerate(text.splitlines(), 1): + s = raw.strip() + if len(s) >= 2 and s[0] in '[{' and s[-1] in ']}': + out.append(Cheat(s[1:-1].strip(), s[0] == '{', no)) + elif out: + for w in s.split(): + if len(w) == 8: + try: + out[-1].words.append(int(w, 16)) + except ValueError: + pass + seen = {} + for c in out: + n = seen[c.name] = seen.get(c.name, 0) + 1 + c.key = fnv((f'{mod}/' + (c.name if n == 1 else f'{c.name}#{n}')).encode()) + return out + + +TITLE = '0100F2C0115B6000' + + +def cheat_files(mods, bid): + """[(mod folder name, cheat file)] of the folders of mods, by name: <mod>/cheats/<bid>.txt or its Atmosphere places + (runtime/mods.c find_root), the file name in any case.""" + out = [] + for mod in sorted((d for d in mods.iterdir() if d.is_dir()), key=lambda d: d.name) if mods.is_dir() else []: + for where in ('cheats', f'atmosphere/contents/{TITLE}/cheats', f'contents/{TITLE}/cheats', f'{TITLE}/cheats'): + folder = mod / where + if folder.is_dir(): + f = next((p for p in sorted(folder.iterdir()) if p.is_file() and p.name.lower() == f'{bid}.txt'.lower()), + None) + if f: + out.append((mod.name, f)) + break + return out + + +def ops(words): + """[(type, [words])] of a cheat; type is the opcode nibble, 0xC0.. for the extended ones. ValueError when the last + opcode is cut short.""" + out, i = [], 0 + while i < len(words): + w = words[i] + t = w >> 28 + width = (w >> 24) & 0xF + if t in (0, 1): + n = 4 if width == 8 else 3 + elif t in (2, 8): + n = 1 + elif t == 3: + n = 2 if (w >> 24) & 0xF == 0 else 1 + elif t in (4, 6): + n = 3 + elif t in (5, 7): + n = 2 + elif t == 9: + n = 1 + ((2 if width == 8 else 1) if (w >> 8) & 0xF else 0) + elif t == 0xA: + n = 2 if (w >> 8) & 0xF in (2, 4, 5) else 1 + else: + t = (w >> 24) & 0xFF if t == 0xC else (w >> 20) & 0xFFF + n = 1 + if i + n > len(words): + raise ValueError(f'opcode {w:08X} is cut short') + out.append((t, words[i:i + n])) + i += n + return out + + +VM_TYPES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA} + + +def vm_ok(o): + """None when runtime/cheats.c runs the opcode, else why not.""" + t, w = o + if t not in VM_TYPES: + return f'opcode {w[0]:08X} is not supported' + if t == 5 and (w[0] >> 12) & 0xF > 1: + return f'opcode {w[0]:08X} (load from a register offset) is not supported' + if t == 0xA and (w[0] >> 8) & 0xF > 5: + return f'opcode {w[0]:08X} is not supported' + if t in (0, 1, 5, 6, 7, 9, 0xA) and (w[0] >> 24) & 0xF not in (1, 2, 4, 8): + return f'opcode {w[0]:08X} has a bad width' + return None + + +# ---- instruction writes + +class Main: + def __init__(self, path): + n = Nso(str(path)) + self.image = bytes(n.image) + self.text = n.text # (offset, size) + self.code_end = n.rodata[0] # .text and its padding up to .rodata, where code caves go + self.lifter = Lifter(BASE, 'c') + + def in_text(self, off): + return self.text[0] <= off < self.text[0] + self.text[1] and off % 4 == 0 + + def in_code(self, off): + return self.text[0] <= off < self.code_end and off % 4 == 0 + + def word(self, off): + return struct.unpack_from('<I', self.image, off)[0] + + def lift(self, w, off): + return self.lifter.lift(w, BASE + off) + + def effect(self, w, off): + ins = self.lift(w, off) + if ins.kind == 'op': + return flags.effect(ins.code or '') + if ins.kind == 'bcond': + return flags.effect(ins.cond) + return (False, None) + + +def code_writes(main, o): + """{offset: word} of an opcode 0 into main's code (.text and its padding; 4 or 8 bytes, no offset register in + use), None for any other opcode; ValueError for a write into the code the patch cannot express.""" + t, w = o + if t != 0 or (w[0] >> 20) & 0xF != 0: + return None + width = (w[0] >> 24) & 0xF + off = ((w[0] & 0xFF) << 32) | w[1] + if not main.in_code(off - off % 4): + return None + if width == 4 and off % 4 == 0: + return {off: w[2]} + if width == 8 and off % 4 == 0: + return {off: w[3], off + 4: w[2]} + raise ValueError(f'a {width}-byte write into the code at main+{off:X}') + + +def branch(w, off): + """(kind, target offset) of a b / bl word at off, else None.""" + if w & 0x7C000000 == 0x14000000: + return ('bl' if w >> 31 else 'b', off + ((((w & 0x3FFFFFF) ^ 0x2000000) - 0x2000000) << 2)) + return None + + +def pc_relative(w): + """adr / adrp, a literal load, or any branch: wrong once moved to another address.""" + return (w & 0x1F000000 == 0x10000000 or w & 0x3B000000 == 0x18000000 or w & 0x7C000000 == 0x14000000 + or w & 0xFE000000 in (0x54000000, 0x34000000, 0x36000000, 0xB4000000, 0xB6000000) + or w & 0xFE000000 == 0xD6000000) + + +def skip_store(body, i): + """body[i] is cbz / cbnz wN / xN, +8 and body[i + 1] a str of that register: the skip can go (the cheat stores + only when the value is non-zero).""" + w = body[i] + if w & 0x7E000000 != 0x34000000 or (w >> 5) & 0x7FFFF != 2 or i + 1 >= len(body): + return False + s = body[i + 1] + return s & 0x3B400000 == 0x39000000 and s & 0x1F == w & 0x1F # str (unsigned offset), same Rt + + +def cave_body(writes, caves, start, back, call): + """The cave's words from start up to its ret (call) or its b to back.""" + body, a = [], start + while True: + if a not in caves: + raise ValueError(f'the cheat branches to main+{a:X}, which it does not write') + w = writes[a] + if call and w == RET: + return body + b = branch(w, a) + if not call and b and b[0] == 'b' and b[1] == back: + return body + body.append(w) + a += 4 + if len(body) > 64: + raise ValueError('a code cave of more than 64 instructions') + + +def convert(main, writes): + """{offset: [replacement words]} of a set of instruction writes; ValueError when it cannot.""" + caves = {a for a in writes if main.word(a) == 0} + sites = {} + for a, w in sorted(writes.items()): + if a in caves or w == main.word(a): + continue + if not main.in_text(a): + raise ValueError(f'main+{a:X} is past the end of the code') + b = branch(w, a) + if b and b[1] in caves: + body = cave_body(writes, caves, b[1], a + 4, b[0] == 'bl') + out = [] + for i, x in enumerate(body): + if skip_store(body, i): + continue + if pc_relative(x): + raise ValueError(f'the code cave at main+{b[1]:X} has a branch or pc-relative instruction') + out.append(x) + repl = out or [NOP] + else: + repl = [w] + orig = main.word(a) + kind = main.lift(orig, a).kind + if kind not in ('op', 'bl', 'b', 'bcond'): + raise ValueError(f'main+{a:X} is a {kind}, which a patch cannot replace') + last = main.lift(repl[-1], a).kind + if last not in ('op', 'bl', 'b', 'bcond', 'ret'): + raise ValueError(f'main+{a:X} gets a {last}') + want = main.effect(orig, a)[1] + got = [e[1] for e in (main.effect(x, a) for x in repl) if e[1]] + if (got[0] if got else None) != want or len(got) > 1: + # a nop over adds / subs Rd, Rd, #imm: subtract 0 instead, the flags still written and Rd unchanged + if repl == [NOP] and orig & 0x1F000000 == 0x11000000 and orig & 0x20000000 and \ + orig & 0x1F == (orig >> 5) & 0x1F: + repl = [orig & ~0x003FFC00] + else: + raise ValueError(f'main+{a:X}: the cheat changes the CPU flags differently from the game') + sites[a] = repl + return sites + + +def classify(main, c): + try: + os_ = ops(c.words) + except ValueError as e: + c.kind, c.note = 'unsupported', str(e) + return + os_ = [o for o in os_ if not (o[0] == 2 and len(os_) == 1)] # [Link] 20000000: a label + if not os_: + c.kind = 'heading' + return + try: + cw = [code_writes(main, o) for o in os_] + except ValueError as e: + c.kind, c.note = 'unsupported', str(e) + return + if not any(x is not None for x in cw): + bad = next((r for r in map(vm_ok, os_) if r), None) + c.kind, c.note = ('unsupported', bad) if bad else ('data', '') + return + try: + if all(x is not None for x in cw): + writes = {k: v for x in cw for k, v in x.items()} + if all(main.word(a) == w for a, w in writes.items()): + c.kind = 'restore' + return + c.sites, c.kind = convert(main, writes), 'master' if c.master else 'code' + return + # 8 mask, writes, 21000000, the original words, 20000000 + t = [o[0] for o in os_] + els = next((i for i, o in enumerate(os_) if o[0] == 2 and (o[1][0] >> 24) & 0xF == 1), -1) + if (t[0] == 8 and t[-1] == 2 and (os_[-1][1][0] >> 24) & 0xF == 0 and els > 1 + and all(x is not None for x in cw[1:els] + cw[els + 1:-1])): + on = {k: v for x in cw[1:els] for k, v in x.items()} + off = {k: v for x in cw[els + 1:-1] for k, v in x.items()} + if set(off) == set(on) and all(main.word(a) == w for a, w in off.items()): + c.sites, c.kind, c.buttons = convert(main, on), 'key', os_[0][1][0] & 0x0FFFFFFF + return + raise ValueError('it mixes instruction writes with other cheat opcodes') + except ValueError as e: + c.kind, c.note, c.sites = 'unsupported', str(e), {} + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('--exefs', type=Path, required=True) + ap.add_argument('--mods', type=Path, required=True, help='the mods folder next to totk.exe') + ap.add_argument('--patches', type=Path, required=True, help='the game version\'s patch file') + ap.add_argument('--out', type=Path, required=True, help='the patch file with the cheats added') + ap.add_argument('--status', type=Path, required=True) + args = ap.parse_args() + sys.stdout.reconfigure(errors='replace') # cheat names in a console without their characters + m = Main(args.exefs / 'main') + text = args.patches.read_text() + files = cheat_files(args.mods, build_id(args.exefs / 'main')) + cheats = [c for mod, f in files for c in parse(f.read_text(encoding='utf-8-sig', errors='replace'), mod)] + sets, sites, status = [], [], [] + for c in cheats: + classify(m, c) + if c.sites: + name = f'cheat_{c.key.lower()}' + label = c.name.encode('ascii', 'replace').decode() # recomp/patches.py reads the file as ASCII + sets.append(f'set {name} TOTK_CHEAT_{c.key} {1 if c.kind == "master" else 0} # {label}') + for a, repl in sorted(c.sites.items()): + sites.append(f'site {name} main {a:x} {m.word(a):08x} {" ".join(f"{w:08x}" for w in repl)}') + content = fnv(' '.join(f'{w:08X}' for w in c.words).encode()) + note = c.note.replace('\t', ' ') + status.append(f'{c.key}\t{content}\t{c.kind}\t{c.buttons:X}\t{note}') + print(f'cheats: {c.kind:11} {c.name}' + (f' ({note})' if note else '')) + if sets: + text += ('\n# Cheats of the mods folder (tools/cheats.py)\n' + '\n'.join(sets) + '\n' + '\n'.join(sites) + '\n') + args.out.write_text(text) + args.status.parent.mkdir(parents=True, exist_ok=True) + args.status.write_text('\n'.join(status) + ('\n' if status else ''), encoding='utf-8') + kinds = {} + for c in cheats: + kinds[c.kind] = kinds.get(c.kind, 0) + 1 + print(f'cheats: {len(cheats)} in {len(files)} files ({", ".join(mod for mod, _ in files) or "none"}): ' + + ', '.join(f'{n} {k}' for k, n in sorted(kinds.items())) + f'; {len(sites)} sites') + + +if __name__ == '__main__': + main() diff --git a/tools/check_isa.py b/tools/check_isa.py new file mode 100644 index 00000000..e6306fdd --- /dev/null +++ b/tools/check_isa.py @@ -0,0 +1,154 @@ +"""Refuse instructions above x86-64-v2 in objects built for --cpu v2. + +The v2 build is for CPUs without AVX, AVX2, FMA, BMI1/2, F16C, LZCNT and MOVBE (a Pentium Gold G6400, an older Core +i7). The compiler keeps generated code inside that set by -march=x86-64-v2, but inline assembly, target attributes and +intrinsics pass through: kern_ap_stub (runtime/kernel.c) saved YMM registers with vmovdqa in the v2 library of 0.3.1 +and crashed such a CPU with an illegal instruction the first time a core was interrupted. tools/build_game.py --cpu v2 +runs this over every runtime object before it links or archives them. + +A function that holds such an instruction behind a runtime CPU check goes in DISPATCHED with that check named. + + python tools/check_isa.py [--objdump PATH] FILE... (objects, archives or executables) + +Standard library only. +""" + +import argparse +import collections +import concurrent.futures +import json +import os +import re +import shutil +import subprocess +import sys +from pathlib import Path + +# Mnemonics outside x86-64-v2 that are not VEX/EVEX encoded, or VEX encoded but without a v prefix (BMI1/2). +# lzcnt and tzcnt decode as bsr and bsf on older CPUs and give other results; tzcnt is left out because the +# compiler emits it on any x86-64 level where bsf would do (a nonzero input, or one it checks first). +NON_V2 = {'andn', 'bextr', 'blsi', 'blsmsk', 'blsr', 'bzhi', 'shlx', 'shrx', 'sarx', 'rorx', 'pdep', 'pext', 'mulx', + 'lzcnt', 'movbe', 'adcx', 'adox', 'sha1rnds4', 'sha1nexte', 'sha1msg1', 'sha1msg2', 'sha256rnds2', + 'sha256msg1', 'sha256msg2', 'aesenc', 'aesenclast', 'aesdec', 'aesdeclast', 'aesimc', 'aeskeygenassist', + 'pclmulqdq', 'rdrand', 'rdseed'} +PREFIXES = {'rep', 'repe', 'repz', 'repne', 'repnz', 'lock', 'notrack', 'data16', 'cs', 'ds', 'es', 'ss', 'fs', 'gs'} +WIDE = re.compile(r'%[yz]mm\d|%k[0-7]\b') + +# Function -> the runtime check that keeps it off CPUs without the instructions. +DISPATCHED = { + 'blocks_ni': 'runtime/sha256.c sha256_blocks: __builtin_cpu_supports("sha") && __builtin_cpu_supports("sse4.1")', +} + + +def find_objdump(hint=None): + """llvm-objdump next to the compiler in use, else on PATH, else GNU objdump; None when there is none.""" + for p in ([Path(hint)] if hint else []): + if p.is_file(): + return str(p) + for name in ('llvm-objdump', 'objdump'): + found = shutil.which(name) + if found: + return found + return None + + +def mnemonic(line): + """The mnemonic of one disassembly line (llvm-objdump or GNU objdump, AT&T syntax), or None.""" + s = line.strip() + m = re.match(r'^[0-9a-f]+:\s*', s) + if m: + s = s[m.end():] + words = s.split() + while words and words[0] in PREFIXES: + words = words[1:] + return words[0] if words else None + + +def is_non_v2(line, mn): + if mn.startswith('v') and mn not in ('verr', 'verw'): + return True # VEX or EVEX: AVX, AVX2, FMA, F16C, AVX-512 + if mn.startswith('k') and WIDE.search(line): + return True # AVX-512 mask registers + return mn in NON_V2 or bool(WIDE.search(line)) + + +def scan_one(path, objdump): + """{(file, function): Counter of mnemonics} of one file.""" + hits = collections.defaultdict(collections.Counter) + r = subprocess.run([objdump, '-d', '--no-show-raw-insn', str(path)], capture_output=True, text=True, + errors='replace') + if r.returncode: + sys.exit(f'check_isa: {objdump} failed on {path}: {r.stderr.strip()[:300]}') + obj = str(path) + func = None + for line in r.stdout.splitlines(): + if 'file format' in line: + obj = line.split(':\tfile format')[0].split(': file format')[0].strip() + continue + m = re.match(r'^[0-9a-f]*\s*<(.+)>:$', line) + if m: + func = m.group(1) + continue + if not line[:1].isspace(): + continue + mn = mnemonic(line) + if mn and func not in DISPATCHED and is_non_v2(line, mn): + hits[(obj, func)][mn] += 1 + return hits + + +def scan(paths, objdump): + """{(file, function): Counter of mnemonics} for every function outside DISPATCHED with a non-v2 instruction.""" + hits = {} + with concurrent.futures.ThreadPoolExecutor(max_workers=min(16, os.cpu_count() or 1)) as pool: + for h in pool.map(lambda p: scan_one(p, objdump), paths): + hits.update(h) + return hits + + +def check_objects(paths, objdump, record): + """Scan the objects changed since the last clean scan (their sizes and mtimes in the JSON file record); return + the failure text, or None. Without an objdump it warns and passes: the check guards the build, it is no part + of it.""" + if not objdump: + print('check_isa: no llvm-objdump or objdump; the v2 instruction check is skipped', file=sys.stderr) + return None + try: + seen = json.loads(record.read_text()) + except (OSError, ValueError): + seen = {} + stamp = {str(p): [p.stat().st_size, p.stat().st_mtime_ns] for p in paths} + todo = [p for p in paths if seen.get(str(p)) != stamp[str(p)]] + hits = scan(todo, objdump) + if hits: + record.unlink(missing_ok=True) + return report(hits) + record.write_text(json.dumps(stamp)) + print(f'check_isa: {len(todo)} of {len(paths)} runtime objects scanned, none above x86-64-v2') + return None + + +def report(hits): + lines = [f' {f} in {o}: ' + ', '.join(f'{k} x{v}' for k, v in c.most_common(6)) + for (o, f), c in sorted(hits.items())] + return ('instructions above x86-64-v2 in a --cpu v2 build (a CPU without AVX/BMI stops on them with an illegal ' + 'instruction); use SSE4.2 code under #ifndef __AVX__, or a runtime CPU check and an entry in ' + 'tools/check_isa.py DISPATCHED:\n' + '\n'.join(lines)) + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('files', nargs='+', type=Path) + ap.add_argument('--objdump', help='llvm-objdump or objdump to use (default: PATH)') + args = ap.parse_args() + objdump = find_objdump(args.objdump) + if not objdump: + sys.exit('check_isa: no llvm-objdump or objdump found') + hits = scan(args.files, objdump) + if hits: + sys.exit(report(hits)) + print(f'check_isa: {len(args.files)} files, no instructions above x86-64-v2') + + +if __name__ == '__main__': + main() diff --git a/tools/data/expected-v1.1.1.json b/tools/data/expected-v1.1.0.json similarity index 75% rename from tools/data/expected-v1.1.1.json rename to tools/data/expected-v1.1.0.json index 5f706acf..a14f90f8 100644 --- a/tools/data/expected-v1.1.1.json +++ b/tools/data/expected-v1.1.0.json @@ -1,8 +1,8 @@ { - "_comment": "Written locally for the player's own 1.1.x update (version value 0x20000, seen as 1.1.1). RomFS hashes filled in after the first merge; no game data here. No rewritten_sha256: this dump was not run through MacNativeGames' NSO rewriter, so the three digests of the other versions have no reference here and none of the tools read them.", + "_comment": "Written locally for the player's own 1.1.0 update (version value 0x20000). RomFS hashes filled in after the first merge; no game data here. No rewritten_sha256: this dump was not run through MacNativeGames' NSO rewriter, so the three digests of the other versions have no reference here and none of the tools read them.", "title_id": "0100F2C0115B6000", "version": 131072, - "display_version": "1.1.1", + "display_version": "1.1.0", "exefs_sha256": { "main": "1664b8cf664524d90ca64deb3ce41709266457df966dcb499485267554eb8cec", "main.npdm": "fa3f60922406ab9e2488c2b8d77b12e9869b435033fd8c27c543c347bc185587", diff --git a/tools/exefs_patches.py b/tools/exefs_patches.py new file mode 100644 index 00000000..7f221a46 --- /dev/null +++ b/tools/exefs_patches.py @@ -0,0 +1,230 @@ +"""ExeFS patches of the mods folder to code patch sets: IPS, IPS32 and IPSwitch (.pchtxt) files for the game's main. + +Lifted code cannot be byte-patched when the game runs, so a patch file's writes into main's code become `site` lines +(recomp/patches.py), one patch set per file, which the runtime switches on while its mod is on (runtime/mods.c). The +writes go through the same rules as the mods' code cheats (tools/cheats.py convert): an instruction replaced in place, +a branch into a code cave the file writes into main's zero padding inlined at the branch. + + python tools/exefs_patches.py --exefs <dir> --mods <mods folder> --patches <patch file> --out <patch file> --status <file> + +The files are <mod>/exefs/*.ips and *.pchtxt (or the Atmosphere places of the exefs folder, as runtime/mods.c finds +them) and <mod>/atmosphere/exefs_patches/<name>/*.ips. A file is for main when its build id matches main's: an IPS +file's name, a pchtxt file's @nsobid line (trailing zeros may be left out). Offsets are as emulators read them: IPS +offsets count the NSO header (0x100 bytes) before main's image; a pchtxt offset adds its `@flag offset_shift` first. +A pchtxt patch line applies between @enabled and @disabled; @stop ends the file. + +--out gets the --patches file followed by one set per usable file (`mod_<hash>`, switch TOTK_MODPATCH_<HASH>, default +off) and its sites. --status gets one line per file, mods by name, tab separated: + + <file hash> <content hash> <kind> <built-in sets> <note> + +file hash: FNV-1a 32 of "<mod folder name>/<path in the mod>" (forward slashes); content hash: the same over the file's +bytes, so the runtime sees a file that changed after the build. kind: + code a set of sites + other for another game version or module: not used + empty no write into main for this version (or only the game's own words) + unsupported <note> says why: a write into main's data, a write the site rules cannot express +built-in sets: the port's own sets (the --patches file's sets that are no cheats) with a site this file patches too, +comma separated, or "-": the runtime switches them off while the mod is on, so the mod's code runs. + +A file the rules cannot express is unsupported as a whole: half of a patch is worse than none. + +Standard library, recomp/ and tools/cheats.py only. +""" +import argparse +import re +import struct +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from cheats import TITLE, Main, convert, fnv # noqa: E402 + +NSO_HEADER = 0x100 +EXEFS_PLACES = ('exefs', f'atmosphere/contents/{TITLE}/exefs', f'contents/{TITLE}/exefs', f'{TITLE}/exefs') + + +def patch_files(mods): + """[(mod folder name, path in the mod, file)] of the folders of mods, by name: the .ips and .pchtxt files of the + first exefs place that exists (runtime/mods.c find_root) and of atmosphere/exefs_patches/<name>/.""" + out = [] + for mod in sorted((d for d in mods.iterdir() if d.is_dir()), key=lambda d: d.name) if mods.is_dir() else []: + folders = [] + exefs = next((mod / w for w in EXEFS_PLACES if (mod / w).is_dir()), None) + if exefs: + folders.append(exefs) + patches = mod / 'atmosphere' / 'exefs_patches' + if patches.is_dir(): + folders += sorted(d for d in patches.iterdir() if d.is_dir()) + for folder in folders: + for f in sorted(folder.iterdir()): + if f.is_file() and f.suffix.lower() in ('.ips', '.pchtxt'): + out.append((mod.name, f.relative_to(mod).as_posix(), f)) + return out + + +def same_build(name, build_id): + """1 when name (hex, any case, trailing zeros left out or not) is main's build id (hex of its 32 bytes).""" + name = name.lower() + return bool(re.fullmatch(r'[0-9a-f]{1,64}', name)) and name.ljust(64, '0') == build_id.ljust(64, '0') + + +# ---- the formats: {offset in main's image: byte} + +def parse_ips(data): + """IPS (PATCH ... EOF, 3-byte offsets) or IPS32 (IPS32 ... EEOF, 4-byte offsets); ValueError when it is neither.""" + if data[:5] == b'PATCH': + width, end, at = 3, b'EOF', 5 + elif data[:5] == b'IPS32': + width, end, at = 4, b'EEOF', 5 + else: + raise ValueError('not an IPS or IPS32 file') + writes = {} + while True: + if data[at:at + len(end)] == end and at + len(end) >= len(data) - 3: + return writes + if at + width + 2 > len(data): + raise ValueError('the IPS file is cut short') + off = int.from_bytes(data[at:at + width], 'big') + size = int.from_bytes(data[at + width:at + width + 2], 'big') + at += width + 2 + if size: + body = data[at:at + size] + at += size + else: # a run: 2-byte length, 1 byte + run = int.from_bytes(data[at:at + 2], 'big') + body = data[at + 2:at + 3] * run + at += 3 + if len(body) != (size or len(body)): + raise ValueError('the IPS file is cut short') + for i, b in enumerate(body): + writes[off - NSO_HEADER + i] = b + + +def pchtxt_value(token): + """The bytes of a pchtxt value: hex digits in byte order, or a quoted string with C escapes.""" + if token.startswith('"'): + if not token.endswith('"') or len(token) < 2: + raise ValueError(f'a string without its closing quote: {token}') + return token[1:-1].encode('utf-8').decode('unicode_escape').encode('latin-1') + if len(token) % 2 or not re.fullmatch(r'[0-9A-Fa-f]+', token): + raise ValueError(f'not a hex value: {token}') + return bytes.fromhex(token) + + +def parse_pchtxt(text): + """(build id or None, {offset in main's image: byte}) of an IPSwitch file.""" + bid, shift, enabled, writes = None, 0, False, {} + for raw in text.splitlines(): + line = raw.split('//', 1)[0].strip() + if not line: + continue + if line.startswith('@stop'): + break + if line.startswith('@nsobid-'): + bid = line[len('@nsobid-'):].strip() + elif line.startswith('@enabled'): + enabled = True + elif line.startswith('@disabled'): + enabled = False + elif line.startswith('@flag'): + f = line.split() + if len(f) >= 3 and f[1] == 'offset_shift': + shift = int(f[2], 0) + elif line.startswith('@') or not enabled: + continue + else: + m = re.fullmatch(r'([0-9A-Fa-f]+)\s+(".*"|\S+)', line) + if not m: + continue # a title in brackets or a line of text + off = int(m.group(1), 16) + shift - NSO_HEADER + for i, b in enumerate(pchtxt_value(m.group(2))): + writes[off + i] = b + return bid, writes + + +# ---- writes to sites + +def word_writes(main, writes): + """{offset: word} of byte writes into main's code (.text and its padding), each word with the bytes the file does + not write kept; ValueError for a write into main's data.""" + words = {} + for off in sorted(writes): + if not main.in_code(off - off % 4): + raise ValueError(f'it writes main\'s data at main+{off:X} (only code patches are supported)') + a = off - off % 4 + w = words.get(a, main.word(a)) + shift = (off % 4) * 8 + words[a] = (w & ~(0xFF << shift) | writes[off] << shift) & 0xFFFFFFFF + return words + + +def builtin_sites(text): + """{offset in main: [set name]} of the patch file's sets that are no cheats or mod patches.""" + out = {} + for line in text.splitlines(): + f = line.split('#', 1)[0].split() + if len(f) >= 4 and f[0] == 'site' and f[2] == 'main' and not f[1].startswith(('cheat_', 'mod_')): + out.setdefault(int(f[3], 16), []).append(f[1]) + return out + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('--exefs', type=Path, required=True) + ap.add_argument('--mods', type=Path, required=True, help='the mods folder next to totk.exe') + ap.add_argument('--patches', type=Path, required=True, help='the patch file so far') + ap.add_argument('--out', type=Path, required=True, help='the patch file with the mods\' ExeFS patches added') + ap.add_argument('--status', type=Path, required=True) + args = ap.parse_args() + sys.stdout.reconfigure(errors='replace') + m = Main(args.exefs / 'main') + raw = (args.exefs / 'main').read_bytes() + bid = raw[0x40:0x60].hex() + text = args.patches.read_text() + builtin = builtin_sites(text) + files = patch_files(args.mods) + sets, sites, status, kinds = [], [], [], {} + for mod, rel, f in files: + data = f.read_bytes() + key, content = fnv(f'{mod}/{rel}'.encode()), fnv(data) + kind, note, over, file_sites = 'code', '', [], {} + try: + if f.suffix.lower() == '.ips': + for_main = same_build(f.stem, bid) + writes = parse_ips(data) if for_main else {} + else: + pid, writes = parse_pchtxt(data.decode('utf-8-sig', errors='replace')) + for_main = pid is not None and same_build(pid, bid) + if not for_main: + kind = 'other' + else: + file_sites = convert(m, word_writes(m, writes)) + if not file_sites: + kind = 'empty' + except ValueError as e: + kind, note, file_sites = 'unsupported', str(e), {} + if file_sites: + name = f'mod_{key.lower()}' + label = f'{mod}/{rel}'.encode('ascii', 'replace').decode() + sets.append(f'set {name} TOTK_MODPATCH_{key} 0 # {label}') + for a, repl in sorted(file_sites.items()): + sites.append(f'site {name} main {a:x} {m.word(a):08x} {" ".join(f"{w:08x}" for w in repl)}') + over += [s for s in builtin.get(a, []) if s not in over] + kinds[kind] = kinds.get(kind, 0) + 1 + status.append(f'{key}\t{content}\t{kind}\t{",".join(over) or "-"}\t{note.replace(chr(9), " ")}') + print(f'exefs: {kind:11} {mod}/{rel}' + (f' ({note})' if note else '') + + (f' ({len(file_sites)} sites, over {", ".join(over)})' if over else + f' ({len(file_sites)} sites)' if file_sites else '')) + if sets: + text += ('\n# ExeFS patches of the mods folder (tools/exefs_patches.py)\n' + '\n'.join(sets) + '\n' + + '\n'.join(sites) + '\n') + args.out.write_text(text) + args.status.parent.mkdir(parents=True, exist_ok=True) + args.status.write_text('\n'.join(status) + ('\n' if status else ''), encoding='utf-8') + print(f'exefs: {len(files)} patch files: ' + ', '.join(f'{n} {k}' for k, n in sorted(kinds.items())) + + f'; {len(sites)} sites') + + +if __name__ == '__main__': + main() diff --git a/tools/extract.py b/tools/extract.py index 839cb35c..5c9bf407 100644 --- a/tools/extract.py +++ b/tools/extract.py @@ -143,7 +143,10 @@ def romfs_verified(romfs, expected): index = romfs / 'index.json' if not index.is_file() or not (romfs / 'romfs.bin').is_file(): return False - report = json.loads(index.read_text()) + try: + report = json.loads(index.read_text()) + except ValueError: # damaged on disk (a flipped bit made it unreadable): extract again instead of failing the setup + return False return report.get('romfs_sha256') == expected['romfs']['sha256'] and report.get('romfs_size') == expected['romfs']['size'] diff --git a/tools/fetch_refs.sh b/tools/fetch_refs.sh index 6addaa70..7c6a2f03 100644 --- a/tools/fetch_refs.sh +++ b/tools/fetch_refs.sh @@ -31,10 +31,10 @@ fetch() { echo "$name: $commit" } -fetch LASRecomp https://github.com/peiikai/LASRecomp.git da35b83a9611d046a9a681aae668d7085954d37f -fetch MacNativeGames https://github.com/ly05010419/MacNativeGames.git b75be8cfa704df3701e86130c8ced268e79d6b11 history -fetch d2r-mobile https://github.com/jhaizhou-ops/d2r-mobile.git 53e7c5bb9ca3ea53e2160005be8fbe920cd9cb53 -fetch nxnative https://github.com/seedds/nxnative.git d5b380b3a80495609c2e751bb994911a638935ad +fetch LASRecomp https://github.com/peiikai/LASRecomp.git a6b4f03844ecfb4012f18deff96da8b410aeafcd +fetch MacNativeGames https://github.com/ly05010419/MacNativeGames.git d1509b4b62211265fad5449cf53cc9f13484ecc5 history +fetch d2r-mobile https://github.com/jhaizhou-ops/d2r-mobile.git fae8c680fe98f79179cf3fab85f7a6c14ee28600 +fetch nxnative https://github.com/seedds/nxnative.git 99330eb81c873c3aafe9fc92194c9687d77fcdbb fetch SwitchRecomp https://github.com/bgyss/SwitchRecomp.git 0f99e1c5c1e78fdc8e01e58a0f8dc8622e82976b fetch deko3d https://github.com/devkitPro/deko3d.git 373bfd43d8b2d805fa2eb7d406dbb823032df958 -fetch SavageWindRecomp https://github.com/Revivedx/SavageWindRecomp.git 575ad88e02f9a89a9a885d90039d48af33c982f4 +fetch SavageWindRecomp https://github.com/Revivedx/SavageWindRecomp.git bf6146c14c37fee8781a601e5a544b93dff6d75e diff --git a/tools/make_dlaa_mask.py b/tools/make_dlaa_mask.py new file mode 100644 index 00000000..8dfa00d0 --- /dev/null +++ b/tools/make_dlaa_mask.py @@ -0,0 +1,203 @@ +"""Writes runtime/dlaa_mask_spv.h: the SPIR-V of DLSS's reactive mask pass (vk_dlaa.c, mask_pass), assembled here like +tools/make_mvobj_fs.py (no GLSL compiler on the machine this was written on). + +The shader, as GLSL: + + #version 450 + layout(local_size_x = 8, local_size_y = 8) in; + layout(binding = 0) uniform sampler2D before; // the light target when the transparents start + layout(binding = 1) uniform sampler2D after; // the light target when DLSS runs + layout(binding = 2, r8) uniform writeonly image2D mask; + layout(push_constant) uniform P { vec4 p; }; // render size xy, threshold z, gain w + void main() { + vec2 g = vec2(gl_GlobalInvocationID.xy); + if (g.x < p.x && g.y < p.y) { + ivec2 c = ivec2(gl_GlobalInvocationID.xy); + vec3 b = texelFetch(before, c, 0).rgb, a = texelFetch(after, c, 0).rgb; + vec3 d = abs(a - b); + float dm = max(max(d.r, d.g), d.b); + float m = max(max(max(a.r, a.g), a.b), max(max(b.r, b.g), b.b)); + float r = dm / max(m, 1e-4); + imageStore(mask, c, vec4(clamp((r - p.z) * p.w, 0.0, 1.0))); + } + } + +The mask is the share of a pixel's colour the transparents changed (particles, smoke, sparks: what has no motion +vectors of its own), 0 where nothing was drawn over the solid scene. + + python tools/make_dlaa_mask.py +""" +import struct +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +OUT = ROOT / 'runtime' / 'dlaa_mask_spv.h' + +SECTIONS = ('cap', 'ext', 'mem', 'entry', 'exec', 'dec', 'types', 'func') + + +class Asm: + def __init__(self): + self.next = 1 + self.sections = {k: [] for k in SECTIONS} + + def id(self): + self.next += 1 + return self.next - 1 + + def op(self, sec, code, *words): + self.sections[sec].append([(len(words) + 1) << 16 | code, *words]) + + def words(self): + out = [0x07230203, 0x00010000, 0, self.next, 0] + for sec in SECTIONS: + for ins in self.sections[sec]: + out.extend(ins) + return out + + +def string(s): + b = s.encode() + b'\0' + b += b'\0' * (-len(b) % 4) + return list(struct.unpack('<%dI' % (len(b) // 4), b)) + + +def f32(x): + return struct.unpack('<I', struct.pack('<f', x))[0] + + +OpCapability, OpExtInstImport, OpExtInst, OpMemoryModel, OpEntryPoint, OpExecutionMode = 17, 11, 12, 14, 15, 16 +OpDecorate, OpMemberDecorate = 71, 72 +OpTypeVoid, OpTypeBool, OpTypeInt, OpTypeFloat, OpTypeVector, OpTypeImage, OpTypeSampledImage = 19, 20, 21, 22, 23, 25, 27 +OpTypeStruct, OpTypePointer, OpTypeFunction = 30, 32, 33 +OpConstant, OpConstantComposite, OpVariable, OpFunction, OpFunctionEnd, OpLabel, OpReturn = 43, 44, 59, 54, 56, 248, 253 +OpLoad, OpAccessChain, OpVectorShuffle, OpCompositeExtract, OpCompositeConstruct = 61, 65, 79, 81, 80 +OpImage, OpImageFetch, OpImageWrite, OpConvertUToF, OpBitcast = 100, 95, 99, 112, 124 +OpFSub, OpFMul, OpFDiv, OpFOrdLessThan, OpLogicalAnd = 131, 133, 136, 184, 167 +OpSelectionMerge, OpBranch, OpBranchConditional = 247, 249, 250 +FAbs, FMax, FClamp = 4, 40, 43 # GLSL.std.450 +UniformConstant, Input, PushConstant = 0, 1, 9 +Binding, DescriptorSet, Block, Offset, BuiltIn, NonReadable = 33, 34, 2, 35, 11, 25 +GlobalInvocationId = 28 + +a = Asm() +a.op('cap', OpCapability, 1) # Shader +a.op('cap', OpCapability, 49) # StorageImageExtendedFormats (r8) +glsl = a.id() +a.op('ext', OpExtInstImport, glsl, *string('GLSL.std.450')) +a.op('mem', OpMemoryModel, 0, 1) # Logical GLSL450 +main = a.id() +t_void, t_fn, t_bool, t_f, t_i, t_u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id() +t_v2, t_v3, t_v4, t_v2i, t_v2u, t_v3u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id() +t_img, t_simg, t_wimg = a.id(), a.id(), a.id() +t_psimg, t_pwimg, t_pgid, t_pc, t_ppc, t_ppv4 = a.id(), a.id(), a.id(), a.id(), a.id(), a.id() +v_before, v_after, v_mask, v_gid, v_p = a.id(), a.id(), a.id(), a.id(), a.id() +c_i0, c_zero, c_one, c_eps = a.id(), a.id(), a.id(), a.id() + +a.op('entry', OpEntryPoint, 5, main, *string('main'), v_gid) # GLCompute +a.op('exec', OpExecutionMode, main, 17, 8, 8, 1) # LocalSize +a.op('dec', OpDecorate, v_gid, BuiltIn, GlobalInvocationId) +for var, binding in ((v_before, 0), (v_after, 1), (v_mask, 2)): + a.op('dec', OpDecorate, var, DescriptorSet, 0) + a.op('dec', OpDecorate, var, Binding, binding) +a.op('dec', OpDecorate, v_mask, NonReadable) +a.op('dec', OpDecorate, t_pc, Block) +a.op('dec', OpMemberDecorate, t_pc, 0, Offset, 0) + +T = 'types' +a.op(T, OpTypeVoid, t_void) +a.op(T, OpTypeFunction, t_fn, t_void) +a.op(T, OpTypeBool, t_bool) +a.op(T, OpTypeFloat, t_f, 32) +a.op(T, OpTypeInt, t_i, 32, 1) +a.op(T, OpTypeInt, t_u, 32, 0) +a.op(T, OpTypeVector, t_v2, t_f, 2) +a.op(T, OpTypeVector, t_v3, t_f, 3) +a.op(T, OpTypeVector, t_v4, t_f, 4) +a.op(T, OpTypeVector, t_v2i, t_i, 2) +a.op(T, OpTypeVector, t_v2u, t_u, 2) +a.op(T, OpTypeVector, t_v3u, t_u, 3) +a.op(T, OpTypeImage, t_img, t_f, 1, 0, 0, 0, 1, 0) # 2D, sampled, format unknown +a.op(T, OpTypeSampledImage, t_simg, t_img) +a.op(T, OpTypeImage, t_wimg, t_f, 1, 0, 0, 0, 2, 15) # 2D, storage, R8 +a.op(T, OpTypePointer, t_psimg, UniformConstant, t_simg) +a.op(T, OpTypePointer, t_pwimg, UniformConstant, t_wimg) +a.op(T, OpTypePointer, t_pgid, Input, t_v3u) +a.op(T, OpTypeStruct, t_pc, t_v4) +a.op(T, OpTypePointer, t_ppc, PushConstant, t_pc) +a.op(T, OpTypePointer, t_ppv4, PushConstant, t_v4) +a.op(T, OpConstant, t_i, c_i0, 0) +a.op(T, OpConstant, t_f, c_zero, f32(0.0)) +a.op(T, OpConstant, t_f, c_one, f32(1.0)) +a.op(T, OpConstant, t_f, c_eps, f32(1e-4)) +a.op(T, OpVariable, t_psimg, v_before, UniformConstant) +a.op(T, OpVariable, t_psimg, v_after, UniformConstant) +a.op(T, OpVariable, t_pwimg, v_mask, UniformConstant) +a.op(T, OpVariable, t_pgid, v_gid, Input) +a.op(T, OpVariable, t_ppc, v_p, PushConstant) + +F = 'func' + + +def ins(code, rtype, *args): + r = a.id() + a.op(F, code, rtype, r, *args) + return r + + +def ext(rtype, inst, *args): + return ins(OpExtInst, rtype, glsl, inst, *args) + + +l_entry, l_body, l_merge = a.id(), a.id(), a.id() +a.op(F, OpFunction, t_void, main, 0, t_fn) +a.op(F, OpLabel, l_entry) +pp = ins(OpAccessChain, t_ppv4, v_p, c_i0) +p = ins(OpLoad, t_v4, pp) +gid = ins(OpLoad, t_v3u, v_gid) +gu = ins(OpVectorShuffle, t_v2u, gid, gid, 0, 1) +gx = ins(OpConvertUToF, t_f, ins(OpCompositeExtract, t_u, gid, 0)) +gy = ins(OpConvertUToF, t_f, ins(OpCompositeExtract, t_u, gid, 1)) +px = ins(OpCompositeExtract, t_f, p, 0) +py = ins(OpCompositeExtract, t_f, p, 1) +inside = ins(OpLogicalAnd, t_bool, ins(OpFOrdLessThan, t_bool, gx, px), ins(OpFOrdLessThan, t_bool, gy, py)) +a.op(F, OpSelectionMerge, l_merge, 0) +a.op(F, OpBranchConditional, inside, l_body, l_merge) +a.op(F, OpLabel, l_body) +c = ins(OpBitcast, t_v2i, gu) + + +def fetch(var): + img = ins(OpImage, t_img, ins(OpLoad, t_simg, var)) + texel = ins(OpImageFetch, t_v4, img, c, 2, c_i0) # Lod 0 + return ins(OpVectorShuffle, t_v3, texel, texel, 0, 1, 2) + + +def maxc(v): + x, y, z = (ins(OpCompositeExtract, t_f, v, k) for k in range(3)) + return ext(t_f, FMax, ext(t_f, FMax, x, y), z) + + +b = fetch(v_before) +af = fetch(v_after) +d = ext(t_v3, FAbs, ins(OpFSub, t_v3, af, b)) +dm = maxc(d) +m = ext(t_f, FMax, ext(t_f, FMax, maxc(af), maxc(b)), c_eps) +r = ins(OpFDiv, t_f, dm, m) +thr = ins(OpCompositeExtract, t_f, p, 2) +gain = ins(OpCompositeExtract, t_f, p, 3) +k = ext(t_f, FClamp, ins(OpFMul, t_f, ins(OpFSub, t_f, r, thr), gain), c_zero, c_one) +out = ins(OpCompositeConstruct, t_v4, k, k, k, k) +a.op(F, OpImageWrite, ins(OpLoad, t_wimg, v_mask), c, out) +a.op(F, OpBranch, l_merge) +a.op(F, OpLabel, l_merge) +a.op(F, OpReturn) +a.op(F, OpFunctionEnd) + +w = a.words() +lines = [', '.join(f'0x{x:08x}' for x in w[i:i + 8]) for i in range(0, len(w), 8)] +OUT.write_text( + '// Generated by tools/make_dlaa_mask.py: DLSS\'s reactive mask pass (vk_dlaa.c mask_pass). Do not edit.\n' + '#pragma once\n#include <stdint.h>\n' + 'static const uint32_t dlaa_mask_spv[] = {\n ' + ',\n '.join(lines) + '\n};\n', newline='\n') +print(f'{OUT}: {len(w)} words') diff --git a/tools/make_mvobj_fs.py b/tools/make_mvobj_fs.py new file mode 100644 index 00000000..58f5e3c9 --- /dev/null +++ b/tools/make_mvobj_fs.py @@ -0,0 +1,163 @@ +"""Writes runtime/vk_mvobj_fs.h: the SPIR-V of the per-object motion fragment shader (vk_mvobj.c), assembled here +because it must be patched at run time (the Location of its input follows the vertex shader variant) and is small. + +The shader, as GLSL: + + #version 450 + layout(location = 0) in vec4 prev; // last frame's clip position (patch 0015); Location patched + layout(set = 0, binding = 1, std140) uniform P { vec4 p; }; // render size xy, this frame's jitter zw (pixels) + layout(location = 0) out vec4 o; + void main() { + vec2 win = gl_FragCoord.xy - p.zw; // this frame's window position, without the jitter + vec2 pn = prev.xy / prev.w; + vec2 pwin = vec2((pn.x * 0.5 + 0.5) * p.x, (0.5 - pn.y * 0.5) * p.y); + o = prev.w > 1e-6 ? vec4(pwin - win, 0.0, 0.0) : vec4(0.0); + } + +The vector is last frame's window position minus this frame's, in render pixels, as runtime/shaders/dlaa_mv.comp +writes it. Its descriptor set and binding are renumbered by vk_pipeline.c like a translated shader's (the .res.json +text in vk_mvobj.c names binding 1 as slot 3). + + python tools/make_mvobj_fs.py +""" +import struct +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +OUT = ROOT / 'runtime' / 'vk_mvobj_fs.h' + + +class Asm: + def __init__(self): + self.next = 1 + self.sections = {k: [] for k in ('cap', 'mem', 'entry', 'exec', 'dec', 'types', 'func')} + self.loc_word = None # index of the patched Location literal in the final words + + def id(self): + self.next += 1 + return self.next - 1 + + def op(self, sec, code, *words): + self.sections[sec].append([(len(words) + 1) << 16 | code, *words]) + return self.sections[sec][-1] + + def words(self): + out = [0x07230203, 0x00010000, 0, self.next, 0] + for sec in ('cap', 'mem', 'entry', 'exec', 'dec', 'types', 'func'): + for ins in self.sections[sec]: + if ins is self.loc_ins: + self.loc_word = len(out) + 3 + out.extend(ins) + return out + + +def string(s): + b = s.encode() + b'\0' + b += b'\0' * (-len(b) % 4) + return list(struct.unpack('<%dI' % (len(b) // 4), b)) + + +def f32(x): + return struct.unpack('<I', struct.pack('<f', x))[0] + + +a = Asm() +OpCapability, OpMemoryModel, OpEntryPoint, OpExecutionMode, OpDecorate, OpMemberDecorate = 17, 14, 15, 16, 71, 72 +OpTypeVoid, OpTypeFunction, OpTypeFloat, OpTypeVector, OpTypePointer, OpTypeStruct, OpTypeInt, OpTypeBool = 19, 33, 22, 23, 32, 30, 21, 20 +OpConstant, OpConstantComposite, OpVariable, OpFunction, OpLabel, OpLoad, OpStore, OpReturn, OpFunctionEnd = 43, 44, 59, 54, 248, 61, 62, 253, 56 +OpAccessChain, OpVectorShuffle, OpCompositeExtract, OpCompositeConstruct, OpFSub, OpFMul, OpFAdd, OpFDiv = 65, 79, 81, 80, 131, 133, 129, 136 +OpFOrdGreaterThan, OpSelect = 186, 169 +Input, Output, Uniform = 1, 3, 2 +Location, Binding, DescriptorSet, Block, Offset, BuiltIn = 30, 33, 34, 2, 35, 11 +FragCoord = 15 + +a.op('cap', OpCapability, 1) # Shader +a.op('mem', OpMemoryModel, 0, 1) # Logical GLSL450 +main = a.id() +t_void, t_fn, t_f, t_v2, t_v4, t_u = a.id(), a.id(), a.id(), a.id(), a.id(), a.id() +t_bool = a.id() +t_pin, t_pout, t_struct, t_pu, t_pf_u = a.id(), a.id(), a.id(), a.id(), a.id() +v_prev, v_frag, v_out, v_p = a.id(), a.id(), a.id(), a.id() +c_half, c_zero, c_eps, c_u0, c_vzero = a.id(), a.id(), a.id(), a.id(), a.id() +t_pv4u = a.id() +a.op('entry', OpEntryPoint, 4, main, *string('main'), v_prev, v_frag, v_out) # Fragment +a.op('exec', OpExecutionMode, main, 7) # OriginUpperLeft +a.loc_ins = a.op('dec', OpDecorate, v_prev, Location, 0) +a.op('dec', OpDecorate, v_frag, BuiltIn, FragCoord) +a.op('dec', OpDecorate, v_out, Location, 0) +a.op('dec', OpDecorate, t_struct, Block) +a.op('dec', OpMemberDecorate, t_struct, 0, Offset, 0) +a.op('dec', OpDecorate, v_p, DescriptorSet, 0) +a.op('dec', OpDecorate, v_p, Binding, 1) +a.op('types', OpTypeVoid, t_void) +a.op('types', OpTypeFunction, t_fn, t_void) +a.op('types', OpTypeFloat, t_f, 32) +a.op('types', OpTypeVector, t_v2, t_f, 2) +a.op('types', OpTypeVector, t_v4, t_f, 4) +a.op('types', OpTypeInt, t_u, 32, 0) +a.op('types', OpTypeBool, t_bool) +a.op('types', OpTypePointer, t_pin, Input, t_v4) +a.op('types', OpTypePointer, t_pout, Output, t_v4) +a.op('types', OpTypeStruct, t_struct, t_v4) +a.op('types', OpTypePointer, t_pu, Uniform, t_struct) +a.op('types', OpTypePointer, t_pv4u, Uniform, t_v4) +a.op('types', OpConstant, t_f, c_half, f32(0.5)) +a.op('types', OpConstant, t_f, c_zero, f32(0.0)) +a.op('types', OpConstant, t_f, c_eps, f32(1e-6)) +a.op('types', OpConstant, t_u, c_u0, 0) +a.op('types', OpConstantComposite, t_v4, c_vzero, c_zero, c_zero, c_zero, c_zero) +a.op('types', OpVariable, t_pin, v_prev, Input) +a.op('types', OpVariable, t_pin, v_frag, Input) +a.op('types', OpVariable, t_pout, v_out, Output) +a.op('types', OpVariable, t_pu, v_p, Uniform) + +F = 'func' +a.op(F, OpFunction, t_void, main, 0, t_fn) +a.op(F, OpLabel, a.id()) +pp = a.id(); a.op(F, OpAccessChain, t_pv4u, pp, v_p, c_u0) +p = a.id(); a.op(F, OpLoad, t_v4, p, pp) +prev = a.id(); a.op(F, OpLoad, t_v4, prev, v_prev) +frag = a.id(); a.op(F, OpLoad, t_v4, frag, v_frag) +fxy = a.id(); a.op(F, OpVectorShuffle, t_v2, fxy, frag, frag, 0, 1) +jit = a.id(); a.op(F, OpVectorShuffle, t_v2, jit, p, p, 2, 3) +win = a.id(); a.op(F, OpFSub, t_v2, win, fxy, jit) +px, py, pw = a.id(), a.id(), a.id() +a.op(F, OpCompositeExtract, t_f, px, prev, 0) +a.op(F, OpCompositeExtract, t_f, py, prev, 1) +a.op(F, OpCompositeExtract, t_f, pw, prev, 3) +nx, ny = a.id(), a.id() +a.op(F, OpFDiv, t_f, nx, px, pw) +a.op(F, OpFDiv, t_f, ny, py, pw) +sx, sy = a.id(), a.id() +a.op(F, OpCompositeExtract, t_f, sx, p, 0) +a.op(F, OpCompositeExtract, t_f, sy, p, 1) +# x: (nx * 0.5 + 0.5) * sx +x1, x2, x3 = a.id(), a.id(), a.id() +a.op(F, OpFMul, t_f, x1, nx, c_half) +a.op(F, OpFAdd, t_f, x2, x1, c_half) +a.op(F, OpFMul, t_f, x3, x2, sx) +# y: (0.5 - ny * 0.5) * sy +y1, y2, y3 = a.id(), a.id(), a.id() +a.op(F, OpFMul, t_f, y1, ny, c_half) +a.op(F, OpFSub, t_f, y2, c_half, y1) +a.op(F, OpFMul, t_f, y3, y2, sy) +wx, wy = a.id(), a.id() +a.op(F, OpCompositeExtract, t_f, wx, win, 0) +a.op(F, OpCompositeExtract, t_f, wy, win, 1) +mx, my = a.id(), a.id() +a.op(F, OpFSub, t_f, mx, x3, wx) +a.op(F, OpFSub, t_f, my, y3, wy) +mv = a.id(); a.op(F, OpCompositeConstruct, t_v4, mv, mx, my, c_zero, c_zero) +ok = a.id(); a.op(F, OpFOrdGreaterThan, t_bool, ok, pw, c_eps) +res = a.id(); a.op(F, OpSelect, t_v4, res, ok, mv, c_vzero) +a.op(F, OpStore, v_out, res) +a.op(F, OpReturn) +a.op(F, OpFunctionEnd) + +w = a.words() +lines = [', '.join(f'0x{x:08x}' for x in w[i:i + 8]) for i in range(0, len(w), 8)] +OUT.write_text( + '// Generated by tools/make_mvobj_fs.py: the per-object motion fragment shader (vk_mvobj.c). Do not edit.\n' + f'#define MVOBJ_FS_LOCATION_WORD {a.loc_word} // the input Location literal, patched to the variant\'s\n' + 'static const uint32_t mvobj_fs_spv[] = {\n ' + ',\n '.join(lines) + '\n};\n', newline='\n') +print(f'{OUT}: {len(w)} words, Location at word {a.loc_word}') diff --git a/tools/make_release.py b/tools/make_release.py index 11bf7e0f..c5fa64a0 100644 --- a/tools/make_release.py +++ b/tools/make_release.py @@ -10,8 +10,9 @@ cached in toolchain/release-dl/ and checked against the sha256 in PINS; a mismat files come out of the python.org installer's own component MSI, unpacked with msiexec /a and expand.exe; the installer is never run) llvm/ llvm-mingw, x86_64 only, the files clang and lld need to compile and link totk.exe - runtime/ libtotkrt.a (build_game.py --make-runtime-lib with llvm/), the runtime headers, romfs.c, patches.c and - ov_zstd.c (compiled per install), deps/ (headers and import libraries of SDL3, Opus, libvpx, Vulkan and VMA) + runtime/ libtotkrt.a and libtotkrt-v2.a (build_game.py --make-runtime-lib with llvm/, x86-64-v3 and v2), the + runtime headers, romfs.c, patches.c and ov_zstd.c (compiled per install), deps/ (headers and import + libraries of SDL3, Opus, libvpx, Vulkan and VMA) bin/ totk_xlate.exe (+ .id), hactool.exe, SDL3.dll (upstream), libopus-0.dll, ninja.exe recomp/, tools/, data/, installer/, licenses/, THIRD_PARTY_NOTICES.md, totk.ini.example VERSION the repository's VERSION plus the commit, 0.1.0+973c626; the installer's release string @@ -24,6 +25,8 @@ Both need one game version extracted and lifted first (tools/extract.py, recomp/ it, generated/v143, and for --prove its build/extracted/<version> (they are read, nothing is written to them). The runtime library reads only what both versions' layout.h share (MOD_COUNT, MOD_MAIN_BASE), so either serves. +A release also needs build/xlate built from the current sources (tools/build_xlate.py); the script stops first if not. + The folder contains nothing derived from the game: check_content runs over it before the script finishes. Then the folder's own python runs a check-only installer pass (no game files: it must get through the release, toolchain and host checks and stop at the missing game files) and the window's --selftest. @@ -44,6 +47,7 @@ import zipfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) +import build_xlate # noqa: E402 import check_content # noqa: E402 import shader_list # noqa: E402 @@ -101,9 +105,10 @@ LLVM_SKIP_DIRS = {'aarch64-w64-mingw32', 'armv7-w64-mingw32', 'i686-w64-mingw32' LLVM_SKIP_SUFFIXES = {'.idl', '.tlb'} # tools/ files the installer steps run (and what they import), copied from the repo. -TOOLS_FILES = ['build_game.py', 'build_shaders.py', 'check_content.py', 'extract.py', 'hostmem.py', 'make_romfs_patches.py', - 'pack_shader_cache.py', 'prepare_shaders.py', 'romfs_index.py', 'shader_census.py', 'shader_list.py', - 'ultrawide_layout.py'] +TOOLS_FILES = ['build_game.py', 'build_shaders.py', 'button_icons.py', 'check_content.py', 'check_isa.py', 'extract.py', + 'hostmem.py', 'make_romfs_patches.py', 'ncz.py', 'pack_shader_cache.py', 'prepare_shaders.py', + 'romfs_index.py', 'shader_census.py', 'shader_list.py', 'ultrawide_layout.py', 'cheats.py', + 'exefs_patches.py'] TOOLS_DIRS = ['vendor/macnativegames', 'data'] # data/: expected hashes RUNTIME_SUFFIXES = {'.h', '.inc', '.ico'} # .ico: the installer window icon @@ -260,7 +265,7 @@ def build_python(out): copy_tree(tk / 'Lib' / 'tkinter', py / 'tkinter', lambda r: r.startswith('__pycache__')) copy_tree(tk / 'tcl', py / 'tcl', lambda r: r.endswith(('.lib', '.sh')) or r.startswith('nmake')) check = subprocess.run([str(py / 'python.exe'), '-I', '-c', - 'import tkinter, numpy, lz4.block, cryptography, cffi, pycparser; ' + 'import tkinter, numpy, lz4.block, cryptography, cffi, pycparser, fontTools.ttLib; ' 'print("python ok: Tcl", tkinter.Tcl().eval("info patchlevel"), "numpy", numpy.__version__)'], env=clean_env(py), capture_output=True, text=True) if check.returncode: @@ -292,6 +297,17 @@ def build_llvm(out): # ---- bin/ and deps --------------------------------------------------------------------------------------------------- +def check_translator(): + """Stop on a translator older than the sources. build/xlate is built by hand and only copied here (the Linux release + builds its own), so without a rebuild a translator change reaches no Windows release, and nothing fails: the runtime + sends job fields the old exe ignores, as happened to gather_bias.""" + idf = MAIN_ROOT / 'build' / 'xlate' / 'totk_xlate.id' + have = idf.read_text(encoding='ascii').strip() if idf.is_file() else 'none' + want = build_xlate.identity() + if have != want: + sys.exit(f'build/xlate has translator {have[:16]}, the sources are {want[:16]}: run tools/build_xlate.py') + + def build_bin(out): b = out / 'bin' b.mkdir(parents=True, exist_ok=True) @@ -363,15 +379,18 @@ def lifted_version(): def runtime_lib(out, llvm): - """Build libtotkrt.a with the folder's llvm/ and copy it, the headers and the install-side sources into runtime/.""" - build = MAIN_ROOT / 'build' / 'release-check' / 'rtlib' + """Build libtotkrt.a (x86-64-v3) and libtotkrt-v2.a (x86-64-v2) with the folder's llvm/, and copy them, the headers + and the install-side sources into runtime/.""" version = lifted_version() gen = MAIN_ROOT / 'generated' / version - run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--version', version, - '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja.exe', '--gen', gen, '--out', build], - env=clean_env(llvm / 'bin', out / 'bin')) rt = out / 'runtime' - shutil.copy2(build / 'libtotkrt.a', rt / 'libtotkrt.a') + for cpu, name in (('v3', 'libtotkrt.a'), ('v2', 'libtotkrt-v2.a')): + build = MAIN_ROOT / 'build' / 'release-check' / f'rtlib-{cpu}' + run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--cpu', cpu, + '--version', version, '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja.exe', + '--gen', gen, '--out', build], + env=clean_env(llvm / 'bin', out / 'bin')) + shutil.copy2(build / 'libtotkrt.a', rt / name) for p in sorted((ROOT / 'runtime').rglob('*')): if p.suffix in RUNTIME_SUFFIXES and p.is_file(): (rt / p.relative_to(ROOT / 'runtime')).parent.mkdir(parents=True, exist_ok=True) @@ -424,12 +443,14 @@ def c_wide(text): def launcher_header(path): - """launcher_text.h: the launcher's two messages in English and Russian, taken from the installer's catalog.""" - lines = ['/* generated by tools/make_release.py from installer/text/{en,ru}.json */'] - for lang in ('en', 'ru'): + """launcher_text.h: the launcher's two messages in English, Russian, Brazilian Portuguese and Simplified Chinese, + taken from the installer's catalog (LAUNCHER_TITLE_PT_BR for pt-BR.json, LAUNCHER_TITLE_ZH_HANS for zh-Hans.json).""" + lines = ['/* generated by tools/make_release.py from installer/text/{en,ru,pt-BR,zh-Hans}.json */'] + for lang in ('en', 'ru', 'pt-BR', 'zh-Hans'): texts = json.loads((ROOT / 'installer' / 'text' / f'{lang}.json').read_text(encoding='utf-8')) for key in ('title', 'python_missing'): - lines.append(f'#define LAUNCHER_{key.upper()}_{lang.upper()} {c_wide(texts["launcher." + key])}') + macro = f'LAUNCHER_{key.upper()}_{lang.upper().replace("-", "_")}' + lines.append(f'#define {macro} {c_wide(texts["launcher." + key])}') path.parent.mkdir(parents=True, exist_ok=True) path.write_text('\n'.join(lines) + '\n', encoding='ascii') @@ -498,7 +519,7 @@ def write_zip(out, path): print(f'{path}: {mib(path.stat().st_size)}') -def prove(out): +def prove(out, cpu='v3'): """Compile the generated C and link totk.exe with the folder's tools only, then run the probes next to it.""" out = out.resolve() version = lifted_version() @@ -507,8 +528,9 @@ def prove(out): chk = MAIN_ROOT / 'build' / 'release-check' env = clean_env(out / 'python', out / 'llvm' / 'bin', out / 'bin') t0 = time.time() - run([out / 'python' / 'python.exe', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', - '--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime', '--runtime-lib', out / 'runtime' / 'libtotkrt.a', + run([out / 'python' / 'python.exe', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', '--cpu', cpu, + '--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime', + '--runtime-lib', out / 'runtime' / ('libtotkrt-v2.a' if cpu == 'v2' else 'libtotkrt.a'), '--ninja', out / 'bin' / 'ninja.exe', '--version', version, '--gen', gen, '--out', chk, '--exefs', ext / 'exefs', '--romfs', ext / 'romfs', '--profile', chk / 'profile'], env=env) print(f'prove: build {time.time() - t0:.0f} s') @@ -531,11 +553,14 @@ def main(): ap.add_argument('--out', type=Path, required=True, help='release folder (replaced)') ap.add_argument('--zip', type=Path, help='also write the folder as this zip') ap.add_argument('--prove', action='store_true', help='only run the compile-and-probe check on an existing folder') + ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3', + help='--prove: build and run the check for this CPU level (default v3)') args = ap.parse_args() out = args.out.resolve() if args.prove: - prove(out) + prove(out, args.cpu) return + check_translator() if out.exists(): shutil.rmtree(out) out.mkdir(parents=True) diff --git a/tools/make_release_linux.py b/tools/make_release_linux.py index 64428053..07a77c94 100644 --- a/tools/make_release_linux.py +++ b/tools/make_release_linux.py @@ -14,9 +14,9 @@ the .NET 10 SDK and Python 3.12+ with Pillow (.venv). The folder holds: file puts the folder, tools/ and recomp/ on sys.path llvm/ LLVM 23 (clang, lld, llvm-ar, the clang resource headers) and llvm/sysroot: the files of the sniper SDK sysroot the game's compile and link read, nothing else (recorded while building the runtime library) - runtime/ libtotkrt.a (build_game.py --make-runtime-lib --sysroot), the runtime headers, romfs.c, patches.c and - ov_zstd.c (compiled per install), totk.png (the window icon), deps/ (Vulkan headers 1.4.363, VMA, zstd - built static; SDL3, Opus and libvpx come from the sysroot) + runtime/ libtotkrt.a and libtotkrt-v2.a (build_game.py --make-runtime-lib --sysroot, x86-64-v3 and v2), the + runtime headers, romfs.c, patches.c and ov_zstd.c (compiled per install), totk.png (the window icon), + deps/ (Vulkan headers 1.4.363, VMA, zstd built static; SDL3, Opus and libvpx come from the sysroot) bin/ totk_xlate (+ .id), hactool, ninja, libSDL3.so.0 (the sysroot's; the game finds it beside itself) recomp/, tools/, data/, installer/, licenses/, THIRD_PARTY_NOTICES.md, totk.ini.example, VERSION AppRun, totk-setup.desktop, totk-setup.png the AppImage entry: python/bin/python3 -m installer.gui @@ -205,14 +205,16 @@ def build_bin(out, llvm, sysroot): # ---- runtime/ ---------------------------------------------------------------------------------------------------------- def runtime_lib(out, llvm, sysroot): - build = CHECK / 'rtlib' - shutil.rmtree(build, ignore_errors=True) + """libtotkrt.a (x86-64-v3) and libtotkrt-v2.a (x86-64-v2), so a machine without AVX2 can install too.""" version = mr.lifted_version() - run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, '--sysroot', sysroot, - '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja', '--version', version, - '--gen', MAIN_ROOT / 'generated' / version, '--out', build]) rt = out / 'runtime' - shutil.copy2(build / 'libtotkrt.a', rt / 'libtotkrt.a') + for cpu, name in (('v3', 'libtotkrt.a'), ('v2', 'libtotkrt-v2.a')): + build = CHECK / f'rtlib-{cpu}' + shutil.rmtree(build, ignore_errors=True) + run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm, + '--sysroot', sysroot, '--cpu', cpu, '--deps', out / 'runtime' / 'deps', '--ninja', out / 'bin' / 'ninja', + '--version', version, '--gen', MAIN_ROOT / 'generated' / version, '--out', build]) + shutil.copy2(build / 'libtotkrt.a', rt / name) for p in sorted((ROOT / 'runtime').rglob('*')): if p.suffix in mr.RUNTIME_SUFFIXES and p.is_file(): (rt / p.relative_to(ROOT / 'runtime')).parent.mkdir(parents=True, exist_ok=True) @@ -521,7 +523,7 @@ def check_bundle(out): print('bundle check: the window built every screen in both languages and closed') -def prove(out): +def prove(out, cpu='v3'): """Compile the generated C and link totk with the folder's tools only, then run the probes beside it.""" out = out.resolve() version = mr.lifted_version() @@ -530,9 +532,10 @@ def prove(out): chk = CHECK / 'game' env = dict(os.environ, PATH=os.pathsep.join([str(out / 'llvm' / 'bin'), str(out / 'bin'), '/usr/bin', '/bin'])) t0 = time.time() - run([out / 'python' / 'bin' / 'python3', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', + run([out / 'python' / 'bin' / 'python3', out / 'tools' / 'build_game.py', '--toolchain', out / 'llvm', '--cpu', cpu, '--sysroot', out / 'llvm' / 'sysroot', '--deps', out / 'runtime' / 'deps', '--runtime', out / 'runtime', - '--runtime-lib', out / 'runtime' / 'libtotkrt.a', '--ninja', out / 'bin' / 'ninja', '--version', version, + '--runtime-lib', out / 'runtime' / ('libtotkrt-v2.a' if cpu == 'v2' else 'libtotkrt.a'), + '--ninja', out / 'bin' / 'ninja', '--version', version, '--gen', gen, '--out', chk, '--exefs', ext / 'exefs', '--romfs', ext / 'romfs', '--profile', chk / 'profile'], env=env) print(f'prove: build {time.time() - t0:.0f} s') @@ -559,10 +562,12 @@ def main(): ap.add_argument('--out', type=Path, required=True, help='release folder (replaced)') ap.add_argument('--appimage', type=Path, help='also pack the folder as this AppImage') ap.add_argument('--prove', action='store_true', help='only run the compile-and-probe check on an existing folder') + ap.add_argument('--cpu', choices=('v2', 'v3'), default='v3', + help='--prove: build and run the check for this CPU level (default v3)') args = ap.parse_args() out = args.out.resolve() if args.prove: - prove(out) + prove(out, args.cpu) return if out.exists(): shutil.rmtree(out) diff --git a/tools/make_romfs_patches.py b/tools/make_romfs_patches.py index 9de0c8ac..acf151d4 100644 --- a/tools/make_romfs_patches.py +++ b/tools/make_romfs_patches.py @@ -1,6 +1,6 @@ # Adapted from MacNativeGames games/totk/scripts/make_romfs_patches.py @ ebe621d (MIT, Copyright (c) 2026 # MacNativeGames contributors). -"""Build the 60 fps RomFS overlay for TotK 1.1.1, 1.2.1 or 1.4.3 (runtime/romfs.c, TOTK_ROMFS_PATCHES). +"""Build the 60 fps RomFS overlay for TotK 1.1.0, 1.2.1 or 1.4.3 (runtime/romfs.c, TOTK_ROMFS_PATCHES). UI/LayoutArchive/Common.Product.<110 or 140>.Nin_NX_NVN.blarc.zs: the weapon / shield / material quick menus capture the game picture for their background when the panes C_CaptureBG_00, C_CaptureClearBG_00 and P_CaptureClearBG_00 become @@ -8,7 +8,7 @@ visible at animation frame 1.0 (anim/ShortCutBG_00_In.bflan), AutoBuilder the sa (anim/AutoBuilder_00_In.bflan). UI animations advance 0.5 frames per game frame at 60 fps, so the capture lands after the world stopped drawing and the background can be black. The visibility keys move to frame 0.0: the live world is captured at once (upstream checked weapon, shield, material and ability wheel at 60 fps, and 30 fps). -The game-over flow fix of the same upstream script is 1.0.0 only (1.1.1, 1.2.1 and 1.4.3 run DmF_SY_GameOver.110) and is +The game-over flow fix of the same upstream script is 1.0.0 only (1.1.0, 1.2.1 and 1.4.3 run DmF_SY_GameOver.110) and is left out. --aspect <w>x<h> (a game resolution of another shape than 16:9, such as 3440x1440 or 1280x800) also lays the UI out @@ -17,21 +17,30 @@ cover the screen and the HUD moves to the screen's corners (wider) or its top an loads that overlay from romfs-patches-<w>x<h> when the game resolution is that size (runtime/romfs.c overlay_init); the camera's side of it is the patch set aspect (recomp/patches-v121.txt, patches-v143.txt). ---xbox-prompts: the same overlay with the A and B, and the X and Y button prompts swapped, for pad_layout = xbox_prompts -(runtime/romfs.c overlay_init loads romfs-patches-xbox in place of romfs-patches). That layout plays an Xbox pad by -position, as nintendo does: the button that does Switch A is labelled B, and the prompts then name the label. Built from -the game's own images and glyphs, nothing added: -- the action prompt (blyt/DoCommand_00.bflyt) draws the letter images Nt_KeyTexA/B/X/Y_00 of timg/__Combined.bntx in - the Common archive: their image data is swapped (same format, size and tiling). Around it, a diamond names the - other face buttons with one-letter text panes (T_B_00, T_X_00, ...). The game fills them from the messages of the - same name in LayoutMsg/DoCommand_00.msbt of each Mals/<language> archive, over the layout's own text: both have - their letters swapped the same way. The diamond's shape (Nt_KeyBtnSubBase*) shows the button's position, which - stays right. -- every other prompt is text: the button glyphs U+E040 A, U+E041 B, U+E042 X, U+E043 Y of scft/External.bfotf, the - same font in each Font/Font*.Nin_NX_NVN.bfarc.zs. Their CFF outlines and horizontal metrics are swapped. +--prompts xbox | xbox-label: the same overlay with Xbox button icons in the game's prompts, for prompts = xbox +(runtime/romfs.c overlay_init loads romfs-patches-xbox or romfs-patches-xbox-label in place of romfs-patches). Drawn +from the game's own files on the player's machine, nothing added (tools/button_icons.py, which needs fontTools): +- both: L, R, ZL, ZR, Plus, Minus and the d-pad become LB, RB, LT, RT, Menu, View and a cross in the button font + (scft/External.bfotf, the same font in each Font/Font*.Nin_NX_NVN.bfarc.zs), and L and ZL in the button guide's images + (Nt_KeyTexL_00 and Nt_KeyTexZL_00 of timg/__Combined.bntx in the Common archive). The fonts grow, so the overlay also + carries the resource size table (System/Resource/ResourceSizeTable.Product.<n>...rsizetable.zs) with their entries + raised by as much. +- xbox, for pad_layout = nintendo. That layout plays an Xbox pad by position: the button that does Switch A is + labelled B, so the face buttons' prompts are swapped to name the labels, A with B and X with Y: + - the action prompt (blyt/DoCommand_00.bflyt) draws the letter images Nt_KeyTexA/B/X/Y_00 of timg/__Combined.bntx in + the Common archive: their image data is swapped (same format, size and tiling). Around it, a diamond names the + other face buttons with one-letter text panes (T_B_00, T_X_00, ...). The game fills them from the messages of the + same name in LayoutMsg/DoCommand_00.msbt of each Mals/<language> archive, over the layout's own text: both have + their letters swapped the same way. The diamond's shape (Nt_KeyBtnSubBase*) shows the button's position, which + stays right. + - every other prompt: the glyphs U+E040 A, U+E041 B, U+E042 X, U+E043 Y of the button font trade places. +- xbox-label, for pad_layout = xbox and auto. Those layouts put Switch A on the button labelled A (auto picks xbox for + every pad not labelled the Nintendo way), so the face buttons keep their letters; the action prompt's diamond keeps + the Switch positions (A on the right). +--xbox-prompts is the older name of --prompts xbox. Input: build/extracted/<version>/romfs (romfs.bin and index.json, verified by tools/extract.py). Output: -build/<version>/romfs-patches/<path> by default (romfs-patches-xbox with --xbox-prompts), and <out>/manifest.tsv (path, size, SHA-256 of the overlay, SHA-256 of the original). Run with .venv-py314. +build/<version>/romfs-patches/<path> by default (romfs-patches-<style> with --prompts), and <out>/manifest.tsv (path, size, SHA-256 of the overlay, SHA-256 of the original). Run with .venv-py314. """ import argparse import compression.zstd as zstd @@ -44,15 +53,18 @@ from pathlib import Path root = Path(__file__).resolve().parents[1] ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) -ap.add_argument('--version', choices=('v111', 'v121', 'v143'), default='v121') +ap.add_argument('--version', choices=('v110', 'v121', 'v143'), default='v121') ap.add_argument('--romfs', type=Path, help='default build/extracted/<version>/romfs') ap.add_argument('--out', type=Path, - help='default build/<version>/romfs-patches (romfs-patches-xbox with --xbox-prompts)') + help='default build/<version>/romfs-patches (romfs-patches-<style> with --prompts)') ap.add_argument('--aspect', help='<w>x<h> of a game resolution of another shape than 16:9: also lay the UI out for it') -ap.add_argument('--xbox-prompts', action='store_true', help='swap the A/B and X/Y button prompts as well') +ap.add_argument('--prompts', choices=('xbox', 'xbox-label'), + help='Xbox button icons in the prompts: xbox for pad_layout = nintendo (A shown as B, X as Y), ' + 'xbox-label for pad_layout = xbox or auto (the face buttons keep their letters)') +ap.add_argument('--xbox-prompts', dest='prompts', action='store_const', const='xbox', help='the same as --prompts xbox') args = ap.parse_args() args.romfs = args.romfs or root / 'build/extracted' / args.version / 'romfs' -args.out = args.out or root / 'build' / args.version / ('romfs-patches-xbox' if args.xbox_prompts else 'romfs-patches') +args.out = args.out or root / 'build' / args.version / (f'romfs-patches-{args.prompts}' if args.prompts else 'romfs-patches') index = json.loads((args.romfs / 'index.json').read_text()) assert index['status'] == 'pass' files = {f['path']: f for f in index['files']} @@ -181,119 +193,6 @@ def msbt_letters(data, swap): return changed -def cff_index(data, pos): - """([(start, end)] of every item, offSize, end) of the CFF INDEX at data[pos:].""" - count = struct.unpack_from('>H', data, pos)[0] - if not count: - return [], 0, pos + 2 - size = data[pos + 2] - offsets = [int.from_bytes(data[pos + 3 + i * size:pos + 3 + (i + 1) * size], 'big') for i in range(count + 1)] - first = pos + 2 + (count + 1) * size - return [(first + offsets[i], first + offsets[i + 1]) for i in range(count)], size, first + offsets[-1] - - -def cff_charstrings(data, cff): - """The offset (from the CFF table) of the CharStrings INDEX: operator 17 of the Top DICT. CID-keyed fonts (ROS, - operator 12 30) are refused: their glyphs can use different private dictionaries.""" - _, _, pos = cff_index(data, cff + data[cff + 2]) # Name INDEX after the header - top, _, _ = cff_index(data, pos) - (pos, end), operands, found = top[0], [], None - while pos < end: - b = data[pos] - if b <= 21: - op = (12, data[pos + 1]) if b == 12 else b - pos += 2 if b == 12 else 1 - assert op != (12, 30), 'CID-keyed CFF' - if op == 17: - found = operands[-1] - operands = [] - elif b == 28: - operands.append(struct.unpack_from('>h', data, pos + 1)[0]); pos += 3 - elif b == 29: - operands.append(struct.unpack_from('>i', data, pos + 1)[0]); pos += 5 - elif b == 30: # real number: nibbles up to an 0xf - pos += 1 - while data[pos] & 0x0f != 0x0f and data[pos] & 0xf0 != 0xf0: - pos += 1 - pos += 1 - operands.append(0) - elif b <= 246: - operands.append(b - 139); pos += 1 - elif b <= 250: - operands.append((b - 247) * 256 + data[pos + 1] + 108); pos += 2 - else: - operands.append(-(b - 251) * 256 - data[pos + 1] - 108); pos += 2 - assert found is not None, 'no CharStrings in the Top DICT' - return found - - -def otf_glyph(data, cmap, code): - """The glyph id of a code point in the format 4 (platform 3 encoding 1) subtable of the cmap at data[cmap:].""" - for k in range(struct.unpack_from('>H', data, cmap + 2)[0]): - platform, encoding, offset = struct.unpack_from('>HHI', data, cmap + 4 + 8 * k) - p = cmap + offset - if (platform, encoding) != (3, 1) or struct.unpack_from('>H', data, p)[0] != 4: - continue - seg = struct.unpack_from('>H', data, p + 6)[0] // 2 - for s in range(seg): - end = struct.unpack_from('>H', data, p + 14 + 2 * s)[0] - start = struct.unpack_from('>H', data, p + 16 + 2 * seg + 2 * s)[0] - if not start <= code <= end: - continue - delta = struct.unpack_from('>h', data, p + 16 + 4 * seg + 2 * s)[0] - at = p + 16 + 6 * seg + 2 * s - ro = struct.unpack_from('>H', data, at)[0] - glyph = code if not ro else struct.unpack_from('>H', data, at + ro + 2 * (code - start))[0] - return (glyph + delta) & 0xffff if glyph else 0 - raise AssertionError(f'U+{code:04X} is not in the font') - - -def otf_checksum(data): - data = bytes(data) + bytes(-len(data) % 4) - return sum(struct.unpack(f'>{len(data) // 4}I', data)) & 0xffffffff - - -def bfotf_swap(data, pairs): - """The BFOTF data with the glyphs of each (code point, code point) pair swapped: CFF outlines and horizontal - metrics, then the table checksums. A BFOTF is an OpenType font after an 8-byte header; from offset 4 on, every - 4-byte word is XORed with one key, which the size field gives away (it holds the font's length).""" - key = bytes(x ^ y for x, y in zip(data[4:8], struct.pack('>I', len(data) - 8))) - plain = bytearray(data[:4]) + bytearray(x ^ key[i % 4] for i, x in enumerate(data[4:])) - font = plain[8:] - assert font[:4] == b'OTTO', 'not a CFF OpenType font' - tables = {} - for k in range(struct.unpack_from('>H', font, 4)[0]): - tag, _, offset, length = struct.unpack_from('>4sIII', font, 12 + 16 * k) - tables[tag] = (12 + 16 * k, offset, length) - cff = tables[b'CFF '][1] - strings, size, end = cff_index(font, cff + cff_charstrings(font, cff)) - hmtx, metrics = tables[b'hmtx'][1], struct.unpack_from('>H', font, tables[b'hhea'][1] + 34)[0] - order = list(range(len(strings))) - for a, b in pairs: - ga, gb = otf_glyph(font, tables[b'cmap'][1], a), otf_glyph(font, tables[b'cmap'][1], b) - assert 0 < ga < len(strings) and 0 < gb < len(strings) and ga < metrics and gb < metrics, (a, b, ga, gb) - order[ga], order[gb] = order[gb], order[ga] - ma, mb = hmtx + 4 * ga, hmtx + 4 * gb - font[ma:ma + 4], font[mb:mb + 4] = font[mb:mb + 4], font[ma:ma + 4] - # The CharStrings INDEX again with the items in the new order: the same length, so nothing after it moves. - body = b''.join(bytes(font[slice(*strings[g])]) for g in order) - first = strings[0][0] - assert first + len(body) == end - font[first:end] = body - at, base = first - (len(strings) + 1) * size, 1 # the offset array ends where the first item starts - for i, g in enumerate(order + [None]): - font[at + i * size:at + (i + 1) * size] = base.to_bytes(size, 'big') - if g is not None: - base += strings[g][1] - strings[g][0] - head = tables[b'head'][1] - struct.pack_into('>I', font, head + 8, 0) - for entry, offset, length in tables.values(): - struct.pack_into('>I', font, entry + 4, otf_checksum(font[offset:offset + length])) - struct.pack_into('>I', font, head + 8, (0xb1b0afba - otf_checksum(font)) & 0xffffffff) - plain[8:] = font - return bytes(plain[:4]) + bytes(x ^ key[i % 4] for i, x in enumerate(plain[4:])) - - def sarc_replace(data, new): """The SARC data with the members named in new (name -> bytes) replaced. The header, SFAT and SFNT stay; the members are laid out again in their old order, each at an offset with at least the alignment its old one had.""" @@ -309,7 +208,7 @@ def sarc_replace(data, new): return bytes(out) -COMMON = {'v111': '110', 'v121': '110', 'v143': '140'} # the version in the name of each game's Common archive +COMMON = {'v110': '110', 'v121': '110', 'v143': '140'} # the version in the name of each game's Common archive ARCHIVE = f'UI/LayoutArchive/Common.Product.{COMMON[args.version]}.Nin_NX_NVN.blarc.zs' MEMBERS = { 'anim/ShortCutBG_00_In.bflan': ('C_CaptureBG_00', 'C_CaptureClearBG_00', 'P_CaptureClearBG_00'), @@ -327,6 +226,19 @@ for p in files: MALS[m[1]] = (int(m[2]), p) MALS = sorted(p for _, p in MALS.values()) LAYOUT_MSG = 'LayoutMsg/DoCommand_00.msbt' +# The resource size table (System/Resource/ResourceSizeTable.Product.<n>...rsizetable.zs, matched as loosely as +# MacNativeGames make_button_icons.py does), likewise the newest version in the RomFS. +RESTBL = max((p for p in files if p.startswith('System/Resource/ResourceSizeTable.') and p.endswith('.rsizetable.zs')), + key=lambda p: int((re.search(r'\.(\d+)\.', p) or [0, 0])[1]), default=None) +by_position = args.prompts == 'xbox' # the face buttons' prompts swapped to the labels (pad_layout = nintendo) +if args.prompts: + assert FONTS, 'no Font/Font*.Nin_NX_NVN.bfarc.zs in the RomFS' + assert MALS or not by_position, 'no Mals/<language>.Product.<version>.sarc.zs in the RomFS' + assert RESTBL, 'no System/Resource/ResourceSizeTable.Product.<n>...rsizetable.zs in the RomFS' + spec = importlib.util.spec_from_file_location('button_icons', Path(__file__).with_name('button_icons.py')) + icons = importlib.util.module_from_spec(spec) + spec.loader.exec_module(icons) + assert icons.TEXT_ARCHIVE in FONTS, f'no {icons.TEXT_ARCHIVE} in the RomFS' with (args.romfs / 'romfs.bin').open('rb') as image: def read(path): @@ -340,7 +252,8 @@ with (args.romfs / 'romfs.bin').open('rb') as image: for name, (lo, hi) in sarc_members(pack).items(): d = zstd.ZstdDict(pack[lo:hi]) dictionaries[d.dict_id] = d - originals = {path: read(path) for path in [ARCHIVE] + (FONTS + MALS if args.xbox_prompts else [])} + prompt_files = (FONTS + (MALS if by_position else []) + [RESTBL]) if args.prompts else [] + originals = {path: read(path) for path in [ARCHIVE] + prompt_files} raw = originals[ARCHIVE] info = zstd.get_frame_info(raw) dictionary = dictionaries[info.dictionary_id] @@ -371,27 +284,43 @@ diff = [i for i in range(len(original)) if original[i] != blarc[i]] touched = [members[m] for m in (*MEMBERS, *layout)] assert diff and all(any(lo <= i < hi for lo, hi in touched) for i in diff), diff outputs = {ARCHIVE: bytes(blarc)} # RomFS path -> new contents (before compression) -if args.xbox_prompts: - bntx_swap(blarc, members['timg/__Combined.bntx'][0], KEY_TEXTURES) +prompts = None +if args.prompts: + sizes = {ARCHIVE: len(original)} # RomFS path -> size of the original's contents + + def unpack(path): + frame = zstd.get_frame_info(originals[path]) + data = zstd.decompress(originals[path], zstd_dict=dictionaries.get(frame.dictionary_id)) + sizes[path] = len(data) + return data + archives = {path: unpack(path) for path in FONTS} + text = sarc_members(archives[icons.TEXT_ARCHIVE]) + missing = [name for name in icons.TEXT_FONTS if name not in text] + assert not missing, f'no {", ".join(missing)} in {icons.TEXT_ARCHIVE}' + drawn = icons.xbox_icons(icons.text_glyphs(archives[icons.TEXT_ARCHIVE][slice(*text[name])] + for name in icons.TEXT_FONTS)) + bntx = members['timg/__Combined.bntx'][0] letters = {} - for member, (lo, hi) in members.items(): - if member.endswith('.bflyt'): - changed = bflyt_letters(blarc, lo, hi, KEY_LETTERS) - if changed: - letters[member] = changed - assert 'blyt/DoCommand_00.bflyt' in letters, 'no letter panes in blyt/DoCommand_00.bflyt' + if by_position: + bntx_swap(blarc, bntx, KEY_TEXTURES) + for member, (lo, hi) in members.items(): + if member.endswith('.bflyt'): + changed = bflyt_letters(blarc, lo, hi, KEY_LETTERS) + if changed: + letters[member] = changed + assert 'blyt/DoCommand_00.bflyt' in letters, 'no letter panes in blyt/DoCommand_00.bflyt' + hud = icons.bntx_redraw(blarc, bntx, icons.HUD_TEXTURES, drawn) outputs[ARCHIVE] = bytes(blarc) - assert FONTS, 'no Font/Font*.Nin_NX_NVN.bfarc.zs in the RomFS' - for path in FONTS: - frame = zstd.get_frame_info(originals[path]) - bfarc = zstd.decompress(originals[path], zstd_dict=dictionaries.get(frame.dictionary_id)) - lo, hi = sarc_members(bfarc)['scft/External.bfotf'] - outputs[path] = sarc_replace(bfarc, {'scft/External.bfotf': bfotf_swap(bfarc[lo:hi], KEY_GLYPHS)}) - assert MALS, 'no Mals/<language>.Product.<version>.sarc.zs in the RomFS' + fonts = {} # each distinct font is drawn once (1.4.3's four archives hold the same one) + for path, bfarc in archives.items(): + lo, hi = sarc_members(bfarc)[icons.FONT] + otf, key = icons.bfotf_decode(bfarc[lo:hi]) + if otf not in fonts: + fonts[otf] = icons.redraw_font(otf, drawn, KEY_GLYPHS if by_position else ()) + outputs[path] = sarc_replace(bfarc, {icons.FONT: icons.bfotf_encode(fonts[otf], key, bfarc[lo:hi])}) messages = {} - for path in MALS: - frame = zstd.get_frame_info(originals[path]) - mals = bytearray(zstd.decompress(originals[path], zstd_dict=dictionaries.get(frame.dictionary_id))) + for path in MALS if by_position else []: + mals = bytearray(unpack(path)) lo, hi = sarc_members(mals)[LAYOUT_MSG] msbt = bytearray(mals[lo:hi]) changed = msbt_letters(msbt, KEY_LETTERS) @@ -399,6 +328,12 @@ if args.xbox_prompts: mals[lo:hi] = msbt outputs[path] = bytes(mals) messages[path] = len(changed) + # An archive that grew needs as much more room in the resource size table, or the game stops when it loads it. + grown = {path: len(data) - sizes[path] for path, data in outputs.items() if len(data) > sizes[path]} + if grown: + outputs[RESTBL] = icons.grow_restbl(unpack(RESTBL), grown) + prompts = dict(style=args.prompts, textures=KEY_TEXTURES if by_position else [], letters=letters, + messages=messages, hud=hud, icons=len(drawn), fonts=FONTS, grown=grown) lines = [] for path, contents in outputs.items(): @@ -415,5 +350,4 @@ for path, contents in outputs.items(): + ''.join(line + '\n' for line in lines)) print(json.dumps(dict(status='pass', path=ARCHIVE, members=patched, changed_bytes=len(diff), ultrawide_layout=dict(aspect=args.aspect, members=len(layout)) if args.aspect else None, - xbox_prompts=dict(textures=KEY_TEXTURES, letters=letters, messages=messages, fonts=FONTS) - if args.xbox_prompts else None))) + prompts=prompts))) diff --git a/tools/ncz.py b/tools/ncz.py new file mode 100644 index 00000000..d41dfe4d --- /dev/null +++ b/tools/ncz.py @@ -0,0 +1,231 @@ +"""Unpack an NSZ or XCZ (a game file compressed by the nsz tool) into a plain NSP. + +An NSZ is an NSP whose NCAs are stored as NCZ files: the first 0x4000 bytes of the NCA (its encrypted header) as they +are, then an NCZSECTN table of the NCA's sections with the AES-CTR key and counter of each, then the rest of the NCA +decrypted and compressed with zstd, either as one stream or as an NCZBLOCK table of independently compressed blocks. +An XCZ is an XCI whose secure partition holds NCZ files. Unpacking encrypts the sections again, so every NCA comes back +byte for byte; its file name is the first half of its SHA-256, which is checked. The output is an NSP of the secure +partition (an XCZ) or of the NSZ's files, with .ncz renamed to .nca. Format: nicoboss/nsz, nsz/NszDecompressor.py +(__decompressNcz) and nsz/BlockDecompressorReader.py; this is our own code. + +usage: ncz.py <file.nsz|file.xcz> <out.nsp>. Runs under .venv-py314 (compression.zstd). +""" +import hashlib +import io +import struct +import sys +from compression import zstd +from pathlib import Path + +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes + +sys.path.insert(0, str(Path(__file__).resolve().parent / 'vendor' / 'macnativegames')) +from extract_exefs import container_entries, read_at, require # noqa: E402 + +HEADER = 0x4000 # the NCA header, kept encrypted and uncompressed +CHUNK = 1 << 20 +CRYPTO_CTR = (3, 4) # AES-CTR and BKTR sections; the other types are stored as they are + + +class DamagedFile(ValueError): + """An NCZ whose unpacked NCA does not match its name's hash, or whose data ends early.""" + + def __init__(self, path, detail): + self.path, self.detail = Path(path), detail + super().__init__(f'{self.path} is damaged or incomplete ({detail})') + + +def compressed(path): + """('nsz' or 'xcz', entries) for a game file holding NCZ files, else None. Raises ValueError for a file that is + neither an NSP nor an XCI.""" + with Path(path).open('rb') as f: + kind, entries = container_entries(f, Path(path).stat().st_size) + if not any(name.lower().endswith('.ncz') for name, _, _ in entries): + return None + return ('nsz' if kind == 'nsp' else 'xcz'), entries + + +def sections(f, start): + """[(offset, size, crypto type, key, counter)] of the NCZ at start, and the offset of its data after the table.""" + head = read_at(f, start + HEADER, 16) + require(head[:8] == b'NCZSECTN', 'NCZ without its NCZSECTN table') + count = struct.unpack_from('<Q', head, 8)[0] + require(0 < count <= 0x10000, 'Invalid NCZ section count') + raw = read_at(f, start + HEADER + 16, count * 0x40) + out = [] + for i in range(count): + offset, size, crypto = struct.unpack_from('<QQQ', raw, i * 0x40) + out.append((offset, size, crypto, raw[i * 0x40 + 0x20:i * 0x40 + 0x30], raw[i * 0x40 + 0x30:i * 0x40 + 0x40])) + out.sort() + require(out[0][0] >= HEADER, 'NCZ section inside the NCA header') + for (a, size, *_), (b, *_) in zip(out, out[1:]): + require(a + size <= b, 'Overlapping NCZ sections') + return out, start + HEADER + 16 + count * 0x40 + + +def nca_size(f, start): + table, _ = sections(f, start) + return table[-1][0] + table[-1][1] + + +def layout(source): + """(entries, NSP header, NSP size) of the NSP that unpack writes for the NSZ or XCZ source: its files in order, + .ncz renamed to .nca, the PFS0 table padded to 0x20 bytes.""" + _, entries = compressed(source) + with Path(source).open('rb') as f: + sizes = [nca_size(f, start) if name.lower().endswith('.ncz') else length for name, start, length in entries] + names = [name[:-4] + '.nca' if name.lower().endswith('.ncz') else name for name, _, _ in entries] + strings = b''.join(n.encode('utf-8') + b'\0' for n in names) + strings += bytes(-(16 + 0x18 * len(names) + len(strings)) % 0x20) + head = b'PFS0' + struct.pack('<III', len(names), len(strings), 0) + offset, name_at = 0, 0 + for name, size in zip(names, sizes): + head += struct.pack('<QQII', offset, size, name_at, 0) + offset += size + name_at += len(name.encode('utf-8')) + 1 + head += strings + return entries, head, len(head) + offset + + +def unpacked_size(source): + """Bytes of the NSP that unpack writes for the NSZ or XCZ source.""" + return layout(source)[2] + + +class Slice(io.RawIOBase): + """Bytes start..end of a file, for zstd.ZstdFile: a stream may hold several frames and must stop at the entry's + end.""" + + def __init__(self, f, start, end): + self.f, self.pos, self.end = f, start, end + + def readable(self): + return True + + def readinto(self, buffer): + n = min(len(buffer), self.end - self.pos) + if n <= 0: + return 0 + self.f.seek(self.pos) + data = self.f.read(n) + buffer[:len(data)] = data + self.pos += len(data) + return len(data) + + +def blocks(f, pos, end, total): + """The NCZBLOCK data at pos, decompressed, in pieces. A block whose stored size is not smaller than its + decompressed size is stored as it is.""" + head = read_at(f, pos, 24) + exponent, count = head[11], struct.unpack_from('<I', head, 12)[0] + size = struct.unpack_from('<Q', head, 16)[0] + require(head[8] == 2 and head[9] == 1 and 14 <= exponent <= 32, 'Unsupported NCZBLOCK header') + require(size == total and count == -(-size // (1 << exponent)), 'NCZBLOCK sizes do not match the sections') + stored = struct.unpack_from(f'<{count}I', read_at(f, pos + 24, 4 * count)) + pos += 24 + 4 * count + for i, length in enumerate(stored): + plain = min(1 << exponent, size - (i << exponent)) + require(pos + length <= end, 'Truncated NCZ block') + data = read_at(f, pos, length) + pos += length + yield zstd.decompress(data) if length < plain else data + + +def stream(f, pos, end, total): + """The decompressed NCA body after the header, in pieces of up to CHUNK bytes.""" + if read_at(f, pos, 8) == b'NCZBLOCK': + yield from blocks(f, pos, end, total) + return + with zstd.ZstdFile(io.BufferedReader(Slice(f, pos, end), CHUNK)) as z: + while data := z.read(CHUNK): + yield data + + +def unpack_ncz(f, start, end, out, name, source, progress): + """Write the NCA of the NCZ at start..end to out; check it against name's hash. progress(bytes) after each piece.""" + table, pos = sections(f, start) + total = table[-1][0] + table[-1][1] + digest = hashlib.sha256() + header = read_at(f, start, HEADER) + out.write(header) + digest.update(header) + progress(HEADER) + at = HEADER + pending = b'' + try: + pieces = stream(f, pos, end, total - HEADER) + for piece in pieces: + pending += piece + while pending and at < total: + section = next((s for s in table if s[0] <= at < s[0] + s[1]), None) + stop = section[0] + section[1] if section else next((s[0] for s in table if s[0] > at), total) + n = min(len(pending), stop - at) + crypto = section and section[2] in CRYPTO_CTR + if crypto and at + n < stop: + n -= n % 16 # the counter of the next piece starts at a whole AES block + if not n: + break + data, pending = pending[:n], pending[n:] + if crypto: + require(at % 16 == 0, 'Unaligned NCZ section') + counter = section[4][:8] + struct.pack('>Q', at >> 4) + data = Cipher(algorithms.AES(section[3]), modes.CTR(counter)).encryptor().update(data) + out.write(data) + digest.update(data) + at += n + progress(n) + if at == total: + break + except (zstd.ZstdError, EOFError) as e: # EOFError: the stream ends before its end-of-frame marker + raise DamagedFile(source, f'{name}: {e}') from None + if at != total or pending: + raise DamagedFile(source, f'{name}: {at - HEADER:,} of {total - HEADER:,} bytes') + stem = name[:-4] + if len(stem) == 32 and digest.hexdigest()[:32] != stem.lower(): + raise DamagedFile(source, f'{name}: hash mismatch') + + +def unpack(source, target, progress=lambda done, total: None): + """Write the NSP of the NSZ or XCZ source to target (through target.part). progress(done, total) in bytes.""" + source, target = Path(source), Path(target) + kind = compressed(source)[0] + entries, head, total = layout(source) + part = target.with_name(target.name + '.part') + done = 0 + + def step(n): + nonlocal done + done += n + progress(done, total) + + with source.open('rb') as f, part.open('wb') as out: + out.write(head) + step(len(head)) + for name, start, length in entries: + if name.lower().endswith('.ncz'): + unpack_ncz(f, start, start + length, out, name, source, step) + continue + for at in range(start, start + length, CHUNK): + data = read_at(f, at, min(CHUNK, start + length - at)) + out.write(data) + step(len(data)) + part.replace(target) + return kind + + +def main(): + source, target = sys.argv[1:3] + shown = [-1] + + def progress(done, total): + percent = done * 100 // total + if percent != shown[0]: + shown[0] = percent + print(f'\r{percent}%', end='', flush=True) + + kind = unpack(source, target, progress) + print(f'\n{kind}: wrote {target}') + + +if __name__ == '__main__': + main() diff --git a/tools/package_build.py b/tools/package_build.py index 3d54a69a..e7882c36 100644 --- a/tools/package_build.py +++ b/tools/package_build.py @@ -93,8 +93,9 @@ def main(): else: print(f'package_build: {f} not in {build}, skipped') n = copy_tree(build / 'romfs-patches', out / 'romfs-patches') - if (build / 'romfs-patches-xbox').is_dir(): # pad_layout = xbox_prompts (make_romfs_patches.py --xbox-prompts) - n += copy_tree(build / 'romfs-patches-xbox', out / 'romfs-patches-xbox') + for name in ('romfs-patches-xbox', 'romfs-patches-xbox-label'): # prompts = xbox (make_romfs_patches.py --prompts) + if (build / name).is_dir(): + n += copy_tree(build / name, out / name) n += copy_tree(exefs, out / 'exefs') n += copy_tree(fonts, out / 'shared-fonts') if not a.no_shader_cache and (build / 'shader-cache').is_dir(): diff --git a/tools/prepare_shaders.py b/tools/prepare_shaders.py index f26aa30d..cca00744 100644 --- a/tools/prepare_shaders.py +++ b/tools/prepare_shaders.py @@ -18,6 +18,11 @@ shader-prepared.bin (one record per variant, in priority order) and builds every pipeline once, so the GPU driver's own disk cache holds them (vk_pipeline.c "stage library prebuild"). The seed files can be deleted afterwards. +A driver whose shader compiler crashes on a variant crashes on it in every run (NVIDIA 615.78.08 on an RTX 5080, issue +#30). The translations and shader-prepared.bin are written by then, and the journal the run leaves names the variants +the driver was building (vk_pipeline.c skip_init), so only the driver step runs again (`totk.exe --warm-shaders`), up to +RETRIES times, and skips them. + Game-derived output stays in the build directory (ignored). The list is read at preparation time from tools/data/shader_list.tsv when that file exists (the installer archive bundles it; it stays out of the repository), else from refs/ through git with the rows its history dropped (tools/shader_list.py); nothing of it is copied into the repository. Needs Python 3.14 (compression.zstd), so run it in .venv-py314 or the embeddable 3.14: @@ -54,6 +59,8 @@ TYPES = {'float': 0, 'sint': 1, 'uint': 2} DEFAULT = {'tex': {str(slot): '2d' for slot in range(32)}} # answers outside a shader's use mask are dropped CONTROL = 0x880 ANS_REC = 4 + 32 + 32 + 147 + 1 +JOURNAL = 'shader-prepare-journal.bin' # vk_pipeline.c skip_init: a 64-byte slot per worker, input id first +RETRIES = 3 BUNDLED_LIST = ROOT / 'tools' / 'data' / 'shader_list.tsv' # the installer archive carries a copy (no git, no refs/) @@ -128,6 +135,13 @@ def code_hash(stage, control, code, length, c1): struct.pack('<I', len(c1)) + c1).digest() +def driver_crashed(out): + """totk died while the graphics driver built variants: the journal it left names at least one.""" + p = out / JOURNAL + data = p.read_bytes() if p.is_file() else b'' + return any(any(data[i:i + 32]) for i in range(0, len(data), 64)) + + def main(): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument('build', type=Path, help='build directory holding totk.exe (build\\v121)') @@ -142,7 +156,7 @@ def main(): extracted = ROOT / 'build' / 'extracted' / a.version listed = len(variants) shaders, _ = inventory(a.romfs or extracted / 'romfs', a.exefs or extracted / 'exefs', everything=False) - # A missing list row says nothing about the walk: the list covers several versions, and 1.1.1 holds 17,587 of its + # A missing list row says nothing about the walk: the list covers several versions, and 1.1.0 holds 17,587 of its # 41,414 rows, so most rows are absent because the game does not have them, not because the walk missed them. A walk # that found next to nothing does say something: the shader directories have moved. if len(shaders) < max(64, listed // 20): @@ -193,6 +207,12 @@ def main(): return 0 exe = out / ('totk.exe' if os.name == 'nt' else 'totk') r = subprocess.run([str(exe), '--prepare-shaders'], cwd=str(out)) + for _ in range(RETRIES): + if not r.returncode or not driver_crashed(out): + break + print(f'prepare_shaders: the graphics driver crashed while building shaders (rc={r.returncode}); building them ' + 'again without the variants it was building (totk.exe --warm-shaders)', flush=True) + r = subprocess.run([str(exe), '--warm-shaders'], cwd=str(out)) print(f'totk.exe --prepare-shaders: rc={r.returncode}; total {time.monotonic() - t0:.1f} s') return r.returncode diff --git a/tools/requirements-installer.txt b/tools/requirements-installer.txt index 49cb95fc..f8a81496 100644 --- a/tools/requirements-installer.txt +++ b/tools/requirements-installer.txt @@ -1,7 +1,9 @@ # Packages of the release's bundled Python 3.14.9 (tools/make_release.py): what the installer steps import. # numpy: recomp/gen.py. lz4: recomp/nso.py, tools/shader_census.py. cryptography, cffi, pycparser: tools/extract.py and the vendored extract scripts. +# fonttools: tools/button_icons.py (make_romfs_patches.py --prompts). numpy==2.5.4 lz4==4.4.5 cryptography==50.0.2 +fonttools==4.68.0 cffi==2.1.1 pycparser==3.11 diff --git a/tools/requirements-py314.txt b/tools/requirements-py314.txt index 2bf9147b..79091637 100644 --- a/tools/requirements-py314.txt +++ b/tools/requirements-py314.txt @@ -7,6 +7,7 @@ capstone==5.0.9 cffi==2.1.1 cryptography==50.0.2 enlighten==1.14.1 +fonttools==4.68.0 jinxed==2.1.0 lz4==4.4.5 nsz==5.0.0 diff --git a/tools/shader_census.py b/tools/shader_census.py index 7f1e1790..5851a0b0 100644 --- a/tools/shader_census.py +++ b/tools/shader_census.py @@ -173,7 +173,7 @@ def startup_package(name): # The RomFS directories that can hold shaders: the agl and gsys packages, the effects, terrain, the UI layout archives -# and the game's own shader packs. Every shader of 1.1.1 (27,576) comes from these six; they are 1,996 entries and +# and the game's own shader packs. Every shader of 1.1.0 (27,576) comes from these six; they are 1,996 entries and # 147 MiB, 0.9% of a RomFS whose every file the walk below used to read and decompress first (65 s against 2.8 s). SHADER_DIRS = ('Shader/', 'Effect/', 'Lib/agl/', 'Lib/Terrain/', 'Lib/gsys/', 'UI/LayoutArchive/') diff --git a/tools/ultrawide_layout.py b/tools/ultrawide_layout.py index 6c2fff1a..de36c28e 100644 --- a/tools/ultrawide_layout.py +++ b/tools/ultrawide_layout.py @@ -22,6 +22,25 @@ FIELD = {'x': 44, 'y': 48, 'scale_x': 68, 'scale_y': 72} KEEP_ROOT = {'AppMenuBG_00', 'PauseMenuBG_00', 'ShopMenuParasailBG_00', 'ShortCutBG_00', 'TitleMenuBG_00', 'EventFade_00', 'EventFadeWipe_00', 'CameraShutter_00', 'PaAppPictureBookDetail_00', 'AppMenuOverlay_00'} +# Layouts whose markers the game places over a point in the world (the lock-on arrow, the name of the item or character +# in front of Link, enemy life bars and notices, speech balloons): the code moves a pane under the root to the target's +# projected position in the 1920x1080 grid, which spans the whole canvas. Under a scaled root that position would be +# scaled too, so the marker lands short of its target, toward the middle of the screen (at half the distance on 32:9). +# Their root keeps scale 1 and the scale goes one level down instead, to the panes listed here: ones the code does not +# move and no animation scales, so the markers keep their 16:9 proportions. +MARKER_ROOT = { + 'AttentionBalloon_00': ('C_Capture_00', 'N_InOut_00'), # N_PosRoot_00 moves; N_InOut_00 animates y and alpha + 'AttentionLockOn_00': ('N_Offset_00',), # N_PosRoot_00 moves; N_Offset_00 animates y + 'EnemyInfo_00': ('Pa_EnemyLife_00', 'Pa_EnemyNotice_00'), # the parts move; N_EnemyLifeArea_* stay screen-sized + 'BalloonMessage_00': (), # the speech balloon part is pooled: its scale goes inside, see BALLOON_PART +} + +# Speech balloons are pooled: the game clones Pa_BalloonMessage_00 into Pa_BalloonMessage_%02d at run time, and the clones +# don't keep a scale set on the template part pane, so they drew at full canvas width. The scale goes on the part +# layout's top panes instead (their offset scaled with them, so they sit where a scaled root would put them); its +# animations only key alpha, visibility and N_Loop_00's y. (pane, x, y) as shipped. +BALLOON_PART = (('C_CaptureBase_00', 226.0, 20.0), ('N_InOut_00', 30.0, -10.0)) + def edits(k, hud_corner): """(layout, pane, field, value) for the aspect: k = 16/9 over the aspect (the root's scale), w = how far a 1920-wide layout's edge moves out in its own units (960 * (1 / k - 1)). OFF moves a pane out of sight.""" @@ -54,6 +73,8 @@ def edits(k, hud_corner): ('PaRecipeCardList_00', 'P_BG_00', 'scale_x', wide), ('SaveLoadIcon_00', 'RootPane', 'x', -960 * (1 - k)), ('ShopMenu_00', 'N_BG_00', 'scale_x', wide), ('StaffRoll_00', 'N_BG_00', 'scale_x', wide), + *[e for pane, x, _ in BALLOON_PART for e in (('PaBalloonMessage_00', pane, 'scale_x', k), + ('PaBalloonMessage_00', pane, 'x', x * k))], ] # Menus framed by two side decorations, a header and a background: the frame stays at the screen's edges. for layout, deco in (('PauseMenuOverlay_00', 'P_Deco{}_00'), ('SystemLoadList_00', 'P_Deco{}_00'), @@ -104,7 +125,9 @@ def tall_edits(k): *[('CameraPointer_00', p, 'scale_y', tall) for p in ('N_InOutScope_00', 'Pa_CameraFocus_00', 'S_Scissor_00', 'W_Shadow_01')], ('CameraSystemWindow_00', 'N_InOut_00', 'scale_y', tall), ('MessageLog_01', 'P_Capture_00', 'scale_y', tall), - *[(layout, 'N_SystemBG_00', 'scale_y', tall) for layout in ('SystemActionGuide_00',) + SYSTEM_MENUS]] + *[(layout, 'N_SystemBG_00', 'scale_y', tall) for layout in ('SystemActionGuide_00',) + SYSTEM_MENUS], + *[e for pane, _, y in BALLOON_PART for e in (('PaBalloonMessage_00', pane, 'scale_y', k), + ('PaBalloonMessage_00', pane, 'y', y * k))]] # The HUD at the screen's top and bottom edges instead of the middle 16:9 out += [(name, 'RootPane', 'y', 540 * (1 - k)) for name in ( 'AppAlbum_00', 'AppPictureBook_00', 'ChallengeLog_00', 'Challenge_00', 'InstantTipsWithPict_00', 'InstantTips_00', @@ -222,8 +245,8 @@ def apply(blarc, members, width, height, hud_corner=True): assert k < 1, 'only for canvases of another shape than 16:9' root_scale = 'scale_x' if wide else 'scale_y' changed, layouts = set(), {} - unknown = sorted(n for n in KEEP_ROOT if f'blyt/{n}.bflyt' not in members) - if unknown: raise SystemExit(f'KEEP_ROOT names no layout: {unknown}') + unknown = sorted(n for n in KEEP_ROOT | MARKER_ROOT.keys() if f'blyt/{n}.bflyt' not in members) + if unknown: raise SystemExit(f'KEEP_ROOT or MARKER_ROOT names no layout: {unknown}') def pane_offset(layout, pane): member = f'blyt/{layout}.bflyt' if member not in members: raise SystemExit(f'{layout}: no layout {member} in the archive') @@ -232,6 +255,14 @@ def apply(blarc, members, width, height, hud_corner=True): return find_pane(layouts[member], layout, pane) for member in sorted(m for m in members if m.startswith('blyt/') and m.endswith('.bflyt')): if member[5:-6] in KEEP_ROOT: continue + if member[5:-6] in MARKER_ROOT: # the root stays at 1, its listed panes take the scale + layout = member[5:-6] + for pane in ('RootPane',) + MARKER_ROOT[layout]: + at = pane_offset(layout, pane) + FIELD[root_scale] + have = struct.unpack_from('<f', blarc, at)[0] + if abs(have - 1.0) > 1e-3: raise SystemExit(f'{layout} {pane} {root_scale}: {have}, expected 1.0') + if pane != 'RootPane': struct.pack_into('<f', blarc, at, k) + continue at = pane_offset(member[5:-6], 'RootPane') + FIELD[root_scale] have = struct.unpack_from('<f', blarc, at)[0] if abs(have - 1.0) > 1e-3: raise SystemExit(f'{member[5:-6]} RootPane {root_scale}: {have}, expected 1.0') diff --git a/tools/xlate/Program.cs b/tools/xlate/Program.cs index 75e0db19..75ee5cb2 100644 --- a/tools/xlate/Program.cs +++ b/tools/xlate/Program.cs @@ -117,6 +117,12 @@ static class SwshXlate public int QueryHostStorageBufferOffsetAlignment() => _job["sbuf_align"] is JsonNode n ? (int)n : 16; // "subgroup": n: lanes per subgroup when the host cannot pin 32 (AMD GCN/Vega run 64): ballots and votes span n lanes. public int QueryHostSubgroupSize() => _job["subgroup"] is JsonNode n ? (int)n : 32; + // "mvprev": true: the vertex shader also writes last frame's position, computed from slots bank + 15 in place of + // the user banks c3-c16 (patch 0015, runtime/vk_mvprev.c); res.json's "mvprev" names the output location (-1: + // none) and "mvprev_banks" the banks the position depends on (bit n: cn). + public bool QueryMotionPrevious() => _job["mvprev"] is JsonValue v && v.TryGetValue<bool>(out bool b) && b; + public int MotionPreviousLocation = -1, MotionPreviousBanks; + public void ReportMotionPrevious(int location, int banks) => (MotionPreviousLocation, MotionPreviousBanks) = (location, banks); } @@ -140,6 +146,7 @@ static class SwshXlate ["uses_frag_coord"] = p.Info.UsesFragCoord, ["frag_out_map"] = p.Info.FragmentOutputMap, ["tex_queried"] = new JsonArray(acc.TexQueried.Select(x => (JsonNode)x).ToArray()), ["img_queried"] = new JsonArray(acc.ImgQueried.Select(x => (JsonNode)x).ToArray()), }; + if (acc.QueryMotionPrevious()) (res["mvprev"], res["mvprev_banks"]) = (acc.MotionPreviousLocation, acc.MotionPreviousBanks); foreach (var b in p.Info.CBuffers) res["cbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot }); foreach (var b in p.Info.SBuffers) res["sbufs"].AsArray().Add((JsonNode)new JsonObject { ["binding"] = b.Binding, ["slot"] = b.Slot, ["cb_slot"] = b.SbCbSlot, ["cb_offset"] = b.SbCbOffset, ["flags"] = (int)b.Flags }); foreach (var (list, name) in new[] { (p.Info.Textures, "textures"), (p.Info.Images, "images") }) diff --git a/tools/xlate/patches/0010-pcss-for-cascade-compares.patch b/tools/xlate/patches/0010-pcss-for-cascade-compares.patch new file mode 100644 index 00000000..4f41aaae --- /dev/null +++ b/tools/xlate/patches/0010-pcss-for-cascade-compares.patch @@ -0,0 +1,135 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +@@ -1456,7 +1456,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + } + else if (isShadow) + { +- if (hasLodLevel) ++ if (hasLodLevel && isArray && coordsCount == 2 && operandsMask == ImageOperandsMask.Lod) ++ { ++ result = GeneratePcssSample(context, declaration, image, pCoords, dRef, lod); ++ } ++ else if (hasLodLevel) + { + result = context.ImageSampleDrefExplicitLod(resultType, image, pCoords, dRef, operandsMask, operands); + } +@@ -1486,6 +1490,92 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + return new OperationResult(swizzledResultType, result); + } + ++ // TOTK: percentage-closer soft shadows for the sun. The deferred lighting shaders take the shadow as four ++ // explicit-LOD compares on a 2D array (the cascades) around the receiver; each of those becomes a PCSS lookup ++ // when the support buffer's pcss.x is above 0 (runtime/vk_exec_draw.c support_fill, TOTK_PCSS): a blocker ++ // search reads the raw depths at eight points within pcss.w (UV), the average depth of those nearer the light ++ // than the receiver gives the penumbra width (receiver - blocker) * pcss.x, clamped to pcss.y..pcss.z (UV), and ++ // eight compares on a disc of that radius average to the result. With pcss.x at 0 the original single compare ++ // runs. The cascades are orthographic, so the depth difference is a distance along the light. ++ private static readonly float[] PcssSearch = [-0.7f, -0.7f, 0.7f, -0.7f, -0.7f, 0.7f, 0.7f, 0.7f, 0f, -1f, 0f, 1f, -1f, 0f, 1f, 0f]; ++ private static readonly float[] PcssDisk = ++ [ ++ -0.9420f, -0.3991f, 0.9456f, -0.7689f, -0.0942f, -0.9294f, 0.3450f, 0.2939f, ++ -0.9159f, 0.4577f, -0.8154f, -0.8791f, -0.3828f, 0.2768f, 0.9748f, 0.7565f, ++ ]; ++ ++ private static SpvInstruction GeneratePcssSample(CodeGenContext context, SamplerDeclaration declaration, SpvInstruction image, ++ SpvInstruction pCoords, SpvInstruction dRef, SpvInstruction lod) ++ { ++ SpvInstruction f32 = context.TypeFP32(), s32 = context.TypeS32(), b = context.TypeBool(); ++ SpvInstruction v2 = context.TypeVector(f32, 2), v3 = context.TypeVector(f32, 3), v4 = context.TypeVector(f32, 4); ++ SpvInstruction iv2 = context.TypeVector(s32, 2), iv3 = context.TypeVector(s32, 3); ++ SpvInstruction zero = context.Constant(f32, 0f), one = context.Constant(f32, 1f); ++ ++ SpvInstruction prmPtr = context.AccessChain(context.TypePointer(StorageClass.Uniform, v4), context.ConstantBuffers[SupportBuffer.Binding], ++ context.Constant(s32, (int)SupportBufferField.Pcss)); ++ SpvInstruction prm = context.Load(v4, prmPtr); ++ SpvInstruction lightSize = context.CompositeExtract(f32, prm, 0); ++ SpvInstruction minR = context.CompositeExtract(f32, prm, 1); ++ SpvInstruction maxR = context.CompositeExtract(f32, prm, 2); ++ SpvInstruction searchR = context.CompositeExtract(f32, prm, 3); ++ ++ SpvInstruction pcssLabel = context.Label(), plainLabel = context.Label(), mergeLabel = context.Label(); ++ SpvInstruction enabled = context.FOrdGreaterThan(b, lightSize, zero); // before the merge: it must precede the branch ++ context.SelectionMerge(mergeLabel, SelectionControlMask.MaskNone); ++ context.BranchConditional(enabled, pcssLabel, plainLabel); ++ ++ context.AddLabel(plainLabel); ++ SpvInstruction plain = context.ImageSampleDrefExplicitLod(f32, image, pCoords, dRef, ImageOperandsMask.Lod, lod); ++ context.Branch(mergeLabel); ++ ++ context.AddLabel(pcssLabel); ++ SpvInstruction raw = context.Image(declaration.ImageType, image); ++ SpvInstruction size3 = context.ImageQuerySizeLod(iv3, raw, context.Constant(s32, 0)); ++ SpvInstruction sizeI = context.VectorShuffle(iv2, size3, size3, 0, 1); ++ SpvInstruction sizeF = context.ConvertSToF(v2, sizeI); ++ SpvInstruction maxI = context.ISub(iv2, sizeI, context.ConstantComposite(iv2, context.Constant(s32, 1), context.Constant(s32, 1))); ++ SpvInstruction zeroI = context.ConstantComposite(iv2, context.Constant(s32, 0), context.Constant(s32, 0)); ++ SpvInstruction uv = context.VectorShuffle(v2, pCoords, pCoords, 0, 1); ++ SpvInstruction layerF = context.CompositeExtract(f32, pCoords, 2); ++ SpvInstruction layerI = context.ConvertFToS(s32, layerF); ++ SpvInstruction searchV = context.CompositeConstruct(v2, searchR, searchR); ++ ++ SpvInstruction sum = zero, count = zero; ++ for (int i = 0; i < PcssSearch.Length; i += 2) ++ { ++ SpvInstruction off = context.ConstantComposite(v2, context.Constant(f32, PcssSearch[i]), context.Constant(f32, PcssSearch[i + 1])); ++ SpvInstruction p = context.FAdd(v2, uv, context.FMul(v2, off, searchV)); ++ SpvInstruction t = context.GlslSClamp(iv2, context.ConvertFToS(iv2, context.FMul(v2, p, sizeF)), zeroI, maxI); ++ SpvInstruction c = context.CompositeConstruct(iv3, context.CompositeExtract(s32, t, 0), context.CompositeExtract(s32, t, 1), layerI); ++ SpvInstruction d = context.CompositeExtract(f32, context.ImageFetch(v4, raw, c, ImageOperandsMask.Lod, context.Constant(s32, 0)), 0); ++ SpvInstruction blocker = context.FOrdLessThan(b, d, dRef); ++ sum = context.FAdd(f32, sum, context.Select(f32, blocker, d, zero)); ++ count = context.FAdd(f32, count, context.Select(f32, blocker, one, zero)); ++ } ++ ++ SpvInstruction avg = context.FDiv(f32, sum, context.GlslFMax(f32, count, one)); ++ SpvInstruction penumbra = context.Select(f32, context.FOrdGreaterThan(b, count, zero), ++ context.FMul(f32, context.FSub(f32, dRef, avg), lightSize), zero); ++ SpvInstruction radius = context.GlslFClamp(f32, penumbra, minR, maxR); ++ SpvInstruction radiusV = context.CompositeConstruct(v2, radius, radius); ++ ++ SpvInstruction acc = zero; ++ for (int i = 0; i < PcssDisk.Length; i += 2) ++ { ++ SpvInstruction off = context.ConstantComposite(v2, context.Constant(f32, PcssDisk[i]), context.Constant(f32, PcssDisk[i + 1])); ++ SpvInstruction p = context.FAdd(v2, uv, context.FMul(v2, off, radiusV)); ++ SpvInstruction c = context.CompositeConstruct(v3, context.CompositeExtract(f32, p, 0), context.CompositeExtract(f32, p, 1), layerF); ++ acc = context.FAdd(f32, acc, context.ImageSampleDrefExplicitLod(f32, image, c, dRef, ImageOperandsMask.Lod, lod)); ++ } ++ ++ SpvInstruction pcss = context.FMul(f32, acc, context.Constant(f32, 2f / PcssDisk.Length)); ++ context.Branch(mergeLabel); ++ ++ context.AddLabel(mergeLabel); ++ return context.Phi(f32, pcss, pcssLabel, plain, plainLabel); ++ } ++ + private static OperationResult GenerateTextureQuerySamples(CodeGenContext context, AstOperation operation) + { + AstTextureOperation texOp = (AstTextureOperation)operation; +--- a/src/Ryujinx.Graphics.Shader/SupportBuffer.cs ++++ b/src/Ryujinx.Graphics.Shader/SupportBuffer.cs +@@ -25,6 +25,7 @@ namespace Ryujinx.Graphics.Shader + TfeOffset, + TfeVertexCount, + PointSize, ++ Pcss, + } + + public struct SupportBuffer +@@ -82,7 +83,8 @@ namespace Ryujinx.Graphics.Shader + new StructureField(AggregateType.Array | AggregateType.FP32, "render_scale", RenderScaleMaxCount), + new StructureField(AggregateType.Vector4 | AggregateType.S32, "tfe_offset"), + new StructureField(AggregateType.S32, "tfe_vertex_count"), +- new StructureField(AggregateType.FP32, "point_size") ++ new StructureField(AggregateType.FP32, "point_size"), ++ new StructureField(AggregateType.Vector4 | AggregateType.FP32, "pcss") + ]); + } + +@@ -98,5 +100,6 @@ namespace Ryujinx.Graphics.Shader + public Vector4<int> TfeOffset; + public Vector4<int> TfeVertexCount; + public Vector4<float> PointSize; ++ public Vector4<float> Pcss; + } + } diff --git a/tools/xlate/patches/0011-shadow-filter-spread.patch b/tools/xlate/patches/0011-shadow-filter-spread.patch new file mode 100644 index 00000000..cd952174 --- /dev/null +++ b/tools/xlate/patches/0011-shadow-filter-spread.patch @@ -0,0 +1,82 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +@@ -853,7 +853,51 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + + private static OperationResult GenerateLoad(CodeGenContext context, AstOperation operation) + { +- return GenerateLoadOrStore(context, operation, isStore: false); ++ OperationResult result = GenerateLoadOrStore(context, operation, isStore: false); ++ if (result.Type == AggregateType.FP32 && IsShadowSpread(context, operation)) ++ { ++ SpvInstruction f32 = context.TypeFP32(), v4 = context.TypeVector(f32, 4); ++ SpvInstruction prmPtr = context.AccessChain(context.TypePointer(StorageClass.Uniform, v4), context.ConstantBuffers[SupportBuffer.Binding], ++ context.Constant(context.TypeS32(), (int)SupportBufferField.ShadowSpread)); ++ SpvInstruction factor = context.CompositeExtract(f32, context.Load(v4, prmPtr), 0); ++ result = new OperationResult(AggregateType.FP32, context.FMul(f32, result.Value, factor)); ++ } ++ ++ return result; ++ } ++ ++ // TOTK: the spread of the sun shadow filter. The deferred lighting shaders take the sun shadow as four compares ++ // at the receiver ± h on the cascade map, h = 0.5 * c[56].xy (UV) of their lighting block (fp_c9, or fp_c5 in ++ // some), the same in every cascade. In the near cascade the map is sharp enough that the four copies of an edge ++ // show apart. In a fragment shader that has a 2D array shadow sampler, reads of element 56 .x and .y from any ++ // constant buffer but fp_c4 (which holds the cascade matrices, row 56 being cascade 3's depth row) are multiplied ++ // by the support buffer's shadow_spread.x (runtime/vk_exec_draw.c support_fill, TOTK_SHADOW_SPREAD; 1 keeps the ++ // game's spread). ++ private static bool IsShadowSpread(CodeGenContext context, AstOperation load) ++ { ++ if (context.Definitions.Stage != ShaderStage.Fragment || ++ load.StorageKind != StorageKind.ConstantBuffer || ++ load.SourcesCount != 4 || ++ load.GetSource(0) is not AstOperand { Type: OperandType.Constant } binding || ++ load.GetSource(1) is not AstOperand { Type: OperandType.Constant, Value: 0 } || ++ load.GetSource(2) is not AstOperand { Type: OperandType.Constant, Value: 56 } || ++ load.GetSource(3) is not AstOperand { Type: OperandType.Constant, Value: < 2 } || ++ !context.Properties.ConstantBuffers.TryGetValue(binding.Value, out BufferDefinition buffer) || ++ buffer.Name == "fp_c4") ++ { ++ return false; ++ } ++ ++ foreach (TextureDefinition texture in context.Properties.Textures.Values) ++ { ++ if ((texture.Type & (SamplerType.Mask | SamplerType.Array | SamplerType.Shadow)) == ++ (SamplerType.Texture2D | SamplerType.Array | SamplerType.Shadow)) ++ { ++ return true; ++ } ++ } ++ ++ return false; + } + + private static OperationResult GenerateLod(CodeGenContext context, AstOperation operation) +--- a/src/Ryujinx.Graphics.Shader/SupportBuffer.cs ++++ b/src/Ryujinx.Graphics.Shader/SupportBuffer.cs +@@ -26,6 +26,7 @@ namespace Ryujinx.Graphics.Shader + TfeVertexCount, + PointSize, + Pcss, ++ ShadowSpread, + } + + public struct SupportBuffer +@@ -84,7 +85,8 @@ namespace Ryujinx.Graphics.Shader + new StructureField(AggregateType.Vector4 | AggregateType.S32, "tfe_offset"), + new StructureField(AggregateType.S32, "tfe_vertex_count"), + new StructureField(AggregateType.FP32, "point_size"), +- new StructureField(AggregateType.Vector4 | AggregateType.FP32, "pcss") ++ new StructureField(AggregateType.Vector4 | AggregateType.FP32, "pcss"), ++ new StructureField(AggregateType.Vector4 | AggregateType.FP32, "shadow_spread") + ]); + } + +@@ -101,5 +103,6 @@ namespace Ryujinx.Graphics.Shader + public Vector4<int> TfeVertexCount; + public Vector4<float> PointSize; + public Vector4<float> Pcss; ++ public Vector4<float> ShadowSpread; + } + } diff --git a/tools/xlate/patches/0012-bitfield-fields-past-bit-31.patch b/tools/xlate/patches/0012-bitfield-fields-past-bit-31.patch new file mode 100644 index 00000000..baf8e717 --- /dev/null +++ b/tools/xlate/patches/0012-bitfield-fields-past-bit-31.patch @@ -0,0 +1,64 @@ +--- a/src/Ryujinx.Graphics.Shader/Instructions/InstEmitBitfield.cs ++++ b/src/Ryujinx.Graphics.Shader/Instructions/InstEmitBitfield.cs +@@ -142,13 +142,46 @@ namespace Ryujinx.Graphics.Shader.Instructions + + Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8)); + ++ // TOTK: Maxwell's BFE (as PTX bfe) cuts a field that runs past bit 31 there, and a field that starts past it ++ // gives 0, or bit 31 repeated when signed and the size is not 0. SPIR-V leaves an offset or offset + count over ++ // 32 undefined, and AMD's bitfield instructions take offset and width modulo 32. TOTK's Depths terrain vertex ++ // shaders take position and size from a register (BFE.R), so they are clamped into the defined range here; ++ // constant fields inside the word are emitted as before. ++ bool constantInRange = srcB.Type == OperandType.Constant && (srcB.Value & 0xff) + ((srcB.Value >> 8) & 0xff) <= 32; ++ Operand positionIn = position; ++ Operand sizeIn = size; ++ ++ if (!constantInRange) ++ { ++ ClampBitfield(context, ref position, ref size); ++ } ++ + Operand res = isSigned + ? context.BitfieldExtractS32(srcA, position, size) + : context.BitfieldExtractU32(srcA, position, size); + ++ if (!constantInRange && isSigned) ++ { ++ // The clamped extract gave 0 for a field that starts past bit 31; a signed one is bit 31 repeated. ++ Operand signBits = context.ConditionalSelect(context.ICompareEqual(sizeIn, Const(0)), Const(0), context.ShiftRightS32(srcA, Const(31))); ++ ++ res = context.ConditionalSelect(context.ICompareGreaterOrEqualUnsigned(positionIn, Const(32)), signBits, res); ++ } ++ + context.Copy(GetDest(rd), res); + +- // TODO: CC, X, corner cases. ++ // TODO: CC, X. ++ } ++ ++ // TOTK: position and size of a bitfield cut to the 32-bit word: the size shrinks to what is left of the word ++ // after the position (0 for a position of 32 or more) and the position stays at most 31, so offset and ++ // offset + count stay inside the range SPIR-V defines. ++ private static void ClampBitfield(EmitterContext context, ref Operand position, ref Operand size) ++ { ++ Operand left = context.ISubtract(Const(32), context.IMinimumU32(position, Const(32))); ++ ++ size = context.IMinimumU32(size, left); ++ position = context.IMinimumU32(position, Const(31)); + } + + private static void EmitBfi(EmitterContext context, Operand srcA, Operand srcB, Operand srcC, int rd) +@@ -157,6 +190,13 @@ namespace Ryujinx.Graphics.Shader.Instructions + + Operand size = context.BitfieldExtractU32(srcB, Const(8), Const(8)); + ++ // TOTK: as in EmitBfe, a field past bit 31 is cut there; one that starts past it inserts nothing (size 0 ++ // after the clamp), so C comes back unchanged, as on Maxwell. ++ if (!(srcB.Type == OperandType.Constant && (srcB.Value & 0xff) + ((srcB.Value >> 8) & 0xff) <= 32)) ++ { ++ ClampBitfield(context, ref position, ref size); ++ } ++ + Operand res = context.BitfieldInsert(srcC, srcA, position, size); + + context.Copy(GetDest(rd), res); diff --git a/tools/xlate/patches/0013-funnel-shift-by-zero.patch b/tools/xlate/patches/0013-funnel-shift-by-zero.patch new file mode 100644 index 00000000..3712b879 --- /dev/null +++ b/tools/xlate/patches/0013-funnel-shift-by-zero.patch @@ -0,0 +1,32 @@ +--- a/src/Ryujinx.Graphics.Shader/Instructions/InstEmitShift.cs ++++ b/src/Ryujinx.Graphics.Shader/Instructions/InstEmitShift.cs +@@ -119,6 +119,10 @@ namespace Ryujinx.Graphics.Shader.Instructions + bool signedShift = maxShift == MaxShift.S64; + int maxShiftConst = isLongShift ? 64 : 32; + ++ // TOTK: the shift amount as a constant, when it is one (taken before the mask makes it an operation). ++ bool constantShift = srcB.Type == OperandType.Constant; ++ int shiftValue = constantShift ? (mask ? srcB.Value & (maxShiftConst - 1) : srcB.Value) : 0; ++ + if (mask) + { + srcB = context.BitwiseAnd(srcB, Const(maxShiftConst - 1)); +@@ -159,6 +163,18 @@ namespace Ryujinx.Graphics.Shader.Instructions + } + } + ++ // TOTK: a shift by 0 shifts the other word by 32, which SPIR-V leaves undefined; AMD's shift instructions take ++ // the amount's low 5 bits and give A | C. Maxwell gives the unshifted word: C for SHF.L, A for SHF.R, 32- and ++ // 64-bit alike. TOTK's Depths terrain vertex shaders shift by registers (SHF.L.R, SHF.R.R). ++ if (!constantShift) ++ { ++ res = context.ConditionalSelect(context.ICompareEqual(srcB, Const(0)), left ? srcC : srcA, res); ++ } ++ else if (shiftValue == 0) ++ { ++ res = left ? srcC : srcA; ++ } ++ + if (!mask) + { + // Clamped shift value. diff --git a/tools/xlate/patches/0014-zero-initialised-locals.patch b/tools/xlate/patches/0014-zero-initialised-locals.patch new file mode 100644 index 00000000..3ffc7a9b --- /dev/null +++ b/tools/xlate/patches/0014-zero-initialised-locals.patch @@ -0,0 +1,18 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs +@@ -33,8 +33,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + { + foreach (AstOperand local in function.Locals) + { +- SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, context.GetType(local.VarType)); +- SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function); ++ SpvInstruction localType = context.GetType(local.VarType); ++ SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, localType); ++ ++ // TOTK: a register read on a path that did not write it reads 0. Without an initializer the load is ++ // undefined, and a compiler that folds undefined values (Mesa's, on the Steam Deck) may compute anything ++ // from it. ++ SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function, context.ConstantNull(localType)); + + context.AddLocalVariable(spvLocal); + context.DeclareLocal(local, spvLocal); diff --git a/tools/xlate/patches/0015-motion-previous-position.patch b/tools/xlate/patches/0015-motion-previous-position.patch new file mode 100644 index 00000000..9e915daa --- /dev/null +++ b/tools/xlate/patches/0015-motion-previous-position.patch @@ -0,0 +1,376 @@ +--- a/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs ++++ b/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs +@@ -184,6 +184,14 @@ namespace Ryujinx.Graphics.Shader + return false; + } + ++ // TOTK: true when a vertex shader should also write last frame's position (Transforms/MotionPrevious.cs), ++ // the user output location it went to (-1: none) and the banks the position depends on (bit n: cn). ++ bool QueryMotionPrevious() => false; ++ ++ void ReportMotionPrevious(int location, int banks) ++ { ++ } ++ + /// <summary> + /// Queries host about the presence of the FrontFacing built-in variable bug. + /// </summary> +--- a/src/Ryujinx.Graphics.Shader/Translation/ResourceManager.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/ResourceManager.cs +@@ -65,7 +65,7 @@ namespace Ryujinx.Graphics.Shader.Translation + _stage = stage; + _stagePrefix = GetShaderStagePrefix(stage); + +- _cbSlotToBindingMap = new SetBindingPair[18]; ++ _cbSlotToBindingMap = new SetBindingPair[32]; // TOTK: slots 18-31 for Transforms/MotionPrevious.cs + _sbSlotToBindingMap = new SetBindingPair[16]; + _cbSlotToBindingMap.AsSpan().Fill(new(-1, -1)); + _sbSlotToBindingMap.AsSpan().Fill(new(-1, -1)); +--- /dev/null ++++ b/src/Ryujinx.Graphics.Shader/Translation/Transforms/MotionPrevious.cs +@@ -0,0 +1,330 @@ ++using Ryujinx.Graphics.Shader.IntermediateRepresentation; ++using System.Collections.Generic; ++using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper; ++ ++namespace Ryujinx.Graphics.Shader.Translation.Transforms ++{ ++ // TOTK: last frame's position for per-object motion vectors (runtime/vk_exec_draw.c, vk_mvprev.c). A vertex shader's ++ // position depends on uniform banks of this frame: the camera (c4), the object's place (c7), its bones (c5), the ++ // clock that moves wind and water (c14), and so on. Every operation the position depends on that reads one of the ++ // user banks c3..c16, directly or through other values, is computed a second time beside the original, reading slot ++ // bank + 15 (18..31) instead, where the runtime binds that bank's block of last frame; the position this gives is ++ // written to a free user output. Control flow stays the original's. Operations that cannot be repeated (stores, ++ // texture operations) keep their original result. The runtime gets the output location and the banks the position ++ // depends on (ReportMotionPrevious): an object with none of them changed since last frame held still. ++ static class MotionPrevious ++ { ++ public const int SlotOffset = 15; ++ ++ private static int Remap(int slot) => slot >= 3 && slot <= 16 ? slot + SlotOffset : -1; ++ ++ // The output location written (-1: none free, or no position store), and in banks the mask of the banks (bit ++ // n: cn) the position depends on. ++ public static int Run(BasicBlock[] blocks, ResourceManager resourceManager, AttributeUsage attributeUsage, out int banks) ++ { ++ banks = 0; ++ int location = -1; ++ ++ for (int l = Constants.MaxAttributes - 1; l >= 0; l--) ++ { ++ if ((attributeUsage.UsedOutputAttributes & (1 << l)) == 0) ++ { ++ location = l; ++ break; ++ } ++ } ++ ++ if (location < 0) ++ { ++ return -1; ++ } ++ ++ // 1. What the position depends on: back from the values the position stores write, through every ++ // operation and phi that defines them. ++ HashSet<INode> slice = new(); ++ Stack<Operand> work = new(); ++ bool stores = false; ++ ++ foreach (BasicBlock block in blocks) ++ { ++ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next) ++ { ++ if (IsPositionStore(node.Value)) ++ { ++ stores = true; ++ work.Push(((Operation)node.Value).GetSource(2)); ++ } ++ } ++ } ++ ++ if (!stores) ++ { ++ return -1; ++ } ++ ++ while (work.Count != 0) ++ { ++ Operand v = work.Pop(); ++ ++ if (v == null || v.Type != OperandType.LocalVariable || v.AsgOp is not INode def || !slice.Add(def)) ++ { ++ continue; ++ } ++ ++ for (int s = 0; s < def.SourcesCount; s++) ++ { ++ work.Push(def.GetSource(s)); ++ } ++ } ++ ++ // 2. The banks the slice reads, and the bindings of those it loads with a computed offset to their ++ // last-frame slot's (only these get one: a binding is declared in the SPIR-V whether used or not, and the ++ // host's layout comes from the used ones). Direct operands (c[slot][offset]) get theirs when the ++ // structured program is made. ++ Dictionary<int, int> bindings = new(); ++ ++ foreach (INode n in slice) ++ { ++ if (n is Operation op && IsBankLoad(op, resourceManager, out int slot)) ++ { ++ banks |= 1 << slot; ++ ++ if (!bindings.ContainsKey(op.GetSource(0).Value)) ++ { ++ bindings[op.GetSource(0).Value] = resourceManager.GetConstantBufferBinding(Remap(slot)); ++ } ++ } ++ ++ for (int s = 0; s < n.SourcesCount; s++) ++ { ++ Operand src = n.GetSource(s); ++ ++ if (src != null && src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0) ++ { ++ banks |= 1 << src.GetCbufSlot(); ++ } ++ } ++ } ++ ++ if (banks == 0) ++ { ++ return -1; ++ } ++ ++ // 3. Which of the slice's values depend on those banks, to a fixed point (phis carry it around loops). ++ HashSet<Operand> tainted = new(); ++ bool changed = true; ++ ++ while (changed) ++ { ++ changed = false; ++ ++ foreach (BasicBlock block in blocks) ++ { ++ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next) ++ { ++ INode n = node.Value; ++ ++ if (!slice.Contains(n) || n.DestsCount == 0 || !Reads(n, tainted, bindings)) ++ { ++ continue; ++ } ++ ++ for (int d = 0; d < n.DestsCount; d++) ++ { ++ Operand dest = n.GetDest(d); ++ ++ if (dest != null && dest.Type == OperandType.LocalVariable && tainted.Add(dest)) ++ { ++ changed = true; ++ } ++ } ++ } ++ } ++ } ++ ++ // 4. A copy of each such operation beside it, with new destinations. ++ Dictionary<Operand, Operand> prev = new(); ++ List<(INode Original, INode Copy)> copies = new(); ++ ++ foreach (BasicBlock block in blocks) ++ { ++ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next) ++ { ++ INode n = node.Value; ++ ++ if (!slice.Contains(n) || n.DestsCount == 0 || !IsTainted(n, tainted)) ++ { ++ continue; ++ } ++ ++ INode copy = null; ++ ++ if (n is PhiNode phi) ++ { ++ Operand dest = Local(); ++ prev[phi.Dest] = dest; ++ copy = new PhiNode(dest); ++ } ++ else if (n is Operation op && op.GetType() == typeof(Operation) && op.Inst != Instruction.Store) ++ { ++ Operand[] dests = new Operand[op.DestsCount]; ++ ++ for (int d = 0; d < dests.Length; d++) ++ { ++ Operand dest = op.GetDest(d); ++ dests[d] = dest != null && dest.Type == OperandType.LocalVariable ? Local() : dest; ++ ++ if (dest != null && dest.Type == OperandType.LocalVariable) ++ { ++ prev[dest] = dests[d]; ++ } ++ } ++ ++ Operand[] sources = new Operand[op.SourcesCount]; ++ ++ for (int s = 0; s < sources.Length; s++) ++ { ++ sources[s] = op.GetSource(s); ++ } ++ ++ Operation c = op.StorageKind != StorageKind.None && dests.Length == 1 ++ ? new Operation(op.Inst, op.StorageKind, dests[0], sources) ++ : new Operation(op.Inst, op.Index, dests, sources); ++ c.ForcePrecise = op.ForcePrecise; ++ copy = c; ++ } ++ ++ if (copy != null) ++ { ++ node = block.Operations.AddAfter(node, copy); ++ copies.Add((n, copy)); ++ } ++ } ++ } ++ ++ // 5. The copies' sources: last frame's values, the remapped banks. ++ foreach ((INode original, INode copy) in copies) ++ { ++ if (copy is PhiNode phiCopy) ++ { ++ PhiNode phi = (PhiNode)original; ++ ++ for (int s = 0; s < phi.SourcesCount; s++) ++ { ++ phiCopy.AddSource(phi.GetBlock(s), Prev(phi.GetSource(s), prev)); ++ } ++ ++ continue; ++ } ++ ++ Operation op = (Operation)original, c = (Operation)copy; ++ ++ for (int s = 0; s < op.SourcesCount; s++) ++ { ++ Operand src = op.GetSource(s); ++ ++ if (s == 0 && op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer && ++ src.Type == OperandType.Constant && bindings.TryGetValue(src.Value, out int binding)) ++ { ++ c.SetSource(s, Const(binding)); ++ } ++ else if (src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0) ++ { ++ c.SetSource(s, Cbuf(Remap(src.GetCbufSlot()), src.GetCbufOffset())); ++ } ++ else ++ { ++ c.SetSource(s, Prev(src, prev)); ++ } ++ } ++ } ++ ++ // 6. Each position store also writes last frame's position to the output. ++ foreach (BasicBlock block in blocks) ++ { ++ for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next) ++ { ++ if (!IsPositionStore(node.Value)) ++ { ++ continue; ++ } ++ ++ Operation op = (Operation)node.Value; ++ Operation store = new(Instruction.Store, StorageKind.Output, null, ++ Const((int)IoVariable.UserDefined), Const(location), op.GetSource(1), Prev(op.GetSource(2), prev)); ++ node = block.Operations.AddAfter(node, store); ++ } ++ } ++ ++ attributeUsage.SetOutputUserAttribute(location); ++ ++ return location; ++ } ++ ++ private static bool IsPositionStore(INode n) ++ { ++ return n is Operation op && op.Inst == Instruction.Store && op.StorageKind == StorageKind.Output && ++ op.SourcesCount == 3 && op.GetSource(0).Type == OperandType.Constant && ++ (IoVariable)op.GetSource(0).Value == IoVariable.Position; ++ } ++ ++ private static bool IsBankLoad(Operation op, ResourceManager resourceManager, out int slot) ++ { ++ slot = -1; ++ ++ return op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer && ++ op.GetSource(0).Type == OperandType.Constant && ++ resourceManager.TryGetConstantBufferSlot(op.GetSource(0).Value, out slot) && Remap(slot) >= 0; ++ } ++ ++ private static Operand Prev(Operand src, Dictionary<Operand, Operand> prev) ++ { ++ return src != null && prev.TryGetValue(src, out Operand p) ? p : src; ++ } ++ ++ private static bool IsTainted(INode n, HashSet<Operand> tainted) ++ { ++ for (int d = 0; d < n.DestsCount; d++) ++ { ++ if (n.GetDest(d) is Operand dest && tainted.Contains(dest)) ++ { ++ return true; ++ } ++ } ++ ++ return false; ++ } ++ ++ private static bool Reads(INode n, HashSet<Operand> tainted, Dictionary<int, int> bindings) ++ { ++ if (n is Operation op && op.Inst == Instruction.Load && op.StorageKind == StorageKind.ConstantBuffer && ++ op.GetSource(0).Type == OperandType.Constant && bindings.ContainsKey(op.GetSource(0).Value)) ++ { ++ return true; ++ } ++ ++ for (int s = 0; s < n.SourcesCount; s++) ++ { ++ Operand src = n.GetSource(s); ++ ++ if (src == null) ++ { ++ continue; ++ } ++ ++ if (src.Type == OperandType.ConstantBuffer && Remap(src.GetCbufSlot()) >= 0) ++ { ++ return true; ++ } ++ ++ if (tainted.Contains(src)) ++ { ++ return true; ++ } ++ } ++ ++ return false; ++ } ++ } ++} +--- a/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs +@@ -301,6 +301,12 @@ namespace Ryujinx.Graphics.Shader.Translation + + Optimizer.RunPass(context); + TransformPasses.RunPass(context); ++ ++ if (i == 0 && Definitions.Stage == ShaderStage.Vertex && GpuAccessor.QueryMotionPrevious()) ++ { ++ int location = MotionPrevious.Run(cfg.Blocks, resourceManager, AttributeUsage, out int banks); ++ GpuAccessor.ReportMotionPrevious(location, banks); ++ } + } + + funcs[i] = new Function(cfg.Blocks, $"fun{i}", false, inArgumentsCount, outArgumentsCount); diff --git a/totk.ini.example b/totk.ini.example index 13f267d0..fff5d78a 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -4,8 +4,16 @@ # '#' and ';' start a comment; [sections] only group lines. [frame rate] -# Frame rate limit: 30, 40, 45, 60, 90, 120, 144, any whole rate from 30 to 360, or display (the refresh rate of the -# window's monitor). Above 60 is experimental and needs realtime = 1. Changes while the game runs take effect at once. +# Frame rate limit: 30, 40, 45, 60, 90, 120, 144, any whole rate from 30 to 240, or display (the refresh rate of the +# window's monitor, at most 240). A higher rate gives 240: above it the game's physics runs out of memory. Above 60 +# is experimental and needs realtime = 1. Changes while the game runs take effect at once. +# With mailbox or fifo presentation (vk_present, the default) a frame reaches the screen at a vertical blank, so a limit +# that does not divide the monitor's refresh rate keeps each frame on screen for a different number of refreshes. On a +# 320 Hz monitor 80, 64, 40 and 32 divide it and 90 does not: 90 fps alternates 3 and 4 refreshes per frame, which reads +# as judder while the frame counter still shows 90. Likewise on 240 Hz 60, 48 and 40 divide it and on 144 Hz 72, 48 and +# 36 do, while 60 does not. vk_present = immediate shows a frame as soon as it is presented, without this quantisation, +# at the cost of tearing. A monitor running with variable refresh (FreeSync, G-Sync) follows the frames instead, so +# there any limit inside its range is even. # fps_limit = 60 # Older key, read only while fps_limit is unset: fps60 = 0 means a limit of 30. # fps60 = 1 @@ -24,10 +32,12 @@ # A thread waiting for a virtual core interrupts the owner when it runs game code: 1 = on, 2 = priority inheritance # without the interrupt, 0 = off (the old scheduling). # async_preempt = 1 -# Replay NVN submissions on their own thread, at most one frame behind the game (1 = on). Adds up to a frame of latency. -# replay_async = 0 -# 1 lifts the virtual core gate: guest threads run without the console's limit of one thread per core. For experiments -# only: the game keeps per-core data without locks and can crash. +# Replay NVN submissions on their own thread, at most one frame behind the game, 0 or 1. Default: on. Adds up to a +# frame of latency; 0 replays them on the game thread. +# replay_async = 1 +# EXPERIMENTAL, THE GAME CRASHES WITH 1. 1 lifts the virtual core gate: guest threads run without the console's limit +# of one thread per core, and the game keeps per-core data without locks. With 1, 1.2.1 and 1.4.3 crash or hang, +# usually at start (#212). Keep 0 to play. # free_cores = 0 # Windows timer resolution of 1 ms for steadier frame pacing (0 = the Windows default). # timer_1ms = 1 @@ -46,16 +56,22 @@ # pipe_prebuild = 1 # Threads that translate known shaders at start (1 to 64; default: half the CPU threads, at most 16). # prewarm_xlate = 8 -# Vulkan graphics pipeline library, 0 or 1. Default: on, except on AMD's driver for RDNA 1 and 2 (RX 5000 and 6000). +# Vulkan graphics pipeline library, 0 or 1. Default: on. # gpl = 1 # Textures and memory. # ASTC texture decoding: native (the GPU's own ASTC support), gpu (a compute shader), cpu, or auto (native when the GPU # has it, gpu otherwise). # astc = auto +# With astc = gpu: re-encode the decoded ASTC textures to BC3, a quarter of the video memory, textures slightly blockier, +# 0 or 1. Default: on when the GPU has less than 5 GB of video memory, off otherwise. +# astc_bc = 1 # Game memory shared with the GPU: auto (mirror on a discrete GPU, import on an integrated one), import (host memory # imported into Vulkan), mirror (pools used as buffers get a copy in video memory) or verify (mirror plus a check of -# every copy; slow). +# every copy; slow). On a discrete GPU import can be far slower than mirror, not slightly slower: the GPU reads the +# game's pools over PCIe. A 5070 Ti Laptop went from 6 ms to 135-230 ms of GPU time per frame with import (5 fps on the +# title screen, bound: gpu, every line of the perf window waiting on a fence). The log names the mode in use: "mem: +# mode import" or "mem: mode mirror". # memory = auto # A new texture created over a live texture's memory starts with that content instead of empty. # tex_alias = 1 @@ -89,7 +105,7 @@ # Vulkan. # Vulkan validation layer: checks every call and slows the game down a lot. Needs the layer installed (Vulkan SDK). # vk_validation = 0 -# A thread of its own makes the Vulkan calls of each frame, 0 or 1. Default: on. +# A thread of its own makes the Vulkan calls of each frame, 0 or 1. Default: on, except on AMD GPUs. # vk_recorder = 1 # Dynamic vertex input, 0 or 1. Default: on, except on Intel's older integrated GPU drivers (Vulkan 1.3.215 and older). # vk_dyn_vertex = 1 @@ -124,8 +140,9 @@ # Game resolution: the size the game draws its picture and interface in (1280x720 to 7680x4320). Above 1920x1080 the # game's texture heap and memory pool grow with it. Another shape than 16:9, such as 3440x1440 on a 21:9 monitor or # the Steam Deck's 1280x800 (16:10), gives the camera's view that shape and lays the HUD out for it. The installer -# prepares the UI layout for ten sizes: 2560x1080, 3440x1440, 3840x1600, 5120x2160 (21:9), 3840x1080, 5120x1440 (32:9), -# 1280x800, 1680x1050, 1920x1200 and 2560x1600 (16:10). Any other size +# prepares the UI layout for fifteen sizes: 2560x1080, 3440x1440, 3840x1600, 5120x2160 (21:9), 3840x1080, 5120x1440 +# (32:9), 1280x800, 1680x1050, 1920x1200, 2560x1600 (16:10), 1920x1280, 2160x1440, 2256x1504, 2880x1920 and 3000x2000 +# (3:2). Any other size # gets the camera's view but a 16:9 HUD, stretched over the picture, unless you build its layout with # tools/make_romfs_patches.py --aspect WxH. Read at start. # output_res = 1920x1080 @@ -147,7 +164,7 @@ # Anisotropic filtering of surface textures: game (the level the game asks for, as on the Switch) or 2, 4, 8, 16. Keeps # ground, roads and walls seen at a shallow angle sharp into the distance. Only filtered, mipmapped textures that tile # are raised; shadow maps, data maps and screen-sized buffers keep the game's setting. Read at start. -# aniso = 16 +# aniso = game # Distant model detail: 1 keeps the detail models have up close at any distance, 0 (the default) lets the game switch # to simpler versions as they recede (recomp/patches-v121.txt, set lod). Costs GPU time. Game version 1.2.1 only. # Read at start. @@ -165,6 +182,67 @@ # 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 256, 512, 768, 1024, 1536 or 2048. Above 960 the texture heap grows with +# it. 256 is coarser than the game's own and is for integrated GPUs short of memory bandwidth. Read at start. +# shadow_res = game + +# Shadow map resolution: the game's sun cascades are 960x960 (and its static shadow maps 1440 and 720) whatever the game +# resolution; 2 to 4 draws them that many times larger on each side, for sharper shadows. 1 = the game's. The game's +# shadow edge filter is narrowed by the same factor (translator patch 0011), so close shadow edges don't look doubled. +# Read at start. +# shadow_scale = 1 +# Sun shadow cascade range in percent of the game's (its cascades end at 20, 40, 80 and 160 m), 50 to 400: higher moves +# the steps where shadows turn softer further away, and spreads the shadow map over more ground. Read at start. +# shadow_range = 100 +# Screen-space contact shadows, strength in percent (0 = off): after the game lights its scene, each pixel traces a +# short way toward the sun through the depth buffer and darkens where something close blocks it, adding the small +# shadows at feet, under stones and in grass that the shadow maps miss. Read at start. +# contact_shadows = 0 +# Soft sun shadows (PCSS), percent of the default penumbra width (0 to 200; 0 = the game's own four-tap filter): sharp +# where an object meets the ground and softer the further its shadow falls. Done in the translated shaders (translator +# patch 0010), so the shader cache is rebuilt once after an update. Read at start. +# pcss = 0 + +# NVIDIA DLSS / DLAA for the 3D scene, on NVIDIA RTX GPUs with the DLSS model from NVIDIA's DLSS SDK next to the +# executable (Windows: nvngx_dlss.dll next to totk.exe; Linux: libnvidia-ngx-dlss.so.<version> next to totk, with the +# NVIDIA driver's libnvidia-ngx.so.1 installed); elsewhere the setting does nothing. The HUD is drawn after it and is not touched. +# Read at start. +# dlaa = 0 +# What DLSS works from: dlaa (the 3D scene at full size), quality, balanced, performance or ultra_performance (the +# scene at 67, 58, 50 or 33 % of the output, scaled up by DLSS; these replace render_scale and sharpen texture filtering +# to match). They apply only with dlaa = 1 and the DLSS model next to the executable; otherwise the scene follows +# render_scale. Changes while the game runs take effect at once (1.4.3; 1.2.1 at the next start). +# dlss = dlaa +# The DLSS model: default (NVIDIA's advice per mode: K for dlaa, quality and balanced, M for performance, L for +# ultra_performance), k (K in every mode) or j (the other transformer model: slightly less ghosting behind moving +# characters, a little more flicker). Any other model by its letter, a to m: on slower RTX cards the older model e is +# cheaper. Changes while the game runs take effect at once (DLSS starts a new history). +# dlss_preset = default +# Per-object motion vectors for DLSS: characters, animals and moving objects get their own motion instead of the +# camera's, so DLSS keeps their history instead of smearing or rejecting it. Costs some CPU time each frame (about 1-2 ms +# on a laptop CPU); 0 leaves camera-only motion. Read at start. +# dlaa_objmv = 1 +# Characters' trails: where a character, animal or held item moves without motion vectors of its own (all of them with +# dlaa_objmv = 0), DLSS can drag a faint copy of it. The G-buffer marks those objects; on them and where they were the +# frame before, DLSS is told to favour the current frame by this percentage (0, 35, 65 or 100; 0 off). Higher removes +# more of the trails and leaves those edges a little less smooth. Changes while the game runs take effect at once. +# dlss_char_mask = 65 +# The game's own FXAA after the post effects; with DLAA on it is not needed. Changes while the game runs take effect at +# once. +# fxaa = 1 + +# HDR output: hdr = 1 sends the picture as HDR10 (needs HDR on in Windows, "Use HDR", or a Wayland compositor with +# colour management; without it the picture stays SDR). The game's tonemap keeps highlights up to hdr_peak_nits (your +# display's peak); hdr_game_nits is the picture's white (paper white, 203 is the HDR reference) and hdr_ui_nits the +# HUD's and the menu's. hdr is read at start, the nits at once. The colour profile above is for SDR only. +# hdr = 0 +# hdr_peak_nits = 1000 +# hdr_game_nits = 203 +# hdr_ui_nits = 203 +# Highlight hue correction in percent (0 to 100): bright colours such as fire turn towards white as in the game's SDR +# picture, matching the light they cast; 0 keeps them fully saturated. Changes take effect at once. +# hdr_hue = 80 [language] # Language: a Windows locale such as de-DE, or a Switch code (en-US, ja, zh-Hant, ...). Default: the Windows locale. @@ -172,10 +250,22 @@ # lang = en-US # System mode reported to the game: docked or handheld. # operation_mode = docked -# Language of the settings menu and the overlay: en or ru. Default: the Windows display language, English if it is -# neither. +# Language of the settings menu and the overlay: en, ru, pt-BR or zh-Hans. Default: the Windows display language, English if +# it is none of them. # menu_lang = en +[discord] +# Rich Presence: "Playing TotK Recomp" with the play time in your Discord status while the game runs, through the +# local IPC of a running Discord client. 1 = on; the Menu tab of the settings menu (F1) switches it too, and the +# change reaches the presence thread at once. +# discord_rpc = 0 +# The application whose name and artwork the status shows. The default is the project's own application, +# registered as "TotK Recomp" with the port's green ring icon as its art. Register your own at +# discord.com/developers (name and art are yours to choose) and put its id here. +# discord_app_id = 1567232599846293504 +# The key of the application's uploaded art asset shown as the status's large image. +# discord_image = totk_512x512 + [audio] # none mixes without opening an audio device. # audio = default @@ -183,6 +273,9 @@ # audio_prefill_ms = 20 # Volume in percent (0 to 100), on top of the game's own. Changes while the game runs take effect at once. # volume = 100 +# Speakers: stereo, 5.1 or auto (5.1 when the Windows speaker setup has six or more channels, stereo otherwise). The +# game is told the same channel count and mixes for it. Takes effect at the next start. +# audio_channels = stereo [input] # Gamepad motion controls (0 = off) and vibration (0 = off). @@ -195,15 +288,34 @@ # rumble_triggers = 0 # Which gamepad button is Switch A: nintendo (by position: east A, south B, west Y, north X), xbox (south A, east B, # west X, north Y, as an Xbox pad labels them; cross A on a PlayStation pad) or auto (nintendo for a pad with Nintendo -# labels, xbox for the others). xbox_prompts: the buttons as nintendo, and the game's prompts swapped to the Xbox -# labels (A with B, X with Y; it loads romfs-patches-xbox, docs/controls.md, "Xbox button prompts"). The bind_ lines -# below apply on top. Changes while the game runs take effect at once; the xbox_prompts prompts at the next start. +# labels, xbox for the others). The bind_ lines below apply on top. Changes while the game runs take effect at once. +# 0.1.9's xbox_prompts is read as nintendo with prompts = xbox. # pad_layout = auto +# The buttons the game's prompts show: nintendo (the game's own) or xbox (LB RB LT RT, Menu, View and a d-pad cross, +# and A B X Y as labelled on the button pad_layout presses: with nintendo, A shows as B and X as Y). Read at start +# (docs/controls.md, "Xbox button prompts"). +# prompts = nintendo # Gamepad stick dead zone in percent of full deflection (0 to 50). Raise it for a stick that drifts: inside the circle # the stick reads centred, and the rest of its travel is scaled to start there. # stick_deadzone = 8 +# Right-stick camera speed (0.1 to 3): multiplies the game's own camera turn rates, top speed included, at each of the +# five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens. +# Read at start. +# stick_sens = 1 +# The dead zone of one stick, in place of stick_deadzone for that stick (0 to 50; unset = stick_deadzone). The Controls +# tab shows each stick's deflection now, so a drifting stick's resting value can be read off. +# lstick_deadzone = 8 +# rstick_deadzone = 8 # Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture). # mouse_sens = 1 +# The keyboard's sticks are all or nothing, so the game only sees a full tilt from them; while this key is held the +# keyboard-driven stick values shrink and the keyboard can walk instead of run. Key names as in the bind_ lines below, +# none turns it off. 0.2 to 0.9, the default holds for anything else. Only the keyboard is scaled: the mouse camera and +# the gamepad are unchanged. The Controls tab sets both. The default stops just above half because a keyboard sample of +# 32767 times 0.5 is one short of the 16384 the game's stick-direction bits need, and below that the keyboard stick +# reads as a partial tilt only. +# walk_key = lshift +# walk_scale = 0.51 # Write the input of this run to a file that replays as a script (docs/controls.md, "Scripted input"). Default: off. # input_record = input.txt # Remap buttons: the Controls tab of the settings menu (F1) writes these lines. One line per Switch button or stick @@ -224,9 +336,32 @@ # bind_X = pad:west, key:space # bind_Y = pad:north, key:mouse_l +[mods] +# Folder with the player's mods, one subfolder per mod (<name>/romfs, <name>/exefs, <name>/cheats). Default: mods next +# to totk.exe. +# mods_dir = +# Load order, top first, names separated by |. The in-game menu (Mods) writes it; a mod not listed goes to the bottom. +# mods_order = +# Mods switched off, names separated by |. +# mods_disabled = +# auto: when an enabled mod has cheats that change the game's code and the game was built without them, the game starts +# the setup's rebuild at start (a few minutes) and starts again after it. off: the game starts with the code it has. +# A rebuild that failed or was cancelled is not started again for the same cheats. Default: auto. +# mods_rebuild = auto + [files] -# Folder with RomFS mod files that replace or add game files. Default: none. +# Developer overlay: a folder with RomFS files and a manifest.tsv (tools/make_romfs_patches.py format). Players put +# their mods into the mods folder instead ([mods]). Default: none. # romfs_mods = +# Folder with the Switch shared fonts (BFTTF files, tools/extract_fonts.py output), for Chinese and Korean text. +# Default: shared-fonts next to totk.exe. TOTK_SHARED_FONTS overrides it. +# shared_fonts = + +[cheats] +# One line per cheat of the mods' cheat files, mods\<mod>\cheats\<build id>.txt (docs/cheats.md); the Cheats tab of +# the menu writes them. cheat_<hash> = 1 switches a cheat on, the hash being FNV-1a 32 of "<mod folder>/<cheat name>". +# Default: every cheat off. +# cheat_0123abcd = 1 [diagnostics] # Extra lines in the log, comma separated: svc, host, ipc, fs, gfx, perf, sched, audio, present or all. The menu's @@ -236,6 +371,10 @@ # takes longer than flight_spike_ms (0 to 60000). # flight = 0 # flight_spike_ms = 100 +# GPU pass marks: after a GPU crash (device lost) the log names the render pass or dispatch the GPU had started and the +# last one it finished. Unset: on AMD GPUs (VK_AMD_buffer_marker). 1: also on NVIDIA GPUs +# (VK_NV_device_diagnostic_checkpoints), which NVIDIA says costs a little. 0: off. +# pass_crumbs = 1 # Largest render target (bytes, 0 to 1073741824) that goes back to its game memory when the game waits for a GPU fence. # A game that reads a larger target without invalidating it sees stale bytes at the default. With trace = gfx the log # names each dirty target over the limit that stays on the GPU ("fence write-back skipped-for-size") and each one over