Merge main into task/menu: keep main's button remapping, Esc cursor release and new settings beside the live settings, with one live mouse sensitivity

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-26 08:27:26 +03:00
commit c3b3c297cf
85 files changed
+12187 -528

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ prints `<log>: rc=<code>`. The game window opens behind the user's windows witho
| `user-save-2026-10-21-monsters` | Moblins and Zonai constructs attack at once; use with `--script tools/input/fight.txt` |
| `user-save-2026-10-21` | Hyrule field facing the castle, rainy; distant sky over the castle |
| `user-save-2026-10-23-pillars` | Hyrule field by standing stone pillars, about 17:40, a Zonai construct beside Link; use with `--script tools/input/backrun.txt` (runs backward from 1550: phantom rocks, a one-frame glitch at about 1605) |
| `user-save-2026-10-24-brightbloom` | Cave, about 20:40: a Brightbloom plant at the lower left (its bulb floats beside the stem, `build/v121-bloom/NOTES.md`), teal Moblin and Bokoblin ahead or Zonai constructs (runs differ) |
| `user-save-2026-10-22-island` | Sky island edge; use with `--script tools/input/island.txt` (runs off, skydives, opens the paraglider with B at frame 1975; X does nothing in a dive) |
`--frames` counts presents. Pick `1500 + the frames you need in-world`.
+24 -3
View File
@@ -6,9 +6,10 @@ Code copied or adapted from another project is listed here with its source, the
|---|---|---|---|
| MacNativeGames extraction scripts and verification hashes | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11`, Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/vendor/macnativegames/` (unmodified), `tools/data/expected-v*.json` |
| LAS Recomp lifter, code generator, FP/SIMD lowering, CPU headers and differential tests | https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f`, Copyright (c) 2026 Peiikai | MIT | `recomp/lifter.py`, `recomp/simd_c.py`, `recomp/nso.py`, `recomp/gen.py`, `recomp/module.py`, `tests/jumptables_check.py`, `runtime/cpu.h`, `runtime/simd.h`, `runtime/simd_fp.h`, `tests/diff/` |
| LAS Recomp host runtime: NSO loader, host-function table, guest-space reservation and re-execution, guest thread start | https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f`, Copyright (c) 2026 Peiikai | MIT | `runtime/loader.c`, `runtime/hostfn.c`, `runtime/memory.c`; M3 tasks T2 and T6: `runtime/kernel.c`, `runtime/boot.c`; M4 T2: `runtime/vk_device.c`, `runtime/gfx_window.c`, M4 T5a: `runtime/tex_astc.c` (`runtime/nvn/astc.c`), `runtime/vk_formats.c` (format table of `runtime/nvn/vulkan/textures.c`), M4 T5b: `runtime/vk_memory.c` (page write generations of `runtime/nvn/vulkan/exec.c`, deferred destruction of `textures.c`), `runtime/vk_texture.c` (image creation, uploads and samplers of `runtime/nvn/vulkan/textures.c`), M4 T6a: `runtime/vk_sync.c` (timeline submissions and waits of `runtime/nvn/vulkan/exec.c`), M4 T6b: `runtime/vk_pipeline.c` (blend, compare and stencil mappings, vertex format table with its 3-component fallback and pipeline cache file of `runtime/nvn/vulkan/exec.c` and `device.c`), M4 T6c1: `runtime/vk_exec.c` (pass begin and end, scissor clip and attachment clears of `runtime/nvn/vulkan/exec.c`), M4 T6c3: `runtime/vk_exec_xfer.c` (copies of `runtime/nvn/vulkan/exec.c`), M4 T6c2: `runtime/vk_exec_draw.c` (uniform buffer spans, texture and sampler descriptors and dispatch flow of `runtime/nvn/vulkan/exec.c`) |
| LAS Recomp host runtime: NSO loader, host-function table, guest-space reservation and re-execution, guest thread start | https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f`, Copyright (c) 2026 Peiikai | MIT | `runtime/loader.c`, `runtime/hostfn.c`, `runtime/memory.c`; M3 tasks T2 and T6: `runtime/kernel.c`, `runtime/boot.c`; M4 T2: `runtime/vk_device.c`, `runtime/gfx_window.c`, M4 T5a: `runtime/tex_astc.c` (`runtime/nvn/astc.c`; its decode ported to GLSL in `runtime/shaders/astc_decode.comp`, compiled into `runtime/astc_decode_spv.h`, and its tables copied into `runtime/vk_astc.c`), `runtime/vk_formats.c` (format table of `runtime/nvn/vulkan/textures.c`), M4 T5b: `runtime/vk_memory.c` (page write generations of `runtime/nvn/vulkan/exec.c`, deferred destruction of `textures.c`), `runtime/vk_texture.c` (image creation, uploads and samplers of `runtime/nvn/vulkan/textures.c`), M4 T6a: `runtime/vk_sync.c` (timeline submissions and waits of `runtime/nvn/vulkan/exec.c`), M4 T6b: `runtime/vk_pipeline.c` (blend, compare and stencil mappings, vertex format table with its 3-component fallback and pipeline cache file of `runtime/nvn/vulkan/exec.c` and `device.c`), M4 T6c1: `runtime/vk_exec.c` (pass begin and end, scissor clip and attachment clears of `runtime/nvn/vulkan/exec.c`), M4 T6c3: `runtime/vk_exec_xfer.c` (copies of `runtime/nvn/vulkan/exec.c`), M4 T6c2: `runtime/vk_exec_draw.c` (uniform buffer spans, texture and sampler descriptors and dispatch flow of `runtime/nvn/vulkan/exec.c`) |
| MacNativeGames TotK host runtime: dynamic linking, identity check, module permissions, guest memory and SVCs, threads, IPC, audio renderer, system services, RomFS and save storage, boot sequence | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/runtime/`, `games/totk/scripts/prepare_runtime.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/loader.c`, `runtime/memory.c`; M3 tasks T2-T6: `runtime/kernel.c`, `runtime/svc.c`, `runtime/ipc.c`, `runtime/ipc.h`, `runtime/audren.c`, `runtime/audren.h`, `runtime/audio_mixer.c`, `runtime/audio_effects.c`, `runtime/audio_effects.h`, `runtime/audio_reverb.c`, `runtime/audio_reverb.h` (M5; SDL3 output replaces CoreAudio), `runtime/ov_system.c` (also `shared_font.c`: the BFTTF layout behind nn::pl), `runtime/ov_input.c` (also `hid_feedback.c`: vibration and six-axis handles and calls), `runtime/motion_fusion.c` (six-axis fusion of `motion_fusion.c`), `runtime/ov_media.c` (M5: hardware Opus decoder over libopus), `runtime/swkbd_win.c` (`swkbd_host.c`: ShowKeyboardArg offsets, UTF-8/UTF-16 conversion, disabled-key filter, result codes; Win32 window replaces the macOS text bar), `runtime/ov_gfx.c`, `runtime/romfs.c`, `runtime/romfs_path.h`, `runtime/fs_host.c`, `runtime/fs.h`, `runtime/boot.c`; M4 T2: `runtime/gfx_window.c` |
| 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 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` |
| d2r-mobile host thread environment (TLR, TLS, thread object), the origin of MacNativeGames' thread layer; d2r-mobile NVN host object layouts, command record format and NVN front end, as adapted for TotK in MacNativeGames' `d2r_gfx` copy | https://github.com/jhaizhou-ops/d2r-mobile @ `53e7c5bb9ca3ea53e2160005be8fbe920cd9cb53` (`dm/host/thread.c`, `dm/host/hle_thread.c`); the NVN layouts and front end via https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/third_party/d2r_gfx/gfx/nvn/nvn_obj.h`, the two types of `nvn_tex.h`, `hle_nvn.c`, `totk_nvn_ext.c`, `nvn_impl.h`, `nvn_hmap.h`, `nvn_pool_index.h`, the request checks and mapping semantics of `totk_vpool.c`, the generation stamps of `nvn_dirty.c`, texture mirroring and texel copies of `nvn_mtl.m`; licence in `games/totk/third_party/d2r_gfx/LICENSE`), Copyright (c) 2026 jhaizhou | MIT | M3 task T2: `runtime/kernel.c`; M4 task T1: `runtime/nvn_obj.h`; M4 task T3c: `runtime/nvn_api.h`, `runtime/nvn_api_core.c`, `runtime/nvn_hmap.h`, `runtime/nvn_pool_index.h`; M4 task T3d: `runtime/nvn_api_obj.c` (also `nvn_pace.h`); M4 task T3e: `runtime/nvn_api_cmd.c`, `runtime/nvn_ext.c`; M4 task T5b: `runtime/vk_memory.c`, `runtime/vk_texture.c`; M4 task T6a: `runtime/vk_sync.c` (side effects written after GPU completion, `hle_nvn.c`); M4 task T6b: `runtime/vk_pipeline.c` (pipeline key and state mapping of `nvn_mtl.m` `pso_key_fill` / `pso_desc`, its `vfmt` vertex formats and the missing-varying fix-up of `msl_varyings`); M4 task T6c1: `runtime/vk_exec.c`, `runtime/vk_exec.h` (op loop, pure-state ops and state defaults of `nvn_mtl.m` `mtl_exec` / `state_op` / `state_reset`); M4 task T6c3: `runtime/vk_exec_xfer.c` (copy, clear, event, counter and conditional-render semantics of `nvn_mtl.m` `copy_t2b` / `copy_b2t` / `copy_t2t` / `clear_tex` / `cond_eval`); M4 task T6c2: `runtime/vk_exec_draw.c` (bind ops, draw and dispatch flow, vertex streams of `nvn_mtl.m` `state_op` / `bind_vertex_streams` / `draw_inner` / `dispatch`) |
| nxnative NVN front end (d2r-mobile's successor): texture view depth-stencil mode bits, stencil-view rule, `Downsample` and indirect draw and dispatch records | https://github.com/seedds/nxnative @ `d5b380b3a80495609c2e751bb994911a638935ad` (`dm/gfx/nvn/hle_nvn.c`, `nvn_obj.h`, `nvn_mtl.m`), Copyright (c) 2026 jhaizhou | MIT | M5: `runtime/nvn_obj.h`, `runtime/nvn_ext.c`, `runtime/nvn_api_cmd.c`, `runtime/vk_exec_draw.c` |
@@ -19,7 +20,7 @@ Code copied or adapted from another project is listed here with its source, the
| deko3d block-linear image layout, as ported in MacNativeGames' `nvn_tex.c` (Apple parts dropped, layout math unchanged; licence text in `games/totk/third_party/d2r_gfx/LICENSE.deko3d`, reproduced in the file header) | https://github.com/devkitPro/deko3d (`source/dk_image.cpp`), via MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/third_party/d2r_gfx/gfx/nvn/nvn_tex.c`, `nvn_tex.h`), Copyright (C) 2018-2020 fincs | zlib | M4 task T3b: `runtime/nvn_tex.c`, `runtime/nvn_tex.h` |
| MacNativeGames texture auto-layout goldens (40 original-SDK size and block-height cases) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/tests/texture_auto_layout/probe.c`), Copyright (c) 2026 jhaizhou | MIT | M4 task T3b: `runtime/nvn_tex_probe.c`; T3f: `runtime/nvn_probe.c` (four of the cases, through the procs) |
| MacNativeGames block-linear deswizzler (address function; copy loop split out as `tex_bl_copy`) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/third_party/d2r_gfx/gfx/tex/nvn_deswizzle.c`), Copyright (c) 2026 jhaizhou | MIT | M4 task T5a: `runtime/tex_deswizzle.c`, `runtime/tex.h` |
| SDL 3.4.18 (MSYS2 `mingw-w64-ucrt-x86_64-SDL3`), dynamic link, `SDL3.dll` and its dependency `libiconv-2.dll` (LGPL-2.1, unmodified, not linked into the exe) copied next to `totk.exe` | https://libsdl.org, Copyright (c) 1997-2026 Sam Lantinga | zlib | `runtime/input_win.c` (gamepad input, rumble, gyro and accelerometer) |
| SDL 3.4.18, dynamic link, `SDL3.dll` copied next to `totk.exe`. Developer builds use the MSYS2 build (`mingw-w64-ucrt-x86_64-sdl3`, which imports `libiconv-2.dll`, LGPL-2.1, unmodified, copied beside it); the installer release ships the upstream release `SDL3-3.4.18-win32-x64.zip`, which imports no libiconv | https://libsdl.org, Copyright (c) 1997-2026 Sam Lantinga | zlib | `runtime/input_win.c` (gamepad input, rumble, gyro and accelerometer) |
| libopus (MSYS2 `mingw-w64-ucrt-x86_64-opus`), dynamic link, `libopus-0.dll` copied next to `totk.exe` | https://opus-codec.org, Copyright (c) 2001-2023 Xiph.Org, Skype Limited, Octasic, Jean-Marc Valin, Timothy B. Terriberry, CSIRO, Gregory Maxwell, Mark Borgerding, Erik de Castro Lopo, Mozilla, Amazon | BSD-3-Clause | `runtime/ov_media.c` (nn::codec hardware Opus decoder) |
| Dear ImGui 1.91.9 (core and Vulkan back end, unmodified), as vendored by LAS Recomp | https://github.com/ocornut/imgui, via https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f` (`third_party/imgui/`), Copyright (c) 2014-2025 Omar Cornut | MIT | `runtime/third_party/imgui/` (for the in-game menu; not compiled yet) |
| Vulkan Memory Allocator 3.4.0 (MSYS2 `mingw-w64-ucrt-x86_64-vulkan-memory-allocator`), header-only, compiled into `totk.exe` | https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator, Copyright (c) 2017-2026 Advanced Micro Devices, Inc. | MIT | `runtime/vk_vma.cpp` (the allocator behind `runtime/vk_memory.c`) |
@@ -32,6 +33,26 @@ These tools run on the developer's or player's machine during the build. They ar
| Tool | Source | License | Purpose |
|---|---|---|---|
| hactool | https://github.com/SciresM/hactool @ `1d64a83450e025622f3468c28fc4164dad2c5ef6`, with patches from MacNativeGames | ISC | NCA and update container extraction |
| hactool (the installer release ships it, see below) | https://github.com/SciresM/hactool @ `1d64a83450e025622f3468c28fc4164dad2c5ef6`, with patches from MacNativeGames | ISC | NCA and update container extraction |
| Capstone | https://www.capstone-engine.org | BSD-3-Clause | ARM64 disassembly for analysis |
| Unicorn | https://www.unicorn-engine.org | GPL-2.0 (Python bindings BSD) | Differential testing of the lifter; never linked into the game |
## Distributed in the installer release
`tools/make_release.py` assembles these into the release folder of `docs/installer.md`. Versions and sha256 of every download are pinned in that script; licence texts are in the folder's `licenses/` and in each component's own folder.
| Component | Source | License | Where in the release |
|---|---|---|---|
| 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/` |
| 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/` |
| libvpx 1.17.0 (MSYS2 `mingw-w64-ucrt-x86_64-libvpx` 1.17.0-1), headers and static library compiled into `totk.exe`; `libvpx-LICENSE.txt` and `libvpx-PATENTS.txt` come from the v1.17.0 source tag | https://github.com/webmproject/libvpx | BSD-3-Clause with the WebM patent grant | `runtime/deps/`, `licenses/` |
| Vulkan-Headers and Vulkan-Loader 1.4.363.0 (MSYS2 packages; headers and `libvulkan-1.dll.a` only, no loader DLL) | https://github.com/KhronosGroup | Apache-2.0 OR MIT | `runtime/deps/` |
| Vulkan Memory Allocator 3.4.0 (MSYS2 package), header | https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator | MIT | `runtime/deps/` |
| Ninja 1.13.2 (`ninja-win.zip`) | https://github.com/ninja-build/ninja/releases/tag/v1.13.2 | Apache-2.0 | `bin/ninja.exe` |
| hactool (SciresM @ `1d64a83450e025622f3468c28fc4164dad2c5ef6` plus MacNativeGames' `portable-patches.diff`: `MNG_TITLE_KEYS` and a relaxed BKTR size check; our `unicode-patches.diff`: an embedded manifest that makes UTF-8 the process code page), built with MSYS2 UCRT64 gcc; imports only KERNEL32 and the UCRT | https://github.com/SciresM/hactool | ISC | `bin/hactool.exe` |
| `totk_xlate.exe` (Ryujinx shader translator, NativeAOT build of `tools/xlate/`, see the Ryujinx row above) | built by `tools/build_xlate.py` | MIT | `bin/totk_xlate.exe` |
| MacNativeGames shader variant list | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/assets/shader_list.tsv`), Copyright (c) 2026 MacNativeGames contributors | MIT | `data/shader_list.tsv` |
+40 -7
View File
@@ -25,8 +25,20 @@ other `totk.exe` or `ninja.exe` runs. The log's first and last lines record the
- `--env TOTK_TRACE=perf,sched`, then `python tools/perf_summary.py <log> <lo> <hi>`: fps, max frame and the
render-thread buckets per 60 frames. Per-thread run, core, host, lock and addr shares come per 120 presents.
- `TOTK_PROFILE=1 TOTK_PROFILE_FRAMES=lo-hi`: sampling profiler (`runtime/prof_win.c`). Resolve `exe+0x...` RVAs
with the build's `totk.map`.
A second `perf: frame N: gpu avg` line per window has the GPU busy time per frame (timestamp queries per submission
part, `vk_sync.c`; upload batches not included), fence waits of the render thread and of other threads, VRAM use and
budget (`VK_EXT_memory_budget`), process RAM and `bound: gpu|cpu|pace` (pace: the frame waited for pacing 15% of the
time or more; gpu: the GPU was busy 80% of the interval or more).
- The pool census (`mem: census`, `vk_memory.c` `vk_mem_census`) is logged at frame 1800, with `perf` every 1800
frames: pools by kind and flags, and the bytes bound or copied per frame by pool class since frame 1200. Also at
frame 600. `mem: class` (once at start) says whether the GPU counts as uma or discrete and lists its heaps and types.
- With `perf`, `mem: dirty` every 60 frames: what a VRAM mirror of the buffer-bound pools would upload (pages the CPU
wrote per frame and per submit, eager and lazy, by pool class), FlushMappedRange calls and bytes, and the mirror
size. The dirty count costs about 1.1 ms of CPU per frame on the submitting thread here (3 ms on an 8th-gen i7);
`perf_summary.py` summarises it.
- `TOTK_PROFILE=<path> TOTK_PROFILE_FRAMES=lo-hi`: sampling profiler (`runtime/prof_win.c`), read with
`tools/prof_report.py <profile> <totk.map>`. With `TOTK_PROFILE_RENDER=1` it samples the render thread only and keeps
call chains: the report adds inclusive time per function and the exe callers of the driver and system DLLs.
- `tools/regress.py <build> --base build/v121-m4 [--routes fight,rock,island,field] [--repeat 3] [--cold] [--validation]
[--out dir]`: runs the routes through `run_game.py --quiet`, scores logs and `TOTK_SHOTS` frames
(`tools/regress_score.py`, tested by `tests/regress_score_test.py`) and prints median and worst per metric. Run the candidate alone and judge it against
@@ -55,11 +67,26 @@ RenderDoc cannot capture: the driver hides `VK_EXT_external_memory_host`.
## Other GPUs
The RX 9070 is the only GPU here; the build also has to run on NVIDIA and older AMD (Vega, wave64 only). New Vulkan code
queries the feature, extension, format or limit it uses and keeps a working path or a logged fallback when it is
missing (`vk_pipeline.c` provoking vertex and subgroup size pinning are examples). Never hard-code what this GPU
The RX 9070 is the only GPU agents can run; the build also has to run on NVIDIA and older AMD (Vega, wave64 only). New
Vulkan code queries the feature, extension, format or limit it uses and keeps a working path or a logged fallback when
it is missing (`vk_pipeline.c` provoking vertex and subgroup size pinning are examples). Never hard-code what this GPU
reports: alignments, subgroup size, depth formats, `maxPushDescriptors`.
Numbers from this machine do not carry over. Measured on the user's other machines (2026-10-24/25, logs and analysis
in `build/mem-plan.md`):
- GTX 1050 Ti (4 GiB, 214 MiB BAR, no ReBAR; i7 8th-10th gen) and RTX 3090 Ti: GPU time 110-120 ms per frame in the
world against 4.3 ms here, because the GPU reads imported host pools over PCIe. The VRAM mirror (cd6f490) brings
the 1050 Ti to 10-11 ms; that machine is now CPU-bound at 25-45 fps (replay 25-30 ms per frame, backlog 0b).
- The same write-watch scan costs about 3x the CPU time it costs here.
- NVIDIA has no ASTC: first texture uploads decode on the CPU, 0.4-0.7 s load frames (backlog 5a.2).
- `cannot alias its pool memory` fills its 16-line log limit on NVIDIA and on the RX 9070 alike (1920x1080, 1600x900,
800x450 render targets); a suspect for the black patches of backlog 0a (`build/alias-plan.md`).
A run on another machine goes through the user: copy `build/release/TotK-perf-log.vbs` next to the `totk.exe` you
hand over. It starts the game with `TOTK_TRACE=perf` and writes `logs\perf-<date>-<time>.log`. Ask for the log; the
user cannot run scripted input there.
## Checks before handing back
`totk.exe --probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex` and `--probe-shader` run from the build directory and
@@ -80,8 +107,14 @@ Check these classes before you re-investigate a symptom that looks like one of t
writes them back first. Other CPU reads of GPU output are not covered yet. A write-back skips a texture whose
storage the CPU rewrote after the GPU render (`vk_texture.c` `write_back`); writing it anyway put stale grass maps
into the far terrain for a second (texture flicker on a hit).
- A one-shot draw (a target not rendered in the previous frame) waits for its shader and pipeline instead of being
skipped: `vk_exec_draw.c` `prepare_draw`. Skipping left the cloud and fog volumes empty for the session.
- The CPU reads GPU-rendered textures in CPU-cached pools: `nvnMemoryPoolInvalidateMappedRange` writes them back
(`nvn_ext.c` `nvn_pool_invalidate`; TotK invalidates its 260x260 R16 grass maps and 1x1 targets), and once the CPU
waits for a fence, targets up to 64 KiB rendered before it go back too (`vk_texture.c`
`vk_texture_write_back_small`): the 128x128 cut map is read with no invalidate. Without it a cut left the grass
standing, with no blades and no insects.
- A one-shot draw (a pass none of whose targets the previous frame rendered) waits for its shader and pipeline instead
of being skipped: `vk_exec_draw.c` `prepare_draw`, `TOTK_PIPE_SKIP`. Skipping left the cloud and fog volumes empty
for the session. Its stage libraries build on idle workers in parallel (`vk_pipeline.c` `lib_wait`).
- Storage buffers bound by Ryujinx first-use order instead of the NVN c0 slot: `vk_exec_draw.c` `ssbo_slot`. It showed
as tile rectangles.
- 3D colour targets rejected: `vk_exec.c` `resolve_target`. The grading LUT was never written, so the dialog
+7
View File
@@ -55,6 +55,13 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app
- Three virtual cores. A guest thread runs guest code only while it holds a core; the others wait in a priority-ordered list.
- Preemption. When a waiting thread has a better priority than a holder on a core it may use, exactly one holder is flagged. The holder checks the flag at its next SVC and at every indirect call (`rt_dispatch`: two loads when nobody waits), checks again under the kernel lock that a better thread still waits, and only then hands its core over. The indirect-call check comes from SavageWindRecomp's BotW port; checking only at SVCs left a better thread waiting up to 26 ms.
- Asynchronous preemption (from MacNativeGames 8bf0893, on unless `TOTK_ASYNC_PREEMPT=0`). Game code can run for
milliseconds with neither (`TOTK_CORE_DIAG=1` measures it): a waiter that has spun 250 us suspends the holder and,
if its host rip is in lifted code at host depth 0 (not inside a host call or SVC), moves it to `kern_ap_stub`, which
saves the volatile registers and gives the core up there. A holder of a guest mutex counts at the priority of its
best waiter, and one inside an SDK spin lock (TLR+0x100 `disable_count`) is not preempted: its interrupt flag is set
and the switch happens at SVC 0x36. `TOTK_ASYNC_PREEMPT=2` keeps those two without interrupting. The interruption is
Win32 (SuspendThread, SetThreadContext); another host needs a signal handler there, as MacNativeGames has.
- Host work gives the core back. Queue submits, presents, GPU waits and shader translation run without a core (`NVN_BLOCKING`, `kern_core_away`), so a thread blocked in the driver does not hold up guest threads.
- `TOTK_FREE_CORES=1` turns the gate off, for experiments.
+160 -49
View File
@@ -4,6 +4,88 @@ Priorities: no artifacts and no crashes first, then 60+ fps without dips, then o
names its evidence; Maxwell item numbers refer to [research/maxwell.md](research/maxwell.md). Remove an item when it is
merged or dropped.
## Open work (2026-10-25)
Everything still to do, in one list. Items below this section give older detail. Merged into main on 2026-10-25:
shader memo and push skip, page-indexed pool resolve cache, region-only Read/WriteTexels, GOB-wide swizzle, Intel
legacy vertex-input fallback, all-thread profile chains (04fb67b); descriptor build skip and update templates
(283bc70; field replay 8.87 to 7.99 ms); ASTC decode in a compute shader and native ASTC where sampled (0a0584e;
monsters2 worst frame 340 to 139 ms); waiting draws' stage libraries on idle workers and skipping draws into passes
with a per-frame target (3753939; cold monsters2 worst 257 to 131 ms). Dropped after measuring: narrower guest-written
marks for WriteTexels and box-only image updates (re-uploads after WriteTexels are 0.003-0.05% of frame time;
`build/v121-astcgpu/texels-narrow.patch`, `build/v121-texbox/texbox-wip.patch`).
Measure before building: every item states the number that decides it.
**Freezes and frame time**
1. Replay thread (5a.1): `task/replayq`, `build/v121-replayq/HANDOFF.md`. Ported onto current main, probes pass; game
checks and async against sync timings unfinished. The laptop is replay-bound (25-30 ms per frame, item 0b).
2. Texture uploads in gameplay, now the largest cause of long frames: `task/texasync`,
`build/v121-texasync/HANDOFF.md`. Step 1 splits the cost (image and memory creation, CPU deswizzle, staging,
copies, locks) before choosing a worker-thread upload, GPU deswizzle, image pooling or a per-frame budget.
3. Virtual-core preemption as Horizon does (MacNativeGames 8bf0893 / e4b4601, PR #488: asynchronous preemption of an
owner in game code, priority inheritance, the SDK's preemption-disable protocol; Mac worst-1% frame 18.4 to 10.6
ms): `task/preempt`, `build/v121-preempt/NOTES.md`. Ported (`TOTK_ASYNC_PREEMPT`, on by default; 0 restores the old
scheduling, 2 keeps inheritance and the disable protocol without interrupting). It cuts the waits it targets
(`TOTK_CORE_DIAG`: game-class waits of 1 ms or more 53 / 41 / 27 to 4 / 10 / 18 in field / fight / monsters2), but
the A/B timings show no mean-fps gain: fight >25 ms frames base 62, port 96 over four pairs (fps 58.7 / 58.2,
inside run-to-run noise); one of three base field runs froze 7 s at frame 2388 with the render thread blocked on
nothing, no port run did. Next: user play with it on, `TOTK_ASYNC_PREEMPT=0` first on a new hang or stutter; if
fight stays worse, try a shorter first spin (`AP_FIRST_NS`, 250 us) or mode 2.
4. Start-up re-warm when the driver cache does not match the exe path or device (5a.3): `task/rewarm` (main merged
in, 0b3bc73), untested in game. Needed before the laptop package.
5. Shared shader list: the newer MacNativeGames lists collapse variants that differ only in texture kind into `"*"`
rows, which our runtime cannot match (it translates for the exact kind). `tools/prepare_shaders.py` reads `"*"`
as 2D (uncommitted), and `build/shader-lists/union.tsv` unions every list revision since b75be8c. Option: match
`"*"` rows at run time as MacNativeGames does (`prepare_shader_seed.py:60-76`).
6. Freeze census: a 10-15 minute user session on current main with `TOTK_FLIGHT=1`, `TOTK_TRACE=perf` and
`TOTK_INPUT_RECORD`, ranking every frame over 50 ms by cause. Decides items 7-9.
7. `TOTK_PIPE_SKIP=fullscreen` as default (cold monsters2: 0 frames over 100 ms, 1% low 33.9 to 41.7 fps; risk: one
frame without a full-screen pass the first time its pipeline appears). Decide from item 6.
8. ASTC per-frame decode budget (4b): the largest batch, the 169 M texel 8x8 array at boot, scales to about 375 ms of
GPU time on a GTX 1050 Ti. Split by mips with `minLod` if laptop runs (`TOTK_ASTC_TIME=1`) show decodes in play.
9. ASTC to BC7 re-encode on the GPU for 4 GB cards: the same array is 677 MB as RGBA8 against 42 MB of blocks.
10. Spin-wait gates (MacNativeGames a038e6b, from nxnative 066f2af / 6d34984, MIT): loops polling memory hand the
core to a queued thread after 50 us. Survey TotK 1.2.1 for such loops first.
11. Over the GPU memory budget, wait for running work and drain deferred releases before failing (MacNativeGames
4de7b4e); 4 GB cards.
12. Later: recording on several threads, descriptor buffers or bindless (touches the translator), CPU/GPU frame
overlap as LAS Recomp 13398126d (only with item 1; keeps the phantom-object guard), an x86-64-v2 build for CPUs
without AVX2/FMA (SavageWindRecomp ae42014b4).
13. Laptop package and test after items 1-5 (GTX 1050 Ti and UHD 630). Untested there: native ASTC on Intel, the
Intel `minStorageBufferOffsetAlignment` padding of the ASTC path, the legacy-driver vertex-input fallback.
**Bugs**
14. Yunobo fades out and vanishes from the main view about 45 frames after the user presses A (ball fades at
1335-1350, gone at 1352; his shadow-map draws stay). Repro: `run_game.py build/v121-yunobo --save
user-save-2026-10-24-yunobo1 --script build/recordings/yunobo-cast2.txt --frames 1360`. Ruled out: occlusion
counts, CPU readback of GPU targets, missing NVN entry points, GPU-side resources: the game itself stops issuing
his main-view draws. Leading hypothesis: game logic fades the model alpha (material c11[33].w) and drops it from
the queues on a CPU input (camera-near fade, GfxColFade, CancelDraw distance or an AI fade). Next: find his c11
buffer, write-watch c11[33].w to its writer, walk up in Ghidra; cheap check first: fps60 and real-time patches
off from present 1316. Debug tools uncommitted in `.worktrees/yunobo` (`build/v121-yunobo/debug-tools.diff`);
detail in `build/v121-yunobo/HANDOFF.md`.
15. Black patches for a moment on the GTX 1050 Ti (item 0a below, `task/alias`, `build/alias-plan.md`).
16. Lantern texture bug (item 6 below): waiting for the user's description or a cropped shot.
17. Validation: `cp_t_tcb_20` R32F view sampled with depth compare in `vkCmdDispatch` (correctness item 8), "VkDescriptorSet
0x0 does not have a binding for vp_c1" in `vkCmdDrawIndexed` (also in `build/v121/logs/val-base.log`), a fragment
write to Location 0 with no colour attachment.
18. The correctness queue below.
**Machine and housekeeping**
19. Faulty RAM: about 25 bugchecks in three days, also at 4800 without EXPO; each stick alone passed MemTest86 test
5. Since 2026-10-25 21:08 one stick (BD100C33) runs in channel A, single-channel, so timings from then on are not
comparable with dual-channel ones. Next: one stick in slot B; if clean, the pair is the fault (RMA the kit).
After the fix: clean `gen.py` and `build_game.py`, all diff tests and probes. Zero-filled cache files from the
2026-10-22 crash are in `build/quarantine-zero-cache-20261025`.
20. Waiting for the user's approval to commit: `docs/backlog.md`, `docs/agent-playbook.md`, `totk.ini.example`,
`tools/prepare_shaders.py`.
21. Clean up merged worktrees and build dirs (bindskip, astcgpu, pipewait, texbox): notes to `build/archive/`.
22. Paused branches: `task/menu` (in-game menu, 2 commits), `task/v143` (game version 1.4.3, item 22).
## Plan (agreed 2026-10-23)
Work the stages in order. At most three agents at once, at most two of them running the game. Done 2026-10-23 and
@@ -12,24 +94,58 @@ removed: validation-layer crash (`build/archive/v121-val`), frame-time summary i
and README, settings file `totk.ini` (`runtime/cfg.c`, `--probe-cfg`; a new variable needs a row in `known[]`;
`build/archive/v121-ini`), portable folder (`tools/package_build.py`, README build step 7; `build/archive/v121-port`),
freezes on new pipelines in fights (cold fight 53.9 to 59.7 fps mean, frames over 50 ms 10 to 0;
`TOTK_GPL_DRAW_BUILD=0` turns it off; `build/archive/v121-pfh`).
`TOTK_GPL_DRAW_BUILD=0` turns it off; `build/archive/v121-pfh`), agent tasks in git worktrees (`tools/worktree.py`,
`private-build` skill), rumble strength and curve (`rumble_strength`, `rumble_curve`, `rumble_triggers`), even
present cadence by back-edge pacing (fight intervals outside 16.7 +- 1 ms 24.5% to 2.4%, +3.3 ms latency;
`TOTK_TRACE=present`; `build/archive/v121-pace`), PC keyboard and mouse layout with the mouse camera (`input_map.c`,
`mouse_sens`; `build/archive/v121-kbm`; the game's button actions and the D-pad order 1-4 still to check in game).
**Stage 1: before the tester weekend**
0a. Black screen patches for a moment, seen by the user on the GTX 1050 Ti in the field (2026-10-25). Not the VRAM
mirror: a verify run that caught the glitch compared 4.45 million mirror pages with 0 mismatches. Leading
hypothesis: a new texture descriptor over a GPU-rendered texture fails `alias_compatible` and starts as a
zero-cleared own-memory image (`vk_texture.c` `entry_of`, `create_tex`, `init_image`). The run's two largest
`left uninitialised` counts (33, 41) fall in its last seconds, where the glitch was. Repro every time: open the
Purah Pad with a right stick click (keyboard C). Next: `task/alias`, per-alias
diagnostic lines and a `TOTK_TEX_FILL=magenta` knob, then a user run. Details: `build/alias-plan.md`.
0b. CPU-bound replay on weaker CPUs: with the mirror the 1050 Ti machine (8th-10th gen i7) renders the GPU part in
10-11 ms, but replay takes 25-30 ms per frame (5.2 ms on the RX 9070 machine), so 25-45 fps. The same machine
replayed in 7-13 ms at 8 fps in import mode. Mirror bookkeeping explains 2-3 ms. A `sched` run rules out core
contention: the render thread spends 79-88% of the wall time on the CPU inside `nvnQueueSubmitCommands`; guest threads
never preempt it and the OS takes about 5%. Suspects: NVIDIA driver cost per command, mirror and texture checks inside replay. Next: a
render-thread profile from the 1050 Ti (7f2f332 writes it on window close). Details: `build/cpu-plan.md`.
1. User play test of the prepared build: F11, menus, movies, rumble and gyro still to check. First report
(2026-10-23): flying smooth, short freezes in fights with monsters.
5a. Fight hitches, the user's main complaint. In a warm fight (`fight.txt`, frames 1500+) the game runs 58.3 fps mean,
5a. Fight hitches and stutters on new content, the user's main complaint. GPU memory mirror merged (cd6f490,
`build/mem-plan.md`; 1050 Ti GPU time 110-120 to 10-11 ms). Order now: 2 (ASTC), 3 (re-warm), 1 (replay thread,
also a candidate fix for 0b). In a warm fight (`fight.txt`, frames 1500+) the game runs 58.3 fps mean,
52.9% of frames over 16.7 ms, 6.3% over 20 ms; frames over 20 ms are 87% replay (19 ms) with no pipeline or
shader waits. The user's play added 0 shader translations and 29 full pipelines.
1. Uneven frames: replay off the critical path. Translate command buffers at `EndRecording` or on a queued replay
thread, so `QueueSubmitCommands` only queues; `TOTK_REPLAY_ASYNC=0` restores the synchronous path. Merge with
the new path off by default if the play test leaves any doubt. Code and probes done in `build/v121-rq`
(0 validation errors); no valid timings yet (its runs fell in the 21:00 file-cache corruption). Next: rebase on
runtime/, measure `fight.txt` warm with `--quiet`.
2. Streaming: the slow loading frames are first texture uploads (ASTC, 367 ms at frame 219 of `fight.txt`).
Measure where the time goes, then decode on the GPU or ahead of use.
thread, so `QueueSubmitCommands` only queues. Merged 2026-10-26 (eb17360) off by default:
`TOTK_REPLAY_ASYNC=1` turns it on. Reads wait for the texture's newest writer, releases are queued, drain
waits give the virtual core up; checks and validation clean (`build/archive/v121-replayq/NOTES.md`). On the
RX 9070 no fps gain: fight and monsters2 are bound by the game threads, field render busy 10 to 1.3 ms. With
the replay itself longer than the frame (the 1050 Ti case) there is nothing to overlap: the game's main thread
waits each frame for the previous frame's GPU work. Next: a user test on the laptops with `replay_async = 1`.
2. Hitches when something new appears: first texture uploads decode ASTC on the CPU inside the frame (367 ms at
frame 219 of `fight.txt` on the RX 9070; 402-714 ms load frames on the GTX 1050 Ti, `build/mem-plan.md`).
Desktop AMD and NVIDIA have no ASTC; Intel Gen9-11 iGPUs do (check that `vk_texture.c` uses it when the
device reports `textureCompressionASTC_LDR`). Plan, after the VRAM mirror:
1. Measure per load: ASTC bytes, decode time, block-linear deswizzle time (`PERF_ASTC` already times decode).
2. Licence check: Granite (Themaister, MIT) is believed to ship an ASTC compute decoder; yuzu's
`astc_decoder.comp` is GPL, read only.
3. Compute-shader decoder (plain Vulkan 1.1 compute, no extension, so every GPU we run on). Oracle: decode
every ASTC texture of the 1.2.1 RomFS with it and with `astc_decode_ldr_mt`, bit-exact match required.
4. In `vk_texture.c`: raw blocks go to staging (4-16x less PCIe traffic), a dispatch writes RGBA8 into the
image; `TOTK_ASTC=cpu` keeps the CPU path. VRAM use stays as today (decoded RGBA8); GPU re-encode to BC7
(yuzu's ASTC recompression) only if 2-4 GB cards run out.
3. Driver pipeline cache after a copy or driver update: the AMD driver keys its cache by exe path, so a moved folder
stutters until `--prepare-shaders` runs again. `task/rewarm` (1f3cd49, WIP) stamps `shader-prepared.bin` with
device, driver and exe path and re-warms at start on a mismatch; builds, probes pass, never run in game. Next
steps in `build/v121-rewarm/HANDOFF.md`. Whether NVIDIA caches pipeline libraries the same way: item 8.
6. Lantern texture bug: waiting for the user's description or a cropped shot.
7a. Keyboard and mouse: the PC layout and mouse camera agreed with the user (details below). Sonnet.
**Stage 2: tester weekend (2026-10-24/25)**
@@ -54,7 +170,14 @@ freezes on new pipelines in fights (cold fight 53.9 to 59.7 fps mean, frames ove
17. Platform layer: no Win32 outside `runtime/platform/` (`tools/check_platform.py`: 1195 uses in 41 files; FAIL in
`motion_fusion.c`, `movie_probe.c`, `ov_movie.c`, `swkbd_win.c`, `vk_exec_draw.c`).
18. Linux: `runtime/platform/linux.c`, as SavageWindRecomp splits windows, linux and macos files.
18. Linux: `runtime/platform/linux.c`, as SavageWindRecomp splits windows, linux and macos files. Write tracking
(`host.h` `mem_written_pages`, `mem_freeze`/`mem_thaw`/`mem_frozen`, used by the VRAM mirror and texture checks):
freezing maps to `mprotect` + a `SIGSEGV` handler; a kernel write (`read()` into guest memory) returns `EFAULT`
instead of faulting, so the same thaw-before-read sites apply. Written pages: `PAGEMAP_SCAN` ioctl with
write-protect matching (kernel 6.7+, made for Wine's `GetWriteWatch`); older kernels: write-protect the mirrored
and texture-bound pages after each sync and mark them in the fault handler (Ryujinx), uploading pools the game
rewrites every frame whole instead of tracking them. Soft-dirty (`/proc/self/clear_refs`) is process-wide and
slow, last resort. Under Proton the Windows build already works: Wine emulates both.
19. Steam Deck: RADV offers Vulkan 1.3 and `VK_EXT_external_memory_host`; 4 Zen 2 cores, so expect 30-40 fps.
20. Launcher (details below).
@@ -117,48 +240,36 @@ freezes on new pipelines in fights (cold fight 53.9 to 59.7 fps mean, frames ove
## Details for later items
- Keyboard and mouse (plan item 7a, agreed 2026-10-23). Today the keyboard map is `input_win.c:34-36` (camera on
I J K L, attack on F) and there is no mouse input at all.
- Layout:
- In-game menu, agreed with the user 2026-10-23. Base: LAS Recomp's menu (MIT, same Vulkan base as ours):
`refs/LASRecomp/runtime/nvn/vulkan/imgui_glue.cpp` (Dear ImGui on our device and dynamic rendering),
`runtime/ui/menu.mm`, `option_rows.mm`, `controls_ui.mm`; SavageWindRecomp has the same menu. Opens with F1/Esc or
Minus+Plus. While it is open, the game gets no input and keeps running (no pause). Every setting is saved to
`totk.ini`. Drawn after the blit of the game image into the swapchain image (`gfx_window.c` around line 536).
Linux comes later (SDL3 window, item 17): the menu and settings code stays free of Win32; only a thin adapter feeds
window and input events to ImGui. Menu text goes through a translation catalog (SavageWindRecomp
`runtime/ui/i18n.mm`), English and Russian. Stages, one branch each:
1. Frame: ImGui, open and close, input, live settings with `cfg_save`, catalog; pages video (fullscreen and window
size, FPS counter, frame limit 30 or 60), controls (rumble strength and curve, gyro on or off), sound (volume),
quit.
2. The remapping page (`controls_ui.mm`; the bindings of `input_map.c` and
[controls.md](controls.md) become the defaults) and mouse sensitivity.
| Switch | Action in the world | Keys |
|---|---|---|
| left stick | move | W A S D |
| right stick | camera | mouse; arrow keys as a second source |
| Y | attack | left mouse button |
| ZL | shield, lock on | right mouse button, also Z |
| ZR | bow | F, also X |
| X | jump | Space |
| B | run, cancel | Left Shift |
| A | interact, pick up | E |
| L | abilities | Q |
| R | throw weapon | R |
| left stick click | crouch | Left Ctrl |
| right stick click | | C |
| D-pad | | 1 2 3 4 |
| Plus | inventory | Tab |
| Minus | map | M |
Later pages: AMD FSR 1 or bilinear scaling of the 900p image to the window (LAS Recomp's Windows upscaler); a
frame-time graph with the wait breakdown of `TOTK_TRACE=perf` (also on F10); VSync (FIFO) for 60 Hz displays
without VRR, if testers report double frames under back-edge pacing; gyro sensitivity.
Esc stays free for the in-game menu. Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys;
menu tabs (L, R) are Q and R. The mouse wheel stays unbound: L and R are abilities and weapon throw in the world.
- Before coding, check in the game what L/R, ZL/ZR, X/Y, the D-pad and the right stick click do in the inventory
and in the world, and correct the action column.
- Mouse camera: raw mouse deltas per frame become right-stick deflection, with sensitivity
(`TOTK_MOUSE_SENS`) and the larger-deflection rule against the gamepad. The cursor is hidden and confined to the
client area while the window is focused and released on focus loss and while F11 changes the window. Not true
mouselook: the game's stick curve and dead zone still apply.
- Update [controls.md](controls.md). Remapping goes into `totk.ini` (plan item 21).
Live settings: today each module reads its `cfg_get` string once and keeps the value (`rumble_cfg` in
`ov_input.c`). The menu needs `cfg_set` and a version counter that consumers check (cheap; per-frame readers
re-read only when it changes), and `cfg_save` that rewrites only the changed lines of `totk.ini`, keeps the user's
comments and adds a missing key under its section (LAS Recomp's `settings_save` rewrites the whole file). A key
set in the environment shows greyed out in the menu. Settings read only at start (RomFS mods, shader preparation,
operation mode) are marked "after restart".
- In-game menu (Dear ImGui over our Vulkan output, as SavageWindRecomp `runtime/ui/menu.mm`). Opens with F1/Esc or
Minus+Plus. While it is open, the game gets no input. Pages:
- fullscreen and window size;
- performance overlay on F10 (fps, or a frame-time graph with the wait breakdown of `TOTK_TRACE=perf`);
- volume;
- async shader builds;
- quit.
Settings live in `totk.ini`. Leave room for later pages: upscaler, frame-rate choices, remapping,
and the software keyboard moved onto SavageWindRecomp's `osk.mm`.
Later: async shader builds, the software keyboard moved onto SavageWindRecomp's `osk.mm`. Rendering above the
game's 900p is a separate study (a game-side resolution patch, as Switch mods do, or scaling every render target
ourselves); MacNativeGames has neither (render scale fixed at 1).
- Gyro: the user's logs show only Xbox One and Series X pads, which have no gyro. With a DualSense or Pro Controller,
check Steam Input (it presents pads as a virtual Xbox pad without sensors) and the game's own motion-aiming option.
- Launcher (plan item 20): one exe for Windows and Linux, SDL3 and Dear ImGui (SavageWindRecomp
`runtime/ui/launcher.mm`, `tools/fetch_zig.sh`).
- The player picks the base game (XCI or NSP), the 1.2.1 update and keys. The launcher checks the version,
+21 -2
View File
@@ -20,13 +20,32 @@ Input comes from the first SDL3 gamepad, the keyboard and the mouse, merged into
| Plus | Start | Tab | menu, inventory |
| Minus | Back | M | map |
Esc is free. Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys; the menu tabs (L, R) are Q and R. The mouse wheel is unbound. The bindings are one table, `input_bindings` in `runtime/input_map.c` (platform-neutral key ids; `runtime/input_win.c` translates Win32 virtual keys), so a remapping page can edit it later.
Esc is unbound in the game; it releases the mouse cursor (see Mouse camera). Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys; the menu tabs (L, R) are Q and R. The mouse wheel is unbound. The bindings are two tables in `runtime/input_map.c`, `input_bindings` (platform-neutral key ids) and `input_pad_bindings` (gamepad inputs by position); `runtime/input_win.c` translates Win32 virtual keys and SDL buttons, so a remapping page can edit both later.
## Remapping
`totk.ini` rebinds the buttons, one line per Switch button: `bind_<button> = <entry>, <entry>, ...`. The button is `A B X Y L R ZL ZR PLUS MINUS LEFT UP RIGHT DOWN LCLICK RCLICK`. An entry is `pad:<name>` or `key:<name>`:
- pad names: `south east west north l r zl zr start back lstick rstick dpad_left dpad_up dpad_right dpad_down` (`zl`, `zr` are the triggers)
- key names: `a` to `z`, `0` to `9`, `space tab lshift lctrl up down left right mouse_l mouse_r mouse_m`
- `none` leaves the button unbound.
A line replaces every default of that button, pad and keyboard, so list the keys you still want. Buttons without a line keep the table above. Names are case-insensitive. An invalid entry logs one `input: bind ...` line and is skipped; a line without a valid entry changes nothing. The sticks and the mouse camera are not remappable. The `TOTK_BIND_<button>` environment variables work like any other setting. Bindings are read once at start.
A pad that reports its face buttons swapped (A/B and X/Y, as some GuliKit models do) is fixed with:
```
bind_A = pad:south, key:E
bind_B = pad:east, key:lshift
bind_X = pad:west, key:space
bind_Y = pad:north, key:mouse_l
```
## Mouse camera
Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * TOTK_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule.
`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. To resize or move the window by hand, alt-tab away first or use F11. `TOTK_INPUT=none` disables the capture too.
`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.
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`.
+120
View File
@@ -0,0 +1,120 @@
# Installer
A public release that turns the player's own game files into a playable TotK folder. The release carries no game
data and no code derived from the game: the installer builds the game on the player's machine. The installed folder
holds only the game; the installer and its tools stay outside it.
## What the player does
1. Downloads `TotK-Setup-<version>-windows.zip` from GitHub Releases, unpacks it anywhere and starts `TotK-Setup.exe`.
2. Picks:
- the base game (XCI or NSP) and the 1.2.1 update (NSP);
- their `prod.keys`;
- 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.
The install folder (`main.c` `portable_paths()` reads this layout; it stays portable):
```
<install folder>/
totk.exe, SDL3.dll, libopus-0.dll, totk_xlate.exe
totk.ini language; the other keys as commented defaults (totk.ini.example)
exefs/, romfs/ extracted from the player's files
profile/ saves
shader and pipeline caches prepared by the setup
totk-install.json what built this folder: release version, input hashes, finished steps
```
## 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.
## Setup steps
1. **Check** before writing anything: the files open with the keys, the update is 1.2.1 (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`) into a temp folder next to the installer.
4. **Compile** the generated C, `modules.c` and `romfs.c` (they bake per-install layout and RomFS hashes) and link
them with the prebuilt runtime library into `totk.exe` (`build_game.py --runtime-lib`, proven on task/llvmmingw:
3 min on 24 cores).
5. **Prepare** the RomFS patches and the shaders (`tools/make_romfs_patches.py`, `tools/prepare_shaders.py`,
`totk.exe --prepare-shaders`) in the install folder.
6. **Clean up** the temp folder (generated C and objects, about 1.3 GB).
A cancelled or failed setup resumes from the last finished step (`totk-install.json`).
## 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]
```
- On a folder with `totk-install.json` only `--dest` is needed; the game files only when the extraction must run again.
- 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 `<temp>/logs/`.
- Exit codes: 0 done, 1 failed, 2 bad command line, 3 cancelled. The window cancels with Ctrl+Break to the
installer's process group.
- Each step records a key over its inputs (runner.py). A step with the recorded key and intact output is skipped, so
resume and update are one rule; output kept from another release is re-hashed first.
- Peak space on the install disk is about 36 GiB, not 22: the extraction copies both Program NCAs (16.3 GiB) next to
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/<en|ru>.json`. A folder with `totk-install.json` needs only the
folder. `--selftest [--shots DIR]` builds every screen in both languages 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
`config.local.toml`.
## Release layout
The unpacked release; `tools/make_release.py` assembles it, CI zips 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
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 include
bin/ totk_xlate.exe, hactool.exe, SDL3.dll, libopus-0.dll, ninja.exe
data/ shader_list.tsv (MacNativeGames, MIT)
THIRD_PARTY_NOTICES.md
totk.ini.example, VERSION the default ini; the release string (git describe of the build)
```
- SDL3 comes from the upstream release, not MSYS2: MSYS2's DLL imports the LGPL libiconv.
- Every bundled third-party piece gets a row in `THIRD_PARTY_NOTICES.md`.
- The release contains nothing game-derived; `tools/check_content.py` runs over it in CI.
## Constraints
- The installer steps contain no Win32 calls: Linux follows with its own compiler and the same steps.
- The keys file path is passed to the tools; key values never appear in the UI, logs or files.
- The window is a front end over the CLI (`python -m installer --base ... --update ... --keys ... --lang ... --dest
...`), which prints one JSON progress event per line. Tests and CI use the CLI.
- Paths may hold any Unicode characters (a Cyrillic user name puts the temp and install folders under it). Every exe
the setup runs or builds reads its arguments and paths as UTF-8 through an `activeCodePage` manifest: hactool
(`toolchain/hactool/unicode-patches.diff`), totk.exe (`runtime/totk.rc`), ninja (upstream). The Python steps write
their files and logs as UTF-8, and `build_game.py` quotes paths with spaces on its Ninja command lines.
- A game file whose data fails its hash checks or ends early stops the setup with `check.input_damaged`, which names
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.
## Later
- Re-warm the driver cache on start after a move, GPU or driver change (WIP on task/rewarm: stamp in
`shader-prepared.bin`).
- NSZ and XCZ input. Linux release.
+1
View File
@@ -52,6 +52,7 @@ A one-step ordering difference between two engine threads can show as one frame
| S4 | rare hang or lost wake | `SignalToAddress` type 2, count <= 0 with waiters: Ryujinx, yuzu and nxnative set `value - 2` | done: `kernel.c:1017` writes `value - 1` and the count > 0 branch writes `nw <= count ? value - 1 : value`, both as mesosphere master `kern_k_address_arbiter.cpp`; 1.2.1 calls type 2 only with count 1, and `value - 2` did not change the phantom rocks (2026-10-23, `build/archive/v121-lock/NOTES.md`) | none |
| S5 | the main thread holds a core a worker needs | yield types 0 / -1 / -2 differ on Horizon; TotK's render-graph wait is a poll plus yield | unknown: all three hand the core only to an equal or better waiter (`kernel.c:835-846`) | trace which yield type 1.2.1's frame-end poll uses |
| S6 | lost or extra wakes | MacNativeGames `dab728a`: `SignalToAddress` wake-all, `SystemEvent` rebinding, targeted wakes | unknown | read the commit, compare with `kernel.c` |
| S7 | stutter: a better thread waits milliseconds for a core whose owner runs game code | MacNativeGames 8bf0893 (PR #488, on by default e4b4601): the waiter signals the owner, which gives the core up when interrupted in top-level game code; priority inheritance through guest mutexes; the SDK's preemption disable (TLR+0x100 / +0x102, SVC 0x36); without the last two their game hung at "Continue". 1.1.2: 58% of long core waits were this class; Mac worst-1% frame 18.4 to 10.6 ms | ported on `task/preempt` (`TOTK_ASYNC_PREEMPT`, SuspendThread and a register-saving stub instead of a signal; `build/v121-preempt/NOTES.md`). `TOTK_CORE_DIAG=1` measured this class at 0.47 / 0.83 / 1.06 ms of thread-time per frame (field / fight / monsters2) with our indirect-call checkpoints, waits to 16+ ms; with the port 0.29 / 0.34 / 0.84 and the waits of 1 ms or more 53 / 41 / 27 to 4 / 10 / 18. 1.2.1's SDK has the disable protocol (sdk f_70051242a8, f_7005124360). A first stub clobbered the flags before saving them (`sub` before `pushfq`) and crashed at boot | see the backlog item for the A/B timings |
## Open: graphics
+9
View File
@@ -95,6 +95,15 @@ Applied the portable part of MacNativeGames `build_app.py:build_hactool()` (diff
2. BKTR: allow 0x200-aligned padding after the subsection table (`nca.c`).
3. BKTR: accept the subsection total-size layout of newer cumulative updates (`nca.c`).
Ours (`toolchain\hactool\unicode-patches.diff`): `hactool.manifest` with `<activeCodePage>UTF-8</activeCodePage>`,
embedded through `hactool.rc` (Makefile rule `hactool.res.o`, `windres`). hactool converts argv, `HOME` and
`USERPROFILE` from UTF-8 to UTF-16 (`filepath.c`); without the manifest they arrive in the ANSI code page, and a
non-ASCII user profile or path stops it with `Failed to convert ... to UTF-16!`. Needs Windows 10 1903 or later.
Rebuild: in an MSYS2 UCRT64 shell (`MSYSTEM=UCRT64 C:\msys64\usr\bin\bash.exe -lc ...`) in `toolchain\src\hactool`,
`make clean && make -j8`, then copy `hactool.exe` to `toolchain\hactool\`. gcc writes temporary files to `TMP`; where
it cannot (sandboxed shells), point `TMP` and `TEMP` at a writable folder.
Not applied: the `MNG_BASE_RANGE_OFFSET/SIZE` base-file hook. It uses `funopen` (BSD/macOS only); on Windows the
base NCA must be extracted to a file first.
+10
View File
@@ -0,0 +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]
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),
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.
"""
+5
View File
@@ -0,0 +1,5 @@
import sys
from installer.cli import main
sys.exit(main())
+78
View File
@@ -0,0 +1,78 @@
"""Command line: python -m installer --base FILE --update FILE --keys FILE --lang CODE --dest DIR [--root DIR]
[--temp DIR] [--verify]
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).
"""
import argparse
import hashlib
import signal
import sys
import time
from pathlib import Path
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
EXIT = {'ok': 0, 'failed': 1, 'cancelled': 3}
class Parser(argparse.ArgumentParser):
def error(self, message):
Reporter().error('cli.usage', {'detail': message})
sys.exit(2)
def temp_folder(base, dest):
"""One temp folder per install folder, so two installs never share generated files."""
tag = hashlib.sha256(str(dest).lower().encode()).hexdigest()[:8]
return base / f'{dest.name or "root"}-{tag}'
def cancel(signum, frame):
raise KeyboardInterrupt
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='1.2.1 update NSP')
ap.add_argument('--keys', type=Path, help='prod.keys')
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, build/install-temp in a '
'repository)')
ap.add_argument('--verify', action='store_true', help='hash every kept file, also those this release made')
args = ap.parse_args(argv)
# The window cancels with Ctrl+Break to the installer's process group (CREATE_NEW_PROCESS_GROUP), which also reaches
# the tools it runs; Python's default for it ends the process without recording anything.
for name in ('SIGBREAK', 'SIGTERM'):
if hasattr(signal, name):
signal.signal(getattr(signal, name), cancel)
layout = layouts.find(args.root)
dest = args.dest.resolve()
temp = temp_folder((args.temp or layout.temp).resolve(), dest)
log = temp / 'logs' / time.strftime('install-%Y%m%d-%H%M%S.log')
rep = Reporter(log_path=log)
t0 = time.monotonic()
try:
state = statefile.load(dest)
except statefile.StateError as e:
rep.error(e.id, {'path': str(e.path)})
rep.emit('end', result='failed', seconds=0)
return 1
ctx = Context(layout, dest, temp, rep, args, state=state, verify=args.verify)
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])
result = run_steps(steps, ctx)
rep.emit('end', result=result, seconds=round(time.monotonic() - t0, 1))
rep.close()
return EXIT[result]
+119
View File
@@ -0,0 +1,119 @@
"""Progress events: one JSON object per line on stdout for the window and tests, the same in English to a log file.
Every event has "type" and "t" (seconds since the start). Types:
start release, mode (install, resume, update), dest, log, steps (the step ids in order)
step step, state: started | done (seconds) | skipped (reason: current, not_needed)
progress step, phase, and done/total/unit (bytes, chunks, objects, shaders) when the amount is known
space dest, need, free (bytes on the install disk; with the temp folder on the same disk need includes it),
temp, temp_need, temp_free, same_disk
warning id, params
error id, params, step (absent outside a step)
end result: ok | failed | cancelled, seconds
Text is never the payload: ids name a step ("step.<id>"), a phase ("phase.<step>.<phase>") or a message ("<id>"),
and installer/text/en.json holds their English text with {param} placeholders. The log renders events with it.
"""
import json
import sys
import threading
import time
from pathlib import Path
CATALOG = Path(__file__).resolve().parent / 'text' / 'en.json'
PROGRESS_EVERY = 0.5 # seconds between two progress events of one phase
def load_catalog(path=CATALOG):
return json.loads(path.read_text(encoding='utf-8'))
def render(catalog, id, params=None):
"""The English text of id, or the id itself when the catalog lacks it."""
text = catalog.get(id, id)
try:
return text.format(**(params or {}))
except (KeyError, IndexError, ValueError):
return f'{text} {params}'
class Reporter:
def __init__(self, out=None, log_path=None):
self.out = out or sys.stdout
self.t0 = time.monotonic()
self.catalog = load_catalog()
self.lock = threading.Lock()
self.log_file = None
self.last_progress = {}
if log_path:
Path(log_path).parent.mkdir(parents=True, exist_ok=True)
self.log_file = open(log_path, 'a', encoding='utf-8', buffering=1)
self.log_path = log_path
def elapsed(self):
return time.monotonic() - self.t0
def log(self, text):
"""A line for the log file only."""
if self.log_file:
with self.lock:
self.log_file.write(f'[{self.elapsed():8.1f}] {text}\n')
def emit(self, type, **fields):
event = {'type': type, 't': round(self.elapsed(), 2), **fields}
with self.lock:
self.out.write(json.dumps(event, ensure_ascii=False) + '\n')
self.out.flush()
self.log(self.describe(event))
return event
def describe(self, e):
c = self.catalog
kind = e['type']
if kind == 'step':
name = render(c, 'step.' + e['step'])
extra = f' ({e["seconds"]:.1f} s)' if 'seconds' in e else f' ({e["reason"]})' if 'reason' in e else ''
return f'{name}: {e["state"]}{extra}'
if kind == 'progress':
text = render(c, f'phase.{e["step"]}.{e["phase"]}')
if e.get('total'):
text += f' {e["done"]}/{e["total"]} {e.get("unit", "")}'.rstrip()
return text
if kind in ('warning', 'error'):
return f'{kind}: {render(c, e["id"], e.get("params"))} [{e["id"]}]'
return json.dumps(e, ensure_ascii=False)
def step(self, step, state, **fields):
return self.emit('step', step=step, state=state, **fields)
def progress(self, step, phase, done=None, total=None, unit=None, force=False):
"""A progress event, at most one per PROGRESS_EVERY per phase unless it is the first or the last."""
now = time.monotonic()
key = (step, phase)
last = self.last_progress.get(key)
final = total is not None and done is not None and done >= total
if last is not None and not final and not force and now - last < PROGRESS_EVERY:
return None
self.last_progress[key] = now
fields = {'step': step, 'phase': phase}
if done is not None:
fields['done'] = done
if total is not None:
fields['total'] = total
if unit:
fields['unit'] = unit
return self.emit('progress', **fields)
def warning(self, id, **params):
return self.emit('warning', id=id, params=params)
def error(self, id, params, step=None):
fields = {'id': id, 'params': params}
if step:
fields['step'] = step
return self.emit('error', **fields)
def close(self):
if self.log_file:
self.log_file.close()
self.log_file = None
+587
View File
@@ -0,0 +1,587 @@
"""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/<en|ru>.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)
"""
import argparse
import json
import os
import sys
import tkinter as tk
from pathlib import Path
from tkinter import filedialog, messagebox, ttk
try:
from PIL import ImageGrab # only the screenshots of --selftest --shots need it; the release has no PIL
except ImportError:
ImageGrab = None
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)
POLL_MS = 100
GLYPH = {PENDING: '\u25cb', RUNNING: '\u25b6', DONE: '\u2714', SKIPPED: '\u2013', FAILED: '\u2716'}
COLOR = {PENDING: '#808080', RUNNING: '#1a5fb4', DONE: '#26a269', SKIPPED: '#808080', FAILED: '#c01c28'}
PAD = 14
def fix_tcl_paths():
"""The embeddable Python keeps Tcl/Tk next to python.exe (python/tcl); point Tcl at it when it is not found."""
tcl = Path(sys.executable).resolve().parent / 'tcl'
if tcl.is_dir() and 'TCL_LIBRARY' not in os.environ:
for lib, var in (('tcl9.0', 'TCL_LIBRARY'), ('tk9.0', 'TK_LIBRARY')):
if (tcl / lib).is_dir():
os.environ[var] = str(tcl / lib)
class App:
def __init__(self, root, ui_lang, layout, run_args=None):
self.root, self.layout = root, layout
self.cat = Catalog(ui_lang)
self.model = Model()
self.process = None
self.closing = False
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['lang'] = tk.StringVar()
self.lang_code = default_game_language(ui_lang)
self.screen = None
root.title(self.cat('ui.title'))
root.minsize(680, 560)
root.protocol('WM_DELETE_WINDOW', self.on_close)
style = ttk.Style(root)
style.configure('Title.TLabel', font=('Segoe UI', 15, 'bold'))
style.configure('Notice.TLabel', background='#fff3cd', foreground='#664d03', padding=8)
style.configure('Err.TLabel', foreground='#c01c28')
top = ttk.Frame(root, padding=(PAD, PAD, PAD, 0))
top.pack(fill='x')
self.title = self.label(top, 'ui.title', style='Title.TLabel')
self.title.pack(side='left')
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.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')
# ---- text -----------------------------------------------------------------------------------------------------
def bind_text(self, widget, option, id, **params):
self.bound.append((widget, option, id, params))
widget.configure(**{option: self.cat(id, **params)})
def label(self, parent, id, **kw):
w = ttk.Label(parent, **kw)
self.bind_text(w, 'text', id)
return w
def button(self, parent, id, command, **kw):
w = ttk.Button(parent, command=command, **kw)
self.bind_text(w, 'text', id)
return w
def retext(self):
self.root.title(self.cat('ui.title'))
for widget, option, id, params in self.bound:
widget.configure(**{option: self.cat(id, **params)})
self.fill_languages()
self.refresh_form()
self.refresh_run()
self.refresh_finish()
self.render_log()
def switch_language(self):
self.cat.set_language(self.ui_var.get())
code = self.selected_lang()
self.retext()
self.vars['lang'].set(self.lang_label(code))
def lang_label(self, code):
return self.cat('lang.' + code)
def fill_languages(self):
code = self.selected_lang()
self.lang_box.configure(values=[self.lang_label(c) for c in ini.LANGUAGES])
self.vars['lang'].set(self.lang_label(code))
def selected_lang(self):
"""The game language code behind the combobox text (kept when it is not one of the labels)."""
shown = self.vars['lang'].get()
for code in ini.LANGUAGES:
for lang in UI_LANGUAGES:
if shown == Catalog(lang)('lang.' + code):
return code
return self.lang_code
# ---- screens --------------------------------------------------------------------------------------------------
def show(self, name):
for f in self.frames.values():
f.pack_forget()
self.frames[name].pack(fill='both', expand=True)
self.screen = name
def notice(self, parent):
w = ttk.Label(parent, style='Notice.TLabel', wraplength=620, justify='left')
self.bind_text(w, 'text', 'ui.notice')
return w
def build_form(self):
f = ttk.Frame(self.body)
self.form_intro = self.label(f, 'ui.intro', wraplength=620, justify='left')
self.form_intro.pack(anchor='w', pady=(0, 8))
self.notice(f).pack(fill='x', pady=(0, 10))
grid = ttk.Frame(f)
grid.pack(fill='x')
grid.columnconfigure(1, weight=1)
rows = (('base', 'ui.base', 'ui.pick_game'), ('update', 'ui.update', 'ui.pick_update'),
('keys', 'ui.keys', 'ui.pick_keys'))
self.form_rows = {}
for i, (name, label_id, pick_id) in enumerate(rows):
lab = self.label(grid, label_id)
lab.grid(row=i, column=0, sticky='w', pady=4, padx=(0, 10))
ttk.Entry(grid, textvariable=self.vars[name]).grid(row=i, column=1, sticky='ew', pady=4)
b = self.button(grid, 'ui.browse', lambda n=name, p=pick_id: self.pick_file(n, p))
b.grid(row=i, column=2, padx=(8, 0))
self.form_rows[name] = lab
i = len(rows)
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)
self.lang_touched = False
self.lang_box.bind('<<ComboboxSelected>>', lambda _e: setattr(self, 'lang_touched', True))
self.fill_languages()
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.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')
self.form_error.pack(anchor='w', pady=(8, 0))
self.label(f, 'ui.keys_private', wraplength=620, justify='left').pack(anchor='w', pady=(8, 0))
bar = ttk.Frame(f)
bar.pack(side='bottom', fill='x', pady=(12, 0))
self.start_button = self.button(bar, 'ui.start', self.start)
self.start_button.pack(side='right')
return f
def build_run(self):
f = ttk.Frame(self.body)
self.run_header = ttk.Label(f, font=('Segoe UI', 11, 'bold'))
self.run_header.pack(anchor='w')
self.step_rows = {}
self.steps_box = ttk.Frame(f)
self.steps_box.pack(fill='x', pady=(8, 8))
self.phase_label = ttk.Label(f, wraplength=620, justify='left')
self.phase_label.pack(anchor='w')
self.bar = ttk.Progressbar(f, mode='determinate', maximum=1000)
self.bar.pack(fill='x', pady=(4, 6))
self.space_label = ttk.Label(f, wraplength=620, justify='left')
self.space_label.pack(anchor='w')
self.warn_label = ttk.Label(f, wraplength=620, justify='left', foreground='#9a6700')
self.warn_label.pack(anchor='w')
bar = ttk.Frame(f)
bar.pack(side='bottom', fill='x', pady=(8, 0))
self.cancel_button = self.button(bar, 'ui.cancel', self.cancel)
self.cancel_button.pack(side='right')
self.log_toggle = self.button(bar, 'ui.show_log', self.toggle_log)
self.log_toggle.pack(side='left')
self.log_frame = ttk.Frame(f)
self.log = tk.Text(self.log_frame, height=10, wrap='word', state='disabled', font=('Consolas', 9))
scroll = ttk.Scrollbar(self.log_frame, command=self.log.yview)
self.log.configure(yscrollcommand=scroll.set)
scroll.pack(side='right', fill='y')
self.log.pack(side='left', fill='both', expand=True)
self.log_open = False
return f
def build_finish(self):
f = ttk.Frame(self.body)
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')
self.finish_text.pack(anchor='w', pady=(8, 8))
self.finish_steps = ttk.Frame(f)
self.finish_steps.pack(fill='x', pady=(0, 8))
self.finish_notice = self.notice(f)
self.finish_notice.pack(fill='x', pady=(8, 0))
bar = ttk.Frame(f)
bar.pack(side='bottom', fill='x', pady=(12, 0))
self.button(bar, 'ui.close', self.on_close).pack(side='right')
self.open_button = self.button(bar, 'ui.open_folder', self.open_folder)
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')
return f
# ---- form -----------------------------------------------------------------------------------------------------
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())
def refresh_form(self):
form = self.form()
state = form.installed_state()
if state is not None:
self.update_hint.configure(text=self.cat('ui.update_mode'))
if state.get('lang') in ini.LANGUAGES and not self.lang_touched:
self.vars['lang'].set(self.lang_label(state['lang']))
else:
self.update_hint.configure(text='')
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'), '*.*')]
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_dest(self):
path = filedialog.askdirectory(title=self.cat('ui.pick_dest'), mustexist=False)
if path:
self.vars['dest'].set(os.path.normpath(path))
# ---- run ------------------------------------------------------------------------------------------------------
def start(self):
form = self.form()
problems = form.problems()
if problems:
self.form_error.configure(text=' '.join(self.cat(p) for p in problems))
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))
self.begin(command, self.layout.root)
def begin(self, command, cwd):
self.model = Model()
self.model.dest = self.vars['dest'].get().strip()
self.clear_log()
self.build_step_rows(self.model.steps)
self.cancel_button.configure(state='normal')
self.bound = [b for b in self.bound if b[0] is not self.cancel_button]
self.bind_text(self.cancel_button, 'text', 'ui.cancel')
self.show('run')
self.refresh_run()
self.process = Process(command, cwd=cwd, helper_python=self.layout.python)
self.root.after(POLL_MS, self.poll)
def build_step_rows(self, steps):
for w in self.steps_box.winfo_children():
w.destroy()
self.step_rows = {}
for s in steps:
row = ttk.Frame(self.steps_box)
row.pack(fill='x', pady=1)
glyph = ttk.Label(row, width=3)
glyph.pack(side='left')
name = ttk.Label(row)
name.pack(side='left')
self.step_rows[s] = (glyph, name)
def poll(self):
if self.process is None:
return
try:
while True:
line = self.process.lines.get_nowait()
if line is not None:
self.feed(line)
except Exception:
pass
code = self.process.poll()
self.refresh_run()
if code is None:
self.root.after(POLL_MS, self.poll)
return
self.model.finish(code)
self.process = None
self.refresh_run()
self.refresh_finish()
self.show('finish')
if self.closing:
self.root.destroy()
def feed(self, line):
"""One stdout line of the CLI: an event updates the model and the log, anything else goes to the log as is."""
event = parse_line(line)
if event is None:
if line.strip():
self.log_entries.append(line.rstrip())
self.append_log(line.rstrip())
return
before = list(self.model.steps)
self.model.apply(event)
if self.model.steps != before:
self.build_step_rows(self.model.steps)
self.log_entries.append(event)
self.append_log(describe_event(self.cat, event))
def refresh_run(self):
m = self.model
for s, (glyph, name) in self.step_rows.items():
state = m.states.get(s, PENDING)
glyph.configure(text=GLYPH[state], foreground=COLOR[state])
name.configure(text=self.cat('step.' + s) + (f' ({self.cat("ui.state_" + state)})'
if state in (SKIPPED, FAILED) else ''),
font=('Segoe UI', 10, 'bold') if state == RUNNING else ('Segoe UI', 10))
mode = m.mode or 'install'
self.run_header.configure(text=self.cat('ui.running_' + mode if mode in ('install', 'resume', 'update')
else 'ui.running_install'))
if m.current and m.phase:
text = self.cat(f'phase.{m.current}.{m.phase}')
if m.total:
text += ' ' + amount(self.cat, m.done, m.total, m.unit)
elif m.current:
text = self.cat('step.' + m.current)
else:
text = ''
self.phase_label.configure(text=text)
frac = m.fraction()
if frac is None:
if str(self.bar.cget('mode')) != 'indeterminate':
self.bar.configure(mode='indeterminate')
self.bar.start(15)
else:
if str(self.bar.cget('mode')) == 'indeterminate':
self.bar.stop()
self.bar.configure(mode='determinate')
self.bar.configure(value=frac * 1000)
self.space_label.configure(text=m.need_text(self.cat))
self.warn_label.configure(text='\n'.join(self.cat(i, **p) for i, p in m.warnings))
def toggle_log(self):
self.log_open = not self.log_open
if self.log_open:
self.log_frame.pack(fill='both', expand=True, pady=(8, 0))
else:
self.log_frame.pack_forget()
self.bound = [b for b in self.bound if b[0] is not self.log_toggle]
self.bind_text(self.log_toggle, 'text', 'ui.hide_log' if self.log_open else 'ui.show_log')
def clear_log(self):
self.log_entries = []
self.render_log()
def render_log(self):
"""The whole log from the kept events in the current language (raw CLI lines stay as they are)."""
self.log.configure(state='normal')
self.log.delete('1.0', 'end')
for entry in self.log_entries:
text = entry if isinstance(entry, str) else describe_event(self.cat, entry)
if text:
self.log.insert('end', text + '\n')
self.log.see('end')
self.log.configure(state='disabled')
def append_log(self, text):
if text:
self.log.configure(state='normal')
self.log.insert('end', text + '\n')
self.log.see('end')
self.log.configure(state='disabled')
def cancel(self):
if self.process:
self.cancel_button.configure(state='disabled')
self.bound = [b for b in self.bound if b[0] is not self.cancel_button]
self.bind_text(self.cancel_button, 'text', 'ui.cancelling')
self.process.cancel()
def on_close(self):
if self.process:
if not messagebox.askyesno(self.cat('ui.title'), self.cat('ui.confirm_cancel')):
return
self.closing = True
self.cancel()
return
self.root.destroy()
# ---- finish ---------------------------------------------------------------------------------------------------
def refresh_finish(self):
m = self.model
result = m.result or 'ok'
self.finish_title.configure(text=self.cat('ui.result_' + result))
if result == 'ok':
text = self.cat('ui.done_text', dest=m.dest)
elif result == 'cancelled':
text = self.cat('ui.cancelled_text')
else:
err = m.error or ('ui.process_died', {'code': m.exit_code}, None)
text = self.cat(err[0], **err[1])
if m.log_path:
text += '\n\n' + self.cat('ui.log_file', path=m.log_path)
self.finish_text.configure(text=text)
for w in self.finish_steps.winfo_children():
w.destroy()
for s in m.steps:
state = m.states.get(s, PENDING)
ttk.Label(self.finish_steps, text=f'{GLYPH[state]} {self.cat("step." + s)}',
foreground=COLOR[state]).pack(anchor='w')
if result == 'ok':
self.open_button.state(['!disabled'])
else:
self.open_button.state(['disabled' if not Path(m.dest or '.').is_dir() else '!disabled'])
def open_folder(self):
dest = self.model.dest
if dest and Path(dest).is_dir():
os.startfile(dest)
# ---- 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']}
SAMPLE_RUN = [
SAMPLE_START,
{'type': 'plan', 'run': ['check', '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',
'temp_need': 0, 'temp_free': 0, 'same_disk': True},
{'type': 'warning', 'id': 'warn.lang_needs_fonts', 'params': {'lang': 'ko'}},
{'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 12.0},
{'type': 'step', 'step': 'extract', 'state': 'started'},
{'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 6_000_000_000, 'total': 16_000_000_000,
'unit': 'bytes'},
]
SAMPLE_OK = [SAMPLE_START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'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': 'lift', 'state': 'skipped', 'reason': 'current'},
{'type': 'step', 'step': 'compile', 'state': 'skipped', 'reason': 'current'},
{'type': 'step', 'step': 'prepare', 'state': 'skipped', 'reason': 'current'},
{'type': 'step', 'step': 'cleanup', 'state': 'done', 'seconds': 0.1},
{'type': 'end', 'result': 'ok', 'seconds': 3}]
def grab(root, path):
if ImageGrab is None:
raise SystemExit('--shots needs Pillow')
root.attributes('-topmost', True)
root.update()
x, y, w, h = root.winfo_rootx(), root.winfo_rooty(), root.winfo_width(), root.winfo_height()
ImageGrab.grab(bbox=(x, y, x + w, y + h), all_screens=True).save(path)
def selftest(layout, shots=None):
"""Every screen in both languages; the window closes itself. Returns 0 when nothing raised."""
fix_tcl_paths()
root = tk.Tk()
root.geometry('720x640+60+40')
app = App(root, 'en', layout)
steps, mixed = [], []
def shot(name):
root.update()
if shots:
Path(shots).mkdir(parents=True, exist_ok=True)
grab(root, Path(shots) / f'{name}.png')
def run_all():
for lang in UI_LANGUAGES:
app.ui_var.set(lang)
app.switch_language()
tag = lang
app.vars['dest'].set('Z:/selftest/TotK')
app.vars['base'].set('Z:/selftest/totk.xci')
app.vars['update'].set('Z:/selftest/totk-1.2.1.nsp')
app.vars['keys'].set('Z:/selftest/prod.keys')
app.show('form')
app.start() # missing files: shows the problems
steps.append(app.form_error.cget('text'))
shot(f'form-{tag}')
app.model = Model()
app.clear_log()
app.build_step_rows(app.model.steps)
app.show('run')
for e in SAMPLE_RUN:
app.feed(json.dumps(e))
app.log_open = False
app.toggle_log()
app.refresh_run()
shot(f'run-{tag}')
if lang == 'en': # the log follows the window language: no English line may remain after the switch
app.ui_var.set('ru')
app.switch_language()
text = app.log.get('1.0', 'end')
mixed.append('Checking' in text or 'Extracting' not in text and 'Распаковка' not in text)
app.ui_var.set('en')
app.switch_language()
app.model = Model()
for e in SAMPLE_OK:
app.model.apply(e)
app.model.dest = 'Z:/selftest/TotK'
app.model.finish(0)
app.refresh_finish()
app.show('finish')
shot(f'finish-ok-{tag}')
app.model = Model()
for e in SAMPLE_RUN[:3] + [{'type': 'error', 'id': 'check.disk_space', 'step': 'check',
'params': {'path': 'Z:/selftest/TotK', 'need_gb': 36.4, 'free_gb': 20.1}},
{'type': 'end', 'result': 'failed', 'seconds': 3}]:
app.model.apply(e)
app.model.finish(1)
app.refresh_finish()
shot(f'finish-failed-{tag}')
app.model = Model()
app.model.finish(3)
app.refresh_finish()
shot(f'finish-cancelled-{tag}')
app.toggle_log()
root.destroy()
root.after(200, run_all)
root.after(60000, root.destroy) # never leaves a window open
root.mainloop()
return 0 if len(steps) == len(UI_LANGUAGES) and all(steps) and mixed == [False] else 1
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--selftest', action='store_true')
ap.add_argument('--shots', type=Path)
ap.add_argument('--lang', choices=UI_LANGUAGES)
ap.add_argument('--root', 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()
root.geometry('720x640')
App(root, args.lang or windows_ui_language(), layout)
root.mainloop()
return 0
if __name__ == '__main__':
sys.exit(main())
+361
View File
@@ -0,0 +1,361 @@
"""The installer window without Tk: text catalogs, the form, the event model and the CLI subprocess.
gui.py draws; everything it decides lives here so tests/installer_gui_test.py runs it headless.
Catalog text of an id in the window language (installer/text/<lang>.json), English when that lacks it
Form what the player entered; command() and problems() build and check the CLI call
Model folds the CLI's JSON events (events.py) into step states, progress, space, errors and the result
Process runs `python -m installer ...`, cancels it with Ctrl+Break and feeds lines to a queue
"""
import ctypes
import json
import os
import queue
import subprocess
import sys
import threading
import time
from dataclasses import dataclass
from pathlib import Path
from . import ini
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')
# 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
def load_texts(lang):
path = TEXT / f'{lang}.json'
return json.loads(path.read_text(encoding='utf-8')) if path.is_file() else {}
class Catalog:
"""Window text: ids from text/<lang>.json, then text/en.json, then the id itself."""
def __init__(self, lang='en'):
self.english = load_texts('en')
self.set_language(lang)
def set_language(self, lang):
self.lang = lang if lang in UI_LANGUAGES else 'en'
self.texts = load_texts(self.lang)
def has(self, id):
return id in self.texts or id in self.english
def __call__(self, id, **params):
text = self.texts.get(id) or self.english.get(id)
if text is None:
return id
return render({id: text}, id, {k: self.param(k, v) for k, v in params.items()})
def param(self, name, value):
"""A message parameter as the player should read it: language codes, step ids and input names become the
catalog's names (the ones the pickers and the step list show); the rest stays as it is."""
prefix = PARAM_NAMES.get(name)
if prefix:
if isinstance(value, (list, tuple)):
return ', '.join(self.param(name, v) for v in value)
ident = prefix + str(value)
if self.has(ident):
return self(ident)
if name == 'known':
return ', '.join(self(prefix + c) if self.has(prefix + c) else c for c in str(value).split(', '))
return flat(value)
def flat(value):
return ', '.join(str(v) for v in value) if isinstance(value, (list, tuple)) else value
def windows_ui_language():
"""'ru' when the Windows display language is Russian, else 'en'."""
try:
langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
except (AttributeError, OSError):
return 'en'
return 'ru' if langid & 0x3ff == LANG_RU else 'en'
def default_game_language(ui):
return 'ru' if ui == 'ru' else 'en-US'
def gib(n):
"""Bytes as decimal gigabytes with one digit, the unit the check.disk_space message uses."""
return f'{n / 1e9:.1f}'
def amount(cat, done, total, unit):
"""'6.0 / 16.0 GB', '120 / 4000 files'..., the progress of a phase whose size is known."""
if unit == 'bytes':
return cat('ui.amount_bytes', done=gib(done or 0), total=gib(total))
id = f'ui.amount_{unit}'
return cat(id if cat.has(id) else 'ui.amount_other', done=done or 0, total=total, unit=unit or '')
# ---- the form ----------------------------------------------------------------------------------------------------
@dataclass
class Form:
base: str = ''
update: str = ''
keys: str = ''
lang: str = ''
dest: str = ''
def installed_state(self):
"""The recorded state of the install folder (totk-install.json), or None. Never raises."""
if not self.dest.strip():
return None
try:
return statefile.load(Path(self.dest.strip()))
except statefile.StateError:
return None
def update_mode(self):
return self.installed_state() is not None
def problems(self):
"""Ids of the entries that are missing or wrong, in screen order. A folder with totk-install.json needs only
itself; the CLI asks for the game files when it must extract again."""
found = []
update = self.update_mode()
if not self.dest.strip():
found.append('ui.need_dest')
if not update:
for name in ('base', 'update', 'keys'):
value = getattr(self, name).strip()
if not value:
found.append(f'ui.need_{name}')
elif not Path(value).is_file():
found.append(f'ui.missing_{name}')
if self.lang not in ini.LANGUAGES:
found.append('ui.need_lang')
else:
for name in ('base', 'update', 'keys'):
value = getattr(self, name).strip()
if value and not Path(value).is_file():
found.append(f'ui.missing_{name}')
return found
def command(self, python, root=None, temp=None, verify=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)):
if value.strip():
cmd += [flag, value.strip()]
if root:
cmd += ['--root', str(root)]
if temp:
cmd += ['--temp', str(temp)]
if verify:
cmd.append('--verify')
return cmd
# ---- events -> state ---------------------------------------------------------------------------------------------
PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped', 'failed'
def parse_line(line):
"""The event dict of a stdout line, or None when it is not a JSON event (a traceback line, noise)."""
line = line.strip()
if not line.startswith('{'):
return None
try:
event = json.loads(line)
except ValueError:
return None
return event if isinstance(event, dict) and isinstance(event.get('type'), str) else None
class Model:
"""What the window shows, folded from events. apply() takes one event; nothing else mutates it."""
def __init__(self):
self.steps = list(STEP_ORDER)
self.states = {s: PENDING for s in self.steps}
self.mode = ''
self.release = ''
self.log_path = ''
self.dest = ''
self.current = None # step id running
self.phase = None
self.done = self.total = None
self.unit = None
self.space = None # the space event
self.warnings = [] # (id, params)
self.error = None # (id, params, step)
self.result = None # ok | failed | cancelled once the end event arrived
self.seconds = None
self.exit_code = None
def apply(self, e):
kind = e['type']
if kind == 'start':
self.__init__() # a new run: nothing of an earlier one stays
self.steps = list(e.get('steps') or STEP_ORDER)
self.states = {s: PENDING for s in self.steps}
self.mode, self.release = e.get('mode', ''), e.get('release', '')
self.log_path, self.dest = e.get('log', ''), e.get('dest', '')
elif kind == 'plan':
for s, reason in (e.get('skip') or {}).items():
if s in self.states and self.states[s] == PENDING:
self.states[s] = SKIPPED
elif kind == 'step':
s, state = e.get('step'), e.get('state')
if s not in self.states:
self.states[s] = PENDING
self.steps.append(s)
if state == 'started':
self.states[s], self.current = RUNNING, s
self.phase = self.done = self.total = self.unit = None
elif state == 'done':
self.states[s] = DONE
self.current = None if self.current == s else self.current
elif state == 'skipped':
self.states[s] = SKIPPED
elif kind == 'progress':
if e.get('step') in self.states and self.states[e['step']] == PENDING:
self.states[e['step']] = RUNNING
self.current = e.get('step', self.current)
self.phase = e.get('phase')
self.done, self.total, self.unit = e.get('done'), e.get('total'), e.get('unit')
elif kind == 'space':
self.space = e
elif kind == 'warning':
self.warnings.append((e.get('id', ''), e.get('params') or {}))
elif kind == 'error':
self.error = (e.get('id', ''), e.get('params') or {}, e.get('step'))
if e.get('step') in self.states:
self.states[e['step']] = FAILED
elif kind == 'end':
self.result = e.get('result')
self.seconds = e.get('seconds')
self.current = None
def fraction(self):
"""0..1 of the current phase, or None when its size is unknown."""
if self.total:
return max(0.0, min(1.0, (self.done or 0) / self.total))
return None
def finish(self, exit_code):
"""The process ended. A missing end event (the process died) counts as failed, or cancelled on exit code 3."""
self.exit_code = exit_code
if self.result is None:
self.result = 'cancelled' if exit_code == 3 else 'failed'
if self.result == 'failed' and self.error is None:
self.error = ('ui.process_died', {'code': exit_code}, self.current)
for s, state in self.states.items():
if state == RUNNING:
self.states[s] = FAILED if self.result == 'failed' else PENDING
def need_text(self, cat):
"""The space line, or '' before the check step reported."""
sp = self.space
if not sp:
return ''
key = 'ui.space_one' if sp.get('same_disk') or not sp.get('temp_need') else 'ui.space_two'
return cat(key, path=sp.get('dest', ''), need=gib(sp.get('need', 0)), free=gib(sp.get('free', 0)),
temp=sp.get('temp', ''), temp_need=gib(sp.get('temp_need', 0)),
temp_free=gib(sp.get('temp_free', 0)))
def describe_event(cat, e):
"""One log line for an event, in the window language."""
kind = e['type']
if kind == 'step':
text = f'{cat("step." + e["step"])}: {cat("ui.state_" + e["state"])}'
if 'seconds' in e:
text += f' ({cat("ui.seconds", n=format(e["seconds"], ".1f"))})'
elif e.get('reason'):
text += f' ({e["reason"]})'
return text
if kind == 'progress':
text = cat(f'phase.{e["step"]}.{e["phase"]}')
if e.get('total'):
text += ' ' + amount(cat, e.get('done'), e['total'], e.get('unit'))
return text
if kind in ('warning', 'error'):
return f'{cat("ui.log_" + kind)}: {cat(e["id"], **(e.get("params") or {}))}'
if kind == 'space':
return cat('ui.log_space', need=gib(e.get('need', 0)), free=gib(e.get('free', 0)))
if kind == 'end':
return f'{cat("ui.log_end")}: {cat("ui.result_" + str(e.get("result")))}'
return ''
# ---- the CLI subprocess ------------------------------------------------------------------------------------------
CREATE_NEW_PROCESS_GROUP = 0x200
CREATE_NO_WINDOW = 0x08000000
_BREAK = """
import ctypes, sys, time
k = ctypes.windll.kernel32
pid = int(sys.argv[1])
k.FreeConsole()
if k.AttachConsole(pid):
k.SetConsoleCtrlHandler(None, True)
k.GenerateConsoleCtrlEvent(1, pid)
time.sleep(1)
k.FreeConsole()
else:
sys.exit(1)
"""
def send_break(proc, python=None):
"""Ctrl+Break to the process group of proc (started with CREATE_NEW_PROCESS_GROUP). The window itself may have no
console (pythonw), so a short helper process attaches to the child's console and sends it."""
helper = [str(python or sys.executable), '-c', _BREAK, str(proc.pid)]
flags = CREATE_NO_WINDOW if os.name == 'nt' else 0
r = subprocess.run(helper, creationflags=flags, capture_output=True)
return r.returncode == 0
class Process:
"""`python -m installer ...` with its stdout lines going to self.lines (a queue).
poll() returns the exit code once the process ended and its output is drained, else None."""
def __init__(self, command, cwd=None, helper_python=None, creationflags=None):
self.command = command
self.helper_python = helper_python
self.lines = queue.Queue()
flags = (CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW) if creationflags is None else creationflags
env = dict(os.environ, PYTHONIOENCODING='utf-8', PYTHONUTF8='1', PYTHONUNBUFFERED='1')
self.proc = subprocess.Popen(command, cwd=cwd, env=env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, creationflags=flags)
self.cancelled_at = None
self.reader = threading.Thread(target=self.read, daemon=True)
self.reader.start()
def read(self):
for raw in iter(self.proc.stdout.readline, b''):
self.lines.put(raw.decode('utf-8', errors='replace'))
self.proc.stdout.close()
def cancel(self):
if self.cancelled_at is None and self.proc.poll() is None:
self.cancelled_at = time.monotonic()
send_break(self.proc, self.helper_python)
def poll(self):
code = self.proc.poll()
if code is not None and not self.reader.is_alive() and self.lines.empty():
return code
return None
def kill(self):
if self.proc.poll() is None:
self.proc.kill()
+65
View File
@@ -0,0 +1,65 @@
// 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.
// The Vulkan loader is opened at run time, so a machine without one gets "loader": false instead of a failed start.
#include <stdio.h>
#include <string.h>
#include <vulkan/vulkan.h>
#ifdef _WIN32
#include <windows.h>
#define open_lib() LoadLibraryA("vulkan-1.dll")
#define lib_sym(l, n) ((void *)GetProcAddress((l), (n)))
#else
#include <dlfcn.h>
#define open_lib() dlopen("libvulkan.so.1", RTLD_NOW)
#define lib_sym(l, n) dlsym((l), (n))
#endif
static void json_string(const char *s) {
putchar('"');
for (; *s; s++) {
if (*s == '"' || *s == '\\') printf("\\%c", *s);
else if ((unsigned char)*s < 0x20) printf("\\u%04x", *s);
else putchar(*s);
}
putchar('"');
}
int main(void) {
__builtin_cpu_init();
printf("{\"x86_64_v3\": %s, ", __builtin_cpu_supports("x86-64-v3") ? "true" : "false");
void *lib = open_lib();
PFN_vkGetInstanceProcAddr gipa = lib ? (PFN_vkGetInstanceProcAddr)lib_sym(lib, "vkGetInstanceProcAddr") : NULL;
if (!gipa) {
printf("\"loader\": false, \"devices\": []}\n");
return 0;
}
printf("\"loader\": true, ");
PFN_vkCreateInstance create = (PFN_vkCreateInstance)gipa(NULL, "vkCreateInstance");
VkApplicationInfo app = {.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "totk-install",
.apiVersion = VK_API_VERSION_1_3};
VkInstanceCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app};
VkInstance inst = VK_NULL_HANDLE;
VkResult r = create ? create(&ci, NULL, &inst) : VK_ERROR_INITIALIZATION_FAILED;
if (r != VK_SUCCESS) {
printf("\"instance\": %d, \"devices\": []}\n", (int)r);
return 0;
}
PFN_vkEnumeratePhysicalDevices enumerate = (PFN_vkEnumeratePhysicalDevices)gipa(inst, "vkEnumeratePhysicalDevices");
PFN_vkGetPhysicalDeviceProperties props = (PFN_vkGetPhysicalDeviceProperties)gipa(inst, "vkGetPhysicalDeviceProperties");
PFN_vkDestroyInstance destroy = (PFN_vkDestroyInstance)gipa(inst, "vkDestroyInstance");
VkPhysicalDevice devs[16];
uint32_t n = 16;
if (enumerate(inst, &n, devs) < 0) n = 0;
printf("\"instance\": 0, \"devices\": [");
for (uint32_t i = 0; i < n; i++) {
VkPhysicalDeviceProperties p;
props(devs[i], &p);
printf("%s{\"name\": ", i ? ", " : "");
json_string(p.deviceName);
printf(", \"api\": [%u, %u, %u], \"type\": %d}", VK_API_VERSION_MAJOR(p.apiVersion),
VK_API_VERSION_MINOR(p.apiVersion), VK_API_VERSION_PATCH(p.apiVersion), (int)p.deviceType);
}
printf("]}\n");
destroy(inst, NULL);
return 0;
}
+69
View File
@@ -0,0 +1,69 @@
"""totk.ini: set the game language and keep everything else (keys and syntax as runtime/cfg.c reads them).
A new file starts from totk.ini.example, whose lines are all commented defaults. The language goes into `lang`
(a Switch language code, runtime/ov_system.c): an active `lang = ...` line is rewritten in place, else the commented
`# lang = ...` line of the example is activated, else the line is added to [language] (created at the end if
missing). Other lines, comments and line order stay as they are.
"""
import os
import re
from pathlib import Path
# runtime/ov_system.c: the codes TOTK_LANG accepts.
LANGUAGES = ('ja', 'en-US', 'en-GB', 'fr', 'fr-CA', 'de', 'it', 'es', 'es-419', 'nl', 'pt', 'pt-BR', 'ru', 'ko',
'zh-CN', 'zh-TW', 'zh-Hans', 'zh-Hant')
# Their text falls back to the Switch shared fonts (runtime/ov_system.c), which the installer does not extract.
NEED_SHARED_FONTS = ('ko', 'zh-CN', 'zh-TW', 'zh-Hans', 'zh-Hant')
ACTIVE = re.compile(r'^(\s*)(?:totk_)?lang(\s*=\s*)(.*?)(\s*)$', re.IGNORECASE)
COMMENTED = re.compile(r'^\s*[#;]\s*(?:totk_)?lang\s*=.*$', re.IGNORECASE)
SECTION = re.compile(r'^\s*\[(.*)\]\s*$')
def set_language(text, lang):
"""text with `lang = <lang>`; see the module docstring."""
lines = text.splitlines()
for i, line in enumerate(lines):
m = ACTIVE.match(line)
if m:
lines[i] = f'{m.group(1)}lang{m.group(2)}{lang}'
return '\n'.join(lines) + '\n'
for i, line in enumerate(lines):
if COMMENTED.match(line):
lines[i] = f'lang = {lang}'
return '\n'.join(lines) + '\n'
for i, line in enumerate(lines):
m = SECTION.match(line)
if m and m.group(1).strip().lower() == 'language':
lines.insert(i + 1, f'lang = {lang}')
return '\n'.join(lines) + '\n'
if lines and lines[-1].strip():
lines.append('')
lines += ['[language]', f'lang = {lang}']
return '\n'.join(lines) + '\n'
def get_language(text):
for line in text.splitlines():
m = ACTIVE.match(line)
if m:
return m.group(3)
return None
def write(dest, lang, example=None):
"""Write dest/totk.ini with lang; an existing file keeps its other lines. Returns the path."""
path = Path(dest) / 'totk.ini'
if path.is_file():
text = path.read_text(encoding='utf-8-sig')
elif example and Path(example).is_file():
text = Path(example).read_text(encoding='utf-8')
else:
text = ''
new = set_language(text, lang)
if path.is_file() and new == text:
return path
tmp = path.with_name('totk.ini.tmp')
tmp.write_text(new, encoding='utf-8', newline='\n')
os.replace(tmp, path)
return path
+38
View File
@@ -0,0 +1,38 @@
/* TotK-Setup.exe: starts python\pythonw.exe -m installer.gui with this exe's folder as the working directory.
No console window. The two message strings come from the catalog (installer/text/<lang>.json, ids launcher.title and
launcher.python_missing); tools/make_release.py writes them into launcher_text.h before it compiles this file.
Build: llvm/bin/clang -O2 -mwindows launcher.c -o TotK-Setup.exe */
#include <windows.h>
#include <wchar.h>
#include "launcher_text.h"
#define LANGID_RUSSIAN 0x19
#define PATH_CHARS (MAX_PATH * 2)
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);
}
int WINAPI WinMain(HINSTANCE instance, HINSTANCE previous, LPSTR command_line, int show)
{
wchar_t folder[PATH_CHARS], python[PATH_CHARS], command[PATH_CHARS + 64];
DWORD n = GetModuleFileNameW(NULL, folder, PATH_CHARS);
STARTUPINFOW si = { sizeof si };
PROCESS_INFORMATION pi;
(void)instance; (void)previous; (void)command_line; (void)show;
if (n == 0 || n >= PATH_CHARS) { fail(); return 1; }
while (n > 0 && folder[n - 1] != L'\\') n--;
folder[n > 0 ? n - 1 : 0] = 0;
if (swprintf(python, PATH_CHARS, L"%ls\\python\\pythonw.exe", folder) < 0 ||
GetFileAttributesW(python) == INVALID_FILE_ATTRIBUTES) { fail(); return 1; }
if (swprintf(command, PATH_CHARS + 64, L"\"%ls\" -m installer.gui", python) < 0) { fail(); return 1; }
if (!CreateProcessW(python, command, NULL, NULL, FALSE, 0, NULL, folder, &si, &pi)) { fail(); return 1; }
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
+113
View File
@@ -0,0 +1,113 @@
"""Where the installer finds what it runs: the one place that maps the release folder and a repository checkout.
Release (docs/installer.md "Release layout"), root = the unpacked folder:
python/python.exe runs every tool (numpy, lz4, cryptography, compression.zstd); its ._pth puts .., ../tools
and ../recomp on sys.path, so `python -m installer` imports from the release folder
recomp/, tools/ the lifter and the scripts; recomp/patches-v121.txt, tools/data/expected-v1.2.1.json
llvm/ llvm-mingw (bin/clang.exe)
runtime/ libtotkrt.a, the headers and install-side sources (romfs.c); 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
totk.ini.example
VERSION the release version, one line
temp/ the default temp folder (created)
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
refs/MacNativeGames; build/install-temp. The version is dev-<commit>.
"""
import os
import subprocess
import sys
from dataclasses import dataclass, field
from pathlib import Path
EXE = '.exe' if os.name == 'nt' else ''
UCRT = Path(r'C:\msys64\ucrt64\bin')
SHADER_LIST_REF = 'origin/HEAD:games/totk/assets/shader_list.tsv' # dev: tools/prepare_shaders.py LIST_REF
@dataclass(frozen=True)
class Layout:
root: Path
dev: bool
release: str
python: Path # runs the tools
tools: Path
recomp: Path
patches: Path # code patch table for recomp/gen.py
expected: Path # expected hashes of the supported update
llvm: Path # llvm-mingw tree
deps: Path # include/ and lib/ for the link
runtime: Path # runtime headers and install-side sources
runtime_lib: Path # libtotkrt.a
ninja: Path
hactool: Path
xlate: Path # totk_xlate.exe; a totk_xlate.id beside it is copied too
dlls: tuple = field(default_factory=tuple)
shader_list: Path | None = None # None: dev, read from refs/MacNativeGames through git
ini_example: Path | None = None
temp: Path | None = None
@property
def clang(self):
return self.llvm / 'bin' / ('clang' + EXE)
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',
self.tools / 'make_romfs_patches.py', self.recomp / 'gen.py', self.patches, self.expected, self.clang,
self.deps / 'include', self.deps / 'lib', self.runtime, self.runtime_lib, self.ninja, self.hactool,
self.xlate, *self.dlls]
if self.shader_list:
need.append(self.shader_list)
return [str(p) for p in need if not p.exists()]
def is_release(root):
return (root / 'python' / ('python' + EXE)).is_file()
def release_layout(root):
version = root / 'VERSION'
return Layout(
root=root, dev=False,
release=version.read_text(encoding='utf-8').strip() if version.is_file() else 'unknown',
python=root / 'python' / ('python' + EXE),
tools=root / 'tools', recomp=root / 'recomp', patches=root / 'recomp' / 'patches-v121.txt',
expected=root / 'tools' / 'data' / 'expected-v1.2.1.json',
llvm=root / 'llvm', deps=root / 'runtime' / 'deps', runtime=root / 'runtime',
runtime_lib=root / 'runtime' / 'libtotkrt.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'),
shader_list=root / 'data' / 'shader_list.tsv', ini_example=root / 'totk.ini.example', temp=root / 'temp')
def dev_layout(root):
commit = subprocess.run(['git', '-C', str(root), 'rev-parse', '--short', 'HEAD'], capture_output=True,
text=True).stdout.strip()
embed = root / 'build' / 'py314-embed' / 'py' / ('python' + EXE)
trees = sorted(p.parent.parent for p in (root / 'toolchain').glob('llvm-mingw-*/llvm-mingw-*/bin/clang' + EXE))
deps = root / 'toolchain' / 'deps-mingw'
return Layout(
root=root, dev=True, release=f'dev-{commit or "unknown"}',
python=embed if embed.is_file() else Path(sys.executable),
tools=root / 'tools', recomp=root / 'recomp', patches=root / 'recomp' / 'patches-v121.txt',
expected=root / 'tools' / 'data' / 'expected-v1.2.1.json',
llvm=trees[-1] if trees else root / 'toolchain' / 'llvm-mingw-missing', deps=deps,
runtime=root / 'runtime', runtime_lib=root / 'build' / 'install-rtlib' / 'libtotkrt.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'))),
shader_list=None, ini_example=root / 'totk.ini.example', temp=root / 'build' / 'install-temp')
def find(root=None):
"""The layout of root (default: the folder above this package)."""
root = Path(root or Path(__file__).resolve().parent.parent).resolve()
return release_layout(root) if is_release(root) else dev_layout(root)
+151
View File
@@ -0,0 +1,151 @@
"""Which steps run, in what order, and the bookkeeping around them: fresh install, resume and update are one rule.
Each step has a key: a fingerprint of everything its output depends on, computed before anything runs (a step's key
includes the keys of the steps it reads from, so a change upstream reaches every step below it). A step is skipped
when totk-install.json records it with the same key and its output still matches the record (Step.current, cheap).
Otherwise it runs. A transient step (its output lives in the temp folder and only the next step reads it) runs only
when the next step runs. `always` steps (check, cleanup) run every time.
Update mode is the same rule with a newer release: the extraction's key depends only on the expected hashes, so a
release that changes the lifter or the runtime keeps romfs/ and exefs/ and rebuilds from lift on. Before keeping the
output of a step recorded by another release (or with --verify), the check step calls Step.verify (expensive: the full
RomFS hash); a mismatch makes the step run again.
A failed or cancelled run leaves the finished steps recorded; the next run starts at the first step that is not.
"""
import datetime
import traceback
from . import state as statefile
class InstallError(Exception):
"""A failure with a message id (installer/text/en.json) and its parameters."""
def __init__(self, id, **params):
super().__init__(id)
self.id, self.params = id, params
class Step:
id = ''
transient = False
always = False
def key(self, ctx):
"""Fingerprint of the step's inputs; may read ctx.keys of earlier steps."""
return ''
def current(self, ctx, record):
"""True when the recorded output is still in place (cheap checks only)."""
return True
def verify(self, ctx, record):
"""True when the recorded output passes the full check (hashes); called before keeping another release's
output."""
return True
def run(self, ctx):
"""Do the work. Returns fields to record with the step."""
return {}
class Context:
def __init__(self, layout, dest, temp, reporter, options, state=None, verify=False):
self.layout, self.dest, self.temp, self.reporter, self.options = layout, dest, temp, reporter, options
self.state = state or statefile.empty(layout.release)
self.verify = verify
self.mode = 'install' if state is None else 'resume' if state.get('release') == layout.release else 'update'
self.keys, self.runs, self.reasons, self.steps, self.forced = {}, {}, {}, [], set()
def record(self, step_id):
return self.state['steps'].get(step_id)
def will_run(self, step_id):
return self.runs.get(step_id, False)
def save(self):
self.dest.mkdir(parents=True, exist_ok=True)
statefile.save(self.dest, self.state)
def plan(self, forced=()):
"""Fill keys (once), runs and reasons. forced: step ids that must run."""
if not self.keys:
for s in self.steps:
self.keys[s.id] = s.key(self)
for s in self.steps:
rec = self.record(s.id)
if s.always or s.id in forced:
self.runs[s.id] = True
else:
self.runs[s.id] = not (rec and rec.get('key') == self.keys[s.id] and s.current(self, rec))
self.reasons[s.id] = '' if self.runs[s.id] else 'current'
for i in range(len(self.steps) - 2, -1, -1):
s, after = self.steps[i], self.steps[i + 1]
if s.transient and self.runs[s.id] and not self.runs[after.id]:
self.runs[s.id], self.reasons[s.id] = False, 'not_needed'
self.forced = set(forced)
def verify_kept(self, progress_step):
"""Full check of every kept step output that another release recorded (or of all with --verify). A step that
fails it is planned to run. Returns the ids that failed."""
failed = []
for s in self.steps:
rec = self.record(s.id)
if self.runs[s.id] or self.reasons[s.id] != 'current' or s.always:
continue
if not (self.verify or rec.get('release') != self.layout.release):
continue
if not s.verify(self, rec):
failed.append(s.id)
if failed:
for f in failed:
self.reporter.warning('state.output_changed', step=f)
self.plan(self.forced | set(failed))
return failed
def now_utc():
return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
def run_steps(steps, ctx):
"""Run the plan. Returns 'ok', 'failed' or 'cancelled'; events and the log say why."""
rep = ctx.reporter
ctx.steps = steps
current = None
try:
ctx.plan()
rep.emit('plan', run=[s.id for s in steps if ctx.runs[s.id]],
skip={s.id: ctx.reasons[s.id] for s in steps if not ctx.runs[s.id]})
for s in steps:
if not ctx.runs[s.id]:
rep.step(s.id, 'skipped', reason=ctx.reasons[s.id])
continue
current = s.id
rep.step(s.id, 'started')
t0 = rep.elapsed()
fields = s.run(ctx) or {}
seconds = round(rep.elapsed() - t0, 1)
if not s.always:
ctx.state['steps'][s.id] = {'key': ctx.keys[s.id], 'release': ctx.layout.release,
'finished': now_utc(), 'seconds': seconds, **fields}
ctx.save() # the first save, after the check, creates the install folder
rep.step(s.id, 'done', seconds=seconds)
current = None
ctx.state['release'] = ctx.layout.release
ctx.save()
return 'ok'
except InstallError as e:
rep.error(e.id, e.params, step=current)
return 'failed'
except statefile.StateError as e:
rep.error(e.id, {'path': str(e.path)}, step=current)
return 'failed'
except KeyboardInterrupt:
rep.log(f'cancelled during {current}')
return 'cancelled'
except Exception as e:
rep.log(traceback.format_exc())
rep.error('internal', {'exception': type(e).__name__, 'detail': str(e)}, step=current)
return 'failed'
+54
View File
@@ -0,0 +1,54 @@
"""totk-install.json in the install folder: what built it and which steps finished.
{"format": 1, "release": "...", "lang": "en-US",
"inputs": {"base": {"name", "size", "sha256"}, "update": {...}},
"steps": {"<step>": {"key": "<input fingerprint>", "release": "...", "finished": "<UTC time>", "seconds": 12.3,
...the step's own fields}}}
A step's key fingerprints everything its output depends on (runner.py), so a step whose recorded key equals the
current one and whose output still checks out is not run again. The file is replaced atomically after each step.
No key value or keys file content is ever stored.
"""
import json
import os
from pathlib import Path
NAME = 'totk-install.json'
FORMAT = 1
class StateError(Exception):
def __init__(self, id, path):
super().__init__(id)
self.id, self.path = id, path
def empty(release):
return {'format': FORMAT, 'release': release, 'inputs': {}, 'steps': {}}
def load(dest):
"""The state of dest, or None when the folder has none. StateError for an unreadable or newer file."""
path = Path(dest) / NAME
if not path.is_file():
return None
try:
data = json.loads(path.read_text(encoding='utf-8'))
except (OSError, ValueError):
raise StateError('state.unreadable', path) from None
if not isinstance(data, dict) or not isinstance(data.get('steps'), dict):
raise StateError('state.unreadable', path)
if data.get('format', 0) > FORMAT:
raise StateError('state.newer_format', path)
return data
def save(dest, data):
path = Path(dest) / NAME
tmp = path.with_name(NAME + '.tmp')
with open(tmp, 'w', encoding='utf-8', newline='\n') as f:
json.dump(data, f, indent=1, ensure_ascii=False)
f.write('\n')
f.flush()
os.fsync(f.fileno())
os.replace(tmp, path)
+646
View File
@@ -0,0 +1,646 @@
"""The six setup steps of docs/installer.md: check, extract, lift, compile, prepare, 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 <temp>/logs/<step>-<tool>.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/, totk_xlate.exe, the shader state of totk.exe --prepare-shaders, totk.ini, totk-install.json.
Temp folder (<temp>/<dest name>-<hash>): check/ (ExeFS of both inputs, the host check), gen/ (C), build/ (objects),
logs/ (kept by the cleanup).
"""
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import threading
import time
from pathlib import Path
from . import ini
from .layout import EXE
from .runner import InstallError, Step
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'tools'))
import extract # noqa: E402
from extract_exefs import (common_tickets, container_entries, load_keys, nca_ctr_key, nca_inventory, # noqa: E402
program_nca)
GiB = 1 << 30
DEST_EXTRA = 4 * GiB # beside the RomFS: shader and pipeline caches (about 2 GB), exefs, totk.exe, seed files
TEMP_NEED = 3 * GiB # generated C (about 0.9 GB) and objects
UPDATE_VERSION = '1.2.1'
HOSTCHECK = Path(__file__).resolve().parent / 'hostcheck.c'
STAMP = '.install-key'
# ---- helpers
def file_sha256(path, progress=None):
"""SHA-256 hex of a file; progress(done, total) after each block."""
h = hashlib.sha256()
total = path.stat().st_size
done = 0
with open(path, 'rb', buffering=0) as f:
buf = bytearray(8 << 20)
view = memoryview(buf)
while n := f.readinto(buf):
h.update(view[:n])
done += n
if progress:
progress(done, total)
return h.hexdigest()
def fingerprint(*parts):
"""SHA-256 over strings and the contents of files and directory trees (a missing path counts as missing)."""
h = hashlib.sha256()
for p in parts:
if isinstance(p, Path):
if p.is_dir():
for f in sorted(p.rglob('*')):
if f.is_file() and '__pycache__' not in f.parts:
h.update(f.relative_to(p).as_posix().encode() + b'\0' + bytes.fromhex(file_sha256(f)))
elif p.is_file():
h.update(p.name.encode() + b'\0' + bytes.fromhex(file_sha256(p)))
else:
h.update(b'missing\0' + p.name.encode())
else:
h.update(str(p).encode())
h.update(b'\1')
return h.hexdigest()
def remove(path):
if path.is_dir() and not path.is_symlink():
shutil.rmtree(path)
elif path.exists() or path.is_symlink():
path.unlink()
def replace_file(src, dst):
"""Copy src over dst through a temporary name, so a cancelled copy never leaves half a file under dst's name."""
tmp = dst.with_name(dst.name + '.tmp')
shutil.copy2(src, tmp)
os.replace(tmp, dst)
def kill_tree(proc):
if proc.poll() is not None:
return
if os.name == 'nt':
subprocess.run(['taskkill', '/T', '/F', '/PID', str(proc.pid)], capture_output=True)
else:
proc.kill()
def run_tool(ctx, step, script, args, parse=None, cwd=None):
"""Run a tool script with the bundled Python. Its output goes to <temp>/logs/<step>-<script>.log and, line by line,
to parse(line). Returns (exit code, log path)."""
logs = ctx.temp / 'logs'
logs.mkdir(parents=True, exist_ok=True)
log = logs / f'{step}-{Path(script).stem}.log'
command = [str(ctx.layout.python), '-c', extract.RUN_SCRIPT, str(script), *map(str, args)]
ctx.reporter.log('run ' + ' '.join(command[3:]))
env = dict(os.environ, PYTHONUNBUFFERED='1', PYTHONIOENCODING='utf-8')
with open(log, 'w', encoding='utf-8', newline='\n') as out:
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd or ctx.layout.root,
env=env, text=True, encoding='utf-8', errors='replace', bufsize=1)
try:
for line in proc.stdout:
out.write(line)
if parse:
parse(line.rstrip())
code = proc.wait()
except BaseException:
kill_tree(proc)
raise
ctx.reporter.log(f'{Path(script).name}: exit code {code}; output in {log}')
return code, log
def free_bytes(path):
"""Free bytes on the disk of path (its nearest existing parent) and that parent's device."""
p = Path(path)
while not p.exists():
p = p.parent
return shutil.disk_usage(p).free, os.stat(p).st_dev
def raise_if_damaged(error, name, path):
"""InstallError check.input_damaged when a reader's ValueError says the file's data fails its hashes or ends early
(extract.DAMAGE_TEXT)."""
if extract.DAMAGE_TEXT.search(str(error)):
raise InstallError('check.input_damaged', input=name, path=str(path), detail=str(error)) from None
def expected_update():
return extract.load_expected()[UPDATE_VERSION]
def shader_list_text(layout):
if layout.shader_list:
return layout.shader_list.read_text(encoding='utf-8')
refs = layout.root / 'refs' / 'MacNativeGames'
r = subprocess.run(['git', '-C', str(refs), 'show', 'origin/HEAD:games/totk/assets/shader_list.tsv'],
capture_output=True, encoding='utf-8')
if r.returncode:
raise InstallError('prepare.shader_list_missing')
return r.stdout
class Watch(threading.Thread):
"""Polls a measure() of bytes written while a long call runs and reports it as progress."""
def __init__(self, reporter, step):
super().__init__(daemon=True)
self.reporter, self.step = reporter, step
self.phase, self.measure, self.total = None, None, None
self.stopped = threading.Event()
def set(self, phase, measure=None, total=None):
self.phase, self.measure, self.total = phase, measure, total
self.reporter.progress(self.step, phase, 0 if total else None, total, 'bytes' if total else None, force=True)
def run(self):
while not self.stopped.wait(1.0):
phase, measure, total = self.phase, self.measure, self.total
if phase and measure:
try:
done = min(measure(), total)
except OSError:
continue
self.reporter.progress(self.step, phase, done, total, 'bytes')
def stop(self):
self.stopped.set()
def size_of(paths):
return sum(p.stat().st_size for p in paths if p.is_file())
# ---- 1. check
class Check(Step):
id = 'check'
always = True
def run(self, ctx):
layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest
missing = layout.missing()
if missing:
raise InstallError('check.release_incomplete', paths=missing[:5])
if dest.exists() and not dest.is_dir():
raise InstallError('check.dest_not_folder', path=str(dest))
if ctx.mode == 'install' and dest.is_dir() and any(dest.iterdir()):
raise InstallError('check.dest_not_empty', path=str(dest))
lang = ctx.options.lang or ctx.state.get('lang')
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))
if lang in ini.NEED_SHARED_FONTS:
rep.warning('warn.lang_needs_fonts', lang=lang)
ctx.lang = lang
ctx.temp.mkdir(parents=True, exist_ok=True)
rep.progress('check', 'host', force=True)
self.host(ctx)
rep.progress('check', 'verify', force=True)
ctx.verify_kept('check')
ncas = 0
if ctx.will_run('extract'):
rep.progress('check', 'inputs', force=True)
ncas = self.inputs(ctx)
self.space(ctx, ncas)
if ctx.will_run('extract'):
rep.progress('check', 'versions', force=True)
self.versions(ctx)
return {}
def host(self, ctx):
layout, work = ctx.layout, ctx.temp / 'check'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('hostcheck' + EXE)
command = [str(layout.clang), '-O1', str(HOSTCHECK), '-I', str(layout.deps / 'include'), '-o', str(exe)]
if os.name != 'nt':
command.append('-ldl')
r = subprocess.run(command, capture_output=True, text=True, errors='replace')
if r.returncode:
ctx.reporter.log(r.stdout + r.stderr)
raise InstallError('check.toolchain_broken', detail=(r.stderr or r.stdout).strip()[-400:])
r = subprocess.run([str(exe)], capture_output=True, text=True, errors='replace')
try:
host = json.loads(r.stdout)
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['loader']:
raise InstallError('check.vulkan_missing')
devices = host['devices']
if not devices:
raise InstallError('check.vulkan_no_device')
if not any(tuple(d['api'][:2]) >= (1, 3) for d in devices):
raise InstallError('check.vulkan_too_old',
devices=', '.join(f'{d["name"]} ({d["api"][0]}.{d["api"][1]})' for d in devices))
ctx.host = host
def inputs(self, ctx):
"""Open both files with the keys as the extraction will. Returns the bytes of both Program NCAs."""
o = ctx.options
for name in ('base', 'update', 'keys'):
path = getattr(o, name)
if not path:
raise InstallError('check.inputs_needed')
if not Path(path).is_file():
raise InstallError('check.input_missing', input=name, path=str(path))
try:
keys = load_keys(Path(o.keys))
except (ValueError, OSError, UnicodeDecodeError):
raise InstallError('check.keys_unreadable') from None
if 'header_key' not in keys:
raise InstallError('check.keys_no_header_key')
total = 0
for name, is_update in (('base', False), ('update', True)):
path = Path(getattr(o, name))
with path.open('rb') as f:
total += self.open_input(f, path, keys, name, is_update)
del keys
return total
@staticmethod
def open_input(f, path, keys, name, is_update):
try:
kind, entries = container_entries(f, path.stat().st_size)
except ValueError as e:
raise_if_damaged(e, name, path)
raise InstallError('check.not_game_file', input=name) from None
if is_update and kind != 'nsp':
raise InstallError('check.update_not_nsp')
try:
tickets = common_tickets(f, entries)
except ValueError as e:
if 'Personalized' in str(e):
raise InstallError('check.ticket_personalized', input=name) from None
raise_if_damaged(e, name, path)
raise InstallError('check.file_unreadable', input=name, detail=str(e)) from None
try:
records = nca_inventory(f, entries, keys)
except ValueError as e:
if 'NCA3' in str(e):
raise InstallError('check.keys_wrong', input=name) from None
raise_if_damaged(e, name, path)
raise InstallError('check.file_unreadable', input=name, detail=str(e)) from None
try:
record, header = program_nca(records, update=is_update)
except ValueError as e:
if is_update and 'BKTR' in str(e):
raise InstallError('check.update_is_base') 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)
except ValueError as e:
if 'ticket' in str(e):
raise InstallError('check.ticket_missing', input=name) from None
raise InstallError('check.keys_too_old', input=name,
generation=max(header[0x206], header[0x220])) from None
return record['size']
def space(self, ctx, ncas):
dest_need = temp_need = 0
if ctx.will_run('extract'):
dest_need += expected_update()['romfs']['size'] + ncas
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('prepare') or ctx.will_run('extract'):
dest_need += DEST_EXTRA
if ctx.will_run('lift') or ctx.will_run('compile'):
temp_need += TEMP_NEED
dest_free, dest_dev = free_bytes(ctx.dest)
temp_free, temp_dev = free_bytes(ctx.temp)
ctx.reporter.log(f'space: install folder needs {dest_need / GiB:.1f} GiB of {dest_free / GiB:.1f} free, '
f'temp folder {temp_need / GiB:.1f} GiB of {temp_free / GiB:.1f}')
if dest_dev == temp_dev:
dest_need += temp_need
temp_need = 0
ctx.reporter.emit('space', dest=str(ctx.dest), need=dest_need, free=dest_free, temp=str(ctx.temp),
temp_need=temp_need, temp_free=temp_free, same_disk=dest_dev == temp_dev)
for path, need, free in ((ctx.dest, dest_need, dest_free), (ctx.temp, temp_need, temp_free)):
if need > free:
raise InstallError('check.disk_space', path=str(path), need_gb=round(need / 1e9, 1),
free_gb=round(free / 1e9, 1))
def versions(self, ctx):
"""Extract both ExeFS into the temp folder (verifying every block) and identify the versions by hash."""
o, work = ctx.options, ctx.temp / 'check'
logs = ctx.temp / 'logs'
logs.mkdir(parents=True, exist_ok=True)
versions = extract.load_expected()
for name, is_update in (('base', False), ('update', True)):
out = work / name
remove(out)
try:
extract.extract_exefs(Path(getattr(o, 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
except ValueError:
raise InstallError('check.exefs_failed', input=name, log=str(logs / f'extract_exefs-{name}.log')) \
from None
problems = extract.exefs_problems(work / 'base' / 'exefs', versions['1.0.0'])
if problems:
raise InstallError('check.base_wrong_version', modules=', '.join(problems))
try:
found = extract.identify_update(work / 'update' / 'exefs', versions)['display_version']
except ValueError:
found = 'unknown'
if found != UPDATE_VERSION:
raise InstallError('check.update_wrong_version', found=found, wanted=UPDATE_VERSION)
# ---- 2. extract
class Extract(Step):
id = 'extract'
def key(self, ctx):
# The output is whatever matches the expected hashes, whichever files and tools produced it.
return fingerprint('extract-1', ctx.layout.expected)
def current(self, ctx, record):
expected = expected_update()
romfs = ctx.dest / 'romfs'
if extract.exefs_problems(ctx.dest / 'exefs', expected) or not extract.romfs_verified(romfs, expected):
return False
st = (romfs / 'romfs.bin').stat()
return st.st_size == record.get('romfs_size') and st.st_mtime_ns == record.get('romfs_mtime_ns')
def verify(self, ctx, record):
image = ctx.dest / 'romfs' / 'romfs.bin'
rep = ctx.reporter
digest = file_sha256(image, lambda d, t: rep.progress('check', 'verify_romfs', d, t, 'bytes'))
return digest == expected_update()['romfs']['sha256']
def run(self, ctx):
o, dest, rep = ctx.options, ctx.dest, ctx.reporter
expected = expected_update()
base, update, keys = Path(o.base), Path(o.update), Path(o.keys)
inputs = {}
for name, path in (('base', base), ('update', 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'):
inputs[name] = known
continue
digest = file_sha256(path, lambda d, t, n=name: rep.progress('extract', 'hash_' + n, d, t, 'bytes'))
inputs[name] = {'name': path.name, 'size': st.st_size, 'mtime_ns': st.st_mtime_ns, 'sha256': digest}
ctx.state['inputs'] = inputs
ctx.save()
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
remove(stale)
rep.progress('extract', 'exefs', force=True)
staged = ctx.temp / 'check' / 'update' / 'exefs'
if not staged.is_dir():
remove(ctx.temp / 'check' / 'update')
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
except ValueError:
raise InstallError('check.exefs_failed', input='update',
log=str(ctx.temp / 'logs' / 'extract_exefs-update.log')) from None
remove(dest / 'exefs')
shutil.move(staged, dest / 'exefs')
problems = extract.exefs_problems(dest / 'exefs', expected)
if problems:
raise InstallError('extract.exefs_mismatch', modules=', '.join(problems))
if extract.sha256(dest / 'exefs' / 'main.npdm') != expected['exefs_sha256']['main.npdm']:
rep.warning('warn.npdm_differs')
romfs = dest / 'romfs'
if self.id in ctx.forced:
remove(romfs) # the full hash failed: index.json still claims the extraction's hash
if not extract.romfs_verified(romfs, expected):
self.merge(ctx, base, update, keys, romfs, expected)
report = json.loads((romfs / 'index.json').read_text())
if report['romfs_size'] != expected['romfs']['size'] or report['romfs_sha256'] != expected['romfs']['sha256']:
raise InstallError('extract.romfs_mismatch', size=report['romfs_size'], sha256=report['romfs_sha256'][:16])
(dest / 'profile').mkdir(exist_ok=True)
st = (romfs / 'romfs.bin').stat()
return {'romfs_size': st.st_size, 'romfs_mtime_ns': st.st_mtime_ns}
def merge(self, ctx, base, update, keys, romfs, expected):
dest, logs = ctx.dest, ctx.temp / 'logs'
ncas = extract.merge_space(base, update, keys)
watch = Watch(ctx.reporter, 'extract')
def phase(name):
if name == 'copy':
watch.set('copy', lambda: size_of(dest.glob('update-merge-*/*.nca')), ncas)
elif name == 'merge':
watch.set('merge', lambda: size_of(dest.glob('update-merge-*/result/romfs.bin')),
expected['romfs']['size'])
else:
watch.set(name)
watch.start()
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),
detail=e.detail) from None
except ValueError as e:
raise InstallError('extract.romfs_merge_failed', detail=str(e), log=str(logs / 'merge_romfs-'
f'{dest.name}.log')) from None
finally:
watch.stop()
# ---- 3. lift
EMITTED = re.compile(r'^emitted (\d+)/(\d+) chunks')
DISCOVERED = re.compile(r'^(\w+): \d+ functions \(.*discovery')
class Lift(Step):
id = 'lift'
transient = True
def key(self, ctx):
return fingerprint('lift-1', ctx.keys['extract'], ctx.layout.recomp, ctx.layout.patches)
def current(self, ctx, record):
gen = ctx.temp / 'gen'
stamp = gen / STAMP
return (gen / 'layout.json').is_file() and stamp.is_file() and stamp.read_text().strip() == ctx.keys['lift']
def run(self, ctx):
layout, rep, gen = ctx.layout, ctx.reporter, ctx.temp / 'gen'
remove(gen)
rep.progress('lift', 'discover', force=True)
def parse(line):
m = EMITTED.match(line)
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], parse)
if code:
raise InstallError('lift.failed', code=code, log=str(log))
(gen / STAMP).write_text(ctx.keys['lift'] + '\n')
return {}
# ---- 4. compile
NINJA_LINE = re.compile(r'^\[(\d+)/(\d+)\] ')
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, *tools,
clang, *[d.name for d in layout.dlls])
def current(self, ctx, record):
exe = ctx.dest / ('totk' + EXE)
if not exe.is_file() or not (ctx.dest / 'romfs-index.bin').is_file():
return False
if not all((ctx.dest / d.name).is_file() for d in ctx.layout.dlls):
return False
return file_sha256(exe) == record.get('exe_sha256')
def run(self, ctx):
layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest
build = ctx.temp / 'build'
phase = ['compile']
def parse(line):
m = NINJA_LINE.match(line)
if m:
rep.progress('compile', phase[0], int(m.group(1)), int(m.group(2)), 'objects')
elif line.startswith('compile: '):
phase[0] = 'link'
rep.progress('compile', 'link', force=True)
rep.progress('compile', 'compile', force=True)
code, log = run_tool(ctx, 'compile', layout.tools / 'build_game.py', [
'--gen', ctx.temp / 'gen', '--out', build, '--toolchain', layout.llvm, '--deps', layout.deps,
'--runtime', layout.runtime, '--runtime-lib', layout.runtime_lib, '--ninja', layout.ninja,
'--expected', layout.expected, '--exefs', dest / 'exefs', '--romfs', dest / 'romfs',
'--profile', dest / 'profile'], parse)
exe = build / ('totk' + EXE)
if code or not exe.is_file():
raise InstallError('compile.failed', code=code, log=str(log))
try:
replace_file(exe, dest / exe.name)
except PermissionError:
raise InstallError('compile.exe_in_use', path=str(dest / exe.name)) from None
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)}
# ---- 5. prepare
TRANSLATED = re.compile(r'gfx_shader: prepare: (\d+) of (\d+) pairs')
class Prepare(Step):
id = 'prepare'
def key(self, ctx):
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')]
return fingerprint('prepare-1', ctx.keys['compile'], layout.xlate, xlate_id, shader_list_text(layout), *tools)
def current(self, ctx, record):
dest = ctx.dest
xlate = dest / ctx.layout.xlate.name
return ((dest / 'shader-prepared.bin').is_file() and (dest / 'romfs-patches' / 'manifest.tsv').is_file()
and xlate.is_file() and file_sha256(xlate) == record.get('xlate_sha256'))
def run(self, ctx):
layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest
xlate = dest / layout.xlate.name
if xlate.is_file() and file_sha256(xlate) != file_sha256(layout.xlate):
# 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)
xlate_id = layout.xlate.with_suffix('.id')
if xlate_id.is_file():
replace_file(xlate_id, xlate.with_suffix('.id'))
else:
remove(xlate.with_suffix('.id'))
rep.progress('prepare', 'romfs_patches', force=True)
remove(dest / 'romfs-patches')
code, log = run_tool(ctx, 'prepare', layout.tools / 'make_romfs_patches.py',
['--romfs', dest / 'romfs', '--out', dest / 'romfs-patches'])
if code:
raise InstallError('prepare.romfs_patches_failed', code=code, log=str(log))
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')
def parse(line):
m = TRANSLATED.search(line)
if m:
rep.progress('prepare', 'translate', int(m.group(1)), int(m.group(2)), 'shaders')
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:
rep.progress('prepare', 'libraries', force=True)
rep.progress('prepare', 'seed', force=True)
code, log = run_tool(ctx, 'prepare', layout.tools / 'prepare_shaders.py',
[dest, '--romfs', dest / 'romfs', '--exefs', 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))
return {'xlate_sha256': file_sha256(xlate)}
# ---- 6. clean up
class Cleanup(Step):
id = 'cleanup'
always = True
def run(self, ctx):
rep, dest = ctx.reporter, ctx.dest
rep.progress('cleanup', 'ini', force=True)
try:
ini.write(dest, ctx.lang, ctx.layout.ini_example)
except OSError as e:
raise InstallError('cleanup.ini_failed', detail=str(e)) from None
ctx.state['lang'] = ctx.lang
(dest / 'profile').mkdir(exist_ok=True)
rep.progress('cleanup', 'temp', force=True)
for stale in dest.glob('update-merge-*'):
remove(stale)
for name in ('gen', 'build', 'check', 'shader_list.tsv'):
remove(ctx.temp / name)
return {}
def all_steps():
return [Check(), Extract(), Lift(), Compile(), Prepare(), Cleanup()]
+172
View File
@@ -0,0 +1,172 @@
{
"step.check": "Checking the computer and the game files",
"step.extract": "Extracting the game",
"step.lift": "Translating the game code to C",
"step.compile": "Compiling totk.exe",
"step.prepare": "Preparing the shaders",
"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.versions": "Checking the game versions",
"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",
"phase.extract.copy": "Copying the game data",
"phase.extract.merge": "Applying the update to the game data",
"phase.extract.diagnose": "Finding which game file is damaged",
"phase.extract.index": "Verifying the game data",
"phase.lift.discover": "Finding the functions",
"phase.lift.emit": "Writing C code",
"phase.compile.compile": "Compiling",
"phase.compile.link": "Linking totk.exe",
"phase.prepare.romfs_patches": "Writing the 60 fps game data patches",
"phase.prepare.seed": "Reading the shaders from the game data",
"phase.prepare.translate": "Translating the shaders",
"phase.prepare.libraries": "Building the shaders for the graphics driver",
"phase.cleanup.ini": "Writing totk.ini",
"phase.cleanup.temp": "Removing temporary files",
"cli.usage": "Invalid command line: {detail}",
"internal": "Unexpected error ({exception}): {detail}",
"state.unreadable": "{path} cannot be read. Choose another folder or delete that file.",
"state.newer_format": "{path} was written by a newer installer. Use that installer or another folder.",
"state.output_changed": "Installed files of step '{step}' changed since they were made; that step runs again.",
"check.release_incomplete": "The installer folder is incomplete; missing: {paths}. Unpack the release again.",
"check.dest_not_folder": "The install location {path} is a file, not a folder.",
"check.dest_not_empty": "The install folder {path} is not empty and holds no earlier installation. Choose an empty folder.",
"check.lang_missing": "Choose the game language.",
"check.lang_unknown": "Unknown language '{lang}'. Known: {known}.",
"check.toolchain_broken": "The bundled compiler does not work: {detail}",
"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.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.2.1 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.file_unreadable": "The {input} file cannot be read: {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.2.1 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 needs {wanted}.",
"check.disk_space": "Not enough free space for {path}: {need_gb} GB needed, {free_gb} GB free.",
"extract.exefs_mismatch": "The extracted game code does not match version 1.2.1 ({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 1.2.1 (size {size}, checksum {sha256}...).",
"lift.failed": "Translating the game code failed (exit code {code}). Details: {log}",
"compile.failed": "Compiling totk.exe failed (exit code {code}). Details: {log}",
"compile.exe_in_use": "{path} is in use. Close the game and 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}",
"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.npdm_differs": "main.npdm differs from the reference 1.2.1 dump; only the code modules are used.",
"ui.title": "TotK Setup",
"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.2.1 (NSP)",
"ui.keys": "prod.keys",
"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.2.1 update file",
"ui.pick_keys": "Choose prod.keys",
"ui.pick_dest": "Choose the install folder",
"ui.type_game": "Game files (NSP, XCI)",
"ui.type_nsp": "NSP files",
"ui.type_keys": "Key files (*.keys)",
"ui.type_all": "All files",
"ui.start": "Start",
"ui.cancel": "Cancel",
"ui.cancelling": "Cancelling...",
"ui.show_log": "Show log",
"ui.hide_log": "Hide log",
"ui.close": "Close",
"ui.back": "Back",
"ui.open_folder": "Open folder",
"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.2.1 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.running_install": "Installing",
"ui.running_resume": "Continuing the earlier setup",
"ui.running_update": "Updating the installation",
"ui.state_started": "started",
"ui.state_done": "done",
"ui.state_skipped": "skipped",
"ui.state_pending": "waiting",
"ui.state_running": "running",
"ui.state_failed": "failed",
"ui.log_warning": "Warning",
"ui.log_error": "Error",
"ui.log_space": "Space needed: {need} GB, free: {free} GB",
"ui.log_end": "Finished",
"ui.result_ok": "The game is ready",
"ui.result_failed": "The setup failed",
"ui.result_cancelled": "The setup was cancelled",
"ui.done_text": "Start totk.exe from {dest}. The setup program and the game files you chose are no longer needed.",
"ui.cancelled_text": "The finished steps are kept. Start the setup again with the same folder to continue.",
"ui.log_file": "Log file: {path}",
"ui.process_died": "The setup stopped unexpectedly (exit code {code}).",
"ui.confirm_cancel": "The setup is running. Cancel it and close the window?",
"ui.space_one": "Free space needed on {path}: {need} GB (free: {free} GB).",
"ui.space_two": "Free space needed on {path}: {need} GB (free: {free} GB); on {temp}: {temp_need} GB (free: {temp_free} GB).",
"ui.amount_bytes": "{done} / {total} GB",
"ui.amount_chunks": "{done} / {total} parts",
"ui.amount_objects": "{done} / {total} files",
"ui.amount_shaders": "{done} / {total} shaders",
"ui.amount_other": "{done} / {total} {unit}",
"ui.seconds": "{n} s",
"param.input.base": "base game",
"param.input.update": "update",
"param.input.keys": "keys",
"launcher.title": "TotK Setup",
"launcher.python_missing": "The setup files are incomplete: the python folder next to TotK-Setup.exe is missing. Unpack the whole archive again and start TotK-Setup.exe from the unpacked folder.",
"lang.ja": "Japanese",
"lang.en-US": "English (US)",
"lang.en-GB": "English (UK)",
"lang.fr": "French",
"lang.fr-CA": "French (Canada)",
"lang.de": "German",
"lang.it": "Italian",
"lang.es": "Spanish",
"lang.es-419": "Spanish (Latin America)",
"lang.nl": "Dutch",
"lang.pt": "Portuguese",
"lang.pt-BR": "Portuguese (Brazil)",
"lang.ru": "Russian",
"lang.ko": "Korean",
"lang.zh-CN": "Chinese (Simplified, China)",
"lang.zh-TW": "Chinese (Traditional, Taiwan)",
"lang.zh-Hans": "Chinese (Simplified)",
"lang.zh-Hant": "Chinese (Traditional)"
}
+172
View File
@@ -0,0 +1,172 @@
{
"step.check": "Проверка компьютера и игровых файлов",
"step.extract": "Распаковка игры",
"step.lift": "Перевод кода игры в C",
"step.compile": "Сборка totk.exe",
"step.prepare": "Подготовка шейдеров",
"step.cleanup": "Уборка",
"phase.check.host": "Проверка процессора и видеокарты",
"phase.check.verify": "Проверка уже установленных файлов",
"phase.check.verify_romfs": "Проверка установленных данных игры",
"phase.check.inputs": "Открытие игровых файлов с помощью ключей",
"phase.check.versions": "Проверка версий игры",
"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": "Компоновка totk.exe",
"phase.prepare.romfs_patches": "Запись патчей данных игры для 60 кадров/с",
"phase.prepare.seed": "Чтение шейдеров из данных игры",
"phase.prepare.translate": "Трансляция шейдеров",
"phase.prepare.libraries": "Сборка шейдеров для драйвера видеокарты",
"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.host_probe_failed": "Проверка компьютера не выполнилась (код выхода {code}).",
"check.cpu_no_avx2": "Этому процессору не хватает AVX2 (x86-64-v3), который нужен игре. AVX2 есть у Intel Haswell (2013) и новее, у AMD Excavator и Zen и новее.",
"check.vulkan_missing": "Драйвер Vulkan не установлен. Установите свежий драйвер видеокарты.",
"check.vulkan_no_device": "Vulkan не находит видеокарту. Установите свежий драйвер видеокарты.",
"check.vulkan_too_old": "Игре нужна видеокарта с Vulkan 1.3; найдено: {devices}. Обновите драйвер видеокарты.",
"check.inputs_needed": "Нужны игровые файлы: выберите базовую игру, обновление 1.2.1 и 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.file_unreadable": "Не удаётся прочитать файл «{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.2.1.",
"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} ГБ.",
"extract.exefs_mismatch": "Распакованный код игры не соответствует версии 1.2.1 ({modules}).",
"extract.romfs_merge_failed": "Не удалось применить обновление к данным игры: {detail}. Подробности: {log}",
"extract.romfs_mismatch": "Распакованные данные игры не соответствуют версии 1.2.1 (размер {size}, контрольная сумма {sha256}...).",
"lift.failed": "Не удалось перевести код игры (код выхода {code}). Подробности: {log}",
"compile.failed": "Не удалось собрать totk.exe (код выхода {code}). Подробности: {log}",
"compile.exe_in_use": "Файл {path} занят. Закройте игру и запустите установку снова.",
"prepare.shader_list_missing": "Нет списка шейдеров.",
"prepare.romfs_patches_failed": "Не удалось записать патчи данных игры (код выхода {code}). Подробности: {log}",
"prepare.shaders_failed": "Не удалось подготовить шейдеры (код выхода {code}). Подробности: {log}",
"cleanup.ini_failed": "Не удаётся записать totk.ini: {detail}",
"warn.lang_needs_fonts": "Для языка «{lang}» нужны системные шрифты Switch, которые установка не извлекает; часть текста может не отображаться.",
"warn.npdm_differs": "main.npdm отличается от эталонного дампа 1.2.1; используются только модули с кодом.",
"ui.title": "Установка TotK",
"ui.intro": "Собирает готовую к запуску папку с Tears of the Kingdom из ваших игровых файлов. Занимает много времени и выполняется один раз.",
"ui.notice": "Папка, которую создаёт установка, содержит игру. Она предназначена только для личного использования: не передавайте и не выкладывайте её.",
"ui.keys_private": "Файл ключей читается на месте. Его содержимое не показывается, не копируется и не сохраняется.",
"ui.base": "Базовая игра (NSP или XCI)",
"ui.update": "Обновление 1.2.1 (NSP)",
"ui.keys": "prod.keys",
"ui.game_language": "Язык игры",
"ui.dest": "Папка установки",
"ui.browse": "Обзор...",
"ui.pick_game": "Выберите файл базовой игры",
"ui.pick_update": "Выберите файл обновления 1.2.1",
"ui.pick_keys": "Выберите prod.keys",
"ui.pick_dest": "Выберите папку установки",
"ui.type_game": "Файлы игры (NSP, XCI)",
"ui.type_nsp": "Файлы NSP",
"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.2.1.",
"ui.need_keys": "Выберите файл prod.keys.",
"ui.need_lang": "Выберите язык игры.",
"ui.missing_base": "Файл базовой игры не найден.",
"ui.missing_update": "Файл обновления не найден.",
"ui.missing_keys": "Файл ключей не найден.",
"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} ГБ, свободно: {free} ГБ",
"ui.log_end": "Завершено",
"ui.result_ok": "Игра готова",
"ui.result_failed": "Установка не удалась",
"ui.result_cancelled": "Установка отменена",
"ui.done_text": "Запустите totk.exe из папки {dest}. Программа установки и выбранные игровые файлы больше не нужны.",
"ui.cancelled_text": "Завершённые шаги сохранены. Чтобы продолжить, снова запустите установку с той же папкой.",
"ui.log_file": "Файл журнала: {path}",
"ui.process_died": "Установка неожиданно остановилась (код выхода {code}).",
"ui.confirm_cancel": "Установка выполняется. Отменить её и закрыть окно?",
"ui.space_one": "Нужно свободного места на {path}: {need} ГБ (свободно: {free} ГБ).",
"ui.space_two": "Нужно свободного места на {path}: {need} ГБ (свободно: {free} ГБ); на {temp}: {temp_need} ГБ (свободно: {temp_free} ГБ).",
"ui.amount_bytes": "{done} / {total} ГБ",
"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-CN": "Китайский (упрощённый, КНР)",
"lang.zh-TW": "Китайский (традиционный, Тайвань)",
"lang.zh-Hans": "Китайский (упрощённый)",
"lang.zh-Hant": "Китайский (традиционный)"
}
File diff suppressed because it is too large. Load diff
+260
View File
@@ -0,0 +1,260 @@
// --probe-astc: the compute ASTC decoder (vk_astc.c, shaders/astc_decode.comp) against astc_decode_ldr, bit for bit,
// for every block footprint, linear and sRGB. Synthetic blocks only (no game files): valid void extents, invalid and
// HDR void extents, random blocks kept by what they exercise (1-4 partitions, dual plane with 1-3 partitions, every
// weight grid and quantization the random modes reach), then plain random blocks (mostly error blocks, HDR endpoint
// modes among them). Each case decodes two regions into two mip levels of an RGBA8 image with cropped edges, and
// compares both the decoder's texels (readback) and what the image holds afterwards. Also times one 2048x2048 texture
// on the CPU (astc_decode_ldr_mt) and on the GPU (wall time of the upload batch).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "host.h"
#include "tex.h"
#include "vk.h"
static uint64_t g_rng = 0x2545F4914F6CDD1Dull;
static uint64_t rnd64(void) {
g_rng ^= g_rng << 13; g_rng ^= g_rng >> 7; g_rng ^= g_rng << 17;
return g_rng;
}
static const uint8_t dims[14][2] = {{4, 4}, {5, 4}, {5, 5}, {6, 5}, {6, 6}, {8, 5}, {8, 6},
{8, 8}, {10, 5}, {10, 6}, {10, 8}, {10, 10}, {12, 10}, {12, 12}};
// Buckets of valid blocks: partitions 1-4 single plane, then dual plane with 1-3 partitions.
enum { NB = 7, PER_BUCKET = 48 };
static const char *const bucket_name[NB] = {"1p", "2p", "3p", "4p", "dual 1p", "dual 2p", "dual 3p"};
static uint64_t g_bucket[NB], g_void, g_err, g_blocks, g_texels;
static int dual_bit(uint32_t m) {
if (!(m & 3) && (m >> 7 & 3) == 2) return 0; // the 6..10 x 6..10 modes have no dual plane
return (int)(m >> 10 & 1);
}
static void void_block(uint8_t b[16], int kind) {
// kind 0: valid LDR extent (random coordinates s0 < s1, t0 < t1), 1: all-ones extent, 2: s0 >= s1 (error),
// 3: HDR (error), 4: reserved bits not 11 (error)
uint64_t lo = 0x1FC | 3ull << 10, s0 = rnd64() & 0xFFF, s1 = s0 + 1 + (rnd64() & 0xFFF), t0 = rnd64() & 0xFFF,
t1 = t0 + 1 + (rnd64() & 0xFFF);
if (s1 > 0x1FFF) s1 = 0x1FFF;
if (t1 > 0x1FFF) t1 = 0x1FFF;
if (kind == 1) s0 = s1 = t0 = t1 = 0x1FFF;
if (kind == 2) { uint64_t x = s0; s0 = s1; s1 = x; }
lo |= s0 << 12 | s1 << 25 | t0 << 38 | t1 << 51;
if (kind == 3) lo |= 0x200;
if (kind == 4) lo &= ~(1ull << 11);
uint64_t hi = rnd64();
memcpy(b, &lo, 8), memcpy(b + 8, &hi, 8);
}
// n blocks for footprint bw x bh: the buckets first, void extents, then random.
static void make_blocks(uint8_t *blk, size_t n, uint32_t bw, uint32_t bh) {
size_t k = 0;
uint8_t tmp[12 * 12 * 4];
int have[NB] = {0};
for (int tries = 0; tries < 400000 && k < n; tries++) {
uint64_t v[2] = {rnd64(), rnd64()};
uint8_t *b = (uint8_t *)v;
uint32_t m = (uint32_t)(v[0] & 0x7FF);
if ((m & 0x1FF) == 0x1FC || !astc_decode_block(b, bw, bh, tries & 1, tmp)) continue;
int p = (int)(v[0] >> 11 & 3), bk = dual_bit(m) ? 4 + p : p;
if (bk >= NB || have[bk] >= PER_BUCKET) continue;
have[bk]++, g_bucket[bk]++;
memcpy(blk + 16 * k++, b, 16);
}
for (int i = 0; i < 40 && k < n; i++, g_void++) void_block(blk + 16 * k++, i % 5);
for (; k < n; k++) {
uint64_t v[2] = {rnd64(), rnd64()};
memcpy(blk + 16 * k, v, 16);
if (!astc_decode_block(blk + 16 * k, bw, bh, false, tmp)) g_err++;
}
}
typedef struct {
VkImage img;
VmaAllocation alloc;
} Img;
static int make_image(VkCtx *c, uint32_t w, uint32_t h, uint32_t levels, int srgb, Img *o) {
VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D,
.format = srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM, .extent = {w, h, 1},
.mipLevels = levels, .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 |
VK_IMAGE_USAGE_SAMPLED_BIT,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
return vmaCreateImage(c->vma, &ci, &ai, &o->img, &o->alloc, NULL) == VK_SUCCESS;
}
static void barrier(VkCommandBuffer cb, VkPipelineStageFlags2 src, VkAccessFlags2 sa, VkPipelineStageFlags2 dst,
VkAccessFlags2 da) {
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = src, .srcAccessMask = sa,
.dstStageMask = dst, .dstAccessMask = da};
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
vkCmdPipelineBarrier2(cb, &di);
}
#define ALL_RW VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT
static void to_general(VkCommandBuffer cb, VkImage img) {
VkImageMemoryBarrier2 ib = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_NONE,
.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 = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, VK_REMAINING_MIP_LEVELS, 0, 1}};
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &ib};
vkCmdPipelineBarrier2(cb, &di);
}
static double now_ms(void) {
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t), QueryPerformanceFrequency(&f);
return (double)t.QuadPart * 1e3 / (double)f.QuadPart;
}
// Decodes regions (w0 x h0 at level 0, w1 x h1 at level 1) of blk on the GPU; out_rb: the decoder's texels, out_img:
// the image's two levels read back. 0 on a Vulkan failure.
static int gpu_decode(VkCtx *c, const uint8_t *blk, size_t n0, size_t n1, uint32_t w0, uint32_t h0, uint32_t w1,
uint32_t h1, uint32_t bw, uint32_t bh, int srgb, uint8_t *out_rb, uint8_t *out_img, double *ms) {
Img im;
if (!make_image(c, w0, h0, 2, srgb, &im)) return 0;
size_t t0 = (size_t)w0 * h0 * 4, t1 = (size_t)w1 * h1 * 4;
VkCommandBuffer cb = vk_upload_begin();
VkBuffer sb, rb, ib;
VmaAllocation ra, ia;
uint8_t *stg = vk_staging((n0 + n1) * 16, &sb, NULL);
uint8_t *rmap = vk_staging(t0 + t1, &rb, &ra), *imap = vk_staging(t0 + t1, &ib, &ia);
memcpy(stg, blk, (n0 + n1) * 16);
to_general(cb, im.img);
VkAstcRegion ar[2] = {{.src = 0, .w = w0, .h = h0, .slices = 1, .level = 0},
{.src = n0 * 16, .w = w1, .h = h1, .slices = 1, .level = 1}};
barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT);
int ok = vk_astc_record(cb, sb, (n0 + n1) * 16, ar, 2, bw, bh, srgb, im.img, VK_IMAGE_ASPECT_COLOR_BIT, rb);
barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW);
VkBufferImageCopy cp[2] = {{.bufferOffset = 0, .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, .imageExtent = {w0, h0, 1}},
{.bufferOffset = t0, .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 1, 0, 1}, .imageExtent = {w1, h1, 1}}};
vkCmdCopyImageToBuffer(cb, im.img, VK_IMAGE_LAYOUT_GENERAL, ib, 2, cp);
barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_HOST_BIT, VK_ACCESS_2_HOST_READ_BIT);
vk_upload_end();
double a = now_ms();
vk_upload_flush(1);
if (ms) *ms = now_ms() - a;
vmaInvalidateAllocation(c->vma, ra, 0, VK_WHOLE_SIZE);
vmaInvalidateAllocation(c->vma, ia, 0, VK_WHOLE_SIZE);
if (out_rb) memcpy(out_rb, rmap, t0 + t1);
if (out_img) memcpy(out_img, imap, t0 + t1);
vmaDestroyBuffer(c->vma, rb, ra), vmaDestroyBuffer(c->vma, ib, ia);
vk_defer_image(im.img, im.alloc);
vk_upload_flush(1);
return ok;
}
static int first_diff(const uint8_t *a, const uint8_t *b, size_t texels, size_t *at) {
for (size_t i = 0; i < texels; i++)
if (memcmp(a + 4 * i, b + 4 * i, 4)) { *at = i; return 1; }
return 0;
}
static int test_case(VkCtx *c, uint32_t bw, uint32_t bh, int srgb) {
// level 0: 37 x 23 blocks, cropped by 1-3 texels; level 1 half of that, other blocks
uint32_t w0 = 37 * bw - 1 - (bw > 5), h0 = 23 * bh - 2, w1 = w0 > 1 ? w0 >> 1 : 1, h1 = h0 > 1 ? h0 >> 1 : 1;
size_t n0 = (size_t)((w0 + bw - 1) / bw) * ((h0 + bh - 1) / bh), n1 = (size_t)((w1 + bw - 1) / bw) * ((h1 + bh - 1) / bh);
uint8_t *blk = malloc((n0 + n1) * 16);
make_blocks(blk, n0, bw, bh);
make_blocks(blk + n0 * 16, n1, bw, bh);
size_t t0 = (size_t)w0 * h0, t1 = (size_t)w1 * h1;
uint8_t *want = malloc((t0 + t1) * 4), *rb = malloc((t0 + t1) * 4), *im = malloc((t0 + t1) * 4);
memset(rb, 0x5A, (t0 + t1) * 4), memset(im, 0xA5, (t0 + t1) * 4);
astc_decode_ldr(blk, w0, h0, bw, bh, srgb, want);
astc_decode_ldr(blk + n0 * 16, w1, h1, bw, bh, srgb, want + t0 * 4);
int bad = 0;
if (!gpu_decode(c, blk, n0, n1, w0, h0, w1, h1, bw, bh, srgb, rb, im, NULL)) {
printf("probe-astc: FAIL %ux%u %s: the GPU decode was not recorded\n", bw, bh, srgb ? "sRGB" : "linear");
bad = 1;
}
for (int lv = 0; lv < 2 && !bad; lv++) {
size_t off = lv ? t0 * 4 : 0, n = lv ? t1 : t0, at;
uint32_t w = lv ? w1 : w0;
for (int which = 0; which < 2 && !bad; which++) {
const uint8_t *got = (which ? im : rb) + off;
if (first_diff(want + off, got, n, &at)) {
const uint8_t *e = want + off + 4 * at, *g = got + 4 * at;
printf("probe-astc: FAIL %ux%u %s level %d (%s): texel (%zu,%zu) cpu %02x%02x%02x%02x gpu %02x%02x%02x%02x\n",
bw, bh, srgb ? "sRGB" : "linear", lv, which ? "image" : "decoder output", at % w, at / w,
e[0], e[1], e[2], e[3], g[0], g[1], g[2], g[3]);
bad = 1;
}
}
}
g_blocks += n0 + n1, g_texels += t0 + t1;
free(blk), free(want), free(rb), free(im);
return bad;
}
// One 2048 x 2048 texture of valid blocks: the CPU worker-pool decode against the GPU time of the dispatches alone
// (vk_astc.c timestamps, TOTK_ASTC_TIME) and the wall time of the upload batch.
static void timing(VkCtx *c, uint32_t bw, uint32_t bh) {
uint32_t w = 2048, h = 2048;
size_t n = (size_t)((w + bw - 1) / bw) * ((h + bh - 1) / bh);
uint8_t *blk = malloc(n * 16), *out = malloc((size_t)w * h * 4), tmp[12 * 12 * 4];
for (size_t k = 0; k < 4096;) { // valid, non-constant blocks: error blocks would flatter the GPU
uint64_t v[2] = {rnd64(), rnd64()};
if ((v[0] & 0x1FF) != 0x1FC && astc_decode_block((uint8_t *)v, bw, bh, false, tmp)) memcpy(blk + 16 * k++, v, 16);
}
for (size_t k = 4096; k < n; k++) memcpy(blk + 16 * k, blk + 16 * (k % 4096), 16);
double a = now_ms();
astc_decode_ldr_mt(blk, w, h, bw, bh, false, out);
double cpu = now_ms() - a, wall = 0;
vk_astc_time_enable(1);
uint64_t t0, c0, t1, c1;
double u0, u1;
vk_astc_time_get(bw, bh, &t0, &c0, &u0);
for (int i = 0; i < 4; i++) gpu_decode(c, blk, n, 1, w, h, 1, 1, bw, bh, 0, NULL, NULL, i ? &wall : NULL);
vk_astc_time_get(bw, bh, &t1, &c1, &u1);
vk_astc_time_enable(0);
double mtex = (double)(t1 - t0) / 1e6;
printf("probe-astc: 2048x2048 %ux%u: CPU (worker pool) %.1f ms; GPU dispatches %.0f us per M texel (%llu decodes "
"timed, %.2f M texels), batch wall %.1f ms\n", bw, bh, cpu, mtex > 0 ? (u1 - u0) / mtex : 0.0,
(unsigned long long)(c1 - c0), mtex, wall);
free(blk), free(out);
}
int probe_astc(void) {
if (!vk_init()) { printf("probe-astc: FAIL vk_init\n"); return 1; }
VkCtx *c = vk_ctx();
printf("probe-astc: GPU %s, textureCompressionASTC_LDR %d, mode %d\n", c->props.deviceName, c->has_astc_ldr,
vk_astc_mode());
int bad = 0;
if (!vk_astc_gpu()) {
printf("probe-astc: FAIL the compute decoder is not available (TOTK_ASTC=cpu or pipeline creation failed)\n");
bad = 1;
}
for (int i = 0; i < 14 && !bad; i++)
for (int s = 0; s < 2 && !bad; s++) bad = test_case(c, dims[i][0], dims[i][1], s);
for (int b = 0; b < NB && !bad; b++)
if (!g_bucket[b]) { printf("probe-astc: FAIL no valid %s block was generated\n", bucket_name[b]); bad = 1; }
if (!bad) {
timing(c, 4, 4);
timing(c, 6, 6);
timing(c, 8, 8);
timing(c, 12, 12);
}
vk_mem_shutdown();
vk_shutdown();
long errors = vk_validation_errors_total();
if (bad || errors) {
printf("probe-astc: FAILED (%s, %ld validation errors)\n", bad ? "a check failed" : "checks passed", errors);
return 1;
}
printf("probe-astc: passed (14 footprints x linear/sRGB, %llu blocks, %llu texels bit-exact with astc_decode_ldr; "
"valid blocks by kind: 1p %llu, 2p %llu, 3p %llu, 4p %llu, dual 1p %llu, dual 2p %llu, dual 3p %llu; "
"%llu void extents, %llu random error blocks; 0 validation errors)\n",
(unsigned long long)g_blocks, (unsigned long long)g_texels, (unsigned long long)g_bucket[0],
(unsigned long long)g_bucket[1], (unsigned long long)g_bucket[2], (unsigned long long)g_bucket[3],
(unsigned long long)g_bucket[4], (unsigned long long)g_bucket[5], (unsigned long long)g_bucket[6],
(unsigned long long)g_void, (unsigned long long)g_err);
return 0;
}
+3
View File
@@ -14,6 +14,7 @@
#include "cfg.h"
#include "gfx.h"
#include "nvn_api.h"
#include "vk.h"
#define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_"
@@ -200,6 +201,7 @@ static void save_shot(const tex_t *t, const char *path) {
view_t view;
memset(&view, 0, sizeof view);
NVNcopyRegion r = {.width = (int)w, .height = (int)h, .depth = 1};
if (gfx_replay_async()) gfx_backend()->finish(); // the presented frame may still be queued for the replay
gfx_backend()->read_texels(t, &view, &r, px, 4ull * w, 4ull * w * h);
if (bgra)
for (uint64_t i = 0; i < 4ull * w * h; i += 4) {
@@ -238,6 +240,7 @@ static void series_shot(const tex_t *t, uint64_t n) {
view_t view;
memset(&view, 0, sizeof view);
NVNcopyRegion r = {.width = (int)w, .height = (int)h, .depth = 1};
if (gfx_replay_async()) gfx_backend()->finish(); // the presented frame may still be queued for the replay
gfx_backend()->read_texels(t, &view, &r, px, 4ull * w, 4ull * w * h);
for (uint32_t y = 0; y < oh; y++)
for (uint32_t x = 0; x < ow; x++) {
+18 -15
View File
@@ -29,21 +29,24 @@ static PlatMutex set_lock = PLAT_MUTEX_INIT; // cfg_set, cfg_save
// 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[] = {
"APP_POOL", "ASSUME_FOCUS", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_STATS",
"CLOTH_REALTIME", "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", "FPS60", "FPS_COUNTER", "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_GUARD", "MENU_LANG", "MENU_TEST", "MENU_TEST_DIR",
"MINIMAL_LAYOUT", "MOTION", "MOUSE_SENS", "MOVIE_HOST",
"MOVIE_PACE", "OPERATION_MODE", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SYNC", "PREWARM_XLATE",
"PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_RENDER", "PUSH_STAGE", "RDOC",
"RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS",
"SHADER_CACHE", "SHARED_FONTS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL",
"SPEED_PROBE", "SPLIT_DRAWS", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TIMER_1MS",
"TRACE", "VK_NO_FLOAT64", "VK_PRESENT", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION",
"VOLUME", "VS_OUTS", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS",
"APP_POOL", "ASSUME_FOCUS", "ASTC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_MINUS",
"BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y",
"BIND_ZL", "BIND_ZR", "CLOTH_REALTIME", "CORE_DIAG", "DESC_TEMPLATE", "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", "FPS60", "FPS_COUNTER", "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_GUARD", "MEMORY", "MENU_LANG", "MENU_TEST",
"MENU_TEST_DIR", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE",
"OPERATION_MODE", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT",
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", "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", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPLIT_DRAWS", "SWKBD_ANSWER",
"SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TIMER_1MS", "TRACE", "VK_DYN_VERTEX", "VK_NO_FLOAT64",
"VK_PRESENT", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VS_OUTS",
"WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS",
};
// The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections).
+7 -1
View File
@@ -691,7 +691,13 @@ static File *file_of(uint64_t h, HANDLE *fh, int *mode) {
static int64_t host_read_at(HANDLE f, void *buf, uint32_t n, uint64_t off) {
OVERLAPPED ov = {.Offset = (DWORD)off, .OffsetHigh = (DWORD)(off >> 32)};
DWORD got = 0;
if (!ReadFile(f, buf, n, &got, &ov)) return GetLastError() == ERROR_HANDLE_EOF ? 0 : -1;
mem_thaw((uintptr_t)buf, n); // the kernel's write would fail on a frozen page instead of faulting
if (!ReadFile(f, buf, n, &got, &ov)) {
DWORD e = GetLastError();
if (e == ERROR_NOACCESS) // a read-only page in the buffer: a frozen region missed its thaw
rt_log("fs: read of %u bytes into %p refused: the buffer is not writable", n, buf);
return e == ERROR_HANDLE_EOF ? 0 : -1;
}
return got;
}
+3
View File
@@ -117,6 +117,9 @@ typedef struct {
void (*write_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img);
void (*guest_written)(uint64_t addr, uint64_t size); // host wrote pool storage (3.2)
void (*guest_read)(uint64_t addr, uint64_t size); // the CPU reads pool storage: GPU-written texels go there first
// The CPU waited for fence: small render targets written by work up to it go to their storage (vk_texture.c
// vk_texture_write_back_small).
void (*cpu_synced)(uint64_t fence);
} GfxBackend;
extern const GfxBackend gfx_null_backend, gfx_vulkan_backend;
const GfxBackend *gfx_backend(void); // TOTK_GPU=null|vulkan (default vulkan)
+17
View File
@@ -5,6 +5,7 @@
// Needs no game files.
#include <stddef.h>
#include <stdio.h>
#include <windows.h>
#include "gfx.h"
@@ -60,6 +61,22 @@ static void check_write_watch(void) {
printf("write-watch: three pages written, max 2: %llu then %llu\n", (unsigned long long)n, (unsigned long long)m);
CHECK(n == 2 && m == 1, "max 2 reported %llu, then %llu (want 2, 1)", (unsigned long long)n, (unsigned long long)m);
// Frozen region: read-only until the first write thaws it (the write lands and is reported); mem_thaw (a file
// read) thaws it and holds it thawed; SetHeapSize below decommits it.
mem_written_pages(heap, 0x200000, pages, 8);
CHECK(mem_freeze(heap) == 1 && mem_frozen(heap), "freeze of an idle committed 2 MiB region");
MEMORY_BASIC_INFORMATION mbi;
CHECK(VirtualQuery((void *)(uintptr_t)heap, &mbi, sizeof mbi) && mbi.Protect == PAGE_READONLY, "frozen region is not read-only");
p[0x3000] = 7;
CHECK(!mem_frozen(heap) && p[0x3000] == 7, "a write into a frozen region did not thaw it");
n = mem_written_pages(heap, 0x200000, pages, 8);
CHECK(n == 1 && pages[0] == heap + 0x3000, "write after a thaw: %llu pages reported (want 1)", (unsigned long long)n);
CHECK(mem_freeze(heap) == 1, "refreeze after a write thaw");
mem_thaw(heap + 0x6000, 0x1000);
CHECK(!mem_frozen(heap) && mem_freeze(heap) == 0, "mem_thaw did not thaw and hold the region");
p[0x6000] = 8;
printf("write-watch: frozen region thawed by a write (reported once) and by mem_thaw (held)\n");
uint64_t a = g->alias.base + 0x10000;
CHECK(mem_map_physical(a, 0x4000) == 0, "MapPhysicalMemory");
mem_written_pages(a, 0x4000, pages, 8);
+2
View File
@@ -132,6 +132,7 @@ static void null_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegi
static void null_guest_written(uint64_t addr, uint64_t size) { (void)addr, (void)size; }
static void null_guest_read(uint64_t addr, uint64_t size) { (void)addr, (void)size; }
static void null_cpu_synced(uint64_t fence) { (void)fence; }
const GfxBackend gfx_null_backend = {
.name = "null",
@@ -151,4 +152,5 @@ const GfxBackend gfx_null_backend = {
.write_texels = null_write_texels,
.guest_written = null_guest_written,
.guest_read = null_guest_read,
.cpu_synced = null_cpu_synced,
};
+1
View File
@@ -17,3 +17,4 @@ WEAK const GfxBackend gfx_null_backend = {.name = "unimplemented"};
#define STUB_PROBE(fn, name) WEAK int fn(void) { printf(name ": not built yet\n"); return 2; }
STUB_PROBE(probe_nvn_tex, "probe-nvn-tex")
STUB_PROBE(probe_vkmem, "probe-vkmem")
STUB_PROBE(probe_astc, "probe-astc")
+1
View File
@@ -217,6 +217,7 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
// The process ends with the window, as in MNG. Save writes go straight to files (runtime/fs_host.c).
rt_log("gfx: window closed; exiting");
vk_pipeline_save();
prof_write(); // ExitProcess runs no atexit handlers
ExitProcess(0);
case WM_ERASEBKGND:
return 1;
+21
View File
@@ -6,6 +6,7 @@
#include <windows.h>
#include "gfx.h"
#include "input.h"
#include "vk.h"
#define SRC_W 640
@@ -147,6 +148,26 @@ int probe_window(void) {
}
printf("probe-window: focus off/on pushed 2 messages (15)\n");
// Cursor release: Esc releases, a client click recaptures, a title bar activation releases. The probe window may not be
// the foreground window, so this checks the release flag, not the clip state.
HWND hw = gfx_window_handle();
SendMessageW(hw, WM_KEYDOWN, VK_ESCAPE, 0);
int esc = input_cursor_released();
SendMessageW(hw, WM_LBUTTONDOWN, MK_LBUTTON, 0);
int click = input_cursor_released();
SendMessageW(hw, WM_MOUSEACTIVATE, (WPARAM)hw, MAKELPARAM(HTCLIENT, WM_LBUTTONDOWN));
int client = input_cursor_released();
SendMessageW(hw, WM_MOUSEACTIVATE, (WPARAM)hw, MAKELPARAM(HTCAPTION, WM_LBUTTONDOWN));
int caption = input_cursor_released();
SendMessageW(hw, WM_ACTIVATEAPP, FALSE, 0);
SendMessageW(hw, WM_ACTIVATEAPP, TRUE, 0);
int back = input_cursor_released();
if (!esc || click || client || !caption || back) {
printf("probe-window: released esc=%d click=%d client=%d caption=%d reactivated=%d\n", esc, click, client, caption, back);
return fail("cursor release");
}
printf("probe-window: cursor released by Esc and title bar activation, recaptured by a click\n");
// Resize: the client area changes, the swapchain follows, presenting still reads back exact.
gfx_window_resize(1280, 720);
RECT rc;
+32 -1
View File
@@ -28,6 +28,7 @@ void prof_write(void); // idempotent; boot.c calls it b
uint64_t perf_begin(void); // start time of a wait; pair with perf_end
void perf_end(int kind, uint64_t t0); // counts render-thread time only
void perf_replay(uint64_t t0); // replay (NVN -> Vulkan recording) time on any thread, waits inside it included
void perf_replay_thread(void); // the calling thread replays for the render thread: perf_end counts its waits
// Events per 60-frame window: graphics pipelines finished by the background workers, draws skipped while pending,
// first texture uploads and re-uploads after CPU writes, draws skipped while a shader translates in the background,
// draws that waited for their pending pipeline and got it, draws into a target the previous frame did not render that
@@ -41,6 +42,16 @@ enum { PERF_CNT_PIPE_ASYNC, PERF_CNT_PIPE_SKIP, PERF_CNT_TEX_UP, PERF_CNT_TEX_RE
void perf_count(int kind);
void perf_present(uint64_t frame);
void perf_summary_flush(void); // prints the frame-time summary of the running period (exit); the 60 s lines come from perf_present
void gfx_exit_log(void); // back end's lines for the exit summary (weak no-op in prof_win.c)
void perf_gpu_wait(uint64_t t0); // a wait for a GPU fence ended (any thread; the render thread's also counts as PERF_FENCE)
// GPU side of the 60-frame perf line, filled by the graphics back end when it set perf_gpu_stats. gpu_ns: GPU busy time
// since the previous call (timestamp queries, overlaps counted once), have_gpu 0 when the device has no timestamps.
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;
} PerfGpu;
extern void (*perf_gpu_stats)(PerfGpu *out);
#define RT_RESULT(module, desc) ((uint32_t)(module) | ((uint32_t)(desc) << 9))
void rt_sha256(const void *p, size_t n, uint8_t out[32]); // BCrypt; fatal if the provider fails
@@ -119,6 +130,18 @@ uint64_t mem_written_pages(uint64_t addr, uint64_t size, uint64_t *pages, uint64
// past max stays reported next time). The range must lie
// inside the stack/heap block or the alias region
// (write watch); otherwise returns UINT64_MAX
uint64_t mem_written_peek(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max);
// the same without the reset: the bits stay for the next
// mem_written_pages (verify checks only)
int mem_freeze(uint64_t a); // makes the 2 MiB region at a (2 MiB aligned, watched,
// committed PAGE_READWRITE) read-only until a write or
// mem_thaw thaws it; 1 frozen (or already), 0 refused
int mem_frozen(uint64_t a); // the 2 MiB region of a is frozen: no page in it changed
// since its freeze
void mem_thaw(uint64_t a, uint64_t n); // makes the frozen regions over [a, a+n) writable again;
// before a kernel write (file read) into guest memory
void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws);
// regions frozen now; freezes and thaws since the last call
void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit
// ---- modules (loader.c, T1)
@@ -155,6 +178,14 @@ void kern_core_back(int *away);
void kern_checkpoint(void); // preemption point, at every SVC and indirect call
// A preempt request is pending for the thread (Cpu.thread): its RtThread starts with the flag (kernel.c).
static inline int kern_preempt_pending(const void *thread) { return __atomic_load_n((const int *)thread, __ATOMIC_RELAXED); }
// Set once before guest threads start (TOTK_CORE_DIAG, TOTK_ASYNC_PREEMPT): host calls (hostfn.c) and SVCs (svc.c) count their depth in
// the thread's second int, so the kernel knows whether a core owner runs game code at the top level.
extern int kern_host_track;
static inline int *kern_host_depth(void *thread) { return (int *)thread + 1; }
void kern_host_left(void *thread); // back at depth 0 with a preempt request pending
int kern_core_diag_on(void); // TOTK_CORE_DIAG
void kern_core_diag_frame(uint64_t frame, uint64_t interval_ns); // perf_present, every present (also logs the
// asynchronous preemption counters)
void kern_sched_report(uint64_t frame); // TOTK_TRACE=sched: per-thread time split, on present
uint32_t kern_handle_new(int kind, void *obj); // kind: KERN_SESSION or KERN_TMEM (ipc.c)
void *kern_handle_get(uint32_t h, int kind);
@@ -218,7 +249,7 @@ int probe_vkdevice(void); // M4 T2: vk_device.c
int probe_nvn(void), probe_nvn_tex(void); // M4 T3: nvn_probe.c
int probe_window(void); // M4 T2
int probe_shader(void); // M4 T4
int probe_vkmem(void), probe_vkexec(void); // M4 T5, T6
int probe_vkmem(void), probe_vkexec(void), probe_astc(void); // M4 T5, T6
int probe_movie(void); // movie_probe.c: host VP9 movie::Decoder on one RomFS movie
int vk_pipeline_prepare_shaders(void); // vk_pipeline.c: --prepare-shaders (tools/prepare_shaders.py)
// gfx_stubs.c defines each of these weakly: prints "probe-x: not built yet", returns 2.
+16
View File
@@ -108,6 +108,18 @@ __attribute__((noinline, noreturn, cold)) static void bad_entry(Cpu *c) {
ldr_describe(c->x[30], lr, sizeof lr));
}
// 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.
__attribute__((noinline)) static void hostfn_tracked(Cpu *c, GuestFn fn) {
void *t = c->thread;
int *d = kern_host_depth(t);
__atomic_store_n(d, *d + 1, __ATOMIC_RELAXED);
fn(c);
int n = *d - 1;
__atomic_store_n(d, n, __ATOMIC_RELAXED);
if (!n && kern_preempt_pending(t)) kern_host_left(t);
}
static void hostfn_entry(Cpu *c) {
uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4;
if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c);
@@ -117,6 +129,10 @@ static void hostfn_entry(Cpu *c) {
char lr[96];
rt_log("host call %s from %s", s->name, ldr_describe(c->x[30] - 4, lr, sizeof lr));
}
if (__builtin_expect(kern_host_track && c->thread, 0)) {
hostfn_tracked(c, s->fn);
return;
}
__attribute__((musttail)) return s->fn(c);
}
+1
View File
@@ -24,6 +24,7 @@ int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly);
// releases the cursor around an F11 window change, (0) captures it again once the client area has its new size.
#ifdef _WIN32
int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res);
int input_cursor_released(void); // Esc or a non-client activation released the cursor (probe)
// The menu's Win32 adapter (ui/menu_win.cpp), called next: F1 / Esc open or close the menu; while it is open, keys,
// mouse and focus go to it. Returns 1 with *res set when the message is fully handled.
int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res);
+124 -11
View File
@@ -1,22 +1,135 @@
// Binding table and the pure mapping functions (input_map.h). No Win32 or SDL here.
// Binding tables (keyboard/mouse and gamepad), their totk.ini overrides and the pure mapping functions (input_map.h).
// No Win32 or SDL here.
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "input_map.h"
#define STICK_MAX 32767
#define DIAGONAL 23170 // STICK_MAX / sqrt(2)
#define MOUSE_FULL_CPS 3000.0
const Binding input_bindings[] = {
{KEY_W, B_SL_UP}, {KEY_A, B_SL_LEFT}, {KEY_S, B_SL_DOWN}, {KEY_D, B_SL_RIGHT}, // move
{KEY_UP, B_SR_UP}, {KEY_LEFT, B_SR_LEFT}, {KEY_DOWN, B_SR_DOWN}, {KEY_RIGHT, B_SR_RIGHT}, // camera, second source
{KEY_MOUSE_L, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR},
{KEY_SPACE, B_X}, {KEY_LSHIFT, B_B}, {KEY_E, B_A}, {KEY_Q, B_L}, {KEY_R, B_R},
{KEY_LCTRL, B_STICK_L}, {KEY_C, B_STICK_R},
{KEY_1, B_LEFT}, {KEY_2, B_UP}, {KEY_3, B_RIGHT}, {KEY_4, B_DOWN},
{KEY_TAB, B_PLUS}, {KEY_M, B_MINUS},
};
const int input_binding_count = (int)(sizeof input_bindings / sizeof *input_bindings);
#define DEFAULT_KEYS \
{KEY_W, B_SL_UP}, {KEY_A, B_SL_LEFT}, {KEY_S, B_SL_DOWN}, {KEY_D, B_SL_RIGHT}, /* move */ \
{KEY_UP, B_SR_UP}, {KEY_LEFT, B_SR_LEFT}, {KEY_DOWN, B_SR_DOWN}, {KEY_RIGHT, B_SR_RIGHT}, /* camera, second source */ \
{KEY_MOUSE_L, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR}, \
{KEY_SPACE, B_X}, {KEY_LSHIFT, B_B}, {KEY_E, B_A}, {KEY_Q, B_L}, {KEY_R, B_R}, \
{KEY_LCTRL, B_STICK_L}, {KEY_C, B_STICK_R}, \
{KEY_1, B_LEFT}, {KEY_2, B_UP}, {KEY_3, B_RIGHT}, {KEY_4, B_DOWN}, \
{KEY_TAB, B_PLUS}, {KEY_M, B_MINUS}
// By position: south is Switch B, east A, west Y, north X (Nintendo layout); the triggers are ZL and ZR.
#define DEFAULT_PADS \
{PAD_SOUTH, B_B}, {PAD_EAST, B_A}, {PAD_WEST, B_Y}, {PAD_NORTH, B_X}, {PAD_LSTICK, B_STICK_L}, {PAD_RSTICK, B_STICK_R}, \
{PAD_L, B_L}, {PAD_R, B_R}, {PAD_START, B_PLUS}, {PAD_BACK, B_MINUS}, {PAD_DPAD_LEFT, B_LEFT}, {PAD_DPAD_UP, B_UP}, \
{PAD_DPAD_RIGHT, B_RIGHT}, {PAD_DPAD_DOWN, B_DOWN}, {PAD_ZL, B_ZL}, {PAD_ZR, B_ZR}
static const Binding default_keys[] = {DEFAULT_KEYS};
static const PadBinding default_pads[] = {DEFAULT_PADS};
Binding input_bindings[INPUT_BINDING_MAX] = {DEFAULT_KEYS};
int input_binding_count = (int)(sizeof default_keys / sizeof *default_keys);
PadBinding input_pad_bindings[INPUT_PAD_BINDING_MAX] = {DEFAULT_PADS};
int input_pad_binding_count = (int)(sizeof default_pads / sizeof *default_pads);
static const char *const button_names[16] = {"A", "B", "X", "Y", "LCLICK", "RCLICK", "L", "R", "ZL", "ZR", "PLUS", "MINUS",
"LEFT", "UP", "RIGHT", "DOWN"}; // indexed by action
static const char *const pad_names[PAD_COUNT] = {
NULL, "south", "east", "west", "north", "lstick", "rstick", "l", "r", "start", "back",
"dpad_left", "dpad_up", "dpad_right", "dpad_down", "zl", "zr"};
static const char *const key_names[KEY_COUNT] = { // letters and digits are handled in key_id
[KEY_SPACE] = "space", [KEY_TAB] = "tab", [KEY_LSHIFT] = "lshift", [KEY_LCTRL] = "lctrl", [KEY_UP] = "up",
[KEY_DOWN] = "down", [KEY_LEFT] = "left", [KEY_RIGHT] = "right", [KEY_MOUSE_L] = "mouse_l", [KEY_MOUSE_R] = "mouse_r",
[KEY_MOUSE_M] = "mouse_m"};
void input_map_reset(void) {
memcpy(input_bindings, default_keys, sizeof default_keys);
input_binding_count = (int)(sizeof default_keys / sizeof *default_keys);
memcpy(input_pad_bindings, default_pads, sizeof default_pads);
input_pad_binding_count = (int)(sizeof default_pads / sizeof *default_pads);
}
static int key_id(const char *name) {
if (name[0] && !name[1]) {
int c = tolower((unsigned char)name[0]);
if (c >= 'a' && c <= 'z') return KEY_A + (c - 'a');
if (c >= '0' && c <= '9') return KEY_0 + (c - '0');
}
for (int k = KEY_SPACE; k < KEY_COUNT; k++)
if (key_names[k] && !_stricmp(key_names[k], name)) return k;
return KEY_NONE;
}
static int pad_id(const char *name) {
for (int p = PAD_SOUTH; p < PAD_COUNT; p++)
if (!_stricmp(pad_names[p], name)) return p;
return PAD_NONE;
}
static char *trim(char *s) {
while (*s == ' ' || *s == '\t') s++;
char *e = s + strlen(s);
while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\r')) *--e = 0;
return s;
}
int input_map_bind(const char *button, const char *list) {
int action = -1;
for (int a = 0; a < 16; a++)
if (!_stricmp(button_names[a], button)) action = a;
if (action < 0) { rt_log("input: bind: unknown button '%s'", button); return 0; }
Binding keys[INPUT_BINDING_MAX];
PadBinding pads[INPUT_PAD_BINDING_MAX];
int nk = 0, np = 0, none = 0, bad = 0;
char buf[256];
snprintf(buf, sizeof buf, "%s", list);
for (char *save = NULL, *tok = strtok_s(buf, ",", &save); tok; tok = strtok_s(NULL, ",", &save)) {
tok = trim(tok);
if (!_stricmp(tok, "none")) { none = 1; continue; }
int ok = 0;
if (!_strnicmp(tok, "pad:", 4)) {
int p = pad_id(trim(tok + 4));
if (p && np < INPUT_PAD_BINDING_MAX) pads[np++] = (PadBinding){(uint8_t)p, (uint8_t)action}, ok = 1;
} else if (!_strnicmp(tok, "key:", 4)) {
int k = key_id(trim(tok + 4));
if (k && nk < INPUT_BINDING_MAX) keys[nk++] = (Binding){(uint8_t)k, (uint8_t)action}, ok = 1;
}
if (!ok) { bad++; rt_log("input: bind %s: ignoring '%s' (expected pad:<name> or key:<name>)", button, tok); }
}
if (!nk && !np && !none) { if (bad) rt_log("input: bind %s: no valid entry, keeping the defaults", button); return 0; }
int w = 0;
for (int i = 0; i < input_binding_count; i++)
if (input_bindings[i].action != action) input_bindings[w++] = input_bindings[i];
input_binding_count = w;
for (int i = 0; i < nk && input_binding_count < INPUT_BINDING_MAX; i++) input_bindings[input_binding_count++] = keys[i];
w = 0;
for (int i = 0; i < input_pad_binding_count; i++)
if (input_pad_bindings[i].action != action) input_pad_bindings[w++] = input_pad_bindings[i];
input_pad_binding_count = w;
for (int i = 0; i < np && input_pad_binding_count < INPUT_PAD_BINDING_MAX; i++)
input_pad_bindings[input_pad_binding_count++] = pads[i];
return 1;
}
void input_map_configure(void) {
input_map_reset();
for (int a = 0; a < 16; a++) {
char var[32];
snprintf(var, sizeof var, "TOTK_BIND_%s", button_names[a]);
const char *v = cfg_get(var);
if (v && input_map_bind(button_names[a], v)) rt_log("input: %s = %s", var + 5, v);
}
}
uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT]) {
uint64_t b = 0;
for (int i = 0; i < input_pad_binding_count; i++)
if (down[input_pad_bindings[i].pad]) b |= 1ull << input_pad_bindings[i].action;
return b;
}
uint64_t input_map_buttons(const uint8_t down[KEY_COUNT]) {
uint64_t b = 0;
+27 -2
View File
@@ -22,9 +22,34 @@ enum {
KEY_COUNT
};
// Gamepad inputs by position (Nintendo layout, as SDL names them), platform-neutral like the key ids. ZL and ZR are the
// analog triggers; input_win.c translates SDL buttons and axes to these (one place).
enum {
PAD_NONE = 0,
PAD_SOUTH, PAD_EAST, PAD_WEST, PAD_NORTH, PAD_LSTICK, PAD_RSTICK, PAD_L, PAD_R, PAD_START, PAD_BACK,
PAD_DPAD_LEFT, PAD_DPAD_UP, PAD_DPAD_RIGHT, PAD_DPAD_DOWN, PAD_ZL, PAD_ZR,
PAD_COUNT
};
enum { INPUT_BINDING_MAX = 96, INPUT_PAD_BINDING_MAX = 48 };
// The tables a remapping page edits. Defaults are the layout in docs/controls.md; input_map_reset restores them.
typedef struct { uint8_t key; uint8_t action; } Binding;
extern const Binding input_bindings[];
extern const int input_binding_count;
typedef struct { uint8_t pad; uint8_t action; } PadBinding;
extern Binding input_bindings[INPUT_BINDING_MAX];
extern int input_binding_count;
extern PadBinding input_pad_bindings[INPUT_PAD_BINDING_MAX];
extern int input_pad_binding_count;
void input_map_reset(void);
// Replaces every binding of the Switch button `button` (script name: A B X Y LCLICK RCLICK L R ZL ZR PLUS MINUS LEFT UP
// RIGHT DOWN, any case) with `list`: comma separated "pad:<name>" and "key:<name>" entries, or "none" for no input.
// Invalid entries are logged and skipped; when none is valid the button keeps its bindings. 1 when applied.
int input_map_bind(const char *button, const char *list);
// Applies the TOTK_BIND_<button> settings (cfg_get) over the defaults. Once, at start.
void input_map_configure(void);
// down[pad] != 0 for the gamepad inputs held. Returns the Switch button bits (0..15) the pad table presses.
uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT]);
// down[key] != 0 for the keys held. Returns the Switch button bits (0..15) the table presses.
uint64_t input_map_buttons(const uint8_t down[KEY_COUNT]);
+32 -15
View File
@@ -1,6 +1,6 @@
// Host input for nn::hid (runtime/ov_input.c): an SDL3 gamepad (no SDL window, no SDL video), the keyboard and the mouse,
// merged into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). The key bindings are
// the table in input_map.c; this file translates Win32 virtual keys to its key ids, reads the mouse (raw input, WM_INPUT)
// merged into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). The key and pad bindings
// are the tables in input_map.c (totk.ini overrides them); this file translates Win32 virtual keys to its key ids, reads the mouse (raw input, WM_INPUT)
// and owns the cursor capture. Keys and their map: docs/controls.md.
// The gamepad also rumbles (input_rumble) and, when it has a gyro and an accelerometer, feeds the six-axis sensor from a
// thread of its own (input_motion_start). Every SDL call on the gamepad is made under `lock`.
@@ -30,13 +30,14 @@ static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thr
static int sensor_ok; // ... and it has both sensors, enabled
static int motion_thread; // 0 not started, 1 running, -1 off (TOTK_MOTION=0, no SDL)
// Gamepad buttons by position: south is Switch B, east A, west Y, north X (Nintendo layout).
static const struct { SDL_GamepadButton sdl; int bit; } pad_buttons[] = {
{SDL_GAMEPAD_BUTTON_SOUTH, B_B}, {SDL_GAMEPAD_BUTTON_EAST, B_A}, {SDL_GAMEPAD_BUTTON_WEST, B_Y},
{SDL_GAMEPAD_BUTTON_NORTH, B_X}, {SDL_GAMEPAD_BUTTON_LEFT_STICK, B_STICK_L}, {SDL_GAMEPAD_BUTTON_RIGHT_STICK, B_STICK_R},
{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, B_L}, {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, B_R}, {SDL_GAMEPAD_BUTTON_START, B_PLUS},
{SDL_GAMEPAD_BUTTON_BACK, B_MINUS}, {SDL_GAMEPAD_BUTTON_DPAD_LEFT, B_LEFT}, {SDL_GAMEPAD_BUTTON_DPAD_UP, B_UP},
{SDL_GAMEPAD_BUTTON_DPAD_RIGHT, B_RIGHT}, {SDL_GAMEPAD_BUTTON_DPAD_DOWN, B_DOWN},
// SDL buttons of the gamepad inputs (input_map.h PAD_*), by position; PAD_ZL and PAD_ZR are the triggers (axes).
static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
[PAD_SOUTH] = SDL_GAMEPAD_BUTTON_SOUTH, [PAD_EAST] = SDL_GAMEPAD_BUTTON_EAST, [PAD_WEST] = SDL_GAMEPAD_BUTTON_WEST,
[PAD_NORTH] = SDL_GAMEPAD_BUTTON_NORTH, [PAD_LSTICK] = SDL_GAMEPAD_BUTTON_LEFT_STICK,
[PAD_RSTICK] = SDL_GAMEPAD_BUTTON_RIGHT_STICK, [PAD_L] = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
[PAD_R] = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, [PAD_START] = SDL_GAMEPAD_BUTTON_START, [PAD_BACK] = SDL_GAMEPAD_BUTTON_BACK,
[PAD_DPAD_LEFT] = SDL_GAMEPAD_BUTTON_DPAD_LEFT, [PAD_DPAD_UP] = SDL_GAMEPAD_BUTTON_DPAD_UP,
[PAD_DPAD_RIGHT] = SDL_GAMEPAD_BUTTON_DPAD_RIGHT, [PAD_DPAD_DOWN] = SDL_GAMEPAD_BUTTON_DPAD_DOWN,
};
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]" or "<frame> LSTICK|RSTICK <x> <y> [hold]", ORed into the real input.
@@ -190,6 +191,7 @@ static int key_vk(int key) {
static volatile LONG mouse_dx, mouse_dy; // raw counts since the last frame sample
static volatile LONG cur_captured; // cursor hidden and confined to the client area
static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's application is active
static volatile LONG cur_released; // Esc, or activation by a click on the title bar or border: until a client click
static volatile LONG cur_suspend; // F11 or a modal size or move loop under way
static SRWLOCK cur_lock = SRWLOCK_INIT; // serialises cursor_update (window thread, input_block callers)
static volatile LONG g_block; // text entry state, see input_block
@@ -214,7 +216,7 @@ static float mouse_sens_get(void) {
static void cursor_update(HWND h) {
const char *in = cfg_get("TOTK_INPUT");
AcquireSRWLockExclusive(&cur_lock);
int want = h && cur_active && !cur_suspend && g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 &&
int want = h && cur_active && !cur_released && !cur_suspend && g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 &&
!(in && !strcmp(in, "none")) && GetForegroundWindow() == h && !IsIconic(h);
RECT r;
POINT tl = {0, 0};
@@ -230,6 +232,8 @@ static void cursor_update(HWND h) {
ReleaseSRWLockExclusive(&cur_lock);
}
int input_cursor_released(void) { return cur_released != 0; }
void input_cursor_suspend(int on) {
InterlockedExchange(&cur_suspend, on != 0);
cursor_update(gfx_window_handle());
@@ -256,7 +260,17 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) {
case WM_SETCURSOR:
if (cur_captured && LOWORD(l) == HTCLIENT) { SetCursor(NULL); *res = TRUE; return 1; }
break;
case WM_KEYDOWN: // Esc is unbound in the game layout, so it only releases the cursor
if (w == VK_ESCAPE) { InterlockedExchange(&cur_released, 1); cursor_update(h); }
break;
case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: // client area only; poll_merged drops the click
if (cur_released) { InterlockedExchange(&cur_released, 0); cursor_update(h); }
break;
case WM_MOUSEACTIVATE: // sent before WM_ACTIVATEAPP: a title bar or border click must not capture
if (LOWORD(l) != HTCLIENT) InterlockedExchange(&cur_released, 1);
break;
case WM_ACTIVATEAPP:
if (!w) InterlockedExchange(&cur_released, 0); // Alt+Tab back in captures again
InterlockedExchange(&cur_active, w != 0);
cursor_update(h);
break;
@@ -329,6 +343,7 @@ static void scan_pads(void) {
}
static void start(void) {
input_map_configure();
const char *e = cfg_get("TOTK_INPUT");
if (e && !strcmp(e, "none")) { state = -1; rt_log("input: disabled (TOTK_INPUT=none)"); return; }
if (!SDL_Init(SDL_INIT_GAMEPAD)) { state = -1; rt_log("input: SDL gamepad init failed (%s), keyboard only", SDL_GetError()); }
@@ -352,15 +367,17 @@ static void pick(int32_t *x, int32_t *y, int32_t kx, int32_t ky) {
if (b > a) { *x = kx; *y = ky; }
}
// Under lock, state 1: the open gamepad's buttons (Switch bits) and sticks; 0 without a connected gamepad.
// Under lock, state 1: the open gamepad's buttons (Switch bits after the TOTK_BIND_* layout) and sticks; 0 without a
// connected gamepad.
static int pad_read(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
if ((!pad || !SDL_GamepadConnected(pad)) && GetTickCount64() >= next_scan) { next_scan = GetTickCount64() + RESCAN_MS; scan_pads(); }
SDL_UpdateGamepads();
if (!pad || !SDL_GamepadConnected(pad)) return 0;
for (size_t i = 0; i < sizeof pad_buttons / sizeof *pad_buttons; i++)
if (SDL_GetGamepadButton(pad, pad_buttons[i].sdl)) *b |= 1ull << pad_buttons[i].bit;
if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON) *b |= 1ull << B_ZL;
if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON) *b |= 1ull << B_ZR;
uint8_t pd[PAD_COUNT] = {0};
for (int p = PAD_SOUTH; p < PAD_ZL; p++) pd[p] = SDL_GetGamepadButton(pad, pad_sdl[p]);
pd[PAD_ZL] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON;
pd[PAD_ZR] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON;
*b |= input_map_pad_buttons(pd);
sl[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX));
sl[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY));
sr[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX));
+599 -15
View File
@@ -1,6 +1,7 @@
// Adapted from MacNativeGames games/totk/runtime/threads.c @ b75be8c (MIT, Copyright (c) 2026 MacNativeGames
// contributors; itself from d2r-mobile dm/host/hle_thread.c), d2r-mobile dm/host/thread.c @ 53e7c5b (MIT, Copyright
// (c) 2026 jhaizhou) and LAS Recomp runtime/kernel.c @ da35b83 (MIT, Copyright (c) 2026 Peiikai).
// (c) 2026 jhaizhou) and LAS Recomp runtime/kernel.c @ da35b83 (MIT, Copyright (c) 2026 Peiikai); the preemption of
// virtual cores from MacNativeGames threads.c @ 33e8fa8 (MIT, as above).
// Threads, synchronization, virtual cores and fibers of the Horizon kernel.
//
// One guest thread = one Windows thread with a 256 MiB reserved host stack (every guest BL is a host call) and its own
@@ -53,6 +54,7 @@ typedef struct Fiber Fiber;
struct RtThread {
int preempt; // first member: rt_dispatch reads it (host.h kern_preempt_pending)
int host_depth; // second member: host calls and SVCs in progress, while kern_host_track (host.h)
uint32_t handle;
uint64_t id;
Cpu *cpu; // the thread's own context
@@ -86,12 +88,28 @@ struct RtThread {
int st; // S_* state since st_since
uint64_t st_since, st_ns[S_N], nblk, npre, pre_since, cpu_last;
uint64_t wobj; // what the current wait is on: owner handle, key, address or first handle
RtThread *target; // as a core waiter: the owner core_kick asked to give its core up for it (holding G)
// TOTK_ASYNC_PREEMPT (holding G)
HANDLE hthr; // the host thread, for SuspendThread / Get/SetThreadContext; open while it may own a core
uint64_t ap_rip; // the interrupted host rip kern_ap_stub returns to
uint64_t ap_last; // the last redirect attempt on this owner
int ap_pending; // redirected to kern_ap_stub, which has not reached kern_ap_yield yet
// TOTK_CORE_DIAG (holding G, except dg_host_end, which only the thread itself writes)
uint64_t dg_since; // as a waiter: core wait start
int dg_cls; // as a waiter: DG_* class at the start of the wait
uint64_t dg_kick; // as an owner: the preempt request
uint64_t dg_host_end; // as an owner: the return from the host call or SVC it was in at the request
uint64_t dg_pc, dg_lr; // as an owner: guest pc and lr at the checkpoint that gave the core up
int dg_host_kick; // as an owner: in a host call or SVC at the request
struct DgRel { uint64_t kick, host_end, pc, lr; int host, pre, prio; uint32_t h; } dg_rel; // as a waiter: the
// owner that released
};
static SRWLOCK G = SRWLOCK_INIT;
static struct { int type; void *obj; } g_h[MAX_H];
static _Thread_local RtThread *tcur;
_Static_assert(offsetof(RtThread, preempt) == 0, "host.h kern_preempt_pending reads the first int");
_Static_assert(offsetof(RtThread, host_depth) == sizeof(int), "host.h kern_host_depth reads the second int");
static RtThread *g_all, *g_wait, *g_main;
static uint64_t g_next_id = 0x51, g_wseq, g_created;
static uint64_t g_main_stack;
@@ -103,7 +121,7 @@ uint64_t kern_now_ns(void) { return plat_now_ns(); }
// ---- TOTK_TRACE=sched accounting (holding G)
#define RW_N 32
static RtThread *g_render; // the thread that presents
static RtThread *g_render; // the thread that presents (sched trace, core diag)
static struct { int st; uint64_t obj, ns; } g_rw[RW_N]; // the render thread's waits by object
static uint64_t g_pre_req, g_pre_n, g_pre_ns, g_pre_max, g_pre_stale; // preempt requests; releases after one and their latency
static uint64_t g_sync_calls, g_sync_blocks; // WaitSynchronization calls and the blocks inside them
@@ -243,32 +261,485 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) {
static RtThread *g_core_owner[NCORE], *g_core_wait;
static int g_cores_on = -1, g_held_max;
static void diag_init(void);
static void ap_init(void);
static int cores_enabled(void) {
if (g_cores_on < 0) g_cores_on = cfg_get("TOTK_FREE_CORES") ? 0 : 1;
if (g_cores_on < 0) {
g_cores_on = cfg_get("TOTK_FREE_CORES") ? 0 : 1;
diag_init();
ap_init();
}
return g_cores_on;
}
static int core_allowed(const RtThread *t, int c) { return (int)((t->mask >> c) & 1); }
// ---- asynchronous preemption (TOTK_ASYNC_PREEMPT, on unless 0)
// Adapted from MacNativeGames games/totk/runtime/threads.c @ 33e8fa8 (8bf0893, PR #488; MIT, Copyright (c) 2026
// MacNativeGames contributors): Horizon gives a core to a strictly better thread the moment that thread is ready.
// Here a better waiter only marks the owner, which gives the core up at its next SVC or indirect call; an owner in a
// stretch of game code with neither keeps it (TOTK_CORE_DIAG: 0.5-1.1 ms of thread-time per frame, waits to 16 ms and
// more, build/v121-preempt/NOTES.md). With it on, three parts, all of which MacNativeGames found necessary:
// - The waiter interrupts the owner (SuspendThread, GetThreadContext) when it has waited AP_FIRST_NS, then every
// millisecond: if the owner's host rip is in the lifted modules' code (g_gen_lo..g_gen_hi) and its host depth is 0
// (not in a host call or SVC, nor in game code a host call made), it holds no host lock, so its rip is moved to
// kern_ap_stub, which saves every volatile register, gives the core up like a checkpoint (kern_ap_yield) and jumps
// back. Otherwise it is only marked, as before.
// - Priority inheritance (eff_prio): a thread owning a guest mutex counts at the priority of the best thread blocked on
// it, for granting, kicking, preempting and yielding. Without it MacNativeGames' game stopped at its first
// "Continue" (a better thread took the core of the low-priority owner of a mutex it then waited for).
// - Horizon's preemption disable: the SDK's spin locks (1.2.1 sdk f_70051242a8, f_7005124360 and siblings) count
// TLR+0x100 (disable_count) while they try or hold the lock, and after clearing it call SVC 0x36 when TLR+0x102
// (interrupt_flag) is set. Such an owner is not preempted, at a checkpoint either: interrupt_flag is set and the
// switch happens at that SVC. Preempting it anyway hung MacNativeGames' game at "Continue" (the lock holder lost its
// core to a thread that then spun on that lock).
// TOTK_ASYNC_PREEMPT=0 keeps the old scheduling exactly: raw priorities, no interruption, no disable_count; =2 keeps
// inheritance and the preemption disable without interrupting owners.
#define AP_FIRST_NS 250000ull // the waiter spins this long before the first interruption (most waits end sooner)
#define AP_GAP_NS 1000000ull // and interrupts the same owner at most this often
static int g_ap = -1, g_ap_int; // g_ap_int: interrupt owners (TOTK_ASYNC_PREEMPT=2: inheritance and disable only)
static int g_ap_log; // redirects logged so far (AP_LOG_N)
#define AP_LOG_N 40
static uintptr_t g_gen_lo, g_gen_hi; // host code of the lifted modules: first and last function start
static uint64_t g_ap_sent, g_ap_taken, g_ap_host, g_ap_pc, g_ap_deferred, g_ap_fail, g_ap_spin_ok;
void kern_ap_stub(void);
static void ap_init(void) {
if (g_ap >= 0) return;
const char *e = cfg_get("TOTK_ASYNC_PREEMPT");
g_ap = !(e && !strcmp(e, "0"));
g_ap_int = g_ap && !(e && !strcmp(e, "2"));
if (g_ap) {
RtModule m[16];
int n = rt_linked_modules(m, 16);
uintptr_t lo = UINTPTR_MAX, hi = 0;
for (int i = 0; i < n; i++)
for (uint64_t k = 0; k < m[i].count; k++) {
uintptr_t f = (uintptr_t)m[i].fns[k];
if (f < lo) lo = f;
if (f > hi) hi = f;
}
const uintptr_t host[] = {(uintptr_t)kern_ap_stub, (uintptr_t)kern_checkpoint, (uintptr_t)rt_svc,
(uintptr_t)rt_dispatch, (uintptr_t)rt_call};
int bad = !n || lo >= hi;
for (unsigned i = 0; i < sizeof host / sizeof *host; i++) bad |= host[i] >= lo && host[i] <= hi;
if (bad) {
rt_log("kernel: asynchronous preemption off: the lifted code range %#llx-%#llx holds host code",
(unsigned long long)lo, (unsigned long long)hi);
g_ap = 0;
} else {
g_gen_lo = lo;
g_gen_hi = hi;
kern_host_track = 1;
}
}
rt_log("kernel: asynchronous preemption of virtual cores %s, priority inheritance and preemption disable %s; lifted "
"code exe+%#llx-exe+%#llx", g_ap_int ? "on" : "off", g_ap ? "on (TOTK_ASYNC_PREEMPT=0 turns all off)" : "off",
(unsigned long long)(g_gen_lo ? g_gen_lo - (uintptr_t)GetModuleHandleW(NULL) : 0),
(unsigned long long)(g_gen_hi ? g_gen_hi - (uintptr_t)GetModuleHandleW(NULL) : 0));
}
// Horizon's priority inheritance: o counts at the priority of the best thread blocked on a guest mutex it owns (the
// owner is read from the mutex word, so it follows hand-offs). Holding G.
static int eff_prio(const RtThread *o) {
int p = o->prio;
for (const RtThread *w = g_wait; w; w = w->next)
if (w->wkind == W_LOCK && w->prio < p && w->waddr &&
(__atomic_load_n((const uint32_t *)(uintptr_t)w->waddr, __ATOMIC_RELAXED) & ~WAITERS_BIT) == o->handle)
p = w->prio;
return p;
}
// The priority the virtual cores go by: raw with TOTK_ASYNC_PREEMPT=0, as before.
static int prio_of(const RtThread *t) { return g_ap > 0 ? eff_prio(t) : t->prio; }
// TLR+0x100 disable_count, +0x102 interrupt_flag (any thread may read them; the flag is set from outside as the
// kernel does).
static volatile uint16_t *ap_tlr(const RtThread *t) { return (volatile uint16_t *)(t->tlr + 0x100); }
static int ap_disabled(const RtThread *t) { return g_ap > 0 && ap_tlr(t)[0]; }
// kern_ap_stub: entered in place of the interrupted instruction of top-level game code. It keeps every register the
// game code may have live (volatile GPRs, flags, YMM0-15, MXCSR; the C calls preserve the rest), gives the core up
// 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.
__asm__(".text\n"
".globl kern_ap_stub\n"
".def kern_ap_stub; .scl 2; .type 32; .endef\n"
".p2align 4\n"
"kern_ap_stub:\n"
".seh_proc kern_ap_stub\n"
" leaq -8(%rsp), %rsp\n" // not sub: the flags are still the interrupted code's until pushfq
".seh_stackalloc 8\n"
" pushfq\n"
".seh_stackalloc 8\n"
" pushq %rax\n"
".seh_pushreg %rax\n"
" pushq %rcx\n"
".seh_pushreg %rcx\n"
" pushq %rdx\n"
".seh_pushreg %rdx\n"
" pushq %r8\n"
".seh_pushreg %r8\n"
" pushq %r9\n"
".seh_pushreg %r9\n"
" pushq %r10\n"
".seh_pushreg %r10\n"
" pushq %r11\n"
".seh_pushreg %r11\n"
" pushq %rbp\n"
".seh_pushreg %rbp\n"
" movq %rsp, %rbp\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"
" stmxcsr 32(%rsp)\n"
" movl $0x1f80, 36(%rsp)\n"
" ldmxcsr 36(%rsp)\n"
" cld\n"
" vzeroupper\n"
" 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"
" movq %rbp, %rsp\n"
" popq %rbp\n"
" popq %r11\n"
" popq %r10\n"
" popq %r9\n"
" popq %r8\n"
" popq %rdx\n"
" popq %rcx\n"
" popq %rax\n"
" popfq\n"
" leaq 8(%rsp), %rsp\n"
" jmpq *-8(%rsp)\n"
".seh_endproc\n");
enum { AP_NONE, AP_RETRY, AP_SENT };
// The waiter w interrupts its target owner if that runs top-level game code (holding G; the owner is suspended only
// between SuspendThread and ResumeThread, during which nothing here takes a lock).
static int ap_try(RtThread *w) {
RtThread *o = w->target;
if (!o || o->cur_core < 0 || !o->preempt) return AP_NONE;
if (!o->hthr || o->ap_pending) return AP_RETRY;
volatile uint16_t *tl = ap_tlr(o);
if (__atomic_load_n(&o->host_depth, __ATOMIC_RELAXED) > 0) {
g_ap_host++;
return AP_RETRY;
}
if (tl[0]) { // preemption disabled: SVC 0x36 switches
tl[1] = 1;
g_ap_deferred++;
return AP_RETRY;
}
uint64_t now = kern_now_ns();
if (o->ap_last && now - o->ap_last < AP_GAP_NS) return AP_RETRY;
o->ap_last = now;
if (SuspendThread(o->hthr) == (DWORD)-1) {
g_ap_fail++;
return AP_RETRY;
}
CONTEXT ctx;
memset(&ctx, 0, sizeof ctx);
ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER; // set back whole, so the kernel restores every GPR
int r = AP_RETRY;
if (!GetThreadContext(o->hthr, &ctx))
g_ap_fail++;
else if (ctx.Rip < g_gen_lo || ctx.Rip > g_gen_hi)
g_ap_pc++;
else if (__atomic_load_n(&o->host_depth, __ATOMIC_RELAXED) > 0)
g_ap_host++;
else if (tl[0]) {
tl[1] = 1;
g_ap_deferred++;
} else {
o->ap_rip = ctx.Rip;
o->ap_pending = 1;
ctx.Rip = (DWORD64)(uintptr_t)kern_ap_stub;
if (SetThreadContext(o->hthr, &ctx)) {
g_ap_sent++;
r = AP_SENT;
} else {
o->ap_pending = 0;
g_ap_fail++;
}
}
ResumeThread(o->hthr);
if (r == AP_SENT && g_ap_log < AP_LOG_N) {
g_ap_log++;
rt_log("kernel: interrupt %#x p%d at exe+%#llx rsp %#llx for %#x p%d", o->handle, o->prio,
(unsigned long long)(o->ap_rip - (uintptr_t)GetModuleHandleW(NULL)), (unsigned long long)ctx.Rsp,
w->handle, w->prio);
}
return r;
}
// ---- TOTK_CORE_DIAG=1: why ready threads wait for a virtual core, and for how long
// Each core wait is split into classes: the owner asked to give the core up ran game code (DG_GAME, the part an
// asynchronous preemption would remove), was in a host call or SVC that kept the core (DG_HOST), every owner the waiter
// may displace was of better or equal priority (DG_EQ: Horizon waits too), or the worse owners were claimed by better
// waiters (DG_NOCORE). Per 60 presents: time per frame and a wait-length histogram per class, the owners that cost
// most; a line per present over 25 ms; a line per wait with over 2 ms of DG_GAME (where the owner reached its
// checkpoint), and one host RIP sample of the owner per such wait (exe RVA, for totk.map).
enum { DG_GAME, DG_HOST, DG_EQ, DG_NOCORE, DG_N };
static const char *const dg_name[DG_N] = {"game", "host", "eq", "nocore"};
#define DG_NB 8 // wait lengths: < 0.05, 0.25, 1, 2, 4, 8, 16 ms, longer
static const uint64_t dg_edge[DG_NB - 1] = {50000, 250000, 1000000, 2000000, 4000000, 8000000, 16000000};
#define DG_LONG_LINES 400
#define DG_SAMPLES 600
#define DG_OWN 32
int kern_host_track;
static int g_diag = -1;
typedef struct {
uint64_t ns[DG_N], n[DG_N], hist[DG_N][DG_NB]; // this window
struct { uint32_t h; int prio, cls; uint64_t ns; } own[DG_OWN]; // this window: owners by class
uint64_t long_frames, long_game_frames;
} DgWindow;
static DgWindow g_dgw;
static uint64_t g_dgf[DG_N], g_dgfr[DG_N]; // this frame: all threads, the render thread
static uint64_t g_dg_samples, g_dg_lines, g_sync36, g_sync36_flag;
static void diag_init(void) {
if (g_diag >= 0) return;
const char *e = cfg_get("TOTK_CORE_DIAG");
g_diag = e && *e && strcmp(e, "0");
if (g_diag) {
kern_host_track = 1;
rt_log("core-diag: on (TOTK_CORE_DIAG)");
}
}
int kern_core_diag_on(void) { return g_diag > 0; }
// A tracked host call or SVC returned to depth 0 with a preempt request pending (host.h); on the thread itself.
void kern_host_left(void *thread) {
RtThread *t = thread;
if (g_diag > 0 && t->dg_kick && t->dg_host_kick && !t->dg_host_end) t->dg_host_end = kern_now_ns();
}
static int dg_in_host(const RtThread *o) { return __atomic_load_n(&o->host_depth, __ATOMIC_RELAXED) > 0; }
static void dg_add(const RtThread *w, int cls, uint64_t ns, uint32_t oh, int oprio) {
if (!ns) return;
g_dgw.ns[cls] += ns;
g_dgf[cls] += ns;
if (w == g_render) g_dgfr[cls] += ns;
if (!oh) return;
int i = 0;
for (; i < DG_OWN && g_dgw.own[i].ns; i++)
if (g_dgw.own[i].h == oh && g_dgw.own[i].cls == cls) break;
if (i == DG_OWN) return;
g_dgw.own[i].h = oh;
g_dgw.own[i].prio = oprio;
g_dgw.own[i].cls = cls;
g_dgw.own[i].ns += ns;
}
// The waiter's class at the start of its wait, after core_kick claimed (or did not claim) an owner for it.
static int dg_class0(const RtThread *t) {
if (t->target) return dg_in_host(t->target) ? DG_HOST : DG_GAME;
for (int i = 0; i < NCORE; i++)
if (core_allowed(t, i) && g_core_owner[i] && g_core_owner[i]->prio > t->prio) return DG_NOCORE;
return DG_EQ;
}
// The waiter got a core: split [dg_since, now] by what the owner that released it did after its preempt request.
static void dg_done(RtThread *t) {
uint64_t now = kern_now_ns(), t0 = t->dg_since, w = now - t0;
t->dg_since = 0;
if (!t0) return;
const struct DgRel *r = &t->dg_rel;
uint64_t part[DG_N] = {0};
uint32_t oh = t->target ? t->target->handle : r->h;
int oprio = t->target ? t->target->prio : r->prio;
if (r->pre && r->kick) {
uint64_t k = r->kick > t0 ? (r->kick < now ? r->kick : now) : t0;
part[t->dg_cls] += k - t0;
if (r->host) {
uint64_t he = r->host_end > k && r->host_end < now ? r->host_end : now;
part[DG_HOST] += he - k;
part[DG_GAME] += now - he;
} else
part[DG_GAME] += now - k;
oh = r->h;
oprio = r->prio;
} else
part[t->dg_cls] += w;
int top = 0;
for (int c = 0; c < DG_N; c++) {
dg_add(t, c, part[c], oh, oprio);
if (part[c] > part[top]) top = c;
}
int b = 0;
while (b < DG_NB - 1 && w >= dg_edge[b]) b++;
g_dgw.n[top]++;
g_dgw.hist[top][b]++;
if (part[DG_GAME] >= 2000000 && g_dg_lines < DG_LONG_LINES) {
g_dg_lines++;
char a[128], l[128];
rt_log("core-diag: wait %.2f ms (game %.2f host %.2f): %#x p%d mask %llx; released by %#x p%d%s at %s lr %s",
w / 1e6, part[DG_GAME] / 1e6, part[DG_HOST] / 1e6, t->handle, t->prio, (unsigned long long)t->mask, r->h,
r->prio, r->pre ? " (preempted)" : "", r->pc ? ldr_describe(r->pc, a, sizeof a) : "-",
r->lr ? ldr_describe(r->lr, l, sizeof l) : "-");
}
t->target = NULL;
memset(&t->dg_rel, 0, sizeof t->dg_rel);
}
// Once per long DG_GAME wait: where the owner's host thread runs (exe RVA). Holding G; the owner is resumed before
// anything that could take a lock it holds.
static void dg_sample(RtThread *t) {
RtThread *o = t->target;
if (!o || o->cur_core < 0 || !o->tid || g_dg_samples >= DG_SAMPLES) return;
HANDLE h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, FALSE, o->tid);
if (!h) return;
CONTEXT ctx;
memset(&ctx, 0, sizeof ctx);
ctx.ContextFlags = CONTEXT_CONTROL;
int depth = 0, ok = 0;
if (SuspendThread(h) != (DWORD)-1) {
ok = GetThreadContext(h, &ctx);
depth = __atomic_load_n(&o->host_depth, __ATOMIC_RELAXED);
ResumeThread(h);
}
CloseHandle(h);
if (!ok) return;
g_dg_samples++;
uintptr_t base = (uintptr_t)GetModuleHandleW(NULL);
rt_log("core-diag: sample owner %#x p%d depth %d rip exe+%#llx (waiter %#x p%d, %.2f ms)", o->handle, o->prio, depth,
(unsigned long long)(ctx.Rip - base), t->handle, t->prio, (kern_now_ns() - t->dg_since) / 1e6);
}
// perf_present, every present: a line for a present over 25 ms, the 60-present summary.
void kern_core_diag_frame(uint64_t frame, uint64_t interval_ns) {
if (g_ap > 0 && frame && frame % 600 == 0) { // TOTK_ASYNC_PREEMPT counters since the start
lock();
uint64_t v[8] = {g_ap_sent, g_ap_taken, g_ap_host, g_ap_pc, g_ap_deferred, g_ap_fail, g_ap_spin_ok, g_sync36};
unlock();
rt_log("kernel: frame %llu: asynchronous preemption: %llu interrupted, %llu cores given up there; refused: "
"%llu in a host call, %llu outside game code, %llu preemption disabled, %llu failed; %llu waits ended "
"in the first spin; SVC 0x36 %llu",
(unsigned long long)frame, (unsigned long long)v[0], (unsigned long long)v[1], (unsigned long long)v[2],
(unsigned long long)v[3], (unsigned long long)v[4], (unsigned long long)v[5], (unsigned long long)v[6],
(unsigned long long)v[7]);
}
if (g_diag <= 0) return;
DgWindow w;
uint64_t f[DG_N], fr[DG_N], s36, s36f;
lock();
g_render = tcur;
memcpy(f, g_dgf, sizeof f);
memcpy(fr, g_dgfr, sizeof fr);
memset(g_dgf, 0, sizeof g_dgf);
memset(g_dgfr, 0, sizeof g_dgfr);
int lng = interval_ns > 25000000ull;
if (lng) {
g_dgw.long_frames++;
if (f[DG_GAME] > 1000000) g_dgw.long_game_frames++;
}
int rep = frame && frame % 60 == 0;
if (rep) {
w = g_dgw;
memset(&g_dgw, 0, sizeof g_dgw);
}
s36 = g_sync36;
s36f = g_sync36_flag;
unlock();
if (lng)
rt_log("core-diag: frame %llu: %.1f ms; core wait game %.2f host %.2f eq %.2f nocore %.2f ms; render thread game "
"%.2f host %.2f eq %.2f nocore %.2f ms",
(unsigned long long)frame, interval_ns / 1e6, f[0] / 1e6, f[1] / 1e6, f[2] / 1e6, f[3] / 1e6,
fr[0] / 1e6, fr[1] / 1e6, fr[2] / 1e6, fr[3] / 1e6);
if (!rep) return;
char s[1024];
int o = 0;
for (int c = 0; c < DG_N; c++) {
o += snprintf(s + o, sizeof s - o, "%s %.3f ms/frame %llu waits [", dg_name[c], w.ns[c] / 60e6,
(unsigned long long)w.n[c]);
for (int b = 0; b < DG_NB; b++)
o += snprintf(s + o, sizeof s - o, "%s%llu", b ? " " : "", (unsigned long long)w.hist[c][b]);
o += snprintf(s + o, sizeof s - o, "]%s", c < DG_N - 1 ? "; " : "");
}
rt_log("core-diag: frames %llu-%llu: %s; long frames %llu (game wait > 1 ms in %llu); SVC 0x36 %llu (flag set %llu)",
(unsigned long long)frame - 59, (unsigned long long)frame, s, (unsigned long long)w.long_frames,
(unsigned long long)w.long_game_frames, (unsigned long long)s36, (unsigned long long)s36f);
o = 0;
for (int k = 0; k < 6; k++) {
int bi = -1;
for (int i = 0; i < DG_OWN; i++)
if (w.own[i].ns && (bi < 0 || w.own[i].ns > w.own[bi].ns)) bi = i;
if (bi < 0) break;
o += snprintf(s + o, sizeof s - o, " %#x p%d %s %.3f;", w.own[bi].h, w.own[bi].prio, dg_name[w.own[bi].cls],
w.own[bi].ns / 60e6);
w.own[bi].ns = 0;
}
if (o) rt_log("core-diag: owners (ms/frame):%s", s);
}
static void core_count(void) {
int n = 0;
for (int i = 0; i < NCORE; i++) n += g_core_owner[i] != NULL;
if (n > g_held_max) g_held_max = n;
}
static struct DgRel g_dg_rel; // TOTK_CORE_DIAG: the owner core_put is releasing, for core_grant
static void core_grant(int c) {
RtThread *b = NULL, **bp = NULL;
for (RtThread **p = &g_core_wait; *p; p = &(*p)->cnext)
if (core_allowed(*p, c) && (!b || (*p)->prio < b->prio || ((*p)->prio == b->prio && (*p)->cseq < b->cseq))) {
int bpri = 0;
for (RtThread **p = &g_core_wait; *p; p = &(*p)->cnext) {
if (!core_allowed(*p, c)) continue;
int pr = prio_of(*p);
if (!b || pr < bpri || (pr == bpri && (*p)->cseq < b->cseq)) {
b = *p;
bp = p;
bpri = pr;
}
}
if (!b) return;
*bp = b->cnext;
b->cnext = NULL;
b->cur_core = c;
g_core_owner[c] = b;
core_count();
if (g_diag > 0) b->dg_rel = g_dg_rel;
WakeConditionVariable(&b->cv);
}
@@ -283,6 +754,12 @@ static void core_put(RtThread *t) {
t->npre++;
}
t->pre_since = 0;
if (g_diag > 0) {
g_dg_rel = (struct DgRel){.kick = t->dg_kick, .host_end = t->dg_host_end, .pc = t->dg_pc, .lr = t->dg_lr,
.host = t->dg_host_kick, .pre = t->preempt, .prio = t->prio, .h = t->handle};
t->dg_kick = t->dg_host_end = t->dg_pc = t->dg_lr = 0;
t->dg_host_kick = 0;
}
t->cur_core = -1;
t->preempt = 0;
g_core_owner[c] = NULL;
@@ -291,8 +768,9 @@ static void core_put(RtThread *t) {
// Whether a waiter better than t may run on core c.
static int core_better_waiter(const RtThread *t, int c) {
int tp = prio_of(t);
for (const RtThread *w = g_core_wait; w; w = w->cnext)
if (w->prio < t->prio && core_allowed(w, c)) return 1;
if (core_allowed(w, c) && prio_of(w) < tp) return 1;
return 0;
}
@@ -302,30 +780,40 @@ static int core_better_waiter(const RtThread *t, int c) {
// again (build/v121-dpre/NOTES.md).
static void core_kick(void) {
RtThread *w[64];
int n = 0, claimed = 0;
int wp[64], op[NCORE], n = 0, claimed = 0;
for (int i = 0; i < NCORE; i++) op[i] = g_core_owner[i] ? prio_of(g_core_owner[i]) : 0;
for (RtThread *p = g_core_wait; p && n < 64; p = p->cnext) {
int i = n++;
for (; i > 0 && (w[i - 1]->prio > p->prio || (w[i - 1]->prio == p->prio && w[i - 1]->cseq > p->cseq)); i--)
int i = n++, pp = prio_of(p);
for (; i > 0 && (wp[i - 1] > pp || (wp[i - 1] == pp && w[i - 1]->cseq > p->cseq)); i--) {
w[i] = w[i - 1];
wp[i] = wp[i - 1];
}
w[i] = p;
wp[i] = pp;
}
for (int k = 0; k < n && claimed != (1 << NCORE) - 1; k++) {
int v = -1;
for (int i = 0; i < NCORE; i++) {
RtThread *o = g_core_owner[i];
if ((claimed >> i & 1) || !core_allowed(w[k], i) || !o || o->prio <= w[k]->prio) continue;
if ((claimed >> i & 1) || !core_allowed(w[k], i) || !o || op[i] <= wp[k]) continue;
RtThread *b = v < 0 ? NULL : g_core_owner[v];
if (!b || o->prio > b->prio || (o->prio == b->prio && o->preempt && !b->preempt)) v = i;
if (!b || op[i] > op[v] || (op[i] == op[v] && o->preempt && !b->preempt)) v = i;
}
if (v < 0) continue;
claimed |= 1 << v;
RtThread *o = g_core_owner[v];
w[k]->target = o;
if (o->preempt) continue;
if (rt_trace & RT_TRACE_SCHED) {
o->pre_since = kern_now_ns();
g_pre_req++;
}
__atomic_store_n(&o->preempt, 1, __ATOMIC_RELAXED);
if (g_diag > 0) {
o->dg_kick = kern_now_ns();
o->dg_host_kick = dg_in_host(o);
o->dg_host_end = 0;
}
__atomic_store_n(&o->preempt, 1, __ATOMIC_RELEASE);
}
}
@@ -351,8 +839,34 @@ static void core_take(RtThread *t) {
t->cseq = ++g_wseq;
t->cnext = g_core_wait;
g_core_wait = t;
t->target = NULL;
if (g_diag > 0) {
t->dg_since = kern_now_ns();
memset(&t->dg_rel, 0, sizeof t->dg_rel);
}
core_kick();
while (t->cur_core < 0) SleepConditionVariableSRW(&t->cv, &G, INFINITE, 0);
if (g_diag > 0) t->dg_cls = dg_class0(t);
if (g_ap_int && t->target && t->cur_core < 0) { // most waits end within microseconds: spin before interrupting
uint64_t end = kern_now_ns() + AP_FIRST_NS;
unlock();
while (__atomic_load_n(&t->cur_core, __ATOMIC_RELAXED) < 0 && kern_now_ns() < end) YieldProcessor();
lock();
if (t->cur_core >= 0) g_ap_spin_ok++;
}
int sampled = 0;
while (t->cur_core < 0) {
DWORD ms = INFINITE;
if (g_ap_int && ap_try(t) != AP_NONE) ms = 1; // the owner keeps its core: again in a millisecond
int sample = g_diag > 0 && !sampled && t->dg_cls == DG_GAME;
if (sample && ms == INFINITE) ms = 2;
SleepConditionVariableSRW(&t->cv, &G, ms, 0);
if (sample && t->cur_core < 0 && kern_now_ns() - t->dg_since >= 2000000) {
sampled = 1;
dg_sample(t);
}
}
if (g_diag > 0) dg_done(t);
t->target = NULL;
acct(t, S_RUN);
}
@@ -393,17 +907,57 @@ void kern_checkpoint(void) {
RtThread *t = tcur;
if (!t || !__atomic_load_n(&t->preempt, __ATOMIC_RELAXED)) return;
lock();
if (t->cur_core >= 0 && core_better_waiter(t, t->cur_core)) {
if (g_diag > 0 && t->cur) {
t->dg_pc = t->cur->pc;
t->dg_lr = t->cur->x[30];
}
int better = t->cur_core >= 0 && core_better_waiter(t, t->cur_core);
if (better && ap_disabled(t)) { // preemption disabled: the switch comes at SVC 0x36
ap_tlr(t)[1] = 1;
t->preempt = 0;
g_ap_deferred++;
} else if (better) {
core_put(t); // clears preempt (after the sched trace reads it)
core_take(t);
} else {
t->preempt = 0;
t->pre_since = 0;
t->dg_kick = t->dg_host_end = 0;
t->dg_host_kick = 0;
g_pre_stale++;
}
unlock();
}
// kern_ap_stub, on the interrupted owner: where to jump back to, then the core given up as at a checkpoint.
uint64_t kern_ap_rip(void) { return tcur->ap_rip; }
void kern_ap_yield(void) {
RtThread *t = tcur;
lock();
t->ap_pending = 0;
if (t->preempt && t->cur_core >= 0 && !ap_disabled(t) && core_better_waiter(t, t->cur_core)) {
if (g_diag > 0) {
t->dg_pc = t->cur->pc;
t->dg_lr = t->cur->x[30];
}
g_ap_taken++;
core_put(t);
core_take(t);
}
unlock();
}
// SVC 0x36 SynchronizePreemptionState: a preemption deferred by disable_count happens here.
static void ap_sync(RtThread *t) {
lock();
if (g_ap > 0 && t->cur_core >= 0 && core_better_waiter(t, t->cur_core)) {
core_put(t);
core_take(t);
}
unlock();
}
void kern_core_stats(int *held_now, int *held_max) {
lock();
int n = 0;
@@ -625,6 +1179,8 @@ static void thread_finish(RtThread *t) {
lock();
core_put(t);
acct(t, S_OFF);
if (t->hthr) CloseHandle(t->hthr); // no waiter interrupts a thread without a core
t->hthr = NULL;
t->exited = 1;
wake_sync_waiters();
if (t->closed) thr_free(t);
@@ -636,11 +1192,22 @@ static void thread_finish(RtThread *t) {
// 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); }
// The calling host thread, for ap_try (TOTK_ASYNC_PREEMPT only).
static HANDLE ap_open_self(void) {
if (!cores_enabled() || !g_ap_int) return NULL;
HANDLE h = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_QUERY_INFORMATION,
FALSE, GetCurrentThreadId());
if (!h) rt_log("kernel: OpenThread for asynchronous preemption failed (error %lu)", GetLastError());
return h;
}
static DWORD WINAPI thread_main(void *p) {
RtThread *t = p;
tcur = t;
t->tid = GetCurrentThreadId();
HANDLE self = ap_open_self();
lock();
t->hthr = self;
while (!t->started) SleepConditionVariableSRW(&t->cv, &G, INFINITE, 0);
core_take(t);
unlock();
@@ -683,7 +1250,9 @@ uint32_t kern_main_thread(Cpu **cpu) {
t->stack_top = stack + MAIN_STACK;
t->cpu->sp = t->stack_top;
t->tid = GetCurrentThreadId();
HANDLE self = ap_open_self();
lock();
t->hthr = self;
thr_link(t);
g_main = t;
g_main_stack = stack;
@@ -869,8 +1438,9 @@ int kern_svc(Cpu *c, uint32_t imm) {
// Yield: hand the core to a waiting thread of equal or better priority.
lock();
int give = 0;
int tp = prio_of(t);
for (RtThread *w = g_core_wait; w && !give; w = w->cnext)
give = t->cur_core >= 0 && core_allowed(w, t->cur_core) && w->prio <= t->prio;
give = t->cur_core >= 0 && core_allowed(w, t->cur_core) && prio_of(w) <= tp;
if (give) {
core_put(t);
core_take(t);
@@ -932,6 +1502,17 @@ int kern_svc(Cpu *c, uint32_t imm) {
x[0] = (uint64_t)(uint32_t)(t->cur_core >= 0 ? t->cur_core : t->core);
return 1;
}
case 0x36: { // SynchronizePreemptionState: the SDK's spin locks call it after clearing TLR+0x100 (disable_count)
// when TLR+0x102 (interrupt_flag) is set; rt_svc's checkpoint has already honoured a pending preempt
RtThread *t = cur(imm);
volatile uint16_t *flag = (volatile uint16_t *)(t->tlr + 0x102);
__atomic_fetch_add(&g_sync36, 1, __ATOMIC_RELAXED);
if (*flag) __atomic_fetch_add(&g_sync36_flag, 1, __ATOMIC_RELAXED);
*flag = 0;
ap_sync(t);
x[0] = R_OK;
return 1;
}
case 0x25: { // GetThreadId(w1 handle -> x1)
lock();
RtThread *t = h_get((uint32_t)x[1], O_THREAD);
@@ -1299,6 +1880,7 @@ static void WINAPI fiber_main(void *p) {
Fiber *f = p;
fiber_resumed();
Cpu *c = f->cpu;
tcur->host_depth = 0; // a new fiber starts at the top level; each context restores its own depth on resume
uint64_t next = rt_call(c, f->func, 1, f->arg);
f->state = F_DONE;
Fiber *n = NULL;
@@ -1335,7 +1917,9 @@ static void fiber_switch_to(RtThread *t, const Cpu *from, Fiber *to) {
t->leaving = t->fiber;
t->fiber = to;
t->cur = dst;
int depth = t->host_depth;
SwitchToFiber(to ? to->host : t->base_fiber);
t->host_depth = depth;
fiber_resumed();
}
+3 -1
View File
@@ -6,6 +6,7 @@
// --probe-kernel, --probe-ipc, --probe-services, --probe-fs task probes (T2-T5)
// --probe-gfx-abi NVN object layouts against nvn.h's reserved sizes (M4 T1); needs no game files
// --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec M4 probes
// --probe-astc the compute ASTC decoder against astc_decode_ldr; needs no game files
// "not built yet", exit 2, until their task lands
// --probe-movie host VP9 movie::Decoder on one RomFS movie; skips without game data
// --boot run the game to the first NVN call (T6)
@@ -387,7 +388,7 @@ static int usage(const char *argv0) {
fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | "
"--probe-cfg | --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-movie | --prepare-shaders | --boot |--frames N [--shot PNG]]\n", argv0);
"--probe-vkexec | --probe-astc | --probe-movie | --prepare-shaders | --boot |--frames N [--shot PNG]]\n", argv0);
return 2;
}
@@ -484,6 +485,7 @@ int main(int argc, char **argv) {
if (!strcmp(mode, "--probe-shader")) return probe_shader();
if (!strcmp(mode, "--probe-vkmem")) return probe_vkmem();
if (!strcmp(mode, "--probe-vkexec")) return probe_vkexec();
if (!strcmp(mode, "--probe-astc")) return probe_astc();
if (!strcmp(mode, "--probe-vkdevice")) return probe_vkdevice();
if (!strcmp(mode, "--probe-movie")) return probe_movie();
if (!strcmp(mode, "--prepare-shaders")) return vk_pipeline_prepare_shaders();
+136 -10
View File
@@ -67,21 +67,142 @@ static void *reserve_at(uint64_t at, uint64_t size, DWORD extra) {
return p;
}
// ---- frozen regions
// A 2 MiB region of the watched reservations that nothing wrote for a while can be made read-only (mem_freeze), so the
// back end's write-watch queries skip it: no page of a frozen region changes before a thaw. The first write faults and
// thaw_handler makes the region writable again before the write retries. A kernel write into guest memory (a file
// read) fails on a read-only page instead of faulting, so file reads thaw their buffer first (mem_thaw), and so does
// every commit, decommit and protection change here. A region stays thawed for FRZ_HOLD_MS after a mem_thaw: a file
// read still in flight writes its buffer later. frz_lock is a leaf lock: nothing writes guest memory under it.
#define FRZ_SHIFT 21
#define FRZ_SIZE (1ull << FRZ_SHIFT)
#define FRZ_HOLD_MS 5000u
static volatile uint8_t frz[RT_ADDRESS_LIMIT >> FRZ_SHIFT];
static uint32_t thaw_ms[RT_ADDRESS_LIMIT >> FRZ_SHIFT]; // frz_lock: GetTickCount of the last thaw request, 0 never
static volatile LONG nfrz;
static SRWLOCK frz_lock = SRWLOCK_INIT;
static volatile LONG64 frz_count[4]; // freezes; thaws by a write fault, by mem_thaw, by a mapping change
static int watched(uint64_t addr, uint64_t size) {
const RtGuestLayout *g = &L;
return (addr >= g->aslr.base && size <= LOW_SIZE && addr - g->aslr.base <= LOW_SIZE - size) ||
(addr >= g->alias.base && size <= ALIAS_SIZE && addr - g->alias.base <= ALIAS_SIZE - size);
}
// frz_lock held exclusively. why: 1 a write fault, 2 mem_thaw (holds the region thawed), 3 a mapping change here.
static void thaw_locked(uint64_t r, int why) {
if (why == 2) thaw_ms[r] = (uint32_t)GetTickCount64() | 1;
if (!frz[r]) return;
DWORD old;
uint64_t a = r << FRZ_SHIFT, end = a + FRZ_SIZE;
if (!VirtualProtect((void *)(uintptr_t)a, FRZ_SIZE, PAGE_READWRITE, &old)) {
// Not all committed (cannot happen while mapping changes thaw under frz_lock): the read-only runs only.
MEMORY_BASIC_INFORMATION mbi;
while (a < end && VirtualQuery((void *)(uintptr_t)a, &mbi, sizeof mbi)) {
uint64_t b = (uint64_t)(uintptr_t)mbi.BaseAddress + mbi.RegionSize;
if (b > end) b = end;
if (mbi.State == MEM_COMMIT && mbi.Protect == PAGE_READONLY &&
!VirtualProtect((void *)(uintptr_t)a, b - a, PAGE_READWRITE, &old))
rt_fatal("memory: thawing %#llx+%#llx failed (error %lu)", (unsigned long long)a,
(unsigned long long)(b - a), GetLastError());
a = b;
}
}
__atomic_store_n(&frz[r], 0, __ATOMIC_RELEASE);
InterlockedDecrement(&nfrz);
InterlockedIncrement64(&frz_count[why]);
}
// frz_lock held exclusively.
static void thaw_range_locked(uint64_t a, uint64_t n, int why) {
if (!n || a >= RT_ADDRESS_LIMIT) return;
uint64_t r0 = a >> FRZ_SHIFT, r1 = (a + n - 1 < RT_ADDRESS_LIMIT ? a + n - 1 : RT_ADDRESS_LIMIT - 1) >> FRZ_SHIFT;
for (uint64_t r = r0; r <= r1; r++) thaw_locked(r, why);
}
void mem_thaw(uint64_t a, uint64_t n) {
AcquireSRWLockExclusive(&frz_lock); // also when nothing is frozen: the hold keeps a read's buffer writable
thaw_range_locked(a, n, 2);
ReleaseSRWLockExclusive(&frz_lock);
}
int mem_freeze(uint64_t a) {
if ((a & (FRZ_SIZE - 1)) || !watched(a, FRZ_SIZE)) return 0;
uint64_t r = a >> FRZ_SHIFT;
MEMORY_BASIC_INFORMATION mbi;
DWORD old;
int ok = 0;
AcquireSRWLockExclusive(&frz_lock);
if (frz[r]) ok = 1;
else if (thaw_ms[r] && (int32_t)((uint32_t)GetTickCount64() - thaw_ms[r]) < (int32_t)FRZ_HOLD_MS) ok = 0; // |1 may be ahead
else if (VirtualQuery((void *)(uintptr_t)a, &mbi, sizeof mbi) && mbi.State == MEM_COMMIT &&
mbi.Protect == PAGE_READWRITE && (uint64_t)(uintptr_t)mbi.BaseAddress + mbi.RegionSize >= a + FRZ_SIZE &&
VirtualProtect((void *)(uintptr_t)a, FRZ_SIZE, PAGE_READONLY, &old)) {
__atomic_store_n(&frz[r], 1, __ATOMIC_RELEASE);
InterlockedIncrement(&nfrz);
InterlockedIncrement64(&frz_count[0]);
ok = 1;
}
ReleaseSRWLockExclusive(&frz_lock);
return ok;
}
int mem_frozen(uint64_t a) { return a < RT_ADDRESS_LIMIT && __atomic_load_n(&frz[a >> FRZ_SHIFT], __ATOMIC_ACQUIRE); }
void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws) {
*frozen = (uint64_t)__atomic_load_n(&nfrz, __ATOMIC_RELAXED);
*freezes = (uint64_t)InterlockedExchange64(&frz_count[0], 0);
*fault_thaws = (uint64_t)InterlockedExchange64(&frz_count[1], 0);
*other_thaws = (uint64_t)InterlockedExchange64(&frz_count[2], 0) + (uint64_t)InterlockedExchange64(&frz_count[3], 0);
}
// The first write into a frozen region thaws it and retries. A fault in a region another thread thawed meanwhile
// retries too; any other fault goes on to the crash handler.
static LONG CALLBACK thaw_handler(EXCEPTION_POINTERS *ep) {
const EXCEPTION_RECORD *er = ep->ExceptionRecord;
if (er->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || er->NumberParameters < 2 || er->ExceptionInformation[0] != 1)
return EXCEPTION_CONTINUE_SEARCH;
uint64_t a = er->ExceptionInformation[1];
if (a >= RT_ADDRESS_LIMIT || !watched(a, 1)) return EXCEPTION_CONTINUE_SEARCH;
AcquireSRWLockExclusive(&frz_lock);
int was = frz[a >> FRZ_SHIFT];
thaw_locked(a >> FRZ_SHIFT, 1);
ReleaseSRWLockExclusive(&frz_lock);
if (was) return EXCEPTION_CONTINUE_EXECUTION;
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery((void *)(uintptr_t)a, &mbi, sizeof mbi) && mbi.State == MEM_COMMIT && mbi.Protect == PAGE_READWRITE)
return EXCEPTION_CONTINUE_EXECUTION;
return EXCEPTION_CONTINUE_SEARCH;
}
// Mapping changes thaw the regions they touch and run under frz_lock, so no freeze sees them half done.
static void commit(uint64_t a, uint64_t n, const char *what) {
if (n && !VirtualAlloc((void *)(uintptr_t)a, n, MEM_COMMIT, PAGE_READWRITE))
AcquireSRWLockExclusive(&frz_lock);
thaw_range_locked(a, n, 3);
int ok = !n || VirtualAlloc((void *)(uintptr_t)a, n, MEM_COMMIT, PAGE_READWRITE);
ReleaseSRWLockExclusive(&frz_lock);
if (!ok)
rt_fatal("memory: commit %s %#llx+%#llx failed (error %lu)", what, (unsigned long long)a,
(unsigned long long)n, GetLastError());
}
static void decommit(uint64_t a, uint64_t n, const char *what) {
if (n && !VirtualFree((void *)(uintptr_t)a, n, MEM_DECOMMIT))
AcquireSRWLockExclusive(&frz_lock);
thaw_range_locked(a, n, 3);
int ok = !n || VirtualFree((void *)(uintptr_t)a, n, MEM_DECOMMIT);
ReleaseSRWLockExclusive(&frz_lock);
if (!ok)
rt_fatal("memory: decommit %s %#llx+%#llx failed (error %lu)", what, (unsigned long long)a,
(unsigned long long)n, GetLastError());
}
static void protect(uint64_t a, uint64_t n, DWORD prot, const char *what) {
DWORD old;
if (n && !VirtualProtect((void *)(uintptr_t)a, n, prot, &old))
AcquireSRWLockExclusive(&frz_lock);
thaw_range_locked(a, n, 3);
int ok = !n || VirtualProtect((void *)(uintptr_t)a, n, prot, &old);
ReleaseSRWLockExclusive(&frz_lock);
if (!ok)
rt_fatal("memory: protect %s %#llx+%#llx failed (error %lu)", what, (unsigned long long)a,
(unsigned long long)n, GetLastError());
}
@@ -129,6 +250,7 @@ void mem_init(void) {
if (reserve_at(at, ALIAS_SIZE, MEM_WRITE_WATCH)) alias = at;
if (!alias) reexec_or_fail("a 64 GiB alias region below 2^39");
AddVectoredExceptionHandler(1, thaw_handler); // before the crash handler
alias_pages = VirtualAlloc(NULL, ALIAS_PAGES, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!alias_pages) rt_fatal("memory: cannot allocate the alias page map");
const char *pool = cfg_get("TOTK_APP_POOL");
@@ -148,20 +270,24 @@ const RtGuestLayout *mem_layout(void) { return &L; }
// Adapted from LAS Recomp runtime/platform/windows.c:40-63 @ da35b83 (MIT, Copyright (c) 2026 Peiikai): GetWriteWatch
// with the reset flag. A range inside one watched reservation (stack and heap block, or alias region) only.
uint64_t mem_written_pages(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max) {
static uint64_t written_pages(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max, DWORD flags) {
if (!max || !size) return 0;
const RtGuestLayout *g = &L;
int low_block = addr >= g->aslr.base && size <= LOW_SIZE && addr - g->aslr.base <= LOW_SIZE - size;
int alias = addr >= g->alias.base && size <= ALIAS_SIZE && addr - g->alias.base <= ALIAS_SIZE - size;
if (!low_block && !alias) return UINT64_MAX;
if (!watched(addr, size)) return UINT64_MAX;
ULONG_PTR n = (ULONG_PTR)max;
DWORD gran;
_Static_assert(sizeof(PVOID) == sizeof(uint64_t), "page addresses are written straight into a uint64_t array");
if (GetWriteWatch(WRITE_WATCH_FLAG_RESET, (void *)(uintptr_t)addr, (SIZE_T)size, (PVOID *)pages, &n, &gran))
return UINT64_MAX;
if (GetWriteWatch(flags, (void *)(uintptr_t)addr, (SIZE_T)size, (PVOID *)pages, &n, &gran)) return UINT64_MAX;
return n;
}
uint64_t mem_written_pages(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max) {
return written_pages(addr, size, pages, max, WRITE_WATCH_FLAG_RESET);
}
uint64_t mem_written_peek(uint64_t addr, uint64_t size, uint64_t *pages, uint64_t max) {
return written_pages(addr, size, pages, max, 0);
}
static uint64_t image_bytes(void) {
uint64_t n = 0;
for (int i = 0; i < ldr_count(); i++) n += ldr_module(i)->size;
+11
View File
@@ -47,7 +47,15 @@ uint64_t nvn_new_serial(void); // next tex_t / prog_
uint64_t nvn_fence_last(void); // fence value of the newest submit (0 before the first)
int nvn_depth_minus_w(void); // 1 after DeviceSetDepthMode(NEAR_IS_MINUS_W), else 0
void nvn_guest_written(uint64_t addr, uint64_t size); // the host or the game wrote pool storage: back end re-reads
// TOTK_TRACE=perf: MemoryPoolFlushMappedRange [0] and BufferFlushMappedRange [1] calls [0] and bytes [1] by the pool's
// CPU class (0 CPU_CACHED, 1 CPU_UNCACHED, 2 other) since the previous call.
void nvn_flush_stats(uint64_t out[2][3][2]);
void nvn_submit_prepare(const uint64_t *handles, int n); // weak no-op; the command-buffer slice reads deferred binds
// Asynchronous replay: as nvn_submit_prepare, writing the handles to replay to out (a clone where a still queued
// submission uses the same recording with other deferred binds)
void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n);
int gfx_replay_async(void); // 1: the back end replays submissions on its own thread
uint64_t gfx_replayed_submits(void); // newest submit number (nvn_submit_count) replayed
#define NVN_HANDLE_TEX(h) ((int)((h) & 0xfffff)) // combined texture handle = texture id | sampler id << 20
#define NVN_HANDLE_SAMP(h) ((int)(((h) >> 20) & 0xfff))
extern GfxStats gfx_counters; // behind gfx_stats(); add with __atomic_fetch_add. frames:
@@ -96,3 +104,6 @@ const char *nvn_op_name(uint32_t op); // "DRAW_IDX", ...; "
// MemoryPoolInvalidateMappedRange: the CPU is about to read pool storage the GPU wrote. Weak no-op in nvn_ext.c; a back
// end that renders into host-side copies defines it and writes them back.
void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size);
// MemoryPoolFlushMappedRange: the CPU wrote [mem + off, mem + off + size). Weak no-op in nvn_ext.c; a back end that keeps
// device-side copies of pool memory (vk_memory.c's VRAM mirror) defines it and copies the range again.
void nvn_pool_flushed(const pool_t *p, uint64_t off, uint64_t size);
+74 -6
View File
@@ -160,7 +160,7 @@ static const tex_t *tex_pin(const NVNtexture *nt) {
// ---------- recording copies
// A handle points just past a copy_prefix. Copies are indexed by the 4 KiB page of their control-memory position, so
// adding control memory again releases the copies recorded there without walking every live copy.
typedef struct { uint64_t magic, bytes, made_at, used_at; } copy_prefix;
typedef struct { uint64_t magic, bytes, made_at, used_at, deferred; } copy_prefix; // deferred: 0 unknown, 1 none, 2 some
_Static_assert(sizeof(copy_prefix) % 8 == 0, "records stay 8-byte aligned");
typedef struct { uintptr_t lo, hi; uint8_t *copy; } cmd_copy;
typedef struct { cmd_copy *v; uint32_t n, cap; } copy_bucket;
@@ -168,6 +168,30 @@ static SRWLOCK copy_lock = SRWLOCK_INIT; // guards the two maps, the counters
static nvn_hmap copy_pages = {.klen = 8}; // page -> copy_bucket
static nvn_hmap copy_live = {.klen = 8}; // handle -> copy_prefix of a live copy
static uint64_t ncopies, copy_bytes, copies_made, nsubmits;
// Asynchronous replay: copies released by the game, and per-submission clones, that a queued submission still reads.
// Freed once gfx_replayed_submits() reaches their used_at. Empty with synchronous replay.
static copy_prefix **zombies;
static uint32_t nzombies, cap_zombies;
static void zombie_add(copy_prefix *pf) { // copy_lock held
if (nzombies == cap_zombies) {
cap_zombies = cap_zombies ? cap_zombies * 2 : 64;
zombies = realloc(zombies, sizeof *zombies * cap_zombies);
if (!zombies) rt_fatal("nvn: out of memory (released recording copies)");
}
zombies[nzombies++] = pf;
}
static void zombie_sweep(uint64_t replayed) { // copy_lock held
for (uint32_t i = 0; i < nzombies;) {
if (zombies[i]->used_at <= replayed) {
free(zombies[i]);
zombies[i] = zombies[--nzombies];
} else {
i++;
}
}
}
static copy_prefix *copy_find(uint64_t handle) { // copy_lock held
return nvn_hm_get(&copy_live, &handle);
@@ -258,7 +282,9 @@ static void copy_add(uintptr_t lo, uintptr_t hi, uint8_t *copy) {
// Adding control memory again retires every recording copy made in it (the driver's handle lived there).
static void release_copies(uintptr_t lo, uintptr_t hi) {
if (hi <= lo) return;
uint64_t replayed = gfx_replayed_submits();
AcquireSRWLockExclusive(&copy_lock);
if (nzombies) zombie_sweep(replayed);
for (uintptr_t pg = lo >> COPY_PAGE_SHIFT; pg <= (hi - 1) >> COPY_PAGE_SHIFT; pg++) {
copy_bucket *b = nvn_hm_get(&copy_pages, &pg);
if (!b) continue;
@@ -271,7 +297,8 @@ static void release_copies(uintptr_t lo, uintptr_t hi) {
ncopies--;
copy_bytes -= pf->bytes;
pf->magic = 0;
free(pf);
if (pf->used_at > replayed) zombie_add(pf); // a queued submission still reads it
else free(pf);
b->v[i] = b->v[--b->n];
}
if (!b->n) {
@@ -836,16 +863,17 @@ NVN_API(nvnCommandBufferSetPatchSize) { (void)c; }
// What the console reads at the submit call is read here, on the game thread: every handle must be a live recording,
// and deferred binds take the handles the game's arrays hold now (the game fills its tables for the next frame as soon
// as the submit returns). The result is written into the recording copy.
static uint64_t def_reads, def_changed;
static uint64_t def_reads, def_changed, n_clones;
static void resolve_deferred(const uint8_t *p, int depth) {
static int resolve_deferred(const uint8_t *p, int depth) { // returns the number of deferred binds
int nd = 0;
for (;;) {
op_hdr h;
memcpy(&h, p, sizeof h);
if (h.size < 8 || (h.size & 7) || h.op >= OP_COUNT_)
rt_fatal("nvn: corrupt recording at %p while resolving deferred binds (op %u size %u)", (const void *)p,
h.op, h.size);
if (h.op == OP_END) return;
if (h.op == OP_END) return nd;
if (h.op == OP_JUMP) {
memcpy(&p, p + 8, 8);
continue;
@@ -854,10 +882,11 @@ static void resolve_deferred(const uint8_t *p, int depth) {
uint64_t t;
memcpy(&t, p + 8, 8);
if (depth >= 16) rt_fatal("nvn: deferred bind resolution: call depth over 16");
resolve_deferred((const uint8_t *)(uintptr_t)t, depth + 1);
nd += resolve_deferred((const uint8_t *)(uintptr_t)t, depth + 1);
} else if (h.op == OP_BIND_TEX || h.op == OP_BIND_IMG || h.op == OP_BIND_SAMP) {
p_bind_handles *b = (p_bind_handles *)(uintptr_t)(p + 8);
if (b->src && b->n > 0 && b->n <= 32) {
nd++;
uint64_t cur[32];
memcpy(cur, (const void *)(uintptr_t)b->src, 8 * (size_t)b->n);
__atomic_fetch_add(&def_reads, 1, __ATOMIC_RELAXED);
@@ -879,6 +908,45 @@ void nvn_submit_prepare(const uint64_t *handles, int n) {
for (int i = 0; i < n; i++) resolve_deferred((const uint8_t *)(uintptr_t)handles[i], 0);
}
// Asynchronous replay. A copy a queued submission still reads keeps that submission's deferred binds: when it holds
// deferred binds (or is not known to hold none), this submission gets a clone, resolved here and freed after its replay.
void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n) {
uint64_t now = __atomic_add_fetch(&nsubmits, 1, __ATOMIC_RELAXED);
uint64_t replayed = gfx_replayed_submits();
AcquireSRWLockExclusive(&copy_lock);
if (nzombies) zombie_sweep(replayed);
for (int i = 0; i < n; i++) {
copy_prefix *pf = copy_find(handles[i]);
if (!pf) {
ReleaseSRWLockExclusive(&copy_lock);
rt_fatal("nvn: QueueSubmitCommands: command handle %#llx is not a live recording (released or never recorded)",
(unsigned long long)handles[i]);
}
out[i] = handles[i];
if (pf->used_at > replayed && pf->used_at != now && pf->deferred != 1) {
size_t sz = sizeof *pf + pf->bytes;
copy_prefix *cl = malloc(sz);
if (!cl) rt_fatal("nvn: out of memory (recording clone, %zu bytes)", sz);
memcpy(cl, pf, sz);
cl->magic = 0; // not a live handle: the game never sees it
cl->used_at = now;
zombie_add(cl);
out[i] = (uintptr_t)(cl + 1);
__atomic_fetch_add(&n_clones, 1, __ATOMIC_RELAXED);
} else {
pf->used_at = now;
}
}
ReleaseSRWLockExclusive(&copy_lock);
for (int i = 0; i < n; i++) {
int nd = resolve_deferred((const uint8_t *)(uintptr_t)out[i], 0);
if (out[i] == handles[i]) ((copy_prefix *)(uintptr_t)out[i] - 1)->deferred = nd ? 2 : 1;
}
if (now % 7200 == 0)
rt_log("nvn: asynchronous replay at submit %llu: %llu recording clones so far, %u copies waiting for the replay",
(unsigned long long)now, (unsigned long long)__atomic_load_n(&n_clones, __ATOMIC_RELAXED), nzombies);
}
uint64_t nvn_submit_count(void) { return __atomic_load_n(&nsubmits, __ATOMIC_RELAXED); }
const char *nvn_op_name(uint32_t op) {
+57 -8
View File
@@ -58,6 +58,11 @@ static void ov_memcpy(Cpu *c) {
HOST_OVERRIDE("memcpy", ov_memcpy);
__attribute__((weak)) void nvn_submit_prepare(const uint64_t *handles, int n) { (void)handles, (void)n; }
__attribute__((weak)) void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n) {
memcpy(out, handles, sizeof *out * (size_t)n);
}
__attribute__((weak)) int gfx_replay_async(void) { return 0; }
__attribute__((weak)) uint64_t gfx_replayed_submits(void) { return nvn_submit_count(); }
// ---------- pools: GPU window (section 3.1) and the address index
static SRWLOCK pool_lock = SRWLOCK_INIT; // guards gva and index
@@ -287,8 +292,31 @@ NVN_API(nvnMemoryPoolFinalize) {
o->span = 0;
o->magic = 0;
}
// void nvnMemoryPoolFlushMappedRange(const NVNmemoryPool *p, ptrdiff_t offset, size_t size): storage is coherent
NVN_API(nvnMemoryPoolFlushMappedRange) { (void)c; }
static uint64_t flush_stat[2][3][2]; // nvn_flush_stats
static void flush_note(int kind, const pool_t *p, uint64_t size) {
uint32_t f = p ? p->flags : 0;
int k = f & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS ? 2 : f & NVN_MEMORY_POOL_FLAGS_CPU_CACHED ? 0
: f & NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED ? 1 : 2;
__atomic_fetch_add(&flush_stat[kind][k][0], 1, __ATOMIC_RELAXED);
__atomic_fetch_add(&flush_stat[kind][k][1], size, __ATOMIC_RELAXED);
}
void nvn_flush_stats(uint64_t out[2][3][2]) {
for (int i = 0; i < 2; i++)
for (int k = 0; k < 3; k++)
for (int j = 0; j < 2; j++) out[i][k][j] = __atomic_exchange_n(&flush_stat[i][k][j], 0, __ATOMIC_RELAXED);
}
// void nvnMemoryPoolFlushMappedRange(const NVNmemoryPool *p, ptrdiff_t offset, size_t size): storage is coherent; a
// back end with device-side copies hears of it (nvn_pool_flushed)
NVN_API(nvnMemoryPoolFlushMappedRange) {
const pool_t *p = arg_p(c, 0);
int64_t off = (int64_t)arg_x(c, 1);
uint64_t size = arg_x(c, 2);
if (rt_trace & RT_TRACE_PERF) flush_note(0, p, size);
if (p && off >= 0 && (uint64_t)off < p->size) nvn_pool_flushed(p, (uint64_t)off, size < p->size - (uint64_t)off ? size : p->size - (uint64_t)off);
}
// NVNmemoryPoolFlags nvnMemoryPoolGetFlags(const NVNmemoryPool *p)
NVN_API(nvnMemoryPoolGetFlags) { ret_x(c, ((const pool_t *)arg_p(c, 0))->flags); }
// void *nvnMemoryPoolMap(const NVNmemoryPool *p): NULL for CPU_NO_ACCESS
@@ -402,6 +430,7 @@ NVN_API(nvnBufferFlushMappedRange) {
if (off < 0 || (uint64_t)off > o->size || size > o->size - (uint64_t)off)
rt_fatal("nvn: BufferFlushMappedRange %#llx+%#llx outside the buffer (%#llx bytes)", (unsigned long long)off,
(unsigned long long)size, (unsigned long long)o->size);
if (rt_trace & RT_TRACE_PERF) flush_note(1, o->pool, size);
uint64_t a = buf_address(o);
if (a && !nvn_gpu_only(a)) nvn_guest_written(a + (uint64_t)off, size);
}
@@ -429,12 +458,13 @@ NVN_API(nvnSyncWait) {
uint64_t timeout = arg_x(c, 1);
if (!s->fenced) { ret_x(c, NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); return; }
const GfxBackend *b = gfx_backend();
if (b->fence_done() >= s->fence) { ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; }
if (b->fence_done() >= s->fence) { b->cpu_synced(s->fence); ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; }
int ok;
{
NVN_BLOCKING();
ok = b->fence_wait(s->fence, timeout);
}
if (ok) b->cpu_synced(s->fence);
ret_x(c, ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED);
}
// void nvnEventBuilderSetDefaults(NVNeventBuilder *b)
@@ -516,8 +546,11 @@ NVN_API(nvnQueueGetError) {
NVN_API(nvnQueueFlush) { (void)c; }
// void nvnQueueFinish(NVNqueue *q)
NVN_API(nvnQueueFinish) {
NVN_BLOCKING();
gfx_backend()->finish();
{
NVN_BLOCKING();
gfx_backend()->finish();
}
gfx_backend()->cpu_synced(gfx_backend()->fence_done());
}
// ---------- Submit latency guard for TotK 1.2.1's per-object uniform double buffer
// NVN starts the GPU when QueueSubmitCommands returns, within microseconds; here the replay into Vulkan takes 15-30 ms
@@ -532,7 +565,9 @@ NVN_API(nvnQueueFinish) {
static uint64_t lod_entered, lod_issued, lod_fence[LOD_RING];
// void nvnQueueSubmitCommands(NVNqueue *q, int n, const NVNcommandHandle *handles): replayed on this thread with its
// core given up (section 2.2); the newest fence value is what QueueFenceSync records
// core given up (section 2.2), or queued for the replay thread (TOTK_REPLAY_ASYNC, vk_backend.c); the newest fence
// value is what QueueFenceSync records. With the queue the fence covers replay and GPU, so the LOD guard below waits
// for both.
NVN_API(nvnQueueSubmitCommands) {
queue_t *q = arg_p(c, 0);
int n = (int)arg_x(c, 1);
@@ -541,8 +576,22 @@ NVN_API(nvnQueueSubmitCommands) {
NVN_BLOCKING();
uint64_t no = __atomic_add_fetch(&lod_entered, 1, __ATOMIC_SEQ_CST);
q->submits++;
nvn_submit_prepare(h, n);
uint64_t f = gfx_backend()->submit(h, n), prev = __atomic_load_n(&last_fence, __ATOMIC_RELAXED);
uint64_t f;
if (gfx_replay_async()) {
// Prepare and queue under one lock: submit numbers follow queue order (gfx_replayed_submits).
static SRWLOCK submit_lock = SRWLOCK_INIT;
uint64_t local[32], *hs = n <= 32 ? local : malloc(sizeof *hs * (size_t)n);
if (!hs) rt_fatal("nvn: out of memory (submit of %d handles)", n);
AcquireSRWLockExclusive(&submit_lock);
nvn_submit_prepare_async(h, hs, n);
f = gfx_backend()->submit(hs, n);
ReleaseSRWLockExclusive(&submit_lock);
if (hs != local) free(hs);
} else {
nvn_submit_prepare(h, n);
f = gfx_backend()->submit(h, n);
}
uint64_t prev = __atomic_load_n(&last_fence, __ATOMIC_RELAXED);
while (f > prev && !__atomic_compare_exchange_n(&last_fence, &prev, f, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {}
__atomic_store_n(&lod_fence[no % LOD_RING], f, __ATOMIC_RELAXED);
__atomic_store_n(&lod_issued, no, __ATOMIC_SEQ_CST);
+10 -3
View File
@@ -11,9 +11,16 @@
#include "nvn_api.h"
#include "nvn_tex.h"
// The CPU is about to read what the GPU wrote into pool storage: a back end that renders into host-side copies (the
// Vulkan back end's images) defines the strong version and writes them back.
__attribute__((weak)) void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size) { (void)p, (void)off, (void)size; }
// The CPU is about to read what the GPU wrote into pool storage: the back end writes its host-side copies (the Vulkan
// back end's images) back first. TotK invalidates its 260x260 R16 grass maps and 1x1 pick targets this way. A
// CPU-invisible pool has no storage to read.
void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size) {
if (!p->mem || nvn_gpu_only((uintptr_t)p->mem) || off >= p->size || !size) return;
if (size > p->size - off) size = p->size - off;
gfx_backend()->guest_read((uintptr_t)p->mem + off, size);
}
__attribute__((weak)) void nvn_pool_flushed(const pool_t *p, uint64_t off, uint64_t size) { (void)p, (void)off, (void)size; }
// ---------- debug labels, markers, groups, checkpoints: meaningful only to NVN debugging tools
// void nvn<Object>SetDebugLabel(NVN<object> *o, const char *label)
+152 -6
View File
@@ -2,7 +2,13 @@
// A host thread wakes every 1 ms and, for every other thread, suspends it, reads Rip and resumes it. The sampler never
// allocates or takes a lock between SuspendThread and ResumeThread and never runs guest code. The profile is text:
// totk-prof 1 / base <hex> / period_us / samples N / thread <tid> <samples> <in-exe samples> / module <base> <size> <path> /
// rip <hex> <count> (the top PROF_TOP Rips)
// rip <hex> <count> (the top PROF_TOP Rips) / stack <count> <rip> <caller> ... (TOTK_PROFILE_RENDER or
// TOTK_PROFILE_CHAINS only, the top PROF_STACKS call chains)
// With TOTK_PROFILE_RENDER (the render thread) or TOTK_PROFILE_CHAINS=1 (every thread, chains of all threads summed: for
// short frame ranges, as each sample copies a stack) the sampler also copies up to STK_COPY bytes of the suspended
// thread's stack and, after
// ResumeThread, unwinds the copy with the .pdata tables (RtlVirtualUnwind), so it takes no lock while the thread is
// suspended. The unwind stops at the first frame outside the copy; deeper callers are cut.
#include <windows.h>
#include <psapi.h>
#include <tlhelp32.h>
@@ -18,11 +24,20 @@
#define PROF_SLOTS (1u << 16) // Rip hash table, open addressing; a full table drops new Rips
#define PROF_TOP 4000 // more than the 200 asked for: the report sums per function
#define PROF_THREADS 512
#define PROF_DEPTH 24 // frames kept per call chain, the sampled Rip first
#define STK_SLOTS (1u << 15) // call-chain hash table, open addressing; a full table drops new chains
#define PROF_STACKS 6000
#define STK_COPY (64u << 10) // stack bytes copied per sample
#define STK_SLACK (64u << 10) // zeros after the copy: a frame reaching past it reads zeros, and a zero Rip ends the chain
typedef struct { uint64_t rip, n; } RipCount;
typedef struct { DWORD tid; HANDLE h; uint64_t n, in_exe; } ThreadSlot;
static RipCount table[PROF_SLOTS];
typedef struct { uint64_t hash, n; uint32_t depth; uint64_t rip[PROF_DEPTH]; } StackCount;
static StackCount stacks[STK_SLOTS];
static uint64_t stacks_dropped;
static uint8_t stk_buf[STK_COPY + STK_SLACK] __attribute__((aligned(16))); // the slack is never written
static ThreadSlot threads[PROF_THREADS];
static int n_threads;
static uint64_t total, dropped;
@@ -32,6 +47,7 @@ static volatile LONG stop, written;
static uintptr_t exe_lo, exe_hi;
static volatile DWORD render_tid; // the thread that presents (perf_present)
static int only_render; // TOTK_PROFILE_RENDER=1: sample the render thread only
static int chains; // call chains are kept: TOTK_PROFILE_RENDER or TOTK_PROFILE_CHAINS
static volatile uint64_t cur_frame; // last presented frame
static uint64_t frame_lo, frame_hi; // TOTK_PROFILE_FRAMES=<lo>-<hi>: sample only while presents lo..hi-1 are pending
@@ -47,6 +63,50 @@ static void count_rip(uint64_t rip) {
dropped++;
}
static void count_stack(const uint64_t *rip, uint32_t depth) {
uint64_t h = 0xcbf29ce484222325ull;
for (uint32_t d = 0; d < depth; d++) h = (h ^ rip[d]) * 0x100000001b3ull;
h |= 1; // 0 marks a free slot
uint32_t i = (uint32_t)(h >> 49);
for (uint32_t k = 0; k < 64; k++, i = (i + 1) & (STK_SLOTS - 1)) {
StackCount *s = &stacks[i];
if (!s->hash) {
s->hash = h, s->depth = depth;
memcpy(s->rip, rip, depth * sizeof *rip);
} else if (s->hash != h || s->depth != depth || memcmp(s->rip, rip, depth * sizeof *rip)) {
continue;
}
s->n++;
return;
}
stacks_dropped++;
}
// Unwinds the stack copied to stk_buf from real address lo (n bytes). Registers that point into the copied range are
// moved into the copy, so frame-pointer frames read the copy too. Returns the number of frames.
static uint32_t unwind_copy(CONTEXT *c, uintptr_t lo, size_t n, uint64_t *rip) {
uintptr_t buf = (uintptr_t)stk_buf, end = buf + n;
DWORD64 *regs[] = {&c->Rbx, &c->Rbp, &c->Rsi, &c->Rdi, &c->R12, &c->R13, &c->R14, &c->R15};
for (unsigned i = 0; i < sizeof regs / sizeof *regs; i++)
if (*regs[i] >= lo && *regs[i] < lo + n) *regs[i] = *regs[i] - lo + buf;
c->Rsp = buf;
uint32_t d = 0;
while (d < PROF_DEPTH && c->Rip && c->Rsp >= buf && c->Rsp + 8 <= end) {
rip[d++] = c->Rip;
DWORD64 base;
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(c->Rip, &base, NULL);
if (f) {
void *hd;
DWORD64 est;
RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, c->Rip, f, c, &hd, &est, NULL);
} else { // a leaf function: the return address is on top
c->Rip = *(DWORD64 *)c->Rsp;
c->Rsp += 8;
}
}
return d;
}
// Re-lists the process's threads: keeps the handle of a thread seen before, opens new ones, closes the gone ones.
static void refresh_threads(DWORD self) {
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
@@ -94,12 +154,24 @@ static DWORD WINAPI sampler_main(void *arg) {
for (int i = 0; i < n_threads; i++) {
ThreadSlot *t = &threads[i];
if (only_render && t->tid != render_tid) continue;
ctx.ContextFlags = CONTEXT_CONTROL;
ctx.ContextFlags = chains ? CONTEXT_CONTROL | CONTEXT_INTEGER : CONTEXT_CONTROL;
if (SuspendThread(t->h) == (DWORD)-1) continue;
BOOL ok = GetThreadContext(t->h, &ctx);
size_t n = 0;
MEMORY_BASIC_INFORMATION mb;
if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT) {
n = (uintptr_t)mb.BaseAddress + mb.RegionSize - ctx.Rsp; // the committed stack above Rsp
if (n > STK_COPY) n = STK_COPY;
memcpy(stk_buf, (void *)ctx.Rsp, n);
}
ResumeThread(t->h);
if (!ok) continue;
count_rip(ctx.Rip);
if (n) {
uint64_t chain[PROF_DEPTH];
uint32_t d = unwind_copy(&ctx, ctx.Rsp, n, chain);
if (d) count_stack(chain, d);
}
total++;
t->n++;
if (ctx.Rip >= exe_lo && ctx.Rip < exe_hi) t->in_exe++;
@@ -113,6 +185,11 @@ static int cmp_desc(const void *a, const void *b) {
return (x < y) - (x > y);
}
static int cmp_stack_desc(const void *a, const void *b) {
uint64_t x = ((const StackCount *)a)->n, y = ((const StackCount *)b)->n;
return (x < y) - (x > y);
}
void prof_write(void) {
perf_summary_flush();
if (!out_path || InterlockedExchange(&written, 1)) return;
@@ -142,6 +219,16 @@ void prof_write(void) {
if (table[i].rip) table[cnt++] = table[i];
qsort(table, cnt, sizeof *table, cmp_desc);
for (int i = 0; i < cnt && i < PROF_TOP; i++) fprintf(f, "rip %llx %llu\n", (unsigned long long)table[i].rip, (unsigned long long)table[i].n);
int ns = 0;
for (uint32_t i = 0; i < STK_SLOTS; i++)
if (stacks[i].hash) stacks[ns++] = stacks[i];
qsort(stacks, ns, sizeof *stacks, cmp_stack_desc);
for (int i = 0; i < ns && i < PROF_STACKS; i++) {
fprintf(f, "stack %llu", (unsigned long long)stacks[i].n);
for (uint32_t d = 0; d < stacks[i].depth; d++) fprintf(f, " %llx", (unsigned long long)stacks[i].rip[d]);
fputc('\n', f);
}
if (stacks_dropped) fprintf(f, "stacks_dropped %llu\n", (unsigned long long)stacks_dropped);
fclose(f);
rt_log("prof: %llu samples, %d Rips -> %s", (unsigned long long)total, cnt, out_path);
}
@@ -158,6 +245,8 @@ void prof_start(void) {
return;
}
only_render = cfg_get("TOTK_PROFILE_RENDER") != NULL;
const char *ch = cfg_get("TOTK_PROFILE_CHAINS");
chains = only_render || (ch && *ch && strcmp(ch, "0"));
const char *fr = cfg_get("TOTK_PROFILE_FRAMES");
if (fr && sscanf(fr, "%llu-%llu", (unsigned long long *)&frame_lo, (unsigned long long *)&frame_hi) != 2) frame_hi = 0;
MODULEINFO mi;
@@ -180,11 +269,32 @@ static uint64_t wait_ns[PERF_N], frame_ns[PERF_N], worst_ns[PERF_N]; // window
uint64_t perf_begin(void) { return kern_now_ns(); }
static volatile DWORD replay_tid; // asynchronous replay (vk_backend.c): its waits hold up the frame too
void perf_replay_thread(void) { replay_tid = GetCurrentThreadId(); }
void perf_end(int kind, uint64_t t0) {
if (!t0 || GetCurrentThreadId() != render_tid) return;
if (!t0) return;
DWORD tid = GetCurrentThreadId();
if (tid != render_tid && tid != replay_tid) return;
uint64_t d = kern_now_ns() - t0;
wait_ns[kind] += d;
frame_ns[kind] += d;
if (tid == render_tid && !replay_tid) { // one thread adds: no atomics on the synchronous path
wait_ns[kind] += d;
frame_ns[kind] += d;
return;
}
__atomic_fetch_add(&wait_ns[kind], d, __ATOMIC_RELAXED);
__atomic_fetch_add(&frame_ns[kind], d, __ATOMIC_RELAXED);
}
static volatile LONG64 other_wait_ns; // perf_gpu_wait on threads other than the render thread, over the window
static volatile LONG64 render_wait_ns; // the same on the render thread
void (*perf_gpu_stats)(PerfGpu *out);
void perf_gpu_wait(uint64_t t0) {
if (!t0 || !perf_on) return;
LONG64 d = (LONG64)(kern_now_ns() - t0);
InterlockedAdd64(GetCurrentThreadId() == render_tid ? &render_wait_ns : &other_wait_ns, d);
}
static volatile LONG64 replay_ns; // perf_replay: summed over the window, any thread
@@ -240,8 +350,13 @@ static void ft_report(uint64_t now, const char *tag) {
ft.t0 = now;
}
__attribute__((weak)) void gfx_exit_log(void) {}
void perf_summary_flush(void) { // at exit (prof_write)
if (summary_t0) ft_report(kern_now_ns(), "exit");
if (summary_t0) {
ft_report(kern_now_ns(), "exit");
gfx_exit_log();
}
summary_t0 = 0;
}
@@ -273,6 +388,35 @@ static void ft_frame(uint64_t frame, uint64_t now, uint64_t d) {
}
}
// The second line of the 60-frame window: GPU busy time (timestamps), the time threads waited for GPU fences, VRAM and
// 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.
static void perf_gpu_line(uint64_t frame, double interval_ms, double pace_share, uint64_t n) {
PerfGpu g;
memset(&g, 0, sizeof g);
if (perf_gpu_stats) perf_gpu_stats(&g);
double rw = InterlockedExchange64(&render_wait_ns, 0) / 1e6 / n, ow = InterlockedExchange64(&other_wait_ns, 0) / 1e6 / n;
double gpu_ms = g.have_gpu ? g.gpu_ns / 1e6 / n : 0;
char gs[96], vs[96];
if (g.have_gpu) snprintf(gs, sizeof gs, "gpu avg %.2f ms (%llu parts timed, %llu not)", gpu_ms,
(unsigned long long)g.gpu_parts, (unsigned long long)g.gpu_missed);
else snprintf(gs, sizeof gs, "gpu avg n/a");
if (g.have_vram) snprintf(vs, sizeof vs, "vram %.0f of %.0f MiB%s", g.vram_used / 1048576.0, g.vram_budget / 1048576.0,
g.vram_exact ? "" : " (own allocations of the heap size)");
else snprintf(vs, sizeof vs, "vram n/a");
PROCESS_MEMORY_COUNTERS_EX pm = {.cb = sizeof pm};
double ws = 0, priv = 0;
if (K32GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pm, sizeof pm))
ws = pm.WorkingSetSize / 1048576.0, priv = pm.PrivateUsage / 1048576.0;
const char *bound = pace_share >= 0.15 ? "pace"
: g.have_gpu ? (gpu_ms >= 0.8 * interval_ms ? "gpu" : "cpu")
: (rw + ow >= 0.5 * interval_ms ? "gpu" : "cpu");
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);
}
void perf_present(uint64_t frame) {
static uint64_t last, sum, max, n, win_start;
static LONG64 win_prev[PERF_CNT_N];
@@ -281,6 +425,7 @@ void perf_present(uint64_t frame) {
if (rt_trace & RT_TRACE_SCHED) kern_sched_report(frame);
uint64_t now = kern_now_ns(), d = last ? now - last : 0;
ft_frame(frame, now, d); // d is 0 on the first present: it only starts the period
kern_core_diag_frame(frame, d);
if (flight_on) flight_frame(frame, now, d, frame_ns, counts);
if (!(rt_trace & RT_TRACE_PERF)) {
memset(frame_ns, 0, sizeof frame_ns);
@@ -329,6 +474,7 @@ void perf_present(uint64_t frame) {
(unsigned long long)shader_keys, (unsigned long long)shader_xlated,
(span > wait_ns[PERF_PACE] ? span - wait_ns[PERF_PACE] : 0) / 1e6 / n,
InterlockedExchange64(&replay_ns, 0) / 1e6 / n);
if (n && span) perf_gpu_line(frame, sum / 1e6 / n, (double)wait_ns[PERF_PACE] / span, n);
sum = max = n = 0;
memset(wait_ns, 0, sizeof wait_ns);
win_start = now;
+7 -2
View File
@@ -84,11 +84,16 @@ static int64_t read_at(HANDLE f, void *buf, uint32_t n, uint64_t off) {
rt_fatal("RomFS: CreateEvent failed (%lu)", GetLastError());
OVERLAPPED ov = {.Offset = (DWORD)off, .OffsetHigh = (DWORD)(off >> 32), .hEvent = ev};
DWORD got = 0;
mem_thaw((uintptr_t)buf, n); // the kernel's write would fail on a frozen page instead of faulting
if (!ReadFile(f, buf, n, &got, &ov)) {
DWORD e = GetLastError();
if (e == ERROR_HANDLE_EOF) return 0;
if (e != ERROR_IO_PENDING) return -1;
if (!GetOverlappedResult(f, &ov, &got, TRUE)) return GetLastError() == ERROR_HANDLE_EOF ? 0 : -1;
if (e == ERROR_IO_PENDING && !GetOverlappedResult(f, &ov, &got, TRUE)) e = GetLastError();
else if (e == ERROR_IO_PENDING) return got;
if (e == ERROR_HANDLE_EOF) return 0;
if (e == ERROR_NOACCESS) // a read-only page in the buffer: a frozen region missed its thaw
rt_log("RomFS: read of %u bytes into %p refused: the buffer is not writable", n, buf);
return -1;
}
return got;
}
+429
View File
@@ -0,0 +1,429 @@
// ASTC LDR 2D decode to RGBA8 on the GPU, one invocation per texel. Ported from runtime/tex_astc.c (itself from LAS
// Recomp runtime/nvn/astc.c @ da35b83a, MIT, Copyright (c) 2026 Peiikai), so the output is the same bytes as
// astc_decode_ldr and astcenc's 8-bit decode: endpoints expand to 16 bits (linear v * 257, sRGB v << 8 | 0x80),
// texel = (e0 * (64 - w) + e1 * w + 32) >> 6, top 8 bits. Error blocks are magenta; HDR endpoint modes give magenta
// endpoints for their partition.
//
// A workgroup of 64 lanes takes up to four blocks of one block row (main below): every lane decodes its block's header,
// the block's lanes share the integer-sequence decode of its weights and colour values through shared memory, and
// each lane then interpolates its own texels.
// Compiled to runtime/astc_decode_spv.h by tools/build_shaders.py; vk_astc.c records the dispatch.
#version 450
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(std430, set = 0, binding = 0) readonly buffer Blocks { uvec4 blk[]; };
layout(std430, set = 0, binding = 1) writeonly buffer Texels { uint px[]; };
// uint 0..11: byte offsets of the weight unquantization rows (QUANT_2..QUANT_32), 12..27: of the colour rows
// (QUANT_6..QUANT_256 minus one); the bytes follow (vk_astc.c astc_tables).
layout(std430, set = 0, binding = 2) readonly buffer Tables { uint tab[]; };
layout(push_constant) uniform Params {
uint in_base; // first block of slice 0 (uvec4 index into blk)
uint out_base; // first texel of slice 0 (index into px)
uint width, height;
uint bw, bh; // block footprint
uint bx; // blocks per row
uint bslice; // blocks per slice
uint srgb;
} pc;
uint tab_byte(uint i) { return (tab[i >> 2] >> ((i & 3u) * 8u)) & 0xFFu; }
// n (<= 32) bits at pos; bits past 127 read as zero.
uint rbits(uvec4 b, int pos, int n) {
if (n == 0 || pos >= 128) return 0u;
int wi = pos >> 5, sh = pos & 31;
uint v = b[wi] >> uint(sh);
if (sh != 0 && wi < 3) v |= b[wi + 1] << uint(32 - sh);
return n >= 32 ? v : v & ((1u << uint(n)) - 1u);
}
// Quantization levels QUANT_2 .. QUANT_256 (0..20): bits, trits, quints of tex_astc.c q_bits / q_trits / q_quints.
int q_bits(int q) {
if (q < 2) return q == 0 ? 1 : 0;
int g = (q - 2) / 3, r = (q - 2) - 3 * g;
return r == 0 ? g + 2 : r == 1 ? g : g + 1;
}
bool q_trit(int q) { return q == 1 || (q >= 2 && (q - 2) % 3 == 2); }
bool q_quint(int q) { return q >= 2 && (q - 2) % 3 == 1; }
int ise_bitcount(int count, int q) {
int n = count * q_bits(q);
if (q_trit(q)) n += (8 * count + 4) / 5;
if (q_quint(q)) n += (7 * count + 2) / 3;
return n;
}
int trit_at(uint T, int k) {
uint C;
int t0, t1, t2, t3, t4;
if (((T >> 2) & 7u) == 7u) {
C = ((T >> 5) & 7u) << 2 | (T & 3u);
t4 = 2; t3 = 2;
} else {
C = T & 0x1Fu;
if (((T >> 5) & 3u) == 3u) { t4 = 2; t3 = int((T >> 7) & 1u); }
else { t4 = int((T >> 7) & 1u); t3 = int((T >> 5) & 3u); }
}
if ((C & 3u) == 3u) {
t2 = 2; t1 = int((C >> 4) & 1u);
t0 = int(((C >> 3) & 1u) << 1 | (((C >> 2) & 1u) & ~(C >> 3) & 1u));
} else if (((C >> 2) & 3u) == 3u) {
t2 = 2; t1 = 2; t0 = int(C & 3u);
} else {
t2 = int((C >> 4) & 1u); t1 = int((C >> 2) & 3u);
t0 = int(((C >> 1) & 1u) << 1 | ((C & 1u) & ~(C >> 1) & 1u));
}
return k == 0 ? t0 : k == 1 ? t1 : k == 2 ? t2 : k == 3 ? t3 : t4;
}
int quint_at(uint Q, int k) {
int q0, q1, q2;
if (((Q >> 1) & 3u) == 3u && ((Q >> 5) & 3u) == 0u) {
uint b0 = Q & 1u;
q2 = int(b0 << 2 | (((Q >> 4) & 1u) & ~b0 & 1u) << 1 | (((Q >> 3) & 1u) & ~b0 & 1u));
q1 = 4; q0 = 4;
} else {
uint C;
if (((Q >> 1) & 3u) == 3u) { q2 = 4; C = ((Q >> 3) & 3u) << 3 | (~(Q >> 5) & 3u) << 1 | (Q & 1u); }
else { q2 = int((Q >> 5) & 3u); C = Q & 0x1Fu; }
if ((C & 7u) == 5u) { q1 = 4; q0 = int((C >> 3) & 3u); }
else { q1 = int((C >> 3) & 3u); q0 = int(C & 7u); }
}
return k == 0 ? q0 : k == 1 ? q1 : q2;
}
// Raw ISE value idx (trit/quint << bits | bits) of a sequence of count values at bit start; tex_astc.c ise_decode
// reads the sequence front to back, this reads one value's group only. Bits of values past count are not read.
uint ise_at(uvec4 b, int start, int count, int q, int idx) {
int nb = q_bits(q);
if (q_trit(q)) {
// value k of a group: nb bits at k * nb + ts[k], then tb[k] trit bits; ts = {0, 2, 4, 5, 7}, tb = {2, 2, 1, 2, 1}
int g = idx / 5, k = idx - 5 * g, gs = start + g * (5 * nb + 8);
uint m = rbits(b, gs + k * nb + (k * 7 + 2) / 4, nb);
uint T = 0u;
for (int j = 0; j < 5; j++) {
int ts = (j * 7 + 2) / 4, tb = j == 2 || j == 4 ? 1 : 2;
if (5 * g + j < count) T |= rbits(b, gs + (j + 1) * nb + ts, tb) << uint(ts);
}
return uint(trit_at(T, k)) << uint(nb) | m;
}
if (q_quint(q)) {
// qs = {0, 3, 5}, qb = {3, 2, 2}
int g = idx / 3, k = idx - 3 * g, gs = start + g * (3 * nb + 7);
uint m = rbits(b, gs + k * nb + (k == 0 ? 0 : k == 1 ? 3 : 5), nb);
uint Q = 0u;
for (int j = 0; j < 3; j++) {
int qs = j == 0 ? 0 : j == 1 ? 3 : 5, qb = j == 0 ? 3 : 2;
if (3 * g + j < count) Q |= rbits(b, gs + (j + 1) * nb + qs, qb) << uint(qs);
}
return uint(quint_at(Q, k)) << uint(nb) | m;
}
return rbits(b, start + idx * nb, nb);
}
uint hash52(uint p) {
p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
p ^= p << 6; p ^= p >> 17;
return p;
}
int part_of(int seed, int count, int x, int y) {
seed += (count - 1) * 1024;
uint rnum = hash52(uint(seed));
int sh1, sh2;
if ((seed & 1) != 0) { sh1 = (seed & 2) != 0 ? 4 : 5; sh2 = count == 3 ? 6 : 5; }
else { sh1 = count == 3 ? 6 : 5; sh2 = (seed & 2) != 0 ? 4 : 5; }
int s[8];
for (int i = 0; i < 8; i++) {
int v = int((rnum >> uint(4 * i)) & 0xFu);
s[i] = (v * v) >> ((i & 1) != 0 ? sh2 : sh1);
}
int a = int((uint(s[0] * x + s[1] * y) + (rnum >> 14)) & 0x3Fu);
int b = int((uint(s[2] * x + s[3] * y) + (rnum >> 10)) & 0x3Fu);
int c = count >= 3 ? int((uint(s[4] * x + s[5] * y) + (rnum >> 6)) & 0x3Fu) : 0;
int d = count >= 4 ? int((uint(s[6] * x + s[7] * y) + (rnum >> 2)) & 0x3Fu) : 0;
if (a >= b && a >= c && a >= d) return 0;
if (b >= c && b >= d) return 1;
if (c >= d) return 2;
return 3;
}
int clamp255(int v) { return clamp(v, 0, 255); }
// v: the partition's unquantized colour values. False for an HDR mode.
bool unpack_endpoints(int cem, int v[8], out ivec4 e0, out ivec4 e1) {
e0 = ivec4(0); e1 = ivec4(0);
switch (cem) {
case 0:
e0 = ivec4(v[0], v[0], v[0], 255); e1 = ivec4(v[1], v[1], v[1], 255);
return true;
case 1: {
int l0 = v[0] >> 2 | (v[1] & 0xC0);
int l1 = min(l0 + (v[1] & 0x3F), 255);
e0 = ivec4(l0, l0, l0, 255); e1 = ivec4(l1, l1, l1, 255);
return true;
}
case 4:
e0 = ivec4(v[0], v[0], v[0], v[2]); e1 = ivec4(v[1], v[1], v[1], v[3]);
return true;
case 5: {
int l0 = v[0], l1 = v[1], a0 = v[2], a1 = v[3];
l0 |= (l1 & 0x80) << 1; a0 |= (a1 & 0x80) << 1;
l1 &= 0x7F; a1 &= 0x7F;
if ((l1 & 0x40) != 0) l1 -= 0x80;
if ((a1 & 0x40) != 0) a1 -= 0x80;
l0 >>= 1; a0 >>= 1; l1 >>= 1; a1 >>= 1;
l1 = clamp255(l1 + l0); a1 = clamp255(a1 + a0);
e0 = ivec4(l0, l0, l0, a0); e1 = ivec4(l1, l1, l1, a1);
return true;
}
case 6:
case 10: {
int s = v[3];
e1 = ivec4(v[0], v[1], v[2], 255);
e0 = ivec4(v[0] * s >> 8, v[1] * s >> 8, v[2] * s >> 8, 255);
if (cem == 10) { e0.w = v[4]; e1.w = v[5]; }
return true;
}
case 8:
case 12: {
ivec4 a = ivec4(v[0], v[2], v[4], cem == 12 ? v[6] : 0);
ivec4 b = ivec4(v[1], v[3], v[5], cem == 12 ? v[7] : 0);
if (a.x + a.y + a.z > b.x + b.y + b.z) {
e0 = b; e1 = a;
e0.x = (e0.x + e0.z) >> 1; e0.y = (e0.y + e0.z) >> 1;
e1.x = (e1.x + e1.z) >> 1; e1.y = (e1.y + e1.z) >> 1;
} else {
e0 = a; e1 = b;
}
if (cem == 8) { e0.w = 255; e1.w = 255; }
return true;
}
case 9:
case 13: {
ivec4 a = ivec4(0), b = ivec4(0);
for (int c = 0; c < 4; c++) {
if (c == 3 && cem != 13) break;
int base = v[2 * c], delta = v[2 * c + 1];
a[c] = base >> 1 | (delta & 0x80);
int d = (delta >> 1) & 0x3F;
if ((d & 0x20) != 0) d -= 0x40;
b[c] = d;
}
int sum = b.x + b.y + b.z;
b += a;
if (sum < 0) {
e0 = b; e1 = a;
e0.x = (e0.x + e0.z) >> 1; e0.y = (e0.y + e0.z) >> 1;
e1.x = (e1.x + e1.z) >> 1; e1.y = (e1.y + e1.z) >> 1;
} else {
e0 = a; e1 = b;
}
e0 = clamp(e0, ivec4(0), ivec4(255)); e1 = clamp(e1, ivec4(0), ivec4(255));
if (cem == 9) { e0.w = 255; e1.w = 255; }
return true;
}
default:
return false;
}
}
const uint MAGENTA = 0xFFFF00FFu;
// What every texel of a block needs from its header (tex_astc.c astc_decode_block up to the weight infill).
struct Hdr {
uint konst; // the colour of a void-extent or error block
bool solid; // konst is the whole block
int wx, wy, dual, quant, wtotal, pcount, cem0, cem1, cem2, cem3, n0, n1, n2, ccount, cq, ccs;
};
Hdr header(uvec4 b, int bw, int bh) {
Hdr H;
H.solid = true; H.konst = MAGENTA;
H.wx = 0; H.wy = 0; H.dual = 0; H.quant = 0; H.wtotal = 0; H.pcount = 1;
H.cem0 = 0; H.cem1 = 0; H.cem2 = 0; H.cem3 = 0; H.n0 = 0; H.n1 = 0; H.n2 = 0; H.ccount = 0; H.cq = 20; H.ccs = -1;
uint mode = b.x & 0x7FFu;
if ((mode & 0x1FFu) == 0x1FCu) { // void extent
if ((mode & 0x200u) != 0u || ((b.x >> 10) & 3u) != 3u) return H;
uint s0 = rbits(b, 12, 13), s1 = rbits(b, 25, 13), t0 = rbits(b, 38, 13), t1 = rbits(b, 51, 13);
bool all_ones = s0 == 0x1FFFu && s1 == 0x1FFFu && t0 == 0x1FFFu && t1 == 0x1FFFu;
if ((s0 >= s1 || t0 >= t1) && !all_ones) return H;
H.konst = (b.z >> 8 & 0xFFu) | (b.z >> 24) << 8 | (b.w >> 8 & 0xFFu) << 16 | (b.w >> 24) << 24;
return H;
}
// block mode (tex_astc.c block_mode_2d)
uint m = mode;
uint r = (m >> 4) & 1u, h = (m >> 9) & 1u, d = (m >> 10) & 1u, a = (m >> 5) & 3u;
int wx, wy;
if ((m & 3u) != 0u) {
r |= (m & 3u) << 1;
uint bb = (m >> 7) & 3u;
uint sel = (m >> 2) & 3u;
if (sel == 0u) { wx = int(bb) + 4; wy = int(a) + 2; }
else if (sel == 1u) { wx = int(bb) + 8; wy = int(a) + 2; }
else if (sel == 2u) { wx = int(a) + 2; wy = int(bb) + 8; }
else {
bb &= 1u;
if ((m & 0x100u) != 0u) { wx = int(bb) + 2; wy = int(a) + 2; }
else { wx = int(a) + 2; wy = int(bb) + 6; }
}
} else {
r |= ((m >> 2) & 3u) << 1;
if (((m >> 2) & 3u) == 0u) return H;
uint bb = (m >> 9) & 3u;
uint sel = (m >> 7) & 3u;
if (sel == 0u) { wx = 12; wy = int(a) + 2; }
else if (sel == 1u) { wx = int(a) + 2; wy = 12; }
else if (sel == 2u) { wx = int(a) + 6; wy = int(bb) + 6; d = 0u; h = 0u; }
else {
if (a == 0u) { wx = 6; wy = 10; }
else if (a == 1u) { wx = 10; wy = 6; }
else return H;
}
}
int dual = int(d);
int wtotal = (wx * wy) << dual;
int quant = int(r) - 2 + 6 * int(h);
int wbits = ise_bitcount(wtotal, quant);
if (wtotal > 64 || wbits < 24 || wbits > 96 || wx > bw || wy > bh) return H;
int pcount = int((b.x >> 11) & 3u) + 1;
if (dual != 0 && pcount == 4) return H;
// colour endpoint modes
int cem0, cem1 = 0, cem2 = 0, cem3 = 0;
int below = 128 - wbits, high = 0;
if (pcount == 1) {
cem0 = int((b.x >> 13) & 0xFu);
} else {
high = 3 * pcount - 4;
below -= high;
uint enc = ((b.x >> 23) & 0x3Fu) | rbits(b, below, high) << 6;
uint base = enc & 3u;
if (base == 0u) {
cem0 = cem1 = cem2 = cem3 = int((enc >> 2) & 0xFu);
below += high;
high = 0;
} else {
base--;
cem0 = int((((enc >> 2) & 1u) + base) << 2 | ((enc >> uint(2 + pcount)) & 3u));
cem1 = int((((enc >> 3) & 1u) + base) << 2 | ((enc >> uint(4 + pcount)) & 3u));
cem2 = int((((enc >> 4) & 1u) + base) << 2 | ((enc >> uint(6 + pcount)) & 3u));
cem3 = int((((enc >> 5) & 1u) + base) << 2 | ((enc >> uint(8 + pcount)) & 3u));
}
}
int n0 = ((cem0 >> 2) + 1) * 2, n1 = pcount > 1 ? ((cem1 >> 2) + 1) * 2 : 0;
int n2 = pcount > 2 ? ((cem2 >> 2) + 1) * 2 : 0, n3 = pcount > 3 ? ((cem3 >> 2) + 1) * 2 : 0;
int ccount = n0 + n1 + n2 + n3;
if (ccount > 18) return H;
int cbits = max((pcount == 1 ? 111 : 99) - wbits - high - (dual != 0 ? 2 : 0), 0);
int cq = -1;
for (int q = 20; q >= 0; q--) {
if (ise_bitcount(ccount, q) <= cbits) { cq = q; break; }
}
if (cq < 4) return H; // QUANT_6
H.solid = false;
H.wx = wx; H.wy = wy; H.dual = dual; H.quant = quant; H.wtotal = wtotal; H.pcount = pcount;
H.cem0 = cem0; H.cem1 = cem1; H.cem2 = cem2; H.cem3 = cem3; H.n0 = n0; H.n1 = n1; H.n2 = n2;
H.ccount = ccount; H.cq = cq;
H.ccs = dual != 0 ? int(rbits(b, below - 2, 2)) : -1;
return H;
}
// A workgroup decodes BPW blocks of one block row when a block has 64 texels or fewer (64 / texels of them), else one
// block: S lanes per block (its texels, at most 64). The lanes of a block decode its weights and colour values into
// shared memory, the first pcount lanes then unpack the endpoints, and each lane writes its texels.
const int MAXB = 4; // 4x4: 4 blocks of 16 lanes
shared uint s_w[MAXB][64]; // unquantized weights 0..64 (dual plane: interleaved as in the bit stream)
shared int s_cv[MAXB][18]; // unquantized colour values
shared uvec2 s_e0[MAXB][4], s_e1[MAXB][4]; // 16-bit endpoints, two channels per uint
uvec2 pack16(ivec4 e) { return uvec2(uint(e.x) | uint(e.y) << 16, uint(e.z) | uint(e.w) << 16); }
int ch16(uvec2 p, int c) { return int(((c < 2 ? p.x : p.y) >> (16 * (c & 1))) & 0xFFFFu); }
void main() {
int bw = int(pc.bw), bh = int(pc.bh), G = bw * bh;
int bpw = G <= 64 ? 64 / G : 1, S = G <= 64 ? G : 64;
int l = int(gl_LocalInvocationID.x), lb = l / S, lane = l - lb * S;
bool live = lb < bpw;
int bxi = int(gl_WorkGroupID.x) * bpw + lb, byi = int(gl_WorkGroupID.y);
live = live && bxi < int(pc.bx);
lb = min(lb, MAXB - 1);
uvec4 b = live ? blk[pc.in_base + gl_WorkGroupID.z * pc.bslice + uint(byi) * pc.bx + uint(bxi)] : uvec4(0u);
Hdr H = header(b, bw, bh);
bool work = live && !H.solid;
if (work) {
uvec4 rev = uvec4(bitfieldReverse(b.w), bitfieldReverse(b.z), bitfieldReverse(b.y), bitfieldReverse(b.x));
uint wrow = tab[H.quant];
for (int j = lane; j < H.wtotal; j += S) s_w[lb][j] = tab_byte(wrow + ise_at(rev, 0, H.wtotal, H.quant, j));
uint crow = H.cq == 20 ? 0u : tab[12 + H.cq - 4];
int cstart = H.pcount == 1 ? 17 : 29;
for (int j = lane; j < H.ccount; j += S) {
uint raw = ise_at(b, cstart, H.ccount, H.cq, j);
s_cv[lb][j] = H.cq == 20 ? int(raw) : int(tab_byte(crow + raw));
}
}
barrier();
if (work && lane < H.pcount) {
int p = lane;
int cem = p == 0 ? H.cem0 : p == 1 ? H.cem1 : p == 2 ? H.cem2 : H.cem3;
int k = p == 0 ? 0 : p == 1 ? H.n0 : p == 2 ? H.n0 + H.n1 : H.n0 + H.n1 + H.n2;
int n = ((cem >> 2) + 1) * 2;
int v[8];
for (int j = 0; j < 8; j++) {
v[j] = 0;
if (j < n) v[j] = s_cv[lb][k + j];
}
ivec4 e0, e1;
if (!unpack_endpoints(cem, v, e0, e1)) { e0 = ivec4(0xFF, 0, 0xFF, 0xFF); e1 = e0; }
if (pc.srgb != 0u) { e0 = e0 << 8 | 0x80; e1 = e1 << 8 | 0x80; }
else { e0 *= 257; e1 *= 257; }
s_e0[lb][p] = pack16(e0);
s_e1[lb][p] = pack16(e1);
}
barrier();
if (!live) return;
bool small = G < 32;
int seed = int((b.x >> 13) & 0x3FFu);
int sx = int((1024 + bw / 2) / (bw - 1)), sy = int((1024 + bh / 2) / (bh - 1));
for (int t = lane; t < G; t += S) {
int x = t % bw, y = t / bw;
uint gx = uint(bxi * bw + x), gy = uint(byi * bh + y);
if (gx >= pc.width || gy >= pc.height) continue;
uint o = H.konst;
if (!H.solid) {
int p = H.pcount > 1 ? part_of(seed, H.pcount, small ? x << 1 : x, small ? y << 1 : y) : 0;
int vx = (sx * x * (H.wx - 1) + 32) >> 6, vy = (sy * y * (H.wy - 1) + 32) >> 6;
int fs = vx & 0xF, ft = vy & 0xF;
int i0 = (vy >> 4) * H.wx + (vx >> 4);
int w11 = (fs * ft + 8) >> 4, w10 = fs - w11, w01 = ft - w11, w00 = 16 - fs - ft + w11;
int wcount = H.wx * H.wy;
int w[2];
for (int pl = 0; pl < 2; pl++) {
w[pl] = 0;
if (pl > H.dual) continue;
int acc = 0;
for (int c = 0; c < 4; c++) {
int idx = i0 + (c == 0 ? 0 : c == 1 ? 1 : c == 2 ? H.wx : H.wx + 1);
int f = c == 0 ? w00 : c == 1 ? w10 : c == 2 ? w01 : w11;
if (f == 0 || idx >= wcount) continue;
acc += int(s_w[lb][H.dual != 0 ? 2 * idx + pl : idx]) * f;
}
w[pl] = (acc + 8) >> 4;
}
uvec2 q0 = s_e0[lb][p], q1 = s_e1[lb][p];
o = 0u;
for (int c = 0; c < 4; c++) {
int wc = c == H.ccs ? w[1] : w[0];
o |= uint((ch16(q0, c) * (64 - wc) + ch16(q1, c) * wc + 32) >> 14) << uint(8 * c);
}
}
px[pc.out_base + (gl_WorkGroupID.z * pc.height + gy) * pc.width + gx] = o;
}
}
+19 -2
View File
@@ -138,8 +138,24 @@ static void get_info(Cpu *c, uint64_t pc) {
c->x[1] = v;
}
static void svc_body(Cpu *c, uint32_t imm, uint64_t pc);
void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) {
kern_checkpoint();
if (__builtin_expect(!kern_host_track || !c->thread, 1)) {
svc_body(c, imm, pc);
return;
}
void *t = c->thread; // kern_host_track: the SVC counted in the thread's host depth (hostfn.c hostfn_tracked)
int *d = kern_host_depth(t);
__atomic_store_n(d, *d + 1, __ATOMIC_RELAXED);
svc_body(c, imm, pc);
int n = *d - 1;
__atomic_store_n(d, n, __ATOMIC_RELAXED);
if (!n && kern_preempt_pending(t)) kern_host_left(t);
}
static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) {
__atomic_fetch_add(&g_svcs, 1, __ATOMIC_RELAXED);
if (rt_trace & RT_TRACE_SVC) {
char at[160];
@@ -200,11 +216,12 @@ void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) {
return;
}
case 0x29: get_info(c, pc); return;
case 0x07: { // ExitProcess, as closing the window (gfx_window.c WM_CLOSE): pipeline cache and shader records
// saved, logs flushed, exit code 0
case 0x07: { // ExitProcess, as closing the window (gfx_window.c WM_CLOSE): pipeline cache, shader records and
// profile saved, logs flushed, exit code 0
char at[160];
rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at));
vk_pipeline_save();
prof_write();
fflush(NULL);
if (svc_test_exit_hook) return svc_test_exit_hook(0);
ExitProcess(0);
+9 -3
View File
@@ -17,6 +17,11 @@ int nvn_deswizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const vo
size_t dst_pitch);
int nvn_swizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src_linear, size_t src_pitch,
void *dst_storage);
// One box of a level and layer, [x, x + wb) bytes by [y, y + h) block rows of slice z (3D textures), between the storage
// and caller rows of pitch bytes, touching nothing else of the level: ReadTexels / WriteTexels of a region cost the
// region, not the level. Same textures as nvn_deswizzle; -1 for others and for a box outside the level.
int nvn_texels_box(const nvn_texdesc *d, unsigned level, unsigned layer, uint32_t x, uint32_t y, uint32_t z, uint32_t wb,
uint32_t h, void *storage, void *rows, size_t pitch, int to_linear);
// Byte offset of (x byte, y block row, z slice) inside one level of block height 2^th and block depth 2^td GOBs.
uint64_t nvn_bl_offset(uint32_t x, uint32_t y, uint32_t z, uint32_t row_bytes, uint32_t rows, unsigned th, unsigned td);
// Geometry of a level as the two functions above use it; -1 for textures they reject.
@@ -44,7 +49,8 @@ typedef struct TexVkFormat {
VkFormatFeatureFlags need; // optimal-tiling features vk must report for sampling and upload
uint8_t expand_rgb; // 3-component format: the upload expands each texel to 4 components (alpha / integer 1)
uint8_t alpha_one; // "X" format: sample alpha as 1 (view swizzle)
uint8_t astc_bw, astc_bh; // nonzero: ASTC, decoded on the CPU by astc_decode_ldr into vk (RGBA8, same colour space)
uint8_t astc_bw, astc_bh; // nonzero: ASTC decoded into vk (RGBA8, same colour space) by vk_astc.c or tex_astc.c;
// zero for native ASTC (vk == ideal, raw blocks as for BC)
uint8_t astc_srgb;
uint8_t depth, stencil; // aspect flags of depth / stencil formats
const char *note; // why a fallback or an expansion was chosen; NULL when the mapping is direct
@@ -52,8 +58,8 @@ typedef struct TexVkFormat {
} TexVkFormat;
// 1 when nvn_format (an NVNformat value) has a Vulkan mapping, 0 otherwise (out->reason says why). The mapping is
// fixed: ASTC always goes through the CPU decoder, D24S8 is D32S8, DEPTH24 is D32. Whether the GPU reports the
// features is a separate question (tex_vk_format_supported).
// fixed per device: ASTC decodes to RGBA8 unless vk_astc_native (TOTK_ASTC) takes the format as it is, D24S8 is
// D32S8, DEPTH24 is D32. Whether the GPU reports the features is a separate question (tex_vk_format_supported).
int tex_vk_format(int nvn_format, TexVkFormat *out);
// 1 when vk_ctx()->phys reports every bit of f->need for f->vk in optimal tiling. Needs vk_init().
int tex_vk_format_supported(const TexVkFormat *f);
+46 -4
View File
@@ -46,15 +46,25 @@ int nvn_deswizzle_geom(const nvn_texdesc *d, unsigned level, unsigned layer,
return 0;
}
// nvn_bl_offset per row instead of per 16 bytes: within a row, GOB column k starts k blocks further, and the four
// 16-byte segments of a GOB row (x % 64 = 0, 16, 32, 48) sit at 0, 32, 256 and 288 of it.
void tex_bl_copy(uint8_t *bl, uint8_t *lin, size_t pitch, uint32_t row_bytes, uint32_t rows, uint32_t slices,
unsigned th, unsigned td, int to_linear) {
static const uint32_t seg[4] = {0, 32, 256, 288};
uint64_t block = 512ull << th << td;
for (uint32_t z = 0; z < slices; z++)
for (uint32_t y = 0; y < rows; y++) {
uint8_t *row = lin + ((size_t)z * rows + y) * pitch;
for (uint32_t x = 0; x < row_bytes; x += 16) { // 16-byte segments: contiguous within a GOB, and 16 divides every block width
uint8_t *row = lin + ((size_t)z * rows + y) * pitch, *g = bl + nvn_bl_offset(0, y, z, row_bytes, rows, th, td);
uint32_t x = 0;
if (to_linear)
for (; x + 64 <= row_bytes; x += 64, g += block)
for (int s = 0; s < 4; s++) memcpy(row + x + 16 * s, g + seg[s], 16);
else
for (; x + 64 <= row_bytes; x += 64, g += block)
for (int s = 0; s < 4; s++) memcpy(g + seg[s], row + x + 16 * s, 16);
for (int s = 0; x < row_bytes; s++, x += 16) { // a partial GOB at the row's end
uint32_t n = row_bytes - x < 16 ? row_bytes - x : 16;
uint8_t *p = bl + nvn_bl_offset(x, y, z, row_bytes, rows, th, td);
if (to_linear) memcpy(row + x, p, n); else memcpy(p, row + x, n);
if (to_linear) memcpy(row + x, g + seg[s], n); else memcpy(g + seg[s], row + x, n);
}
}
}
@@ -80,6 +90,38 @@ static int move(const nvn_texdesc *d, unsigned level, unsigned layer, uint8_t *b
return 0;
}
int nvn_texels_box(const nvn_texdesc *d, unsigned level, unsigned layer, uint32_t x, uint32_t y, uint32_t z, uint32_t wb,
uint32_t h, void *storage, void *rows, size_t pitch, int to_linear) {
uint8_t *st = storage, *ln = rows;
if (nvn_tex_is_linear(d)) {
if (level || layer || z || d->samples > 1 || (d->target != 0 && d->target != 1 && d->target != 7)) return -1;
int bw, bh, bpb = nvn_fmt_bpb(d->format, &bw, &bh);
if (!bpb) return -1;
size_t row = (size_t)((d->width + bw - 1) / bw) * bpb;
uint32_t nrows = d->target == 0 ? 1 : (d->height + bh - 1) / bh, stride = nvn_tex_linear_stride(d);
if (stride < row || x + (size_t)wb > row || y + (uint64_t)h > nrows) return -1;
for (uint32_t r = 0; r < h; r++) {
uint8_t *p = st + (size_t)(y + r) * stride + x, *q = ln + r * pitch;
if (to_linear) memcpy(q, p, wb); else memcpy(p, q, wb);
}
return 0;
}
uint32_t row_bytes, nrows, slices, bpb; uint64_t base; unsigned th, td;
if (nvn_deswizzle_geom(d, level, layer, &row_bytes, &nrows, &slices, &base, &th, &td, &bpb)) return -1;
if (x + (uint64_t)wb > row_bytes || y + (uint64_t)h > nrows || z >= slices) return -1;
for (uint32_t r = 0; r < h; r++) {
uint8_t *q = ln + r * pitch;
for (uint32_t i = 0; i < wb;) {
uint32_t xb = x + i, n = 16 - xb % 16; // contiguous up to the end of its 16-byte segment
if (n > wb - i) n = wb - i;
uint8_t *p = st + base + nvn_bl_offset(xb, y + r, z, row_bytes, nrows, th, td);
if (to_linear) memcpy(q + i, p, n); else memcpy(p, q + i, n);
i += n;
}
}
return 0;
}
int nvn_deswizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src, void *dst, size_t dst_pitch) {
return move(d, level, layer, (uint8_t *)src, (uint8_t *)dst, dst_pitch, 1);
}
+60 -1
View File
@@ -118,6 +118,65 @@ static int test_deswizzle(void) {
return 0;
}
// nvn_texels_box against the whole-level path on random boxes: a box read equals the box cut from the deswizzled level,
// and a box write leaves the storage as deswizzle, overwrite the box, swizzle does (nothing outside the box changes).
static int check_box(const nvn_texdesc *d, unsigned lv, unsigned layer) {
uint32_t rb, rows, sl, bpb; uint64_t base; unsigned th, td;
if (nvn_deswizzle_geom(d, lv, layer, &rb, &rows, &sl, &base, &th, &td, &bpb)) { printf("probe-vkmem: FAIL box geometry\n"); return 1; }
nvn_texlayout L;
nvn_tex_layout(d, &L);
uint8_t *store = malloc(L.size), *want = malloc(L.size), *lin = malloc((size_t)rb * rows * sl);
for (uint64_t i = 0; i < L.size; i++) store[i] = rnd8();
uint32_t cols = rb / bpb;
for (int k = 0; k < 8; k++) {
uint32_t bx = rnd8() % cols, bn = 1 + rnd8() % (cols - bx), y = rnd8() % rows, h = 1 + rnd8() % (rows - y);
uint32_t z = sl > 1 ? rnd8() % sl : 0, x = bx * bpb, wb = bn * bpb;
size_t pitch = wb + 8;
uint8_t *box = malloc(pitch * h), *ref = malloc(pitch * h);
// read
if (nvn_deswizzle(d, lv, layer, store, lin, rb)) { printf("probe-vkmem: FAIL box deswizzle\n"); return 1; }
memset(box, 0xAB, pitch * h), memset(ref, 0xAB, pitch * h);
for (uint32_t r = 0; r < h; r++) memcpy(ref + r * pitch, lin + ((size_t)z * rows + y + r) * rb + x, wb);
if (nvn_texels_box(d, lv, layer, x, y, z, wb, h, store, box, pitch, 1) || memcmp(box, ref, pitch * h)) {
printf("probe-vkmem: FAIL box read format %#x level %u layer %u box %u,%u,%u %ux%u\n", d->format, lv, layer, x, y, z, wb, h);
return 1;
}
// write
for (size_t i = 0; i < pitch * h; i++) box[i] = rnd8();
memcpy(want, store, L.size);
for (uint32_t r = 0; r < h; r++) memcpy(lin + ((size_t)z * rows + y + r) * rb + x, box + r * pitch, wb);
if (nvn_swizzle(d, lv, layer, lin, rb, want)) { printf("probe-vkmem: FAIL box swizzle\n"); return 1; }
if (nvn_texels_box(d, lv, layer, x, y, z, wb, h, store, box, pitch, 0) || memcmp(store, want, L.size)) {
printf("probe-vkmem: FAIL box write format %#x level %u layer %u box %u,%u,%u %ux%u\n", d->format, lv, layer, x, y, z, wb, h);
return 1;
}
g_cases++;
free(box), free(ref);
}
free(store), free(want), free(lin);
return 0;
}
static int test_box(void) {
// one format per block size: 1, 8 and 16 bytes per texel, 8 and 16 bytes per 4x4 block
int fmt[5] = {0}, n = 0;
static const int want_bpb[5] = {1, 8, 16, 8, 16}, want_bw[5] = {1, 1, 1, 4, 4};
for (int f = 1; f < 0x200 && n < 5; f++) {
int bw, bh, bpb = nvn_fmt_bpb(f, &bw, &bh);
for (int i = 0; i < 5; i++)
if (!fmt[i] && bpb == want_bpb[i] && bw == want_bw[i] && bh == bw) fmt[i] = f, n++;
}
for (int i = 0; i < 5; i++) {
if (!fmt[i]) continue;
nvn_texdesc d2 = {.target = 1, .format = fmt[i], .width = 300, .height = 170, .depth = 1, .levels = 3};
nvn_texdesc da = {.target = 4, .format = fmt[i], .width = 96, .height = 80, .depth = 3, .levels = 2};
nvn_texdesc d3 = {.target = 2, .format = fmt[i], .width = 72, .height = 40, .depth = 5, .levels = 2};
for (unsigned lv = 0; lv < 2; lv++)
if (check_box(&d2, lv, 0) || check_box(&da, lv, 2) || check_box(&d3, lv, 0)) return 1;
}
return 0;
}
// ---- ASTC ----
static void void_block(uint8_t b[16], uint16_t r, uint16_t g, uint16_t bl, uint16_t a, int hdr) {
uint64_t lo = 0xFFFFFFFFFFFFFDFCull; // 0x1FC mode, LDR, reserved bits 11, extent all ones
@@ -522,7 +581,7 @@ static int test_gpu(void) {
}
int probe_vkmem(void) {
if (test_deswizzle() || test_astc() || test_formats()) return 1;
if (test_deswizzle() || test_box() || test_astc() || test_formats()) return 1;
printf("probe-vkmem: cpu part passed (%u deswizzle cases, %u astc blocks, formats: %u mapped, %u supported by the GPU, "
"%u unsupported by the GPU, %u unmapped, %u with a fallback)\n",
g_cases, g_astc_blocks, n_mapped, n_supported, n_unsupported, n_unmapped, n_fallback);
+18
View File
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Embedded by totk.rc. activeCodePage makes UTF-8 the process code page (Windows 10 1903 and later): the narrow file
functions (fopen, CreateFileA, GetModuleFileNameA) and getenv then carry UTF-8, the same encoding platform/win32.c
and fs_host.c convert with, so an install folder or user profile with a non-ASCII name works. -->
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker"/>
</requestedPrivileges>
</security>
</trustInfo>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
</windowsSettings>
</application>
</assembly>
+3
View File
@@ -0,0 +1,3 @@
/* The application manifest of totk.exe (totk.manifest). tools/build_game.py compiles it with the runtime objects, so it
is also a member of libtotkrt.a, which the release links with --whole-archive. */
1 24 "totk.manifest"
+96 -1
View File
@@ -37,7 +37,8 @@ typedef struct VkCtx {
int validation; // layer active
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_robustness2, has_buffer_device_address, has_fragment_shader_interlock,
has_astc_ldr; // textureCompressionASTC_LDR (enabled when reported)
// 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
@@ -114,13 +115,65 @@ void vk_guest_written(uint64_t addr, uint64_t size); // a host write write-wat
// GPU address -> backing (section 3.1). avail: bytes from offset that stay contiguous in buffer. alloc/alloc_offset:
// the device-local memory behind physical and (through the page table) virtual pool addresses, NULL for imported pools.
// cls: the pool class of the backing (VK_MCLS_*), for the traffic census.
typedef struct {
VkBuffer buffer;
VkDeviceSize offset, avail;
VmaAllocation alloc;
VkDeviceSize alloc_offset;
int cls;
void *pool; // the imported pool record behind the span (NULL for device memory), for VK_MEM_BOUND
} VkMemSpan;
// Traffic census (vk_memory.c vk_mem_census): bytes the replay binds or copies, by pool class and use. Imported pools by
// their CPU access flag; DEVICE is physical memory (directly or through a virtual pool); SCRATCH the replay's transient
// copies. CPU_READ counts bytes host code reads from pool storage in place of the GPU (misaligned binds, byte indices,
// counter words); GPU_WRITE what copies, fills, counter reports and uniform updates write.
enum { VK_MCLS_NONE, VK_MCLS_HOST_NOACC, VK_MCLS_HOST_UNC, VK_MCLS_HOST_CACHED, VK_MCLS_DEVICE, VK_MCLS_SCRATCH, VK_MCLS_N };
enum { VK_MUSE_VB, VK_MUSE_IB, VK_MUSE_UBO, VK_MUSE_SSBO, VK_MUSE_IND, VK_MUSE_COPY_SRC, VK_MUSE_GPU_WRITE,
VK_MUSE_CPU_READ, VK_MUSE_N };
void vk_mem_count(int cls, int use, uint64_t bytes);
// TOTK_TRACE=perf dirty census (vk_memory.c): the replay notes each imported pool it binds as a vertex, index, uniform,
// storage or indirect buffer; at the start of every submit the pages the CPU wrote in such pools since the previous
// submit are counted from the shared page generations and logged as `mem: dirty` every 60 frames. Nothing when tracing
// is off: VK_MEM_BOUND is one flag test at the call site.
void vk_mem_bound_note(void *pool);
#define VK_MEM_BOUND(sp) do { if ((rt_trace & RT_TRACE_PERF) && (sp).pool) vk_mem_bound_note((sp).pool); } while (0)
void vk_mem_dirty_submit(void); // vk_sync_submit, under its lock, before the replay
// VRAM mirror of buffer-bound pools (vk_memory.c, build/mem-plan.md). vk_mirror_on: TOTK_MEMORY=mirror|verify, or the
// device class is discrete and TOTK_MEMORY is unset or auto; set at device selection. The replay (submitting thread
// under vk_sync_submit's lock) calls:
// - VK_MEM_USE at a vertex, index, uniform, storage or indirect binding of a span vk_mem_resolve returned: the pool's
// mirror is created on first use and sp.buffer becomes it (same offsets); a pool that stays imported keeps its buffer.
// - VK_MEM_PICK where a copy, fill or update reads or writes a span: the mirror if the pool has one, never a new one.
// - vk_mem_gpu_wrote after recording a GPU write of [sp.offset, sp.offset + size) into sp.buffer: a mirror's bytes go
// back to guest memory at the end of the submission. No-op for imported buffers.
// - vk_mem_mirror_begin at the start of a submission's recording, vk_mem_mirror_end (records the write-backs; returns
// the commands recorded) after its last barrier.
// vk_sync.c records the copies the mirrors' first accesses queued (vk_mem_mirror_pending) into a prologue command
// buffer with vk_mem_mirror_prologue and submits it right before each part.
extern int vk_mirror_on;
void vk_mem_use(VkMemSpan *sp);
void vk_mem_pick(VkMemSpan *sp);
#define VK_MEM_USE(sp) do { if (vk_mirror_on && (sp).pool) vk_mem_use(&(sp)); } while (0)
#define VK_MEM_PICK(sp) do { if (vk_mirror_on && (sp).pool) vk_mem_pick(&(sp)); } while (0)
void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size);
void vk_mem_mirror_begin(void);
int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence);
int vk_mem_mirror_pending(void);
void vk_mem_mirror_prologue(VkCommandBuffer cb);
int vk_mem_mirror_probe(uint64_t addr); // --probe-vkexec: the pool at addr gets its mirror as on a first buffer use;
// 1 when it has one
void vk_mem_class_log(const VkPhysicalDeviceProperties *p, const VkPhysicalDeviceMemoryProperties *m); // once
int vk_mem_class_of(uint64_t addr); // VK_MCLS_* of the pool at addr (NONE when unbacked)
void vk_mem_census(uint64_t frame); // at each present: pool census and traffic, logged at frame 1800 and,
// with TOTK_TRACE=perf, every 1800 frames
// VRAM in use and its budget, summed over the DEVICE_LOCAL heaps. exact 1: VK_EXT_memory_budget (the whole process),
// 0: this allocator's own allocations against the heap sizes. 0 when unknown.
int vk_mem_vram(uint64_t *used, uint64_t *budget, int *exact);
int vk_mem_resolve(uint64_t addr, uint64_t size, VkMemSpan *out); // 1 when all of [addr, addr+size) is backed
// Changes whenever vk_mem_resolve plus VK_MEM_USE may give another span for an address: a pool added, finalized or
// mapped, a mirror created, refused or forgotten, a submission started.
uint64_t vk_mem_gen(void);
// Page write generations over guest memory (mem_written_pages plus vk_guest_written). Returns 1 when a page of the
// range was written after generation `since`, 0 when not, -1 when the range is not write-watched (treat as written);
// *now is the generation to pass next time.
@@ -146,6 +199,27 @@ uint64_t vk_upload_flush(int wait);
void vk_defer_image(VkImage img, VmaAllocation alloc); // alloc NULL: vkDestroyImage only
void vk_defer_view(VkImageView view);
void vk_defer_buffer(VkBuffer buf, VmaAllocation alloc);
// ---- ASTC (vk_astc.c). TOTK_ASTC = auto | gpu | native | cpu | check; see vk_astc.c.
enum { VK_ASTC_CPU, VK_ASTC_GPU, VK_ASTC_NATIVE, VK_ASTC_CHECK };
int vk_astc_mode(void); // decided once per device (logged); VK_ASTC_CPU before vk_init
int vk_astc_native(VkFormat astc_fmt); // 1: images of this ASTC format are created as such and get the raw blocks
int vk_astc_gpu(void); // 1: ASTC images decoded to RGBA8 by the compute decoder (vk_astc_record)
// One image region decoded from raw blocks: tight rows of 16-byte blocks at byte src of the source buffer (slices one
// after the other), w x h texels and slices deep, written at offset of level / layer.
typedef struct { VkDeviceSize src; uint32_t w, h, slices, level, layer; VkOffset3D offset; } VkAstcRegion;
// Records into cb, the open upload batch (between vk_upload_begin and vk_upload_end): src (TRANSFER_SRC, src_size
// bytes of blocks) goes to a device-local scratch buffer, a dispatch per region decodes it, and the texels are copied
// into img (VK_IMAGE_LAYOUT_GENERAL). The caller records its usual barriers around an image copy before and after the
// call. readback (optional, host-visible, TRANSFER_DST): also gets the decoded texels, the regions one after the other.
// 0 when the decoder is unavailable or the scratch buffer cannot be allocated (nothing recorded).
int vk_astc_record(VkCommandBuffer cb, VkBuffer src, VkDeviceSize src_size, const VkAstcRegion *rg, int n, uint32_t bw,
uint32_t bh, int srgb, VkImage img, VkImageAspectFlags aspect, VkBuffer readback);
void vk_astc_stats(uint64_t *dispatches, uint64_t *texels); // since start
// TOTK_ASTC_TIME=1: GPU timestamps around each vk_astc_record's dispatches (logged per footprint and the largest one)
void vk_astc_time_enable(int on);
int vk_astc_time_get(uint32_t bw, uint32_t bh, uint64_t *texels, uint64_t *calls, double *us); // totals so far
void vk_astc_time_summary(void); // logs the totals
void vk_astc_release(void); // vk_device.c, before the device goes: frees the decoder's objects
void vk_mem_note_submit(uint64_t timeline_value); // T6: after each submit that signals ctx->timeline with this value
void vk_mem_collect(void); // frees what has completed
typedef struct { uint64_t imported_pools, physical_pools, virtual_pools, import_failures, upload_batches; } VkMemStats;
@@ -185,17 +259,25 @@ void vk_texture_unlock(void);
const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view);
VkSampler vk_sampler_by_id_locked(int id);
uint32_t vk_texture_slots_gen(void);
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
uint64_t vk_texture_last_write(const tex_t *t); // timeline value of the newest submission recorded to write it; 0: none
size_t vk_texture_texel_span(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint64_t row, uint64_t img); // bytes ReadTexels spans; 0 for a bad region
int vk_texture_small_dirty(void); // a small mirrored target holds GPU texels its storage lacks
void vk_texture_write_back_small(uint64_t fence); // writes those rendered by work up to fence back to storage
void vk_texture_debug(const VkTexImage *img, uint32_t *uploads, int *last_change, int *pages_written); // vk_dump
int vk_texture_slot_swz(int id, uint8_t swz[4]); // vk_dump: swizzle of the descriptor in slot id
int vk_texture_read_image(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row,
uint64_t img); // always from the image (GPU copy, waited for); 0 on failure
int vk_texture_cpu_stale(uint64_t addr, uint64_t size); // a GPU-written mirrored texture there has stale storage
uint64_t vk_texture_dirty_fence(uint64_t addr, uint64_t size); // newest recorded writer of such a texture; 0: none
void vk_texture_write_back(uint64_t addr, uint64_t size); // writes those images back into their storage (waits)
void vk_texture_range_gone(uint64_t lo, uint64_t size, VmaAllocation alloc); // vk_pool_fini: drop the images there
void vk_texture_stats(uint64_t *images, uint64_t *uploads, uint64_t *reuploads);
void vk_texture_shutdown(void); // vk_mem_shutdown calls it
void vk_texture_new_submit(void); // a queue submit starts: draw bindings check storage again
uint32_t vk_texture_epoch(void); // the write-watch snapshot epoch vk_texture_new_submit started
VkSampler vk_sampler_by_id(int id); // VK_NULL_HANDLE for an empty slot
VkSampler vk_sampler_for(const samp_par *s); // cached by content
int vk_sampler_params(int id, samp_par *out); // parameters of a sampler slot; 0 for an empty slot
@@ -208,8 +290,10 @@ void rt_write_png(const char *path, const uint8_t *rgba, uint32_t w, uint32_t h)
// once the GPU reached it and the completion thread ran its side effects. Guest or host threads.
void vk_sync_start(void); // vk_init (fatal without a device) and the completion thread; idempotent
uint64_t vk_sync_submit(const uint64_t *handles, int n); // records with vk_exec_record, submits; returns the value
uint64_t vk_sync_reserve(void); // asynchronous replay: the value the next queued submission signals
int vk_sync_fence_wait(uint64_t value, uint64_t timeout_ns); // 1 when done; 0 on timeout or a value never submitted
uint64_t vk_sync_fence_done(void); // highest done value
uint64_t vk_sync_issued(void); // newest submitted value (the one being recorded is this + 1)
void vk_sync_finish(void); // every submitted value done
void vk_sync_shutdown(void); // finish, stop the completion thread, free the command buffers
// The op-stream replay (vk_exec.c: T6c): records the command handles of one submission into cb, which the caller has
@@ -229,6 +313,7 @@ void rdoc_init(void); // runtime/rdoc_win.c: TOTK_RDOC debug
int rdoc_active(void);
void rdoc_after_present(uint64_t frame);
void vk_present_drain(void); // returns once every queued present was submitted to gfx_queue
int vk_replay_async(void); // TOTK_REPLAY_ASYNC (default 1): submissions replay on the replay thread
void vk_program_release(const prog_t *p); // GfxBackend.program_release: pipelines (T6b); weak no-op in vk_backend.c
void vk_backend_shutdown(void); // presents, vk_sync_shutdown, vk_mem_shutdown, vk_shutdown in that order
@@ -290,7 +375,13 @@ typedef struct {
VkDescriptorSetLayout set_layout1;
int push1;
uint32_t n_sets;
// Descriptor update template per set (vk_pipeline.c make_tpl): reads one VkDescSlot per descriptor, in binding order,
// from the start of the set's descriptors. VK_NULL_HANDLE: the set is written with VkWriteDescriptorSet structs.
VkDescriptorUpdateTemplate tpl[2];
} VkPipeInfo;
// One descriptor of the packed array a descriptor update template reads (VkPipeInfo.tpl), 24 bytes for every type; the
// bytes a type leaves unused are zero, so equal descriptors compare equal byte for byte.
typedef union { VkDescriptorImageInfo image; VkDescriptorBufferInfo buffer; VkBufferView view; uint64_t w[3]; } VkDescSlot;
// Both return the cached pipeline (created on first use; failures are cached and logged once). info: may be NULL.
// Valid until vk_pipeline_shutdown; thread-safe.
VkPipeline vk_pipeline_graphics(const VkPipeState *s, const VkPipeInfo **info);
@@ -324,6 +415,10 @@ VkPrimitiveTopology vk_pipeline_topology(int nvn_prim); // quads -> triangle l
// rewrites those draws; polygon -> fan
VkFormat vk_vertex_format(int nvn_format, int *type); // attribute format (supported fallback), GFX_TYPE_* in *type
PFN_vkCmdPushDescriptorSetKHR vk_pipeline_push_fn(void); // after the first vk_pipeline_* call
// Descriptor update templates (after the first vk_pipeline_* call): NULL when templates are off (TOTK_DESC_TEMPLATE=0,
// a device below Vulkan 1.1) or, for the push variant, vkCmdPushDescriptorSetWithTemplateKHR is missing.
PFN_vkCmdPushDescriptorSetWithTemplateKHR vk_pipeline_push_tpl_fn(void);
PFN_vkUpdateDescriptorSetWithTemplate vk_pipeline_update_tpl_fn(void);
// Probe only: 1 selects stage-independent layouts for pipelines looked up from now on, 0 the shared set 0; returns
// the previous mode. 1 needs vk_caps.gpl (the independent-sets flag comes with the extension).
int vk_pipeline_set_indep(int on);
+541
View File
@@ -0,0 +1,541 @@
// ASTC textures on the GPU: the TOTK_ASTC knob, a compute decoder that turns raw blocks into the RGBA8 image the CPU
// decoder (tex_astc.c) would have written, and the native ASTC formats where the device samples them itself.
//
// TOTK_ASTC = auto (default): native when the device reports textureCompressionASTC_LDR and the format's features,
// else gpu. gpu: compute decode into RGBA8 (runtime/shaders/astc_decode.comp). native: like auto, but logs when the
// device cannot. cpu: astc_decode_ldr_mt on the uploading thread, as before. check: gpu, and every upload is also
// decoded on the CPU and compared (waits for the GPU; for testing only).
//
// The decode is recorded into the upload batch (vk_memory.c) in chunks of at most 32 MiB of texels: a chunk's raw
// blocks go from staging to a device-local scratch buffer, its dispatches write RGBA8 texels behind them, and a
// buffer-to-image copy puts them into the image; the next chunk reuses the scratch after a barrier. The scratch
// buffer is freed once the batch completes (vk_defer_buffer).
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "astc_decode_spv.h"
#include "cfg.h"
#include "host.h"
#include "tex.h"
#include "vk.h"
// tex_astc.c's unquantization tables, as the shader reads them (Tables in astc_decode.comp): 12 + 16 row offsets in
// bytes, then the bytes. Kept in step with tex_astc.c by the oracle (--probe-astc), which compares every mode.
static const uint8_t wunq_data[] = {
0,64,
0,32,64,
0,21,43,64,
0,16,32,48,64,
0,64,12,52,25,39,
0,9,18,27,37,46,55,64,
0,64,7,57,14,50,21,43,28,36,
0,64,17,47,5,59,23,41,11,53,28,36,
0,4,8,12,17,21,25,29,35,39,43,47,52,56,60,64,
0,64,16,48,3,61,19,45,6,58,23,41,9,55,26,38,13,51,29,35,
0,64,8,56,16,48,24,40,2,62,11,53,19,45,27,37,5,59,13,51,22,42,30,34,
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,
};
static const uint16_t wunq_off[12] = {0,2,5,9,14,20,28,38,50,66,86,110};
static const uint8_t cunq_data[] = {
0,255,51,204,102,153,
0,36,73,109,146,182,219,255,
0,255,28,227,56,199,84,171,113,142,
0,255,69,186,23,232,92,163,46,209,116,139,
0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255,
0,255,67,188,13,242,80,175,27,228,94,161,40,215,107,148,54,201,121,134,
0,255,33,222,66,189,99,156,11,244,44,211,77,178,110,145,22,233,55,200,88,167,121,134,
0,8,16,24,33,41,49,57,66,74,82,90,99,107,115,123,132,140,148,156,165,173,181,189,198,206,214,222,
231,239,247,255,
0,255,32,223,65,190,97,158,6,249,39,216,71,184,104,151,13,242,45,210,78,177,110,145,19,236,52,203,
84,171,117,138,26,229,58,197,91,164,123,132,
0,255,16,239,32,223,48,207,65,190,81,174,97,158,113,142,5,250,21,234,38,217,54,201,70,185,86,169,
103,152,119,136,11,244,27,228,43,212,59,196,76,179,92,163,108,147,124,131,
0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,
130,134,138,142,146,150,154,158,162,166,170,174,178,182,186,190,195,199,203,207,211,215,219,223,227,
231,235,239,243,247,251,255,
0,255,16,239,32,223,48,207,64,191,80,175,96,159,112,143,3,252,19,236,35,220,51,204,67,188,83,172,
100,155,116,139,6,249,22,233,38,217,54,201,71,184,87,168,103,152,119,136,9,246,25,230,42,213,58,197,
74,181,90,165,106,149,122,133,13,242,29,226,45,210,61,194,77,178,93,162,109,146,125,130,
0,255,8,247,16,239,24,231,32,223,40,215,48,207,56,199,64,191,72,183,80,175,88,167,96,159,104,151,
112,143,120,135,2,253,10,245,18,237,26,229,35,220,43,212,51,204,59,196,67,188,75,180,83,172,91,164,
99,156,107,148,115,140,123,132,5,250,13,242,21,234,29,226,37,218,45,210,53,202,61,194,70,185,78,177,
86,169,94,161,102,153,110,145,118,137,126,129,
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,
70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,
126,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,
177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,215,217,219,221,223,225,
227,229,231,233,235,237,239,241,243,245,247,249,251,253,255,
0,255,8,247,16,239,24,231,32,223,40,215,48,207,56,199,64,191,72,183,80,175,88,167,96,159,104,151,
112,143,120,135,1,254,9,246,17,238,25,230,33,222,41,214,49,206,57,198,65,190,73,182,81,174,89,166,
97,158,105,150,113,142,121,134,3,252,11,244,19,236,27,228,35,220,43,212,51,204,59,196,67,188,75,180,
83,172,91,164,99,156,107,148,115,140,123,132,4,251,12,243,20,235,28,227,36,219,44,211,52,203,60,195,
68,187,76,179,84,171,92,163,100,155,108,147,116,139,124,131,6,249,14,241,22,233,30,225,38,217,46,
209,54,201,62,193,70,185,78,177,86,169,94,161,102,153,110,145,118,137,126,129,
0,255,4,251,8,247,12,243,16,239,20,235,24,231,28,227,32,223,36,219,40,215,44,211,48,207,52,203,56,
199,60,195,64,191,68,187,72,183,76,179,80,175,84,171,88,167,92,163,96,159,100,155,104,151,108,147,
112,143,116,139,120,135,124,131,1,254,5,250,9,246,13,242,17,238,21,234,25,230,29,226,33,222,37,218,
41,214,45,210,49,206,53,202,57,198,61,194,65,190,69,186,73,182,77,178,81,174,85,170,89,166,93,162,
97,158,101,154,105,150,109,146,113,142,117,138,121,134,125,130,2,253,6,249,10,245,14,241,18,237,22,
233,26,229,30,225,34,221,38,217,42,213,46,209,50,205,54,201,58,197,62,193,66,189,70,185,74,181,78,
177,82,173,86,169,90,165,94,161,98,157,102,153,106,149,110,145,114,141,118,137,122,133,126,129,
};
static const uint16_t cunq_off[16] = {0,6,14,24,36,52,72,96,128,168,216,280,360,456,584,744};
_Static_assert(sizeof wunq_data == 142 && sizeof cunq_data == 936, "unquantization table size");
enum { TAB_BYTES = 28 * 4, TAB_WUNQ = TAB_BYTES, TAB_CUNQ = TAB_BYTES + 144, TAB_SIZE = TAB_CUNQ + 936 };
typedef struct {
uint32_t in_base, out_base, width, height, bw, bh, bx, bslice, srgb;
} Push;
static SRWLOCK g_lock = SRWLOCK_INIT;
static struct {
VkDevice dev; // the device the objects below belong to (probes recreate it)
int mode; // VK_ASTC_*; -1 before the first vk_astc_mode with a device
int failed; // pipeline creation failed on dev: gpu falls back to cpu
VkDescriptorSetLayout dsl;
VkPipelineLayout layout;
VkPipeline pipe;
PFN_vkCmdPushDescriptorSetKHR push;
VkBuffer tab; // device-local copy of the tables
VmaAllocation tab_alloc;
int tab_uploaded;
} G = {.mode = -1};
static uint64_t n_dispatch, n_texels;
// TOTK_ASTC_TIME=1 (or vk_astc_time_enable): timestamps around the dispatches of each scratch chunk (the decode
// alone) and around a whole vk_astc_record call (one texture upload or WriteTexels: copies and barriers included),
// read back by later calls. Per footprint: texels and dispatch time; a new largest call is logged with both times.
// Needs hostQueryReset (a slot is reset on the host after its read).
#define TQ 256
static SRWLOCK t_lock = SRWLOCK_INIT;
static int t_on = -1; // -1: knob not read yet
static VkQueryPool t_pool;
static VkDevice t_dev;
static double t_ns_per_tick;
static uint64_t t_mask;
static struct { uint64_t texels; uint32_t id; uint8_t fp, busy, whole; } t_slot[TQ]; // whole: the call, else a chunk
static int t_next;
static struct { uint64_t texels, chunks; double us; } t_fp[13 * 13]; // [bw * 13 + bh]: dispatch time of the chunks
#define TA 64 // calls in flight, by id % TA
typedef struct { uint32_t id, w, h, regions, chunks, done; int complete; uint64_t texels; double us, total; uint8_t fp; } TCall;
static TCall t_call[TA];
static uint32_t t_id;
static uint64_t t_max_texels;
static double t_max_us, t_max_total;
static uint8_t t_max_fp;
static void time_init(VkCtx *c) { // t_lock held
if (t_on < 0) {
const char *e = cfg_get("TOTK_ASTC_TIME");
t_on = e && *e && *e != '0';
}
if (t_on <= 0 || (t_pool && t_dev == c->device)) return;
t_pool = VK_NULL_HANDLE;
memset(t_slot, 0, sizeof t_slot);
VkPhysicalDeviceVulkan12Features f12 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES};
VkPhysicalDeviceFeatures2 f2 = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &f12};
vkGetPhysicalDeviceFeatures2(c->phys, &f2);
uint32_t nq = 0;
vkGetPhysicalDeviceQueueFamilyProperties(c->phys, &nq, NULL);
VkQueueFamilyProperties *qp = calloc(nq ? nq : 1, sizeof *qp);
vkGetPhysicalDeviceQueueFamilyProperties(c->phys, &nq, qp);
uint32_t bits = c->gfx_family < nq ? qp[c->gfx_family].timestampValidBits : 0;
free(qp);
VkQueryPoolCreateInfo qi = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, .queryType = VK_QUERY_TYPE_TIMESTAMP,
.queryCount = 2 * TQ};
if (!f12.hostQueryReset || !bits || vkCreateQueryPool(c->device, &qi, NULL, &t_pool) != VK_SUCCESS) {
rt_log("vk: TOTK_ASTC_TIME: no hostQueryReset or no timestamps on the queue; ASTC decodes not timed");
t_pool = VK_NULL_HANDLE;
t_on = 0;
return;
}
vkResetQueryPool(c->device, t_pool, 0, 2 * TQ);
t_dev = c->device;
t_ns_per_tick = c->props.limits.timestampPeriod;
t_mask = bits >= 64 ? ~0ull : (1ull << bits) - 1;
}
// Takes in the slots whose timestamps are available; t_lock held.
static void time_poll_locked(VkCtx *c) {
if (!t_pool || t_dev != c->device) return;
for (int i = 0; i < TQ; i++) {
if (!t_slot[i].busy) continue;
uint64_t r[4];
if (vkGetQueryPoolResults(c->device, t_pool, 2 * (uint32_t)i, 2, sizeof r, r, 16,
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT) != VK_SUCCESS ||
!r[1] || !r[3])
continue;
double us = (double)((r[2] - r[0]) & t_mask) * t_ns_per_tick / 1e3;
int fp = t_slot[i].fp;
if (!t_slot[i].whole) t_fp[fp].texels += t_slot[i].texels, t_fp[fp].chunks++, t_fp[fp].us += us;
TCall *k = &t_call[t_slot[i].id % TA];
if (k->id == t_slot[i].id) {
if (t_slot[i].whole) k->total = us;
else k->us += us, k->texels += t_slot[i].texels;
if (++k->done == k->chunks + 1 && k->complete && k->texels > t_max_texels) {
t_max_texels = k->texels, t_max_us = k->us, t_max_total = k->total, t_max_fp = k->fp;
rt_log("vk: ASTC time: largest decode so far %.2f M texels (%dx%d; %u regions, the first %ux%u; %u scratch "
"chunks): dispatches %.0f us of GPU time (%.0f us per M texel), %.0f us with the copies",
(double)k->texels / 1e6, k->fp / 13, k->fp % 13, k->regions, k->w, k->h, k->chunks, k->us,
k->us * 1e6 / (double)k->texels, k->total);
}
}
t_slot[i].busy = 0;
vkResetQueryPool(c->device, t_pool, 2 * (uint32_t)i, 2);
}
}
// A free slot for a call's id (t_lock held); -1 when the ring is full.
static int time_slot(uint32_t id, uint8_t fp, uint64_t texels, int whole) {
for (int i = 0; i < TQ; i++, t_next = (t_next + 1) % TQ)
if (!t_slot[t_next].busy) {
int k = t_next;
t_slot[k].busy = 1, t_slot[k].id = id, t_slot[k].fp = fp, t_slot[k].texels = texels;
t_slot[k].whole = (uint8_t)whole;
t_next = (k + 1) % TQ;
return k;
}
return -1;
}
void vk_astc_time_enable(int on) {
AcquireSRWLockExclusive(&t_lock);
t_on = on;
ReleaseSRWLockExclusive(&t_lock);
}
int vk_astc_time_get(uint32_t bw, uint32_t bh, uint64_t *texels, uint64_t *calls, double *us) {
VkCtx *c = vk_ctx();
if (!c || bw > 12 || bh > 12) return 0;
AcquireSRWLockExclusive(&t_lock);
time_poll_locked(c);
int fp = (int)(bw * 13 + bh);
*texels = t_fp[fp].texels, *calls = t_fp[fp].chunks, *us = t_fp[fp].us;
ReleaseSRWLockExclusive(&t_lock);
return 1;
}
void vk_astc_time_summary(void) {
VkCtx *c = vk_ctx();
if (!c) return;
AcquireSRWLockExclusive(&t_lock);
if (t_on > 0) {
time_poll_locked(c);
for (int fp = 0; fp < 13 * 13; fp++)
if (t_fp[fp].chunks)
rt_log("vk: ASTC time: %dx%d: %llu dispatch batches, %.2f M texels, %.0f us GPU (%.0f us per M texel)",
fp / 13, fp % 13, (unsigned long long)t_fp[fp].chunks, (double)t_fp[fp].texels / 1e6, t_fp[fp].us,
t_fp[fp].us * 1e6 / (double)t_fp[fp].texels);
if (t_max_texels)
rt_log("vk: ASTC time: largest decode %.2f M texels (%dx%d): dispatches %.0f us GPU, %.0f us with the copies",
(double)t_max_texels / 1e6, t_max_fp / 13, t_max_fp % 13, t_max_us, t_max_total);
}
ReleaseSRWLockExclusive(&t_lock);
}
static void fill_tables(uint8_t *p) {
uint32_t off[28];
for (int i = 0; i < 12; i++) off[i] = TAB_WUNQ + wunq_off[i];
for (int i = 0; i < 16; i++) off[12 + i] = TAB_CUNQ + cunq_off[i];
memset(p, 0, TAB_SIZE);
memcpy(p, off, sizeof off);
memcpy(p + TAB_WUNQ, wunq_data, sizeof wunq_data);
memcpy(p + TAB_CUNQ, cunq_data, sizeof cunq_data);
}
// Objects of the current device; under g_lock (exclusive).
static void reset_for(VkCtx *c) {
if (G.dev == c->device) return;
memset(&G.dsl, 0, sizeof G - offsetof(__typeof__(G), dsl)); // the old device's objects went with it
G.dev = c->device;
G.failed = 0;
G.mode = -1;
}
static int create_pipeline(VkCtx *c) {
G.push = (PFN_vkCmdPushDescriptorSetKHR)vkGetDeviceProcAddr(c->device, "vkCmdPushDescriptorSetKHR");
VkDescriptorSetLayoutBinding b[3];
for (int i = 0; i < 3; i++)
b[i] = (VkDescriptorSetLayoutBinding){.binding = (uint32_t)i, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.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 = 3, .pBindings = b};
VkPushConstantRange pr = {VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(Push)};
VkShaderModuleCreateInfo mi = {.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = sizeof astc_decode_spv,
.pCode = astc_decode_spv};
VkShaderModule m = VK_NULL_HANDLE;
if (!G.push || vkCreateDescriptorSetLayout(c->device, &dci, NULL, &G.dsl) != VK_SUCCESS) return 0;
VkPipelineLayoutCreateInfo li = {.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1,
.pSetLayouts = &G.dsl, .pushConstantRangeCount = 1, .pPushConstantRanges = &pr};
if (vkCreatePipelineLayout(c->device, &li, NULL, &G.layout) != VK_SUCCESS) return 0;
if (vkCreateShaderModule(c->device, &mi, NULL, &m) != VK_SUCCESS) return 0;
LARGE_INTEGER t0, t1, fq;
QueryPerformanceCounter(&t0);
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 = G.layout};
VkResult r = vkCreateComputePipelines(c->device, VK_NULL_HANDLE, 1, &ci, NULL, &G.pipe);
vkDestroyShaderModule(c->device, m, NULL);
if (r != VK_SUCCESS) return 0;
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = TAB_SIZE,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT};
VmaAllocationCreateInfo aci = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
if (vmaCreateBuffer(c->vma, &bi, &aci, &G.tab, &G.tab_alloc, NULL) != VK_SUCCESS) return 0;
QueryPerformanceCounter(&t1);
QueryPerformanceFrequency(&fq);
rt_log("vk: ASTC compute decoder ready (pipeline built in %.1f ms)", (double)(t1.QuadPart - t0.QuadPart) * 1e3 / (double)fq.QuadPart);
return 1;
}
static int native_device(VkCtx *c) { return c->has_astc_ldr; }
static const char *mode_name(int m) {
return m == VK_ASTC_CPU ? "cpu" : m == VK_ASTC_GPU ? "gpu" : m == VK_ASTC_NATIVE ? "native" : "check";
}
int vk_astc_mode(void) {
VkCtx *c = vk_ctx();
if (!c) return VK_ASTC_CPU;
AcquireSRWLockShared(&g_lock);
int m = G.dev == c->device ? G.mode : -1;
ReleaseSRWLockShared(&g_lock);
if (m >= 0) return m;
AcquireSRWLockExclusive(&g_lock);
reset_for(c);
if (G.mode < 0) {
const char *e = cfg_get("TOTK_ASTC");
int want = !e || !*e || !strcmp(e, "auto") ? -1 : !strcmp(e, "cpu") ? VK_ASTC_CPU : !strcmp(e, "gpu") ? VK_ASTC_GPU
: !strcmp(e, "native") ? VK_ASTC_NATIVE : !strcmp(e, "check") ? VK_ASTC_CHECK : -2;
if (want == -2) rt_log("vk: TOTK_ASTC=%s is not cpu, gpu, native, check or auto: auto", e), want = -1;
if ((want == -1 || want == VK_ASTC_NATIVE) && native_device(c)) m = VK_ASTC_NATIVE;
else {
if (want == VK_ASTC_NATIVE) rt_log("vk: TOTK_ASTC=native: the device lacks textureCompressionASTC_LDR; gpu");
m = want == VK_ASTC_CPU ? VK_ASTC_CPU : want == VK_ASTC_CHECK ? VK_ASTC_CHECK : VK_ASTC_GPU;
}
if ((m == VK_ASTC_GPU || m == VK_ASTC_CHECK || m == VK_ASTC_NATIVE) && !G.pipe && !G.failed &&
!create_pipeline(c)) {
G.failed = 1;
rt_log("vk: ASTC compute decoder unavailable on %s: ASTC decodes on the CPU", c->props.deviceName);
}
if (G.failed && m != VK_ASTC_NATIVE) m = VK_ASTC_CPU;
G.mode = m;
rt_log("vk: ASTC textures: %s (textureCompressionASTC_LDR %d)", mode_name(m), native_device(c));
}
m = G.mode;
ReleaseSRWLockExclusive(&g_lock);
return m;
}
int vk_astc_native(VkFormat astc_format) {
VkCtx *c = vk_ctx();
if (!c || vk_astc_mode() != VK_ASTC_NATIVE) return 0;
VkFormatProperties p;
vkGetPhysicalDeviceFormatProperties(c->phys, astc_format, &p);
VkFormatFeatureFlags need = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
VK_FORMAT_FEATURE_TRANSFER_SRC_BIT;
return (p.optimalTilingFeatures & need) == need;
}
int vk_astc_gpu(void) {
int m = vk_astc_mode();
if (m == VK_ASTC_GPU || m == VK_ASTC_CHECK) return 1;
if (m != VK_ASTC_NATIVE) return 0;
AcquireSRWLockShared(&g_lock); // native mode, a format the device lacks: the decoder when it exists
int ok = G.pipe != VK_NULL_HANDLE;
ReleaseSRWLockShared(&g_lock);
return ok;
}
static void mem_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 src, VkAccessFlags2 sa, VkPipelineStageFlags2 dst,
VkAccessFlags2 da) {
VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = src, .srcAccessMask = sa,
.dstStageMask = dst, .dstAccessMask = da};
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb};
vkCmdPipelineBarrier2(cb, &di);
}
// Scratch per vk_astc_record: texels of at most this many bytes at a time (or one region, when larger), so a large
// texture array (169 M texels, 677 MB as RGBA8, on monsters2) does not need its whole RGBA8 copy as scratch.
#define CHUNK_OUT (32ull << 20)
static VkDeviceSize region_out(const VkAstcRegion *r) { return (VkDeviceSize)r->w * r->h * r->slices * 4; }
static VkDeviceSize region_in(const VkAstcRegion *r, uint32_t bw, uint32_t bh) {
return (VkDeviceSize)((r->w + bw - 1) / bw) * ((r->h + bh - 1) / bh) * r->slices * 16;
}
int vk_astc_record(VkCommandBuffer cb, VkBuffer src, VkDeviceSize src_size, const VkAstcRegion *rg, int n, uint32_t bw,
uint32_t bh, int srgb, VkImage img, VkImageAspectFlags aspect, VkBuffer readback) {
static volatile long n_fail;
(void)src_size;
VkCtx *c = vk_ctx();
if (!c || !vk_astc_gpu() || bw < 4 || bh < 4 || bw > 12 || bh > 12) return 0;
AcquireSRWLockShared(&g_lock);
VkPipeline pipe = G.dev == c->device ? G.pipe : VK_NULL_HANDLE;
ReleaseSRWLockShared(&g_lock);
if (!pipe) return 0;
// chunks of consecutive regions; scratch = the largest chunk's blocks, then its texels
VkDeviceSize out_total = 0, cap_in = 0, cap_out = 0, ci = 0, co = 0;
for (int i = 0; i < n; i++) {
VkDeviceSize o = region_out(&rg[i]), b = region_in(&rg[i], bw, bh);
if (co && co + o > CHUNK_OUT) ci = co = 0;
ci += b, co += o, out_total += o;
if (ci > cap_in) cap_in = ci;
if (co > cap_out) cap_out = co;
}
if (!out_total) return 1;
VkDeviceSize al = c->props.limits.minStorageBufferOffsetAlignment; // the texels bind at cap_in
if (al > 1) cap_in = (cap_in + al - 1) / al * al;
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = cap_in + cap_out,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT};
VmaAllocationCreateInfo aci = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
VkBuffer sb;
VmaAllocation sa;
if (vmaCreateBuffer(c->vma, &bi, &aci, &sb, &sa, NULL) != VK_SUCCESS) {
if (InterlockedIncrement(&n_fail) <= 16)
rt_log("vk: ASTC scratch buffer of %llu bytes failed: texture left undecoded",
(unsigned long long)(cap_in + cap_out));
return 0;
}
AcquireSRWLockExclusive(&g_lock);
int need_tab = !G.tab_uploaded;
G.tab_uploaded = 1;
VkBuffer tab = G.tab;
ReleaseSRWLockExclusive(&g_lock);
if (need_tab) { // first use on this device: the tables go up in this batch, before the dispatch
VkBuffer tb;
uint8_t *p = vk_staging(TAB_SIZE, &tb, NULL);
fill_tables(p);
VkBufferCopy cp = {0, 0, TAB_SIZE};
vkCmdCopyBuffer(cb, tb, tab, 1, &cp);
}
int timed = 0, whole = -1;
uint32_t id = 0;
uint8_t fp = (uint8_t)(bw * 13 + bh);
AcquireSRWLockExclusive(&t_lock);
time_init(c);
if (t_on > 0 && t_pool) {
time_poll_locked(c);
timed = 1;
id = ++t_id;
t_call[id % TA] = (TCall){.id = id, .w = rg[0].w, .h = rg[0].h, .regions = (uint32_t)n, .fp = fp, .complete = 1};
whole = time_slot(id, fp, out_total / 4, 1);
if (whole < 0) t_call[id % TA].complete = 0;
}
VkQueryPool tpool = t_pool;
ReleaseSRWLockExclusive(&t_lock);
if (whole >= 0) vkCmdWriteTimestamp2(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, tpool, 2 * (uint32_t)whole);
VkDescriptorBufferInfo dbi[3] = {{sb, 0, cap_in}, {sb, cap_in, cap_out}, {tab, 0, TAB_SIZE}};
VkWriteDescriptorSet w[3];
for (int i = 0; i < 3; i++)
w[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstBinding = (uint32_t)i,
.descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.pBufferInfo = &dbi[i]};
VkBufferImageCopy *copies = calloc((size_t)n, sizeof *copies);
VkBufferCopy *bcp = calloc((size_t)n, sizeof *bcp);
if (!copies || !bcp) rt_fatal("vk: out of memory (ASTC regions)");
VkDeviceSize rb_off = 0;
int chunks = 0;
for (int i0 = 0; i0 < n; chunks++) {
int i1 = i0;
VkDeviceSize in = 0, out = 0;
for (; i1 < n && (i1 == i0 || out + region_out(&rg[i1]) <= CHUNK_OUT); i1++) {
bcp[i1 - i0] = (VkBufferCopy){rg[i1].src, in, region_in(&rg[i1], bw, bh)};
in += region_in(&rg[i1], bw, bh), out += region_out(&rg[i1]);
}
if (chunks) // the previous chunk's dispatches and image copy are done with the scratch
mem_barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_READ_BIT,
VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
VK_ACCESS_2_TRANSFER_WRITE_BIT | VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT);
vkCmdCopyBuffer(cb, src, sb, (uint32_t)(i1 - i0), bcp);
mem_barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
VK_ACCESS_2_SHADER_STORAGE_READ_BIT);
int slot = -1; // the timestamps bracket this chunk's dispatches: the first waits for the blocks copy
if (timed) {
AcquireSRWLockExclusive(&t_lock);
slot = time_slot(id, fp, out / 4, 0);
if (slot < 0) t_call[id % TA].complete = 0;
ReleaseSRWLockExclusive(&t_lock);
}
if (slot >= 0) vkCmdWriteTimestamp2(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, tpool, 2 * (uint32_t)slot);
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe);
G.push(cb, VK_PIPELINE_BIND_POINT_COMPUTE, G.layout, 0, 3, w);
VkDeviceSize ib = 0, ob = 0;
for (int i = i0; i < i1; i++) {
const VkAstcRegion *r = &rg[i];
uint32_t bx = (r->w + bw - 1) / bw, by = (r->h + bh - 1) / bh;
Push pc = {.in_base = (uint32_t)(ib / 16), .out_base = (uint32_t)(ob / 4), .width = r->w, .height = r->h,
.bw = bw, .bh = bh, .bx = bx, .bslice = bx * by, .srgb = (uint32_t)(srgb != 0)};
vkCmdPushConstants(cb, G.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof pc, &pc);
uint32_t bpw = bw * bh <= 64 ? 64 / (bw * bh) : 1; // blocks per workgroup, as astc_decode.comp main
vkCmdDispatch(cb, (bx + bpw - 1) / bpw, by, r->slices);
copies[i - i0] = (VkBufferImageCopy){.bufferOffset = cap_in + ob, .bufferRowLength = r->w,
.bufferImageHeight = r->h, .imageSubresource = {aspect, r->level, r->layer, 1},
.imageOffset = r->offset, .imageExtent = {r->w, r->h, r->slices}};
ib += region_in(r, bw, bh), ob += region_out(r);
n_texels += (uint64_t)r->w * r->h * r->slices;
}
if (slot >= 0)
vkCmdWriteTimestamp2(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, tpool, 2 * (uint32_t)slot + 1);
mem_barrier(cb, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT);
vkCmdCopyBufferToImage(cb, sb, img, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)(i1 - i0), copies);
if (readback) {
VkBufferCopy rc = {cap_in, rb_off, out};
vkCmdCopyBuffer(cb, sb, readback, 1, &rc);
rb_off += out;
}
i0 = i1;
}
if (readback)
mem_barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_HOST_BIT,
VK_ACCESS_2_HOST_READ_BIT);
if (whole >= 0) vkCmdWriteTimestamp2(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, tpool, 2 * (uint32_t)whole + 1);
if (timed) {
AcquireSRWLockExclusive(&t_lock);
t_call[id % TA].chunks = (uint32_t)chunks;
ReleaseSRWLockExclusive(&t_lock);
}
if ((n_dispatch + (uint64_t)n) / 1024 != n_dispatch / 1024 || !n_dispatch) {
rt_log("vk: ASTC: %llu regions, %.1f M texels decoded on the GPU so far",
(unsigned long long)(n_dispatch + (uint64_t)n), (double)n_texels / 1e6);
vk_astc_time_summary();
}
n_dispatch += (uint64_t)n;
free(copies), free(bcp);
vk_defer_buffer(sb, sa);
return 1;
}
void vk_astc_release(void) {
VkCtx *c = vk_ctx();
vk_astc_time_summary();
AcquireSRWLockExclusive(&t_lock);
if (c && t_pool && t_dev == c->device) vkDestroyQueryPool(c->device, t_pool, NULL);
t_pool = VK_NULL_HANDLE, t_dev = VK_NULL_HANDLE;
ReleaseSRWLockExclusive(&t_lock);
AcquireSRWLockExclusive(&g_lock);
if (c && G.dev == c->device) {
if (G.pipe) vkDestroyPipeline(c->device, G.pipe, NULL);
if (G.layout) vkDestroyPipelineLayout(c->device, G.layout, NULL);
if (G.dsl) vkDestroyDescriptorSetLayout(c->device, G.dsl, NULL);
if (G.tab) vmaDestroyBuffer(c->vma, G.tab, G.tab_alloc);
}
memset(&G.dsl, 0, sizeof G - offsetof(__typeof__(G), dsl));
G.dev = VK_NULL_HANDLE;
G.mode = -1;
G.failed = 0;
ReleaseSRWLockExclusive(&g_lock);
}
void vk_astc_stats(uint64_t *dispatches, uint64_t *texels) {
*dispatches = n_dispatch;
*texels = n_texels;
}
+374 -21
View File
@@ -10,8 +10,19 @@
// Back-edge pacing: the present thread submits the blit at once, then holds the host present (vkQueuePresentKHR) until
// the frame's display time from the pacer (nvn_api_obj.c), so present-to-present intervals stay even whatever the
// replay took. The blit is already on the queue, so the wait blocks no submission.
//
// Asynchronous replay (TOTK_REPLAY_ASYNC, default 1; 0 keeps the synchronous path unchanged): nvnQueueSubmitCommands
// and nvnQueuePresentTexture only queue. Each item gets its timeline value at enqueue (vk_sync_reserve), in queue
// order; the replay thread runs the synchronous submit and present bodies in that order, so every value is signalled
// by the submission it was handed out for. The game runs at most one frame ahead: queueing present N waits until the
// replay thread handled present N-1. Calls that read or free GPU state that queued work may use drain the queue first
// (replay_drain), each cause counted. Design and the orderings it keeps: build/v121-replayq/NOTES.md.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "flight.h"
#include "gfx.h"
#include "host.h"
@@ -21,7 +32,7 @@
#define WEAK __attribute__((weak))
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued; } PresentJob;
typedef struct { VkImage image; VkFormat format; int w, h, has_crop, crop[4]; uint64_t frame, at, queued, value; } PresentJob;
static SRWLOCK pl = SRWLOCK_INIT;
static CONDITION_VARIABLE pcv = CONDITION_VARIABLE_INIT; // job queued, job taken, job submitted
@@ -52,7 +63,10 @@ static DWORD WINAPI present_main(void *arg) {
ReleaseSRWLockExclusive(&pl);
if (flight_on) vk_thumbs_frame(j.frame);
vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.w, j.h, j.has_crop ? j.crop : NULL);
vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too
uint64_t f = vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too
if (j.value && f != j.value)
rt_fatal("vk: the present of frame %llu signalled %llu, queued as %llu", (unsigned long long)j.frame,
(unsigned long long)f, (unsigned long long)j.value);
AcquireSRWLockExclusive(&pl);
busy = 0;
WakeAllConditionVariable(&pcv);
@@ -84,22 +98,28 @@ void vk_present_drain(void) {
perf_end(PERF_DRAIN, p0);
}
static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) {
// The present after the frame count: resolves the image and hands the blit to the present thread. queued: when the game
// presented; value: the timeline value queued for this present (asynchronous replay), 0 for the next one (synchronous).
static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uint64_t queued, uint64_t frame,
uint64_t value) {
static volatile long n_skipped;
vk_sync_start();
uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
perf_present(frame); // before vk_exec_frame_log, which resets the frame's exec counts the flight recorder reads
vk_exec_frame_log(t, 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);
const VkTexImage *img = vk_texture_get(t);
if (!img || !(img->aspect & VK_IMAGE_ASPECT_COLOR_BIT)) {
if (InterlockedIncrement(&n_skipped) <= 8)
rt_log("vk: present of texture %p (format %#x) skipped: no colour image", (const void *)t, t ? t->d.format : 0);
if (value) { // the queued value is still signalled, in order
vk_present_drain();
uint64_t f = vk_sync_submit(NULL, 0);
if (f != value) rt_fatal("vk: skipped present signalled %llu, queued as %llu", (unsigned long long)f, (unsigned long long)value);
}
return;
}
PresentJob j = {.image = img->image, .format = img->format, .w = (int)img->width, .h = (int)img->height, .frame = frame,
.at = at_ns, .queued = kern_now_ns()};
.at = at_ns, .queued = queued, .value = value};
if (crop) {
j.has_crop = 1;
for (int i = 0; i < 4; i++) j.crop[i] = crop[i];
@@ -120,45 +140,375 @@ static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) {
if (rdoc_active()) { vk_present_drain(); rdoc_after_present(frame); }
}
// Pipelines (T6b) drop a released program's pipelines here.
WEAK void vk_program_release(const prog_t *p) { (void)p; }
// ---- Asynchronous replay
enum { RI_SUBMIT, RI_PRESENT, RI_RELEASE };
typedef struct {
int kind, n, has_crop, crop[4];
uint64_t value, submit_no, at, queued;
uint64_t *h;
const tex_t *tex; // RI_RELEASE: a copy the item owns (the game may reuse the object's memory)
} ReplayItem;
#define RQ_CAP 1024 // a frame queues 1 submit and 1 present, and up to a few hundred releases
enum { DR_POOL_FINI, DR_MAP_VIRTUAL, DR_TEX_RELEASE, DR_PROG_RELEASE, DR_FINISH, DR_READ_TEXELS, DR_WRITE_TEXELS,
DR_GUEST_READ, DR_FRAME, DR_FULL, DR_CHECK, DR_N };
static const char *const dr_name[DR_N] = {
"pool_fini", "map_virtual", "texture_release (queued)", "program_release", "finish", "read_texels (writer fence)",
"write_texels", "guest_read (writer fence)", "frame bound", "queue full", "check drain"};
static int vk_be_pool_init(pool_t *p) { vk_sync_start(); return vk_pool_init(p); }
static void vk_be_pool_fini(pool_t *p) { vk_sync_start(); vk_present_drain(); vk_pool_fini(p); }
static int vk_be_pool_map_virtual(pool_t *v, int n, const void *requests) {
vk_sync_start();
vk_present_drain();
return vk_pool_map_virtual(v, n, requests);
static SRWLOCK rl = SRWLOCK_INIT;
static CONDITION_VARIABLE rcv = CONDITION_VARIABLE_INIT; // item queued, item taken, item done
static ReplayItem rq[RQ_CAP];
static int rq_head, rq_n, r_busy, r_started;
static uint64_t r_presents_queued, r_presents_done, r_submit_done, game_frames;
// Under rl: calls, calls that waited and the time waited per cause, since the last replay_log line and over the run.
static uint64_t dr_calls[DR_N], dr_waits[DR_N], dr_ns[DR_N], dr_tcalls[DR_N], dr_twaits[DR_N], dr_tns[DR_N];
// TOTK_REPLAY_CHECK: ReadTexels compared with a read after a full drain (under rl): checked, differing, differing
// although no submission recorded a write to the texture in between, differing with the queue idle at the first read.
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) {
const char *e = cfg_get("TOTK_REPLAY_ASYNC");
on = e && !strcmp(e, "1"); // off by default: no measured gain yet, one more frame of latency
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);
}
return on;
}
static void vk_be_texture_register(int id, const tex_t *t, const view_t *v) { vk_sync_start(); vk_texture_register(id, t, v); }
static void vk_be_sampler_register(int id, const samp_par *s) { vk_sync_start(); vk_sampler_register(id, s); }
static void vk_be_texture_release(const tex_t *t) { vk_sync_start(); vk_present_drain(); vk_texture_release(t); }
static void vk_be_program_release(const prog_t *p) { vk_program_release(p); }
static uint64_t vk_be_submit(const uint64_t *handles, int n) {
// The front end's view (nvn_api_core.c, nvn_api_cmd.c): replay is asynchronous only on this back end.
int gfx_replay_async(void) { return gfx_backend() == &gfx_vulkan_backend && vk_replay_async(); }
// The newest submit number (nvn_submit_count) whose replay is done: recording copies it used may be freed.
uint64_t gfx_replayed_submits(void) {
if (!gfx_replay_async()) return nvn_submit_count();
AcquireSRWLockShared(&rl);
uint64_t v = r_submit_done;
ReleaseSRWLockShared(&rl);
return v;
}
static uint64_t submit_sync(const uint64_t *handles, int n) {
vk_sync_start();
vk_present_drain();
vk_texture_new_submit();
return vk_sync_submit(handles, n);
}
static void vk_be_finish(void) { vk_sync_start(); vk_present_drain(); vk_sync_finish(); }
static DWORD WINAPI replay_main(void *arg) {
(void)arg;
perf_replay_thread();
AcquireSRWLockExclusive(&rl);
for (;;) {
while (!rq_n) SleepConditionVariableSRW(&rcv, &rl, INFINITE, 0);
ReplayItem it = rq[rq_head];
rq_head = (rq_head + 1) % RQ_CAP;
rq_n--;
r_busy = 1;
WakeAllConditionVariable(&rcv);
ReleaseSRWLockExclusive(&rl);
if (it.kind == RI_SUBMIT) {
uint64_t f = submit_sync(it.h, it.n);
if (f != it.value)
rt_fatal("vk: replayed submission signalled %llu, queued as %llu", (unsigned long long)f,
(unsigned long long)it.value);
free(it.h);
} else if (it.kind == RI_RELEASE) {
vk_present_drain();
vk_texture_release(it.tex);
free((void *)it.tex);
} else {
uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
present_image(it.tex, it.has_crop ? it.crop : NULL, it.at, it.queued, frame, it.value);
}
AcquireSRWLockExclusive(&rl);
if (it.kind == RI_SUBMIT) r_submit_done = it.submit_no;
else r_presents_done++;
r_busy = 0;
WakeAllConditionVariable(&rcv);
}
return 0;
}
static void wait_counted(int why, uint64_t t0) { // rl held
uint64_t d = kern_now_ns() - t0;
dr_waits[why]++, dr_twaits[why]++;
dr_ns[why] += d, dr_tns[why] += d;
perf_end(PERF_DRAIN, t0);
}
// A guest thread waiting for the replay gives its virtual core up, as around any blocking host wait (NVN_BLOCKING):
// guest_read (memcpy, pool invalidate), pool and texture finalize run without it, and a drain lasts up to a replay.
// Called and returning with rl held; rl is not held while the core is taken back.
static int core_away_unlocked(void) {
ReleaseSRWLockExclusive(&rl);
int away = kern_core_away();
AcquireSRWLockExclusive(&rl);
return away;
}
static void core_back_unlocked(int *away) {
if (!*away) return;
ReleaseSRWLockExclusive(&rl);
kern_core_back(away);
AcquireSRWLockExclusive(&rl);
}
// Queues an item (waits while the queue is full) and returns its value, reserved under rl so values follow queue order.
static uint64_t replay_enqueue(ReplayItem *it) {
AcquireSRWLockExclusive(&rl);
if (!r_started) {
HANDLE h = CreateThread(NULL, 0, replay_main, NULL, 0, NULL);
if (!h) rt_fatal("vk: cannot start the replay thread (error %lu)", GetLastError());
CloseHandle(h);
r_started = 1;
}
// rl is dropped while the core is taken back, and another thread may fill the queue again meanwhile: check again.
while (rq_n == RQ_CAP) {
uint64_t p0 = perf_begin();
dr_calls[DR_FULL]++, dr_tcalls[DR_FULL]++;
int away = core_away_unlocked();
while (rq_n == RQ_CAP) SleepConditionVariableSRW(&rcv, &rl, INFINITE, 0);
wait_counted(DR_FULL, p0);
core_back_unlocked(&away);
}
it->value = it->kind == RI_RELEASE ? 0 : vk_sync_reserve(); // a release signals nothing
rq[(rq_head + rq_n) % RQ_CAP] = *it;
rq_n++;
WakeAllConditionVariable(&rcv);
ReleaseSRWLockExclusive(&rl);
return it->value;
}
// Returns once every queued item was replayed and submitted. Back-end entries only: the replay thread never calls them.
static void replay_drain(int why) {
if (!gfx_replay_async()) return;
AcquireSRWLockExclusive(&rl);
dr_calls[why]++, dr_tcalls[why]++;
if (rq_n || r_busy) {
uint64_t p0 = perf_begin();
int away = core_away_unlocked();
while (rq_n || r_busy) SleepConditionVariableSRW(&rcv, &rl, INFINITE, 0);
wait_counted(why, p0);
core_back_unlocked(&away);
}
ReleaseSRWLockExclusive(&rl);
}
// What made the game wait for the replay: since the last line (every 1800 game frames), or over the run (at exit).
static void replay_log(uint64_t frame, int total) {
char buf[1024];
int k = 0;
AcquireSRWLockExclusive(&rl);
const uint64_t *c = total ? dr_tcalls : dr_calls, *w = total ? dr_twaits : dr_waits, *t = total ? dr_tns : dr_ns;
for (int i = 0; i < DR_N && k < (int)sizeof buf - 96; i++)
if (c[i] || w[i])
k += snprintf(buf + k, sizeof buf - (size_t)k, "%s %s %llu calls, %llu waited %.1f ms", k ? ";" : "", dr_name[i],
(unsigned long long)c[i], (unsigned long long)w[i], t[i] / 1e6);
if (ck_reads && k < (int)sizeof buf - 128)
k += snprintf(buf + k, sizeof buf - (size_t)k, "; check (run): %llu reads, %llu differ, %llu without a recorded write, %llu with the queue idle",
(unsigned long long)ck_reads, (unsigned long long)ck_diff, (unsigned long long)ck_diff_unexplained,
(unsigned long long)ck_diff_idle);
memset(dr_calls, 0, sizeof dr_calls);
memset(dr_waits, 0, sizeof dr_waits);
memset(dr_ns, 0, sizeof dr_ns);
ReleaseSRWLockExclusive(&rl);
rt_log("vk: replay queue %s frame %llu:%s", total ? "over the run to" : "at", (unsigned long long)frame,
k ? buf : " no waits");
}
// The run total, once: the game exits through prof_write (boot.c _exit, the window close), probes through
// vk_backend_shutdown.
void gfx_exit_log(void) {
static LONG done;
if (gfx_replay_async() && r_started && !InterlockedExchange(&done, 1)) replay_log(game_frames, 1);
}
static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) {
vk_sync_start();
if (!gfx_replay_async()) {
uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED);
perf_present(frame); // before vk_exec_frame_log, which resets the frame's exec counts the flight recorder reads
present_image(t, crop, at_ns, kern_now_ns(), frame, 0);
return;
}
// Frame bound: the replay thread handled the previous present before this one is queued.
AcquireSRWLockExclusive(&rl);
dr_calls[DR_FRAME]++, dr_tcalls[DR_FRAME]++;
if (r_presents_done < r_presents_queued) {
uint64_t p0 = perf_begin();
while (r_presents_done < r_presents_queued) SleepConditionVariableSRW(&rcv, &rl, INFINITE, 0);
wait_counted(DR_FRAME, p0);
}
r_presents_queued++;
ReleaseSRWLockExclusive(&rl);
uint64_t frame = ++game_frames;
perf_present(frame);
if (frame % 1800 == 0) replay_log(frame, 0);
ReplayItem it = {.kind = RI_PRESENT, .tex = t, .at = at_ns, .queued = kern_now_ns()};
if (crop) {
it.has_crop = 1;
for (int i = 0; i < 4; i++) it.crop[i] = crop[i];
}
replay_enqueue(&it);
}
// Pipelines (T6b) drop a released program's pipelines here.
WEAK void vk_program_release(const prog_t *p) { (void)p; }
static int vk_be_pool_init(pool_t *p) { vk_sync_start(); return vk_pool_init(p); }
static void vk_be_pool_fini(pool_t *p) { vk_sync_start(); replay_drain(DR_POOL_FINI); vk_present_drain(); vk_pool_fini(p); }
static int vk_be_pool_map_virtual(pool_t *v, int n, const void *requests) {
vk_sync_start();
replay_drain(DR_MAP_VIRTUAL);
vk_present_drain();
return vk_pool_map_virtual(v, n, requests);
}
static void vk_be_texture_register(int id, const tex_t *t, const view_t *v) { vk_sync_start(); vk_texture_register(id, t, v); }
static void vk_be_sampler_register(int id, const samp_par *s) { vk_sync_start(); vk_sampler_register(id, s); }
// Asynchronous: the release is queued behind the submissions before it, so their replay still resolves the texture's
// slots to its image (a released slot follows another live texture at the storage), and the ones after it see it
// released, the order of the synchronous replay. vk_texture_release reads only the object's key fields: a copy.
static void vk_be_texture_release(const tex_t *t) {
vk_sync_start();
if (!gfx_replay_async()) {
vk_present_drain();
vk_texture_release(t);
return;
}
if (!t || !t->mem) return;
tex_t *copy = malloc(sizeof *copy);
if (!copy) rt_fatal("vk: out of memory (replay queue)");
*copy = *t;
ReplayItem it = {.kind = RI_RELEASE, .tex = copy};
AcquireSRWLockExclusive(&rl);
dr_calls[DR_TEX_RELEASE]++, dr_tcalls[DR_TEX_RELEASE]++;
ReleaseSRWLockExclusive(&rl);
replay_enqueue(&it);
}
static void vk_be_program_release(const prog_t *p) { replay_drain(DR_PROG_RELEASE); vk_program_release(p); }
// Asynchronous: the handles (nvn_submit_prepare_async ran under the front end's submit lock) are copied and queued;
// the value returned is the one the replay will signal.
static uint64_t vk_be_submit(const uint64_t *handles, int n) {
if (!gfx_replay_async()) return submit_sync(handles, n);
vk_sync_start();
ReplayItem it = {.kind = RI_SUBMIT, .n = n, .submit_no = nvn_submit_count()};
if (n > 0) {
it.h = malloc(sizeof *it.h * (size_t)n);
if (!it.h) rt_fatal("vk: out of memory (replay queue)");
memcpy(it.h, handles, sizeof *it.h * (size_t)n);
}
return replay_enqueue(&it);
}
static void vk_be_finish(void) { vk_sync_start(); replay_drain(DR_FINISH); vk_present_drain(); vk_sync_finish(); }
static int replay_check(void) {
static int on = -1;
if (on < 0) {
const char *e = cfg_get("TOTK_REPLAY_CHECK");
on = e && strcmp(e, "0");
if (on) rt_log("vk: TOTK_REPLAY_CHECK: every ReadTexels is read again after a full replay drain and compared");
}
return on;
}
// ReadTexels with the replay queued: waits for the newest submission recorded to write the texture (replayed and
// completed), not for the whole queue. The readback copy goes through the upload batch on the same queue, after every
// submission the replay has submitted. Submissions still queued or being recorded run after the copy, as on hardware
// when the GPU has not reached them yet: the game only sees their texels after waiting on a sync, and every sync
// value covers replay and GPU, so a read the game synchronised sees them. TOTK_REPLAY_CHECK compares with a full drain.
static void vk_be_read_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row,
uint64_t img) {
vk_sync_start();
if (!gfx_replay_async()) {
vk_read_texels(t, v, r, dst, row, img);
return;
}
uint64_t f = vk_texture_last_write(t);
AcquireSRWLockExclusive(&rl);
dr_calls[DR_READ_TEXELS]++, dr_tcalls[DR_READ_TEXELS]++;
int idle = !rq_n && !r_busy;
ReleaseSRWLockExclusive(&rl);
if (f && vk_sync_fence_done() < f) {
uint64_t t0 = kern_now_ns();
vk_sync_fence_wait(f, UINT64_MAX); // counts as a fence wait in perf
uint64_t d = kern_now_ns() - t0;
AcquireSRWLockExclusive(&rl);
dr_waits[DR_READ_TEXELS]++, dr_twaits[DR_READ_TEXELS]++;
dr_ns[DR_READ_TEXELS] += d, dr_tns[DR_READ_TEXELS] += d;
ReleaseSRWLockExclusive(&rl);
}
vk_read_texels(t, v, r, dst, row, img);
if (!replay_check()) return;
size_t n = vk_texture_texel_span(t, v, r, row, img);
uint8_t *again = n ? malloc(n) : NULL;
if (!again) return;
memcpy(again, dst, n); // bytes between the rows stay the caller's
uint64_t f0 = vk_texture_last_write(t);
replay_drain(DR_CHECK);
uint64_t f1 = vk_texture_last_write(t);
vk_read_texels(t, v, r, again, row, img);
int differ = memcmp(again, dst, n) != 0;
static volatile long n_logged;
if (differ && InterlockedIncrement(&n_logged) <= 40) {
size_t at = 0;
while (at < n && again[at] == dst[at]) at++;
rt_log("vk: replay check: ReadTexels of %p (format %#x, %dx%dx%d at %d,%d,%d) differs at byte %zu of %zu; writer "
"fence %llu at the read, %llu after the drain, done %llu; queue %s at the read",
(void *)t->mem, t->d.format, r->width, r->height, r->depth, r->xoffset, r->yoffset, r->zoffset, at, n,
(unsigned long long)f0, (unsigned long long)f1, (unsigned long long)vk_sync_fence_done(), idle ? "idle" : "busy");
}
AcquireSRWLockExclusive(&rl);
ck_reads++;
if (differ) {
ck_diff++;
if (f1 == f0) ck_diff_unexplained++;
if (idle) ck_diff_idle++;
}
ReleaseSRWLockExclusive(&rl);
free(again);
}
static void vk_be_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row,
uint64_t img) {
vk_sync_start();
replay_drain(DR_WRITE_TEXELS);
vk_present_drain();
vk_write_texels(t, v, r, src, row, img);
}
static void vk_be_guest_written(uint64_t addr, uint64_t size) { vk_sync_start(); vk_guest_written(addr, size); }
// A CPU read of GPU-written mirrored storage (memcpy, pool invalidate). With the replay queued it waits for the newest
// submission recorded to write the textures there, not for the whole queue: the write-back copy then runs after their
// renders, and clears gpu_dirty only for content that is on the queue. Submissions not replayed yet render after the
// copy, as on hardware when the GPU has not reached them; they mark the texture dirty again for a later read.
static void vk_be_guest_read(uint64_t addr, uint64_t size) {
if (!vk_texture_cpu_stale(addr, size)) return;
vk_sync_start();
if (gfx_replay_async()) {
uint64_t f = vk_texture_dirty_fence(addr, size);
AcquireSRWLockExclusive(&rl);
dr_calls[DR_GUEST_READ]++, dr_tcalls[DR_GUEST_READ]++;
ReleaseSRWLockExclusive(&rl);
if (f && vk_sync_fence_done() < f) {
uint64_t t0 = kern_now_ns();
NVN_BLOCKING();
vk_sync_fence_wait(f, UINT64_MAX);
uint64_t d = kern_now_ns() - t0;
AcquireSRWLockExclusive(&rl);
dr_waits[DR_GUEST_READ]++, dr_twaits[DR_GUEST_READ]++;
dr_ns[DR_GUEST_READ] += d, dr_tns[DR_GUEST_READ] += d;
ReleaseSRWLockExclusive(&rl);
}
}
vk_present_drain();
vk_texture_write_back(addr, size);
}
// After a fence wait: the value is done, so its replay is too; a later submission the replay records meanwhile marks
// its targets with a later fence, which write_back_small leaves alone. No drain.
static void vk_be_cpu_synced(uint64_t fence) {
if (!vk_texture_small_dirty()) return;
vk_sync_start();
vk_present_drain();
vk_texture_write_back_small(fence);
}
const GfxBackend gfx_vulkan_backend = {
.name = "vulkan",
@@ -178,9 +528,12 @@ const GfxBackend gfx_vulkan_backend = {
.write_texels = vk_be_write_texels,
.guest_written = vk_be_guest_written,
.guest_read = vk_be_guest_read,
.cpu_synced = vk_be_cpu_synced,
};
void vk_backend_shutdown(void) {
replay_drain(DR_FINISH);
gfx_exit_log();
vk_present_drain();
vk_sync_shutdown();
vk_mem_shutdown();
+3
View File
@@ -165,6 +165,7 @@ static int pick_device(void) {
}
free(qp);
rt_log("vk: using gpu %d: %s", best, g_vk.props.deviceName);
vk_mem_class_log(&g_vk.props, &g_vk.mem_props);
return 1;
}
@@ -268,6 +269,7 @@ static int create_device(void) {
else if (!h->textureCompressionBC) miss = "textureCompressionBC";
if (miss) { rt_log("vk: required feature missing on %s: %s", g_vk.props.deviceName, miss); free(c); return 0; }
g_vk.has_buffer_device_address = c->f12.bufferDeviceAddress;
g_vk.has_astc_ldr = h->textureCompressionASTC_LDR;
{
const char *e = cfg_get("TOTK_FEEDBACK");
int layout_ok = has_fbl && has_lr && c->fbl.attachmentFeedbackLoopLayout && c->lr.dynamicRenderingLocalRead;
@@ -325,6 +327,7 @@ static long g_errors_total; // outlives the device so the probe can print it
static void destroy_all(void) {
gfx_window_release_vk();
if (g_vk.device) vkDeviceWaitIdle(g_vk.device);
if (g_vk.device) vk_astc_release();
if (g_vk.timeline) vkDestroySemaphore(g_vk.device, g_vk.timeline, NULL);
if (g_vk.vma) vmaDestroyAllocator(g_vk.vma);
if (g_vk.device) vkDestroyDevice(g_vk.device, NULL);
+1
View File
@@ -399,6 +399,7 @@ static void buf_snap(VkExec *ex, int pass) {
if (!bd_size) return;
VkMemSpan s;
if (!vk_mem_resolve(bd_addr, bd_size, &s) || s.avail < bd_size) return;
VK_MEM_PICK(s); // the GPU's view of the buffer
BufJob *j = calloc(1, sizeof *j);
if (!j) rt_fatal("vk_dump: out of memory");
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = bd_size,
+34 -6
View File
@@ -47,6 +47,8 @@ VkExecFrame vk_exec_frame;
static struct { const uint8_t *mem; uint32_t w, h, fmt, n; } fw[64]; // textures written this frame
static int nfw;
uint64_t vk_exec_recording_fence(void) { return x_here ? X.fence : 0; }
void vk_exec_frame_written(const tex_t *t) {
if (!(rt_trace & RT_TRACE_GFX) || !t) return;
for (int i = 0; i < nfw; i++)
@@ -306,14 +308,38 @@ static int tfr_recent(const VkExecTarget *t, uint64_t stamp) {
return tfr_match(e, t) && (e->last == stamp ? e->prev : e->last) == stamp - 1;
}
int vk_exec_targets_recent(VkExec *ex) {
int vk_exec_targets_recent(VkExec *ex, int any) {
resolve_rt(ex);
uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1;
int n = 0;
for (int i = 0; i < 8; i++)
if (ex->color[i].image && (n++, !tfr_recent(&ex->color[i], stamp))) return 0;
if (ex->depth.image && (n++, !tfr_recent(&ex->depth, stamp))) return 0;
return n > 0;
int n = 0, recent = 0;
for (int i = 0; i < 9; i++) {
const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth;
if (!t->image) continue;
n++;
if (tfr_recent(t, stamp)) recent++;
else if (!any) return 0;
}
return any ? recent > 0 : n > 0;
}
// One line naming each target of the current pass: format, extent, layer and the two frames it was last rendered in
// (prepare_draw's pipeline wait log).
void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) {
resolve_rt(ex);
size_t n = 0;
buf[0] = 0;
for (int i = 0; i < 9 && n < cap; i++) {
const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth;
if (!t->image) continue;
const TargetFrames *e = tfr_slot(t);
int m = tfr_match(e, t);
int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu", n ? "; " : "", i < 8 ? 'c' : 'd',
i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer,
(unsigned long long)(m ? e->last : 0), (unsigned long long)(m ? e->prev : 0));
if (w < 0) break;
n += (size_t)w;
}
if (!n) snprintf(buf, cap, "no targets");
}
// ---- passes and barriers
@@ -686,6 +712,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t
vk_exec_frame.submits++;
split_draws = part_start = 0;
x_here = 1;
if (n) vk_mem_mirror_begin();
for (int i = 0; i < n; i++) {
if (!handles[i]) rt_fatal("vk: command handle %d of a submission is null", i);
walk(ex, nvn_op_first(handles[i]), 0);
@@ -693,6 +720,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t
vk_exec_pass_end(ex);
x_here = 0;
if (ex->recorded) vk_exec_barrier(ex); // later submissions see this one's writes
if (n) ex->recorded += vk_mem_mirror_end(ex->cb, fence); // VRAM mirror bytes the GPU wrote go back to guest memory
if (ex->nviews) {
ViewList *l = malloc(sizeof *l + sizeof(VkImageView) * (size_t)ex->nviews);
if (!l) rt_fatal("vk: out of memory");
+6 -3
View File
@@ -123,9 +123,11 @@ void vk_exec_draw_new_cb(void);
VkBuffer vk_exec_inline_buffer(VkDeviceSize size);
// Scissor 0 clipped to the render area of the open pass; extent 0: nothing to draw.
VkRect2D vk_exec_scissor(const VkExec *ex);
// 1: every target of the current pass was rendered in the previous presented frame (per-frame targets); 0 when one
// was not, or the pass has none.
int vk_exec_targets_recent(VkExec *ex);
// 1: every target of the current pass (any 1: at least one) was rendered in the previous presented frame (per-frame
// targets); 0 otherwise, or when the pass has none.
int vk_exec_targets_recent(VkExec *ex, int any);
// The current pass's targets with their formats, extents and the frames they were last rendered in (diagnostics).
void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap);
// TOTK_TRACE=gfx: replay counters of the current frame, logged and reset at each present (vk_exec_frame_log).
typedef struct {
@@ -136,6 +138,7 @@ typedef struct {
} VkExecFrame;
extern VkExecFrame vk_exec_frame;
void vk_exec_frame_written(const tex_t *t); // vk_texture_gpu_written: t rendered or copied into
uint64_t vk_exec_recording_fence(void); // fence of the submission this thread records, else 0
void vk_exec_frame_log(const tex_t *presented, uint64_t frame);
// TOTK_DUMP=<frame> (vk_dump.c): between presents frame-1 and frame, every pass's targets and every dispatch's storage
+553 -98
View File
@@ -16,6 +16,7 @@
// sampler descriptor lookups and the dispatch flow follow LAS Recomp runtime/nvn/vulkan/exec.c:1851-1908, 2300-2516
// @ da35b83a (MIT, Copyright (c) 2026 Peiikai). The support buffer layout is Ryujinx's SupportBuffer.cs (MIT).
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -116,14 +117,55 @@ static struct {
VkDescriptorSet ds_bound[2]; // allocated set bound per set number (0: none or pushed since)
VkDescriptorSet ds_last[2]; // last allocated and written set per set number, contents in DS
VkDescriptorSetLayout ds_last_layout[2];
int ds_pushed[2]; // set number holds the push of DS.psig under ds_layout
int res_valid; // the graphics sets bound hold what the inputs in RS give (bind_resources)
} B;
// Contents of B.ds_last: the writes' bindings, types, counts and descriptor infos. A draw whose allocated set would
// hold the same reuses the set instead of allocating and writing one (Task 2.2's per-draw cost).
#define DS_SIG_BYTES (MAX_DESC * 24 + MAX_BINDINGS * 12)
static struct { uint32_t n[2]; uint8_t sig[2][DS_SIG_BYTES], tmp[DS_SIG_BYTES]; } DS;
// Contents of B.ds_last: the set's packed descriptors (VkDescSlot), which with its layout determine every write. A draw
// whose allocated set would hold the same reuses the set instead of allocating and writing one (Task 2.2's per-draw
// cost). psig: the same for the last push per set number; a push equal to it under the same layout is not recorded
// again (the descriptors stay bound across pipeline binds of that layout).
#define DS_SIG_BYTES (MAX_DESC * sizeof(VkDescSlot))
static struct { uint32_t n[2], pn[2]; uint8_t sig[2][DS_SIG_BYTES], psig[2][DS_SIG_BYTES]; } DS;
// TOTK_BIND_STATS counters (bind_stats).
static struct {
int on;
uint64_t draws, bind[2], desc[2], push, push_skip, set_new, set_reuse, ac_miss, same_prog, same_all, bind_skip;
} BS = {-1};
// The inputs of the last graphics bind_resources (bind_sig) and what its build counted for the traffic census, which a
// skipped build counts again. RS_BYTES bounds the bound-slot bytes a pipeline reads (RsList): per binding at most one
// c0 storage table, otherwise one bound range or handle per descriptor.
#define RS_BYTES (MAX_BINDINGS * sizeof(((VkExec *)0)->ssbo[0]) + MAX_DESC * sizeof(p_bind_buf))
typedef struct { int cls, use; uint64_t bytes; } RsCount;
static struct {
int on; // TOTK_BIND_SKIP: 1 skip (default), 2 check, 0 off
uint32_t hn; // words of h the last signature filled; 0: none
uint64_t h[24]; // generations, feedback images, support inputs, base instance
const VkPipeInfo *info; // the pipeline whose RsList ranges body holds, as of bind op generation gen
uint64_t gen;
uint8_t body[RS_BYTES];
int rec; // a graphics build is running: count_use records, scratch copies mark volatile
int volatile_; // the build copied guest bytes into transient memory: a rebuild differs
RsCount cnt[2 * VK_MCLS_N], pcnt[2 * VK_MCLS_N];
int ncnt, pncnt, cnt_over;
int cmds; // check mode: pushes, binds and set writes the build recorded
uint32_t ckn; // check mode: the last build's packed descriptors (bytes in ck)
uint8_t ck[DS_SIG_BYTES];
} RS = {-1};
// The bound-slot bytes of VkExec a pipeline's descriptors read, as ranges merged where they touch, built once per
// pipeline info: c0's storage table, the uniform and storage ranges of the bound slots and the texture and image
// handles. sup and c0: the support inputs and the base instance count too. ok 0: never skipped (too many descriptors).
typedef struct { uint32_t off, len; } RsRange;
typedef struct { uint32_t n, bytes; uint8_t sup, c0, ok; RsRange r[]; } RsList;
static nvn_hmap rs_lists; // VkPipeInfo pointer -> RsList, until shutdown
static const VkPipeInfo *rs_last; // the last lookup
static const RsList *rs_last_l;
static uint32_t c0_dim[3], c0_binst; // the dispatch size and base instance of the draw or dispatch being bound
static uint64_t bind_gen = 1; // bumped by every bind op that writes ex->prog, ubo, ssbo, tex or img
static void cb_done(void *arg) { // completion thread
CbRes *r = arg;
@@ -338,6 +380,8 @@ void vk_exec_draw_shutdown(void) {
vmaDestroyImage(c->vma, SD[i].image, SD[i].alloc);
}
memset(SD, 0, sizeof SD);
nvn_hm_clear(&rs_lists, free);
rs_last = NULL, rs_last_l = NULL;
for (int i = 0; i < nspare_pool; i++) vkDestroyDescriptorPool(c->device, spare_pool[i], NULL);
for (int i = 0; i < nspare_chunk; i++) vmaDestroyBuffer(c->vma, spare_chunk[i].buf, spare_chunk[i].alloc);
nspare_pool = nspare_chunk = 0;
@@ -475,8 +519,13 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v,
// ---- program to shaders
// (program, stage, answers) -> shader, 4-way set-associative, most recently used first. A miss costs a SHA-256 of the
// whole shader code (gfx_shader.c req_init), slow on CPUs without SHA instructions: a field frame looks up ~3000 and
// the 2048-slot direct-mapped table this replaced missed 3% of them on conflicts.
#define SHADER_SETS 2048
#define SHADER_WAYS 4
typedef struct { uint64_t serial; int stage; int used; GfxShaderAnswers a; const GfxShader *s; } ShaderEnt;
static ShaderEnt shader_tab[2048];
static ShaderEnt shader_tab[SHADER_SETS][SHADER_WAYS];
static int find_stage(const prog_t *p, int nvn_stage, const uint8_t **ctl, const uint8_t **code, uint32_t *len) {
for (uint32_t i = 0; i < p->nstage && i < 6; i++) {
@@ -492,9 +541,10 @@ static int find_stage(const prog_t *p, int nvn_stage, const uint8_t **ctl, const
return 0;
}
// Texture and image types of a stage's first 32 slots, kept while its handles and every slot's resolution
// (vk_texture_slots_gen) stay the same: consecutive draws mostly bind the same slots, and each lookup is a hash probe.
// Texture and image types of a stage's first 32 slots, kept while every slot's resolution (vk_texture_slots_gen)
// stays the same: only slots whose handle changed are looked up again, each lookup being a hash probe.
static struct { int valid; uint32_t gen; uint64_t tex[32], img[32]; uint8_t tt[32], it[32]; } AC[6];
static int shader_memo_on(void);
static void answers_for(VkExec *ex, int st, const uint8_t *ctl, GfxShaderAnswers *a) {
memset(a, 0, sizeof *a);
@@ -509,21 +559,39 @@ static void answers_for(VkExec *ex, int st, const uint8_t *ctl, GfxShaderAnswers
typeof(AC[0]) *c = &AC[st];
vk_texture_lock();
uint32_t gen = vk_texture_slots_gen(); // before the lookups: one that creates an entry bumps it
if (!c->valid || c->gen != gen || memcmp(c->tex, ex->tex[st], sizeof c->tex) || memcmp(c->img, ex->img[st], sizeof c->img)) {
int all = !c->valid || c->gen != gen;
if (all || memcmp(c->tex, ex->tex[st], sizeof c->tex) || memcmp(c->img, ex->img[st], sizeof c->img)) {
for (int slot = 0; slot < 32; slot++) {
view_t v;
int hv;
uint64_t h = ex->tex[st][slot];
const VkTexImage *img = h ? vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv) : NULL;
c->tt[slot] = img ? (uint8_t)gfx_type_of(img, &v, hv) : 0;
if (all || h != c->tex[slot]) {
const VkTexImage *img = h ? vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv) : NULL;
c->tt[slot] = img ? (uint8_t)gfx_type_of(img, &v, hv) : 0;
}
h = ex->img[st][slot];
img = h ? vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv) : NULL;
c->it[slot] = img ? (uint8_t)gfx_type_of(img, &v, hv) : 0;
if (all || h != c->img[slot]) {
const VkTexImage *img = h ? vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv) : NULL;
c->it[slot] = img ? (uint8_t)gfx_type_of(img, &v, hv) : 0;
}
}
memcpy(c->tex, ex->tex[st], sizeof c->tex);
memcpy(c->img, ex->img[st], sizeof c->img);
c->gen = gen, c->valid = 1;
BS.ac_miss++;
}
if (shader_memo_on() == 2) // check mode: every slot looked up again
for (int slot = 0; slot < 32; slot++) {
view_t v;
int hv;
uint64_t h = ex->tex[st][slot], hi = ex->img[st][slot];
const VkTexImage *img = h ? vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv) : NULL;
int tt = img ? gfx_type_of(img, &v, hv) : 0;
img = hi ? vk_texture_by_id_locked((int)(hi & 0xfffff), &v, &hv) : NULL;
int it = img ? gfx_type_of(img, &v, hv) : 0;
if (tt != c->tt[slot] || it != c->it[slot])
rt_fatal("vk_exec: kept texture types of stage %d slot %d differ from a lookup", st, slot);
}
vk_texture_unlock();
memcpy(a->tex_type, c->tt, sizeof c->tt);
memcpy(a->img_type, c->it, sizeof c->it);
@@ -542,11 +610,20 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin
GfxShaderAnswers a;
answers_for(ex, st, ctl, &a);
uint64_t serial = ex->prog->serial;
ShaderEnt *e = NULL;
ShaderEnt *set = NULL;
if (serial) {
uint64_t h = (serial * 0x9E3779B97F4A7C15ull ^ (uint64_t)st) ^ nvn_hm_hash(&a, sizeof a); // a is zero-padded
e = &shader_tab[(h ^ h >> 29) & 2047];
if (e->used && e->serial == serial && e->stage == st && !memcmp(&e->a, &a, sizeof a)) return e->s;
set = shader_tab[(h ^ h >> 29) & (SHADER_SETS - 1)];
for (int w = 0; w < SHADER_WAYS && set[w].used; w++) {
ShaderEnt *e = &set[w];
if (e->serial != serial || e->stage != st || memcmp(&e->a, &a, sizeof a)) continue;
if (w) { // to the front
ShaderEnt hit = *e;
memmove(&set[1], &set[0], w * sizeof *set);
set[0] = hit;
}
return set[0].s;
}
}
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)
@@ -555,7 +632,10 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin
if (pending && *pending) return NULL;
if (!s) warn_once(W_SHADER, "a stage %d shader of program %llu failed to translate: draws with it are skipped", st,
(unsigned long long)serial);
else if (e) e->used = 1, e->serial = serial, e->stage = st, e->a = a, e->s = s;
else if (set) { // in front, the least recently used way drops out
memmove(&set[1], &set[0], (SHADER_WAYS - 1) * sizeof *set);
set[0] = (ShaderEnt){.serial = serial, .stage = st, .used = 1, .a = a, .s = s};
}
return s;
}
@@ -569,22 +649,44 @@ static VkDescriptorBufferInfo zero_info(uint64_t max) {
return (VkDescriptorBufferInfo){Z.zbuf, 0, max < ZERO_BYTES ? max : ZERO_BYTES};
}
// A buffer descriptor for [addr, addr+size) of guest memory: its backing when aligned, a copy in transient memory when
// the backing is host memory with an unaligned offset. 0 when neither works.
static int bind_range(VkExec *ex, uint64_t addr, uint64_t size, uint64_t align, uint64_t max, VkDescriptorBufferInfo *out) {
// vk_mem_count, also kept per (class, use) while a graphics build runs (RS).
static void count_use(int cls, int use, uint64_t n) {
vk_mem_count(cls, use, n);
if (!RS.rec) return;
for (int i = 0; i < RS.ncnt; i++)
if (RS.cnt[i].cls == cls && RS.cnt[i].use == use) {
RS.cnt[i].bytes += n;
return;
}
if (RS.ncnt < (int)(sizeof RS.cnt / sizeof RS.cnt[0])) RS.cnt[RS.ncnt++] = (RsCount){cls, use, n};
else RS.cnt_over = 1;
}
// A buffer descriptor for [addr, addr+size) of guest memory: its backing (a pool's VRAM mirror when it has one) when
// aligned, a copy in transient memory when the backing is host memory with an unaligned offset. 0 when neither works.
// written: the shader stores into it (a mirror's range goes back to guest memory at the end of the submission).
static int bind_range(VkExec *ex, uint64_t addr, uint64_t size, uint64_t align, uint64_t max, int use, int written,
VkDescriptorBufferInfo *out) {
VkMemSpan sp;
if (!addr || !vk_mem_resolve(addr, 1, &sp)) return 0;
uint64_t n = size && size < sp.avail ? size : sp.avail;
if (n > max) n = max;
if (!n) return 0;
if (sp.offset % align == 0) {
VK_MEM_USE(sp);
*out = (VkDescriptorBufferInfo){sp.buffer, sp.offset, n};
if (written) vk_mem_gpu_wrote(&sp, n);
count_use(sp.cls, use, n);
VK_MEM_BOUND(sp);
return 1;
}
if (sp.alloc) return 0;
VkBuffer b;
VkDeviceSize o;
RS.volatile_ = 1;
memcpy(scratch(ex, n, &b, &o), (const void *)(uintptr_t)addr, n);
vk_mem_count(sp.cls, VK_MUSE_CPU_READ, n);
vk_mem_count(VK_MCLS_SCRATCH, use, n);
*out = (VkDescriptorBufferInfo){b, o, n};
return 1;
}
@@ -661,11 +763,12 @@ static VkDescriptorBufferInfo ubo_info(VkExec *ex, int st, int slot) {
uint32_t len, c1;
if (find_stage(ex->prog, st, &ctl, &code, &len)) {
memcpy(&c1, ctl + 0x700, 4);
if (c1 < len && bind_range(ex, (uint64_t)(uintptr_t)code + c1, len - c1, align, max, &bi)) return bi;
if (c1 < len && bind_range(ex, (uint64_t)(uintptr_t)code + c1, len - c1, align, max, VK_MUSE_UBO, 0, &bi))
return bi;
}
} else if (slot >= 3 && slot - 3 < 18) {
const p_bind_buf *b = &ex->ubo[st][slot - 3];
if (bind_range(ex, b->addr, b->size, align, max, &bi)) return bi;
if (bind_range(ex, b->addr, b->size, align, max, VK_MUSE_UBO, 0, &bi)) return bi;
}
return zero_info(max);
}
@@ -695,7 +798,8 @@ static VkDescriptorBufferInfo ssbo_info(VkExec *ex, int st, const GfxShaderRes *
const p_bind_buf *b = ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot];
// vk_caps.c: shaders translated for sbuf_align address the buffer from its address rounded down to it.
uint64_t lead = vk_caps.sbuf_align ? b->addr & (vk_caps.sbuf_align - 1) : 0;
if (bind_range(ex, b->addr - lead, b->size ? b->size + lead : 0, align, max, &bi)) {
if (bind_range(ex, b->addr - lead, b->size ? b->size + lead : 0, align, max, ubo ? VK_MUSE_UBO : VK_MUSE_SSBO,
written, &bi)) {
if (written && bi.buffer != VK_NULL_HANDLE) note_written(ex, b->addr - lead, bi.range);
return bi;
}
@@ -818,6 +922,7 @@ static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage,
int hv;
const VkTexImage *img = vk_texture_by_id_locked((int)(h & 0xfffff), &v, &hv);
if (img) view = get_view(ex, img, &v, hv, r->type, storage);
if (img && storage) vk_texture_storage_bound_locked(img);
if (img && !storage && ex->nfbl && in_fbl(ex, img->image)) layout = VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT;
if (!storage && !separate) samp = vk_sampler_by_id_locked((int)(h >> 20 & 0xfff));
}
@@ -861,6 +966,12 @@ static VkDescriptorSet alloc_set(VkExec *ex, VkDescriptorSetLayout layout) {
return VK_NULL_HANDLE;
}
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;
in[2] = ex->point_size > 0 ? ex->point_size : 1.0f;
}
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};
@@ -871,50 +982,257 @@ static void support_fill(uint8_t *b, float w, float h, float point_size) {
for (int i = 0; i < 73; i++) memcpy(b + 192 + 16 * i, &one, 4); // render scale 1: the host renders at guest size
}
// TOTK_BIND_STATS=1: bindings and descriptors per draw of each stage, logged every 20000 draws (Task 2.2's choice of
// the pushed stage).
static struct { int on; uint64_t draws, bind[2], desc[2]; } BS = {-1};
static void bind_stats(const VkPipeInfo *info) {
// TOTK_BIND_STATS=1: bindings and descriptors per draw of each stage (Task 2.2's choice of the pushed stage), and how
// often a draw repeats the previous one, logged every 20000 draws: pushes recorded and skipped, allocated sets written
// and reused, stage texture-type recomputations (answers_for), draws with the previous draw's program, bound state
// and textures (shaders and pipeline would be the same) and those that also have its buffers and viewport (all
// descriptors would be the same). BS is declared beside DS.
static int bind_stats_on(void) {
if (BS.on < 0) {
const char *e = cfg_get("TOTK_BIND_STATS");
BS.on = e && *e && strcmp(e, "0");
}
if (!BS.on || info->bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS) return;
return BS.on;
}
static void bind_stats(const VkPipeInfo *info) {
if (!bind_stats_on() || info->bind_point != VK_PIPELINE_BIND_POINT_GRAPHICS) return;
for (uint32_t i = 0; i < info->n_bindings; i++) {
const VkPipeBinding *b = &info->bindings[i];
int fs = info->n_sets > 1 ? b->set : !(b->stages & VK_SHADER_STAGE_VERTEX_BIT);
BS.bind[fs]++, BS.desc[fs] += b->count ? b->count : 1;
}
if (++BS.draws % 20000 == 0)
if (++BS.draws % 20000 == 0) {
double d = (double)BS.draws;
rt_log("vk_exec: per draw over %llu draws: vertex %.2f bindings %.2f descriptors, fragment %.2f bindings %.2f descriptors",
(unsigned long long)BS.draws, (double)BS.bind[0] / BS.draws, (double)BS.desc[0] / BS.draws,
(double)BS.bind[1] / BS.draws, (double)BS.desc[1] / BS.draws);
(unsigned long long)BS.draws, BS.bind[0] / d, BS.desc[0] / d, BS.bind[1] / d, BS.desc[1] / d);
rt_log("vk_exec: per draw: pushes %.3f skipped %.3f, sets written %.3f reused %.3f, texture types %.3f; "
"same program and state as the draw before %.3f, also same buffers %.3f; descriptor builds skipped %.3f",
BS.push / d, BS.push_skip / d, BS.set_new / d, BS.set_reuse / d, BS.ac_miss / d, BS.same_prog / d,
BS.same_all / d, BS.bind_skip / d);
}
}
// The bound state a draw reads, compared with the previous draw's (TOTK_BIND_STATS only).
static void repeat_stats(const VkExec *ex, const VkPipeInfo *info) {
static struct {
const prog_t *prog;
VkPipeState pipe;
uint64_t tex[2][128], img[2][32];
const VkPipeInfo *info;
p_bind_buf ubo[2][18], ssbo[2][16];
float vp[6], point_size;
} last, cur;
if (!bind_stats_on()) return;
memset(&cur, 0, sizeof cur);
cur.prog = ex->prog, cur.pipe = ex->pipe, cur.info = info;
const int st[2] = {NVN_STAGE_VS, NVN_STAGE_FS};
for (int k = 0; k < 2; k++) {
memcpy(cur.tex[k], ex->tex[st[k]], sizeof cur.tex[k]), memcpy(cur.img[k], ex->img[st[k]], sizeof cur.img[k]);
memcpy(cur.ubo[k], ex->ubo[st[k]], sizeof cur.ubo[k]), memcpy(cur.ssbo[k], ex->ssbo[st[k]], sizeof cur.ssbo[k]);
}
memcpy(cur.vp, ex->vp[0], sizeof cur.vp), cur.point_size = ex->point_size;
size_t prog_part = offsetof(typeof(cur), info);
if (!memcmp(&cur, &last, prog_part)) {
BS.same_prog++;
if (!memcmp((const char *)&cur + prog_part, (const char *)&last + prog_part, sizeof cur - prog_part)) BS.same_all++;
}
last = cur;
}
static int push_skip_on(void) { // TOTK_PUSH_SKIP=0: every graphics draw pushes its set again
static int on = -1;
if (on < 0) {
const char *e = cfg_get("TOTK_PUSH_SKIP");
on = !(e && !strcmp(e, "0"));
}
return on;
}
// A graphics draw whose descriptor inputs equal the previous graphics draw's in this command buffer skips building its
// sets: every write would be the same, the push would be skipped and the allocated set reused without a bind (about a
// quarter of field and monsters2 draws, TOTK_BIND_STATS). The inputs, compared and stored in place, are what
// bind_resources and its helpers read beyond the pipeline info's fixed bindings and Z's stand-ins:
// - the info (bindings, layouts), the program and its serial (the compiler-constant range of UBO slot 1);
// - vk_texture_slots_gen, vk_texture_epoch (a slot's image, view and once-per-submit storage check), vk_sampler_gen;
// - vk_mem_gen (buffer and offset of a span, mirror creation and per-submission sync);
// - the images in the feedback layout (the sampled layout);
// - per binding: the support inputs, c0's storage table and base instance, the bound uniform and storage ranges, the
// texture and image handles (RsList; compared only when a bind op ran since the last draw, bind_gen).
// The side effects of a build repeat as no-ops in the same command buffer and submission: c0 and support buffers kept
// in B, note_written's ranges, vk_mem_gpu_wrote's write-backs, the per-submission mirror sync and dirty census note,
// the views cached per submission. The census counts are added again. A build that copied guest bytes into transient
// memory (an unaligned host range) is never skipped after: the copy may differ. B.res_valid says the sets bound hold
// the last build; vk_exec_draw_new_cb's memset clears it, and every build clears it until it ends.
// TOTK_BIND_SKIP=0 builds every draw; =check builds every draw and stops when a draw with equal inputs gets other
// writes, census counts or descriptor commands.
static int bind_skip_on(void) {
if (RS.on < 0) {
const char *e = cfg_get("TOTK_BIND_SKIP");
RS.on = e && !strcmp(e, "check") ? 2 : !(e && !strcmp(e, "0"));
if (RS.on != 1) rt_log("vk_exec: descriptor build skip %s", RS.on ? "check" : "off");
}
return RS.on;
}
static void rs_add(RsRange *r, uint32_t *n, const VkExec *ex, const void *at, size_t len) {
uint32_t off = (uint32_t)((const uint8_t *)at - (const uint8_t *)ex);
if (*n && r[*n - 1].off + r[*n - 1].len == off) r[*n - 1].len += (uint32_t)len;
else r[(*n)++] = (RsRange){off, (uint32_t)len};
}
static const RsList *rs_list(const VkExec *ex, const VkPipeInfo *info) {
if (info == rs_last) return rs_last_l;
if (!rs_lists.klen) nvn_hm_init(&rs_lists, sizeof info);
RsList *l = nvn_hm_get(&rs_lists, &info);
if (!l) {
static RsRange tmp[MAX_DESC];
uint32_t n = 0, nd = 0;
int sup = 0, c0 = 0, ok = 1;
for (uint32_t i = 0; i < info->n_bindings && ok; i++) {
const VkPipeBinding *b = &info->bindings[i];
const GfxShaderRes *r = b->res;
uint32_t cnt = b->count ? b->count : 1;
if ((nd += cnt) > MAX_DESC) { ok = 0; break; } // the build stops on it
int st = nvn_stage_of(b->stage);
if (b->kind == VK_PIPE_SUPPORT) {
sup = 1;
} else if (b->kind == GFX_RES_UBO) {
for (uint32_t k = 0; k < cnt; k++) {
int slot = r->slot + (int)k;
if (slot == 0 && c0_on()) c0 = 1, rs_add(tmp, &n, ex, ex->ssbo[st], sizeof ex->ssbo[st]);
else if (slot >= 3 && slot - 3 < 18) rs_add(tmp, &n, ex, &ex->ubo[st][slot - 3], sizeof(p_bind_buf));
} // slot 1: the program (bind_sig); others: the zero buffer
} else if (b->kind == GFX_RES_SSBO) { // every descriptor of the binding is the same range
int ubo, slot = ssbo_slot(r, st, &ubo);
if (slot >= 0 && slot < 16) rs_add(tmp, &n, ex, ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot], sizeof(p_bind_buf));
} else if ((b->kind == GFX_RES_TEX || b->kind == GFX_RES_IMG) && b->type != VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
b->type != VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
int img = b->kind == GFX_RES_IMG, cap = img ? 32 : 128;
for (uint32_t k = 0; k < cnt; k++) {
int slot = r->slot + (int)k;
if (slot >= 0 && slot < cap) rs_add(tmp, &n, ex, img ? &ex->img[st][slot] : &ex->tex[st][slot], 8);
}
}
}
uint64_t bytes = 0;
for (uint32_t i = 0; i < n; i++) bytes += tmp[i].len;
if (bytes > RS_BYTES) ok = 0;
l = malloc(sizeof *l + sizeof *tmp * n);
if (!l) rt_fatal("vk: out of memory");
l->n = n, l->bytes = (uint32_t)bytes, l->sup = (uint8_t)sup, l->c0 = (uint8_t)c0, l->ok = (uint8_t)ok;
memcpy(l->r, tmp, sizeof *tmp * n);
nvn_hm_put(&rs_lists, &info, l);
}
rs_last = info, rs_last_l = l;
return l;
}
// The signature of a graphics draw's descriptor inputs into RS; 1 when it equals the stored one. The bound-slot bytes
// are compared only when a bind op ran since they were stored or the pipeline changed: otherwise they are the same.
// Check mode compares them anyway and stops when the bind op generation said they could not differ but they do.
static int bind_sig(const VkExec *ex, const VkPipeInfo *info) {
const RsList *l = rs_list(ex, info);
if (!l->ok) return RS.hn = 0, RS.info = NULL, 0;
uint64_t *h = RS.h, diff = 0;
uint32_t n = 0;
#define PUT(v) do { uint64_t v_ = (uint64_t)(v); diff |= h[n] ^ v_; h[n++] = v_; } while (0)
PUT((uintptr_t)info);
PUT((uintptr_t)ex->prog);
PUT(ex->prog->serial);
PUT((uint64_t)vk_texture_slots_gen() << 32 | vk_texture_epoch());
PUT((uint64_t)vk_sampler_gen() << 32 | (uint32_t)ex->nfbl);
PUT(vk_mem_gen());
for (int i = 0; i < ex->nfbl && i < 9; i++) PUT((uintptr_t)ex->fbl_img[i]);
if (l->sup) {
float in[3];
support_in(ex, in);
uint64_t a;
uint32_t ps;
memcpy(&a, in, 8), memcpy(&ps, &in[2], 4);
PUT(a);
PUT(ps);
}
if (l->c0) PUT(c0_binst);
#undef PUT
diff |= n != RS.hn;
RS.hn = n;
int fast = RS.info == info && RS.gen == bind_gen;
if (fast && RS.on != 2) return !diff;
const uint8_t *e = (const uint8_t *)ex;
uint8_t *d = RS.body;
int bd = 0;
for (uint32_t i = 0; i < l->n; i++) {
const RsRange *r = &l->r[i];
if (!bd && memcmp(d, e + r->off, r->len)) bd = 1;
if (bd) memcpy(d, e + r->off, r->len);
d += r->len;
}
if (fast && bd) rt_fatal("vk_exec: descriptor build skip check: bound slots changed without a bind op");
RS.info = info, RS.gen = bind_gen;
return !diff && !bd;
}
// Writes allocated set s from its packed descriptors d: through the layout's update template, or with the write
// structs w[i0..i1) when it has none.
static void write_set(const VkPipeInfo *info, uint32_t s, VkDescriptorSet set, const VkDescSlot *d, VkWriteDescriptorSet *w,
uint32_t i0, uint32_t i1) {
if (i1 == i0) return;
if (info->tpl[s]) {
vk_pipeline_update_tpl_fn()(vk_ctx()->device, set, info->tpl[s], d);
return;
}
for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set;
vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL);
}
// Writes the pipeline's sets from the bound state: set 0, and set 1 with independent layouts. Each is pushed into the
// command buffer, or allocated, written and bound; a graphics set whose contents equal the last one allocated for that
// set number in this command buffer is reused, and bound again only when another layout or a push came between.
// set number in this command buffer is reused, and bound again only when another layout or a push came between. A
// graphics push equal to the last one at its set number, with no other layout or bind between, is not recorded.
// The descriptors go into one packed array d, a VkDescSlot per descriptor in binding order, which a set's update
// template reads directly (VkPipeInfo.tpl); write structs are filled only when a set has no template.
static void bind_resources(VkExec *ex, const VkPipeInfo *info) {
static VkWriteDescriptorSet w[MAX_BINDINGS];
static VkDescriptorBufferInfo bi[MAX_DESC];
static VkDescriptorImageInfo ii[MAX_DESC];
static VkDescSlot d[MAX_DESC];
static VkBufferView bv[MAX_DESC];
if (info->n_bindings > MAX_BINDINGS) rt_fatal("vk: %u descriptor bindings exceed the replay's %d", info->n_bindings, MAX_BINDINGS);
bind_stats(info);
int gfx = info->bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS;
int skip = gfx ? bind_skip_on() : 0, same = 0, was_valid = B.res_valid;
if (skip) {
same = bind_sig(ex, info);
if (same && was_valid && skip == 1) {
for (int i = 0; i < RS.ncnt; i++) vk_mem_count(RS.cnt[i].cls, RS.cnt[i].use, RS.cnt[i].bytes);
BS.bind_skip++;
ex->recorded++;
return;
}
B.res_valid = 0; // until the build below ends
if (skip == 2) memcpy(RS.pcnt, RS.cnt, sizeof RS.cnt), RS.pncnt = RS.ncnt;
RS.rec = 1, RS.ncnt = 0, RS.cnt_over = 0, RS.volatile_ = 0, RS.cmds = 0;
}
uint32_t n_sets = info->n_sets ? info->n_sets : 1;
uint32_t lo[2] = {0, 0}, hi[2] = {0, 0}, d0[2] = {0, 0}; // bindings [lo, hi) and first descriptor per set
int need_w = 0; // a set without a template: fill the write structs
for (uint32_t s = 0, i0 = 0; s < n_sets; s++) {
uint32_t i1 = i0;
while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) i1++;
lo[s] = i0, hi[s] = i1, i0 = i1;
if (i1 > lo[s] && !info->tpl[s]) need_w = 1;
}
uint32_t nd = 0;
for (uint32_t i = 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;
if (nd + cnt > MAX_DESC) rt_fatal("vk: %u descriptors exceed the replay's %d", nd + cnt, MAX_DESC);
w[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstBinding = b->binding, .descriptorCount = cnt, .descriptorType = b->type};
int st = nvn_stage_of(b->stage);
if (n_sets == 2 && i == lo[1]) d0[1] = nd;
VkDescSlot *o = &d[nd];
int st = nvn_stage_of(b->stage), kind = 0; // kind: 0 buffer, 1 image, 2 texel buffer view
if (b->kind == VK_PIPE_SUPPORT) {
float vw = ex->in_pass && ex->has_vp ? ex->vp[0][2] : (float)ex->pass_w;
float vh = ex->in_pass && ex->has_vp ? ex->vp[0][3] : (float)ex->pass_h;
float in[3] = {vw, vh, ex->point_size > 0 ? ex->point_size : 1.0f};
float in[3];
support_in(ex, in);
if (!B.sup_valid || memcmp(in, B.sup_in, sizeof in)) { // support_fill reads only these
uint8_t tmp[SUPPORT_BYTES];
support_fill(tmp, in[0], in[1], in[2]);
@@ -927,38 +1245,45 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) {
B.sup_buf = sb, B.sup_off = so, B.sup_valid = 1;
}
}
bi[nd] = (VkDescriptorBufferInfo){B.sup_buf, B.sup_off, SUPPORT_BYTES};
w[i].pBufferInfo = &bi[nd];
o->buffer = (VkDescriptorBufferInfo){B.sup_buf, B.sup_off, SUPPORT_BYTES};
} else if (b->kind == GFX_RES_UBO) {
for (uint32_t k = 0; k < cnt; k++) bi[nd + k] = ubo_info(ex, st, r->slot + (int)k);
w[i].pBufferInfo = &bi[nd];
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) {
for (uint32_t k = 0; k < cnt; k++) bi[nd + k] = ssbo_info(ex, st, r, r->flags & 1);
w[i].pBufferInfo = &bi[nd];
for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ssbo_info(ex, st, r, r->flags & 1);
} else if (b->kind == GFX_RES_TEX || b->kind == GFX_RES_IMG) {
int img = b->kind == GFX_RES_IMG;
if (b->type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER || b->type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
for (uint32_t k = 0; k < cnt; k++) bv[nd + k] = Z.zview; // buffer textures are not replayed yet
w[i].pTexelBufferView = &bv[nd];
kind = 2;
for (uint32_t k = 0; k < cnt; k++) { // buffer textures are not replayed yet
o[k].w[0] = 0, o[k].w[1] = 0, o[k].w[2] = 0;
o[k].view = Z.zview;
if (need_w) bv[nd + k] = Z.zview;
}
} else {
kind = 1;
vk_texture_lock(); // tex_info looks up with the texture lock held
for (uint32_t k = 0; k < cnt; k++) {
int slot = r->slot + (int)k;
uint64_t h = 0;
if (img) h = slot >= 0 && slot < 32 ? ex->img[st][slot] : 0;
else h = slot >= 0 && slot < 128 ? ex->tex[st][slot] : 0;
tex_info(ex, r, h, img, r->separate, &ii[nd + k]);
tex_info(ex, r, h, img, r->separate, &o[k].image);
o[k].w[2] = (uint32_t)o[k].image.imageLayout; // zero the tail padding
}
vk_texture_unlock();
w[i].pImageInfo = &ii[nd];
}
} else {
rt_fatal("vk: binding %u has an unknown kind %d", (unsigned)b->binding, (int)b->kind);
}
if (need_w) {
w[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstBinding = b->binding, .descriptorCount = cnt, .descriptorType = b->type};
if (kind == 0) w[i].pBufferInfo = &o->buffer; // a VkDescSlot is as large as either info struct
else if (kind == 1) w[i].pImageInfo = &o->image;
else w[i].pTexelBufferView = &bv[nd];
}
nd += cnt;
}
uint32_t n_sets = info->n_sets ? info->n_sets : 1;
int gfx = info->bind_point == VK_PIPELINE_BIND_POINT_GRAPHICS;
// Under the validation layer, a new graphics layout binds set 1 before set 0 is pushed or bound. The layer (SDK
// 1.4.363, PipelineLayoutCompatDef::operator==) takes any two INDEPENDENT_SETS layouts as compatible, so
// vkCmdPushDescriptorSetKHR keeps the push set it made for an earlier layout and drops writes to bindings that
@@ -968,67 +1293,81 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) {
if (gfx && info->layout != B.ds_layout) { // another layout: sets bound before may be disturbed
B.ds_layout = info->layout;
B.ds_bound[0] = B.ds_bound[1] = VK_NULL_HANDLE;
B.ds_pushed[0] = B.ds_pushed[1] = 0;
set1_first = n_sets == 2 && vk_ctx()->validation;
}
uint32_t lo[2] = {0, 0}, hi[2] = {0, 0};
for (uint32_t s = 0, i0 = 0; s < n_sets; s++) {
uint32_t i1 = i0;
while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) i1++;
lo[s] = i0, hi[s] = i1, i0 = i1;
}
for (uint32_t k = 0; k < n_sets; k++) {
uint32_t s = set1_first ? 1 - k : k, i0 = lo[s], i1 = hi[s];
const VkDescSlot *ds = &d[d0[s]];
uint32_t dbytes = (uint32_t)(((s + 1 < n_sets ? d0[s + 1] : nd) - d0[s]) * sizeof *d);
if (s && i1 == i0) { // no fragment stage: set 1 is empty and unused
if (set1_first) { // bound only to drop the layer's push set (comment above)
VkDescriptorSet set = alloc_set(ex, info->set_layout1);
vkCmdBindDescriptorSets(ex->cb, info->bind_point, info->layout, 1, 1, &set, 0, NULL);
B.ds_bound[1] = set;
B.ds_bound[1] = set, B.ds_pushed[1] = 0;
RS.cmds++;
}
continue;
}
if (s ? info->push1 : info->push) {
vk_pipeline_push_fn()(ex->cb, info->bind_point, info->layout, s, i1 - i0, w + i0);
if (gfx) B.ds_bound[s] = VK_NULL_HANDLE;
uint32_t n = gfx && push_skip_on() ? dbytes : 0;
if (n && B.ds_pushed[s] && DS.pn[s] == n && !memcmp(DS.psig[s], ds, n)) {
BS.push_skip++;
continue;
}
if (info->tpl[s]) vk_pipeline_push_tpl_fn()(ex->cb, info->tpl[s], info->layout, s, ds);
else vk_pipeline_push_fn()(ex->cb, info->bind_point, info->layout, s, i1 - i0, w + i0);
BS.push++;
RS.cmds++;
if (gfx) {
B.ds_bound[s] = VK_NULL_HANDLE, B.ds_pushed[s] = n != 0;
if (n) DS.pn[s] = n, memcpy(DS.psig[s], ds, n);
}
} else if (!gfx || s > 1) {
VkDescriptorSet set = alloc_set(ex, s ? info->set_layout1 : info->set_layout);
for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set;
if (i1 > i0) vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL);
write_set(info, s, set, ds, w, i0, i1);
vkCmdBindDescriptorSets(ex->cb, info->bind_point, info->layout, s, 1, &set, 0, NULL);
} else {
VkDescriptorSetLayout sl = s ? info->set_layout1 : info->set_layout;
uint32_t n = 0;
for (uint32_t k = i0; k < i1; k++) {
const VkWriteDescriptorSet *x = &w[k];
uint32_t hdr[3] = {x->dstBinding, (uint32_t)x->descriptorType, x->descriptorCount};
memcpy(DS.tmp + n, hdr, sizeof hdr), n += sizeof hdr;
if (x->pImageInfo) { // field by field: the struct's tail padding is not written
for (uint32_t d = 0; d < x->descriptorCount; d++) {
const VkDescriptorImageInfo *ii1 = &x->pImageInfo[d];
memcpy(DS.tmp + n, &ii1->sampler, 8), memcpy(DS.tmp + n + 8, &ii1->imageView, 8);
memcpy(DS.tmp + n + 16, &ii1->imageLayout, 4), n += 20;
}
} else if (x->pBufferInfo) {
memcpy(DS.tmp + n, x->pBufferInfo, sizeof *x->pBufferInfo * x->descriptorCount);
n += (uint32_t)(sizeof *x->pBufferInfo * x->descriptorCount);
} else {
memcpy(DS.tmp + n, x->pTexelBufferView, sizeof *x->pTexelBufferView * x->descriptorCount);
n += (uint32_t)(sizeof *x->pTexelBufferView * x->descriptorCount);
}
}
VkDescriptorSet set = B.ds_last[s];
if (!set || B.ds_last_layout[s] != sl || DS.n[s] != n || memcmp(DS.sig[s], DS.tmp, n)) {
if (!set || B.ds_last_layout[s] != sl || DS.n[s] != dbytes || memcmp(DS.sig[s], ds, dbytes)) {
set = alloc_set(ex, sl);
for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set;
if (i1 > i0) vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL);
B.ds_last[s] = set, B.ds_last_layout[s] = sl, DS.n[s] = n;
memcpy(DS.sig[s], DS.tmp, n);
write_set(info, s, set, ds, w, i0, i1);
B.ds_last[s] = set, B.ds_last_layout[s] = sl, DS.n[s] = dbytes;
memcpy(DS.sig[s], ds, dbytes);
BS.set_new++;
RS.cmds++;
} else {
BS.set_reuse++;
}
if (B.ds_bound[s] != set) {
vkCmdBindDescriptorSets(ex->cb, info->bind_point, info->layout, s, 1, &set, 0, NULL);
B.ds_bound[s] = set;
B.ds_bound[s] = set, B.ds_pushed[s] = 0;
RS.cmds++;
}
}
}
if (skip) {
RS.rec = 0;
// An equal next draw records nothing when every push set holds its push (TOTK_PUSH_SKIP on) and nothing made
// the build unrepeatable; allocated sets are always bound to the one written last.
int valid = !RS.volatile_ && !RS.cnt_over && RS.hn;
for (uint32_t s = 0; s < n_sets; s++)
if (hi[s] > lo[s] && (s ? info->push1 : info->push) && !B.ds_pushed[s]) valid = 0;
if (skip == 2) { // with the pipeline info equal (bind_sig), the packed descriptors determine every write
uint32_t n = (uint32_t)(nd * sizeof *d);
int wr = n != RS.ckn || memcmp(RS.ck, d, n);
int cn = RS.ncnt != RS.pncnt || memcmp(RS.cnt, RS.pcnt, sizeof RS.cnt[0] * (size_t)RS.ncnt);
if (same && was_valid && (wr || cn || RS.cmds))
rt_fatal("vk_exec: descriptor build skip check: a draw of program %llu with the inputs of the previous one "
"gets %s", (unsigned long long)ex->prog->serial,
RS.cmds ? "descriptor commands" : wr ? "other writes" : "other census counts");
if (same && was_valid) BS.bind_skip++; // the draws a skip would have spared, compared instead
RS.ckn = n;
memcpy(RS.ck, d, n);
}
B.res_valid = valid;
}
ex->recorded++;
}
@@ -1082,6 +1421,8 @@ static void record_fixed(VkExec *ex) {
static void bind_vertex_buffers(VkExec *ex) {
VkBuffer vb[17];
VkDeviceSize vo[17], vsz[17], vstr[17];
int vcls[17] = {0};
void *vpool[17] = {0};
int dyn = vk_pipeline_vertex_input_dynamic();
for (int b = 0; b < 17; b++) {
vb[b] = Z.zbuf, vo[b] = 0, vsz[b] = ZERO_BYTES, vstr[b] = b < 16 ? ex->pipe.vstream[b].stride : 0;
@@ -1089,18 +1430,81 @@ static void bind_vertex_buffers(VkExec *ex) {
const NVNbufferRange *r = &ex->vbuf[b];
VkMemSpan sp;
if (!r->address || !vk_mem_resolve(r->address, 1, &sp)) continue;
VK_MEM_USE(sp);
uint64_t n = r->size && r->size < sp.avail ? r->size : sp.avail;
vb[b] = sp.buffer, vo[b] = sp.offset, vsz[b] = n;
vb[b] = sp.buffer, vo[b] = sp.offset, vsz[b] = n, vcls[b] = sp.cls, vpool[b] = sp.pool;
}
if (B.vb_valid && !memcmp(B.vb, vb, sizeof vb) && !memcmp(B.vo, vo, sizeof vo) && !memcmp(B.vsz, vsz, sizeof vsz) &&
(dyn || !memcmp(B.vstr, vstr, sizeof vstr)))
return;
vkCmdBindVertexBuffers2(ex->cb, 0, 17, vb, vo, vsz, dyn ? NULL : vstr);
ex->recorded++;
for (int b = 0; b < 17; b++)
if (vcls[b]) vk_mem_count(vcls[b], VK_MUSE_VB, vsz[b]);
if (rt_trace & RT_TRACE_PERF)
for (int b = 0; b < 17; b++)
if (vpool[b]) vk_mem_bound_note(vpool[b]);
memcpy(B.vb, vb, sizeof vb), memcpy(B.vo, vo, sizeof vo), memcpy(B.vsz, vsz, sizeof vsz), memcpy(B.vstr, vstr, sizeof vstr);
B.vb_valid = 1;
}
// TOTK_PIPE_SKIP: which draws are skipped, rather than waited for, while their shaders or pipeline build.
// strict (0): only draws whose pass targets were all rendered in the previous presented frame; a pass with a target
// that was not counts as one-shot and waits.
// any (1, default): also draws in a pass with at least one target rendered in the previous frame (a per-frame depth
// buffer or g-buffer with a colour target the frame before did not use: half-resolution effect buffers in fights).
// Passes with none (volumes, LUTs, shadow layers seen for the first time) still wait.
// fullscreen (2): as any, and full-screen draws into such passes are skipped instead of waiting from their budget.
static int pipe_skip_mode(void) {
static int mode = -1;
if (mode < 0) {
const char *e = cfg_get("TOTK_PIPE_SKIP");
mode = !e || !*e ? 1 : !strcmp(e, "strict") || !strcmp(e, "0") ? 0 : !strcmp(e, "fullscreen") || !strcmp(e, "2") ? 2 : 1;
static const char *const what[3] = {"only draws into per-frame passes (TOTK_PIPE_SKIP=strict)",
"draws into passes with a target the previous frame rendered",
"those and full-screen draws (TOTK_PIPE_SKIP=fullscreen)"};
rt_log("vk_exec: draws skipped while their pipeline builds: %s", what[mode]);
}
return mode;
}
// The previous draw's shaders, kept while its program, bound pipeline state, the vertex and fragment stages' first 32
// texture and image handles and every slot's resolution (vk_texture_slots_gen, read unlocked: a registration racing
// with it is ordered after this draw, as answers_for's own check would order it) stay the same: answers_for reads
// nothing else, so shader_for would return the same shaders. About 65% of field draws repeat them
// (TOTK_BIND_STATS). TOTK_SHADER_MEMO=0 turns it off; =check looks the shaders up on every hit too, and every slot's
// texture type in answers_for, and stops on a difference.
typedef struct {
const prog_t *prog;
uint64_t serial;
uint32_t gen;
uint64_t tex[2][32], img[2][32];
VkPipeState pipe;
} MemoKey;
static struct { int on, valid, has_fs; MemoKey k; const GfxShader *vs, *fs; } SM = {-1};
static void memo_key(const VkExec *ex, uint32_t gen, MemoKey *k) {
k->prog = ex->prog, k->serial = ex->prog->serial, k->gen = gen;
memcpy(k->tex[0], ex->tex[NVN_STAGE_VS], sizeof k->tex[0]), memcpy(k->tex[1], ex->tex[NVN_STAGE_FS], sizeof k->tex[1]);
memcpy(k->img[0], ex->img[NVN_STAGE_VS], sizeof k->img[0]), memcpy(k->img[1], ex->img[NVN_STAGE_FS], sizeof k->img[1]);
memcpy(&k->pipe, &ex->pipe, sizeof k->pipe);
}
static int memo_match(const VkExec *ex, uint32_t gen, const MemoKey *k) {
return k->prog == ex->prog && k->serial == ex->prog->serial && k->gen == gen &&
!memcmp(k->tex[0], ex->tex[NVN_STAGE_VS], sizeof k->tex[0]) && !memcmp(k->tex[1], ex->tex[NVN_STAGE_FS], sizeof k->tex[1]) &&
!memcmp(k->img[0], ex->img[NVN_STAGE_VS], sizeof k->img[0]) && !memcmp(k->img[1], ex->img[NVN_STAGE_FS], sizeof k->img[1]) &&
!memcmp(&k->pipe, &ex->pipe, sizeof k->pipe);
}
static int shader_memo_on(void) {
if (SM.on < 0) {
const char *e = cfg_get("TOTK_SHADER_MEMO");
SM.on = e && !strcmp(e, "check") ? 2 : !(e && !strcmp(e, "0"));
}
return SM.on;
}
// Pass, shaders, pipeline, fixed state and resources for a draw. 0: the draw is skipped.
static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) {
if (ex->render_disabled) return 0;
@@ -1111,16 +1515,31 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) {
VkRect2D sc = vk_exec_scissor(ex);
if (!sc.extent.width || !sc.extent.height) return 0;
if (!ex->prog) return warn_once(W_PROGRAM, "a draw without a bound program is skipped"), 0;
int has_vs, has_fs, vs_wait, fs_wait;
const GfxShader *vs = shader_for(ex, NVN_STAGE_VS, &has_vs, &vs_wait, 0);
const GfxShader *fs = shader_for(ex, NVN_STAGE_FS, &has_fs, &fs_wait, 0);
int has_vs, has_fs, vs_wait = 0, fs_wait = 0, memo = shader_memo_on();
const GfxShader *vs, *fs;
uint32_t gen = memo ? vk_texture_slots_gen() : 0; // before shader_for, whose lookups may bump it
if (memo && SM.valid && memo_match(ex, gen, &SM.k)) {
vs = SM.vs, fs = SM.fs, has_vs = 1, has_fs = SM.has_fs;
if (memo == 2) {
int hv, hf, wv, wf;
const GfxShader *v2 = shader_for(ex, NVN_STAGE_VS, &hv, &wv, 0), *f2 = shader_for(ex, NVN_STAGE_FS, &hf, &wf, 0);
if (v2 != vs || f2 != fs || hf != has_fs || wv || wf)
rt_fatal("vk_exec: shader memo of program %llu differs from the lookup", (unsigned long long)ex->prog->serial);
}
} else {
SM.valid = 0;
vs = shader_for(ex, NVN_STAGE_VS, &has_vs, &vs_wait, 0);
fs = shader_for(ex, NVN_STAGE_FS, &has_fs, &fs_wait, 0);
if (memo && vs && !vs_wait && !fs_wait && (!has_fs || fs))
memo_key(ex, gen, &SM.k), SM.vs = vs, SM.fs = fs, SM.has_fs = has_fs, SM.valid = 1;
}
if (!has_vs) return warn_once(W_NO_VS, "a draw with a program that has no vertex stage is skipped"), 0;
// A draw into a target the previous frame did not render (or into none) waits for a pending shader or pipeline:
// one-shot draws (volumes and LUTs generated once, later only sampled) would leave their target empty for the
// session. A draw into per-frame targets is skipped until they are ready and comes back next frame.
int oneshot = -1; // -1: not looked up
int oneshot = -1, skip = pipe_skip_mode(); // -1: not looked up
if (vs_wait || fs_wait) {
if (!(oneshot = !vk_exec_targets_recent(ex))) return perf_count(PERF_CNT_SHADER_SKIP), 0; // skipped until ready
if (!(oneshot = !vk_exec_targets_recent(ex, skip >= 1))) return perf_count(PERF_CNT_SHADER_SKIP), 0; // skipped until ready
perf_count(PERF_CNT_ONESHOT_WAIT);
if (vs_wait) vs = shader_for(ex, NVN_STAGE_VS, &has_vs, NULL, 1);
if (fs_wait) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, NULL, 1);
@@ -1137,29 +1556,43 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) {
const VkPipeInfo *info = NULL;
int pending;
// A draw of at most 6 vertices over half the render area or more (a full-screen triangle or quad, a tile) waits for
// its pipeline: skipping it can leave a black tile. The waits share 50 ms per presented frame. A one-shot draw
// waits without that budget. Everything else is skipped while its pipeline is built.
// its pipeline: skipping it can leave a black tile. The waits share 50 ms per presented frame
// (TOTK_PIPE_SKIP=fullscreen skips them instead). A one-shot draw waits without that budget. Everything else is
// skipped while its pipeline is built.
static uint64_t wait_frame;
static uint64_t wait_left_ns;
uint32_t wait_ms = 0;
uint64_t t0 = 0;
if (oneshot > 0) {
wait_ms = ONESHOT_WAIT_MS;
} else if (count <= 6 && (uint64_t)sc.extent.width * sc.extent.height * 2 >= (uint64_t)ex->pass_w * ex->pass_h) {
} else if (skip < 2 && count <= 6 && (uint64_t)sc.extent.width * sc.extent.height * 2 >= (uint64_t)ex->pass_w * ex->pass_h) {
uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
if (f != wait_frame) wait_frame = f, wait_left_ns = 50000000;
wait_ms = (uint32_t)(wait_left_ns / 1000000);
if (wait_ms) t0 = kern_now_ns();
}
uint64_t w0 = kern_now_ns();
VkPipeline p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms);
if (t0) {
uint64_t spent = kern_now_ns() - t0;
wait_left_ns = spent < wait_left_ns ? wait_left_ns - spent : 0;
}
if (pending && oneshot < 0 && !vk_exec_targets_recent(ex)) {
const char *why = oneshot > 0 ? "one-shot, shader" : wait_ms ? "full-screen" : NULL;
if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) {
perf_count(PERF_CNT_ONESHOT_WAIT);
why = "one-shot";
p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS);
}
uint64_t waited = kern_now_ns() - w0;
static int wait_logs;
if (why && waited >= 2000000 && wait_logs < 200) {
char tg[512];
wait_logs++;
vk_exec_targets_describe(ex, tg, sizeof tg);
rt_log("vk_exec: frame %llu: a draw waited %.1f ms for its pipeline (%s%s): %u vertices, pass %ux%u, %s",
(unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1, waited / 1e6, why,
pending ? ", skipped after all" : "", count, ex->pass_w, ex->pass_h, tg);
}
if (pending) return 0; // a background worker is building it: the draw is skipped until it is ready
if (!p || !info) return warn_once(W_PIPELINE, "a graphics pipeline could not be created: draws with it are skipped"), 0;
bind_pipeline(ex, VK_PIPELINE_BIND_POINT_GRAPHICS, p);
@@ -1169,6 +1602,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) {
B.st = s, B.st_valid = 1;
}
record_fixed(ex);
repeat_stats(ex, info);
bind_resources(ex, info);
if (fbl & ex->fb_dirty) feedback_barrier(ex);
feedback_written(ex, &s);
@@ -1247,6 +1681,8 @@ static void op_draw_idx(VkExec *ex, const p_draw_idx *d) {
if (!vk_mem_resolve(d->ibuf, bytes, &sp)) return warn_once(W_INDEX, "index buffer %#llx is not backed: draws with it are skipped", (unsigned long long)d->ibuf);
if (d->itype == NVN_INDEX_TYPE_UNSIGNED_BYTE) { // no uint8 index type here: widen (host-readable backing only)
if (sp.alloc) return warn_once(W_INDEX, "byte indices in device-local memory: draw skipped");
vk_mem_count(sp.cls, VK_MUSE_CPU_READ, bytes);
vk_mem_count(VK_MCLS_SCRATCH, VK_MUSE_IB, 2 * bytes);
uint16_t *ix = malloc(sizeof *ix * (size_t)d->count);
if (!ix) rt_fatal("vk: out of memory");
for (int i = 0; i < d->count; i++) {
@@ -1258,8 +1694,11 @@ static void op_draw_idx(VkExec *ex, const p_draw_idx *d) {
} else if (sp.offset % isize[d->itype]) {
return warn_once(W_INDEX, "index buffer %#llx is not aligned to its index size: draw skipped", (unsigned long long)d->ibuf);
} else {
VK_MEM_USE(sp);
vkCmdBindIndexBuffer(ex->cb, sp.buffer, sp.offset, d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
ex->recorded++;
vk_mem_count(sp.cls, VK_MUSE_IB, bytes);
VK_MEM_BOUND(sp);
}
vkCmdDrawIndexed(ex->cb, (uint32_t)d->count, d->instances > 0 ? (uint32_t)d->instances : 1, 0, d->base_vertex,
(uint32_t)d->base_instance);
@@ -1280,9 +1719,14 @@ static void op_draw_idx_ind(VkExec *ex, const p_draw_idx_ind *d) {
return warn_once(W_INDEX, "index buffer %#llx is not backed or not aligned: indirect draws skipped", (unsigned long long)d->ibuf);
if (!prepare_draw(ex, d->prim, 0x7fffffff, restart_for(ex, d->prim, d->itype))) return;
vk_exec_frame.draws++;
VK_MEM_USE(ip);
VK_MEM_USE(ib);
vkCmdBindIndexBuffer(ex->cb, ib.buffer, ib.offset, d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexedIndirect(ex->cb, ip.buffer, ip.offset, 1, sizeof(VkDrawIndexedIndirectCommand));
ex->recorded += 2;
vk_mem_count(ip.cls, VK_MUSE_IND, sizeof(VkDrawIndexedIndirectCommand));
VK_MEM_BOUND(ip);
VK_MEM_BOUND(ib);
}
// ---- dispatch and uniform buffer updates
@@ -1323,6 +1767,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) {
VkPipeline p = vk_pipeline_compute(cs, &info);
if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped");
vk_exec_barrier(ex);
VK_MEM_USE(ip);
bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p);
c0_dim[0] = c0_dim[1] = c0_dim[2] = 0;
B.c0_valid[2] = 0; // a fresh c0 range: the copy below must not change one an earlier dispatch reads
@@ -1336,6 +1781,8 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) {
}
vkCmdDispatchIndirect(ex->cb, ip.buffer, ip.offset);
ex->recorded++;
vk_mem_count(ip.cls, VK_MUSE_IND, sizeof(VkDispatchIndirectCommand));
VK_MEM_BOUND(ip);
if (vk_dump_armed) vk_dump_dispatch(ex, cs, p, info, &(p_dispatch){0, 0, 0, d->caller});
}
@@ -1348,9 +1795,12 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) {
VkBuffer b;
VkDeviceSize o;
memcpy(scratch(ex, u->size, &b, &o), u->data, u->size);
VK_MEM_PICK(sp);
VkBufferCopy cp = {o, sp.offset, u->size};
vkCmdCopyBuffer(ex->cb, b, sp.buffer, 1, &cp);
vk_mem_gpu_wrote(&sp, u->size);
ex->recorded++;
vk_mem_count(sp.cls, VK_MUSE_GPU_WRITE, u->size);
note_written(ex, u->dst, u->size);
}
@@ -1369,6 +1819,7 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) {
case OP_BIND_PROGRAM: {
const p_bind_program *b = payload;
ex->prog = b->prog, ex->prog_stages = b->stages;
bind_gen++;
break;
}
case OP_BIND_STATE: {
@@ -1411,17 +1862,20 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) {
case OP_BIND_UBO: {
const p_bind_buf *b = payload;
if (b->stage >= 0 && b->stage < 6 && b->index >= 0 && b->index < 18) ex->ubo[b->stage][b->index] = *b;
bind_gen++;
break;
}
case OP_BIND_SSBO: {
const p_bind_buf *b = payload;
if (b->stage >= 0 && b->stage < 6 && b->index >= 0 && b->index < 16) ex->ssbo[b->stage][b->index] = *b;
bind_gen++;
break;
}
case OP_BIND_TEX: {
const p_bind_handles *b = payload;
bind_handles(b->stage >= 0 && b->stage < 6 ? ex->tex[b->stage] : NULL, 128, b);
ex->tex_serial++;
bind_gen++;
break;
}
case OP_BIND_SAMP: {
@@ -1432,6 +1886,7 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) {
case OP_BIND_IMG: {
const p_bind_handles *b = payload;
bind_handles(b->stage >= 0 && b->stage < 6 ? ex->img[b->stage] : NULL, 32, b);
bind_gen++;
break;
}
case OP_UPDATE_UB: op_update_ub(ex, payload); break;
+55 -1
View File
@@ -802,6 +802,54 @@ static int test_nvnx(const GfxBackend *b, uint8_t *guest, int *bytes) {
return 0;
}
// VRAM mirror (vk_memory.c; skipped in import mode): a pool given its mirror as on a first buffer use, after which
// buffer copies and fills read and write the mirror. The first use copies the whole pool; CPU writes between
// submissions reach the mirror through write watch; GPU writes reach guest memory by the fence, and only the bytes
// written: CPU bytes beside a GPU fill keep their values.
static int test_mirror(const GfxBackend *b, uint8_t *guest, int *bytes) {
*bytes = 0;
if (!vk_mirror_on) return 0;
pool_t pool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = guest + 0xC00000, .size = 0x100000,
.game_mem = guest + 0xC00000};
CHECK(b->pool_init(&pool), "pool_init of a CPU-visible pool for the mirror part");
uint8_t *m = pool.mem;
for (int i = 0; i < 4096; i++) m[i] = (uint8_t)(i * 13 + 1);
memset(m + 0x1000, 0xEE, 0x5000);
CHECK(vk_mem_mirror_probe((uint64_t)(uintptr_t)m), "the pool got no VRAM mirror");
static _Alignas(8) uint8_t stream[1024];
uint64_t a = (uint64_t)(uintptr_t)m;
// 1: B2B in the mirror after its first-use copy, and a fill; both written back.
uint8_t *p = put(stream, OP_BEGIN, NULL, 0);
const p_copy_b2b c1 = {a, a + 0x1000, 4096};
const p_clear_buf f1 = {a + 0x2000, 0x800, 0x5a5a5a5au, 0};
p = put(p, OP_COPY_B2B, &c1, sizeof c1);
p = put(p, OP_CLEAR_BUF, &f1, sizeof f1);
CHECK(xf_submit(b, stream, p), "fence_wait of the first mirror submission timed out");
for (int i = 0; i < 4096; i++) CHECK(m[0x1000 + i] == m[i], "mirror B2B byte %d is %02x, expected %02x", i, m[0x1000 + i], m[i]);
for (int i = 0; i < 0x800; i++) CHECK(m[0x2000 + i] == 0x5a, "mirror fill byte %d is %02x", i, m[0x2000 + i]);
for (int i = 0x800; i < 0x1000; i++) CHECK(m[0x2000 + i] == 0xEE, "byte %#x past the mirror fill is %02x", i, m[0x2000 + i]);
// 2: the CPU rewrites the source; the next submission copies the new bytes.
for (int i = 0; i < 4096; i++) m[i] = (uint8_t)(i * 7 + 5);
p = put(stream, OP_BEGIN, NULL, 0);
const p_copy_b2b c2 = {a, a + 0x3000, 4096};
p = put(p, OP_COPY_B2B, &c2, sizeof c2);
CHECK(xf_submit(b, stream, p), "fence_wait of the second mirror submission timed out");
for (int i = 0; i < 4096; i++)
CHECK(m[0x3000 + i] == m[i], "B2B byte %d after a CPU write is %02x, expected %02x (write not mirrored)", i,
m[0x3000 + i], m[i]);
// 3: CPU bytes in the page of a GPU fill, written before the submission, survive the write-back.
memset(m + 0x4000, 0x11, 0x800);
p = put(stream, OP_BEGIN, NULL, 0);
const p_clear_buf f3 = {a + 0x4800, 0x800, 0x77777777u, 0};
p = put(p, OP_CLEAR_BUF, &f3, sizeof f3);
CHECK(xf_submit(b, stream, p), "fence_wait of the third mirror submission timed out");
for (int i = 0; i < 0x800; i++) CHECK(m[0x4000 + i] == 0x11, "CPU byte %#x beside a fill is %02x", i, m[0x4000 + i]);
for (int i = 0; i < 0x800; i++) CHECK(m[0x4800 + i] == 0x77, "fill byte %#x is %02x", i, m[0x4800 + i]);
b->pool_fini(&pool);
*bytes = 4096 + 0x1000 + 4096 + 0x1000;
return 0;
}
int probe_vkexec(void) {
layouts_equal = 0;
main_tid = GetCurrentThreadId();
@@ -819,8 +867,9 @@ int probe_vkexec(void) {
if (!bad) bad = test_xfer(b, guest, &xfer_bytes);
int zero_bytes = 0;
if (!bad) bad = test_c0() || test_zero(b, guest, &zero_bytes);
int nvnx_bytes = 0;
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);
b->finish();
vk_pipeline_shutdown(); // the pipelines and the draw part's stand-ins go before the device
vk_exec_draw_shutdown();
@@ -860,5 +909,10 @@ int probe_vkexec(void) {
printf("probe-vkexec: c0 and zero parts passed (c0 tables, dispatch size, point size and base instance at their offsets, "
"unbound slots zero; %d bytes of an unwritten physical-pool texture read zero, TOTK_FIX_ZERO=%s)\n",
zero_bytes, cfg_get("TOTK_FIX_ZERO") ? cfg_get("TOTK_FIX_ZERO") : "1");
if (vk_mirror_on)
printf("probe-vkexec: mirror part passed (B2B and fill in a VRAM mirror written back by the fence, a CPU write "
"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");
return 0;
}
+21 -8
View File
@@ -275,6 +275,7 @@ static void report_counter(VkExec *ex, const p_report_counter *p) {
VkMemSpan m;
if (!vk_mem_resolve(p->addr, sizeof(NVNcounterData), &m))
rt_fatal("vk: counter report at %#llx is in no memory pool", (unsigned long long)p->addr);
vk_mem_count(m.cls, VK_MUSE_GPU_WRITE, sizeof(NVNcounterData));
if (samples) oq_report(ex, p->addr);
else {
uint64_t *a = malloc(sizeof *a);
@@ -297,6 +298,7 @@ static int counter_word(uint64_t addr, uint64_t *v) {
VkMemSpan m;
if (nvn_gpu_only(addr) || !vk_mem_resolve(addr, sizeof *v, &m)) return 0;
memcpy(v, (const void *)(uintptr_t)addr, sizeof *v);
vk_mem_count(m.cls, VK_MUSE_CPU_READ, sizeof *v);
return 1;
}
@@ -377,9 +379,10 @@ static VkExtent3D sub_extent(const Sub *s) {
return (VkExtent3D){s->w, s->h, s->img->type == VK_IMAGE_TYPE_3D ? s->nz : 1};
}
// Buffer side of a texture <-> buffer copy: bytes covered, pitches, Vulkan buffer span.
// Buffer side of a texture <-> buffer copy: bytes covered, pitches, Vulkan buffer span (a VRAM mirror when the pool
// has one).
static int buffer_side(const VkExec *ex, const Sub *s, uint64_t addr, uint64_t *size, VkBufferImageCopy *c,
VkBuffer *buf) {
VkMemSpan *span) {
uint32_t wb = (s->w + (uint32_t)s->bw - 1) / (uint32_t)s->bw, hb = (s->h + (uint32_t)s->bh - 1) / (uint32_t)s->bh;
uint64_t row = ex->copy_stride > 0 ? (uint64_t)ex->copy_stride : (uint64_t)wb * (uint64_t)s->bpb;
if (row % (uint64_t)s->bpb || row < (uint64_t)wb * (uint64_t)s->bpb) {
@@ -395,7 +398,8 @@ static int buffer_side(const VkExec *ex, const Sub *s, uint64_t addr, uint64_t *
(unsigned long long)addr, (unsigned long long)*size);
return 0;
}
*buf = m.buffer;
VK_MEM_PICK(m);
*span = m;
*c = (VkBufferImageCopy){.bufferOffset = m.offset, .bufferRowLength = (uint32_t)(row / (uint64_t)s->bpb) * (uint32_t)s->bw,
.bufferImageHeight = hb * (uint32_t)s->bh, .imageSubresource = sub_layers(s),
.imageOffset = sub_offset(s), .imageExtent = sub_extent(s)};
@@ -407,15 +411,16 @@ static void copy_buffer_image(VkExec *ex, const p_copy_b2t *p, int to_image) {
if (!sub_resolve(p->tex, &p->view, p->has_view, &p->r, 1, &s)) return;
uint64_t size;
VkBufferImageCopy c;
VkBuffer buf;
if (!buffer_side(ex, &s, p->src, &size, &c, &buf)) return;
VkMemSpan m;
if (!buffer_side(ex, &s, p->src, &size, &c, &m)) return;
vk_mem_count(m.cls, to_image ? VK_MUSE_COPY_SRC : VK_MUSE_GPU_WRITE, size);
vk_exec_pass_end(ex);
vk_exec_barrier(ex);
if (to_image) vkCmdCopyBufferToImage(ex->cb, buf, s.img->image, VK_IMAGE_LAYOUT_GENERAL, 1, &c);
else vkCmdCopyImageToBuffer(ex->cb, s.img->image, VK_IMAGE_LAYOUT_GENERAL, buf, 1, &c);
if (to_image) vkCmdCopyBufferToImage(ex->cb, m.buffer, s.img->image, VK_IMAGE_LAYOUT_GENERAL, 1, &c);
else vkCmdCopyImageToBuffer(ex->cb, s.img->image, VK_IMAGE_LAYOUT_GENERAL, m.buffer, 1, &c);
ex->recorded++;
if (to_image) vk_texture_gpu_written(p->tex);
else note_gpu_write(ex, p->src, size);
else vk_mem_gpu_wrote(&m, size), note_gpu_write(ex, p->src, size);
}
static void copy_t2t(VkExec *ex, const p_copy_t2t *p) {
@@ -471,6 +476,8 @@ static void copy_b2b(VkExec *ex, const p_copy_b2b *p) {
}
uint64_t n = left < s.avail ? left : s.avail;
if (n > d.avail) n = d.avail;
VK_MEM_PICK(s);
VK_MEM_PICK(d);
if (s.buffer == d.buffer && s.offset < d.offset + n && d.offset < s.offset + n) {
warn_once(W_OVERLAP, "buffer copy %#llx -> %#llx overlaps within one buffer: skipped",
(unsigned long long)p->src, (unsigned long long)p->dst);
@@ -479,7 +486,10 @@ static void copy_b2b(VkExec *ex, const p_copy_b2b *p) {
if (!started) vk_exec_pass_end(ex), vk_exec_barrier(ex), started = 1;
VkBufferCopy bc = {s.offset, d.offset, n};
vkCmdCopyBuffer(ex->cb, s.buffer, d.buffer, 1, &bc);
vk_mem_gpu_wrote(&d, n);
ex->recorded++;
vk_mem_count(s.cls, VK_MUSE_COPY_SRC, n);
vk_mem_count(d.cls, VK_MUSE_GPU_WRITE, n);
src += n, dst += n, left -= n;
}
if (started) note_gpu_write(ex, p->dst, p->size - left);
@@ -507,8 +517,11 @@ static void clear_buf(VkExec *ex, const p_clear_buf *p) {
uint64_t n = left < d.avail ? left : d.avail & ~3ull;
if (!n) break;
if (!started) vk_exec_pass_end(ex), vk_exec_barrier(ex), started = 1;
VK_MEM_PICK(d);
vkCmdFillBuffer(ex->cb, d.buffer, d.offset, n, p->value);
vk_mem_gpu_wrote(&d, n);
ex->recorded++;
vk_mem_count(d.cls, VK_MUSE_GPU_WRITE, n);
dst += n, left -= n;
}
if (started) note_gpu_write(ex, p->addr, p->size - left);
+9 -4
View File
@@ -1,8 +1,8 @@
// NVN format -> VkFormat for the textures of the M4 graphics layer.
// Ported from the format table of LAS Recomp runtime/nvn/vulkan/textures.c @ da35b83a (MIT, Copyright (c) 2026 Peiikai).
// Differences from LAS: the table is indexed by the NVNformat values of nvn.h and covers all of them, ASTC is always
// decoded on the CPU (tex_astc.c) into RGBA8, D24S8 is always D32S8 (no probing: the RX 9070 lacks it), 3-component
// formats expand to 4 components, and formats with no Vulkan equivalent are reported with a reason.
// Differences from LAS: the table is indexed by the NVNformat values of nvn.h and covers all of them, ASTC decodes to
// RGBA8 unless the device samples it (vk_astc.c, TOTK_ASTC), D24S8 is always D32S8 (no probing: the RX 9070 lacks
// it), 3-component formats expand to 4 components, and formats with no Vulkan equivalent are reported with a reason.
#include <string.h>
#include "tex.h"
@@ -99,11 +99,16 @@ int tex_vk_format(int nvn_format, TexVkFormat *o) {
unsigned i = (f - 0x79) % 14;
int srgb = f >= 0x87;
o->ideal = srgb ? astc[i].srgb : astc[i].unorm;
if (vk_astc_native(o->ideal)) { // TOTK_ASTC auto / native on a device that samples ASTC: raw blocks, like BC
o->vk = o->ideal;
o->note = "native ASTC";
return 1;
}
o->vk = srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
o->astc_bw = astc[i].bw;
o->astc_bh = astc[i].bh;
o->astc_srgb = (uint8_t)srgb;
o->note = "the RX 9070 has no ASTC: decoded on the CPU to RGBA8";
o->note = "no native ASTC: decoded to RGBA8 (vk_astc.c compute decoder, or tex_astc.c with TOTK_ASTC=cpu)";
return 1;
}
if ((f >= 0x51 && f <= 0x68) || f == 0x6B || f == 0x6C) {
+1073 -50
View File
File diff suppressed because it is too large. Load diff
+310 -58
View File
@@ -88,6 +88,11 @@ static struct {
VkDynamicState dyn[32];
uint32_t n_dyn;
PFN_vkCmdPushDescriptorSetKHR push;
int tpl; // descriptor update templates on (make_tpl)
PFN_vkCreateDescriptorUpdateTemplate tpl_create;
PFN_vkDestroyDescriptorUpdateTemplate tpl_destroy;
PFN_vkUpdateDescriptorSetWithTemplate tpl_update;
PFN_vkCmdPushDescriptorSetWithTemplateKHR tpl_push;
PFN_vkCmdSetVertexInputEXT set_vi;
PFN_vkCmdSetDepthClampEnableEXT set_clamp;
PFN_vkCmdSetPolygonModeEXT set_poly;
@@ -110,6 +115,7 @@ typedef struct Layout {
int push1; // set 1 is a push-descriptor set
uint32_t n0; // independent layouts: b[0..n0) are set 0, the rest set 1
VkPipelineLayout half[2]; // GPL libraries: {set 0, null}, {null, set 1} (made on first use)
VkDescriptorUpdateTemplate tpl[2]; // per set (make_tpl); VK_NULL_HANDLE: written with write structs
VkDescriptorSetLayoutBinding b[];
} Layout;
@@ -242,7 +248,29 @@ static int ensure_init(void) {
P.f_dual_src = f2.features.dualSrcBlend;
#define PROC(field, name) (P.field = (void *)vkGetDeviceProcAddr(c->device, name))
PROC(push, "vkCmdPushDescriptorSetKHR");
P.dyn_vi = c->has_vertex_input_dynamic_state && PROC(set_vi, "vkCmdSetVertexInputEXT");
// Descriptor update templates (core 1.1; the push variant comes with VK_KHR_push_descriptor on 1.1): the driver
// walks a layout prepared when the layout is made instead of parsing write structs every draw (DXVK does so for the
// same reason). TOTK_DESC_TEMPLATE=0 keeps the write structs.
const char *dt = cfg_get("TOTK_DESC_TEMPLATE");
if (dt && !strcmp(dt, "0")) {
rt_log("vk_pipeline: descriptor update templates off (TOTK_DESC_TEMPLATE=0)");
} else if (c->props.apiVersion >= VK_API_VERSION_1_1 && PROC(tpl_create, "vkCreateDescriptorUpdateTemplate") &&
PROC(tpl_destroy, "vkDestroyDescriptorUpdateTemplate") && PROC(tpl_update, "vkUpdateDescriptorSetWithTemplate")) {
P.tpl = 1;
if (!PROC(tpl_push, "vkCmdPushDescriptorSetWithTemplateKHR"))
rt_log("vk_pipeline: vkCmdPushDescriptorSetWithTemplateKHR missing: push sets are written with write structs");
} else {
rt_log("vk_pipeline: descriptor update templates unavailable: sets are written with write structs");
}
// TOTK_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on Intel's frozen Windows driver for its
// older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which draws nothing with it (LAS Recomp
// 61fe44ff, its issues #8 and #357).
const char *dv = cfg_get("TOTK_VK_DYN_VERTEX");
int intel_legacy = c->props.vendorID == 0x8086 && c->props.apiVersion <= VK_MAKE_API_VERSION(0, 1, 3, 215);
int dv_on = dv && *dv ? strcmp(dv, "0") != 0 : !intel_legacy;
P.dyn_vi = dv_on && c->has_vertex_input_dynamic_state && PROC(set_vi, "vkCmdSetVertexInputEXT");
if (!dv_on && c->has_vertex_input_dynamic_state)
rt_log("vk_pipeline: dynamic vertex input off (%s)", dv && *dv ? "TOTK_VK_DYN_VERTEX=0" : "Intel's older Windows driver draws nothing with it");
if (c->has_eds3) {
P.dyn_clamp = e3.extendedDynamicState3DepthClampEnable && P.f_depth_clamp && PROC(set_clamp, "vkCmdSetDepthClampEnableEXT");
P.dyn_poly = e3.extendedDynamicState3PolygonMode && PROC(set_poly, "vkCmdSetPolygonModeEXT");
@@ -533,9 +561,12 @@ typedef struct {
VkVertexInputBindingDescription2EXT b[17];
} VtxInput;
// An attribute the game left without a format is a fixed one on Maxwell, a single 32-bit component of 0 (deko3d
// gpu_3d_vbo.cpp binds unused attributes as IsFixed 1x32 float): it reads (0, 0, 0, 1), as Vulkan expands a
// one-component format. TOTK's foliage shaders scale wind sway by 1 - colour.w; shapes without a vertex colour
// (Brightbloom stem and leaves) must not sway, or they bend away from the parts that have one (the bulb).
static VkFormat zero_format(int type) {
return type == GFX_TYPE_UINT ? VK_FORMAT_R32G32B32A32_UINT : type == GFX_TYPE_SINT ? VK_FORMAT_R32G32B32A32_SINT
: VK_FORMAT_R32G32B32A32_SFLOAT;
return type == GFX_TYPE_UINT ? VK_FORMAT_R32_UINT : type == GFX_TYPE_SINT ? VK_FORMAT_R32_SINT : VK_FORMAT_R32_SFLOAT;
}
// Attributes for the Locations the vertex shader reads; one without a usable NVN attribute reads VK_PIPE_ZERO_BINDING.
@@ -577,6 +608,8 @@ static void vertex_input(const VkPipeState *s, VtxInput *v) {
int vk_pipeline_vertex_input_dynamic(void) { return ensure_init() && P.dyn_vi; }
PFN_vkCmdPushDescriptorSetKHR vk_pipeline_push_fn(void) { return ensure_init() ? P.push : NULL; }
PFN_vkCmdPushDescriptorSetWithTemplateKHR vk_pipeline_push_tpl_fn(void) { return ensure_init() && P.tpl ? P.tpl_push : NULL; }
PFN_vkUpdateDescriptorSetWithTemplate vk_pipeline_update_tpl_fn(void) { return ensure_init() && P.tpl ? P.tpl_update : NULL; }
int vk_pipeline_set_indep(int on) {
if (!ensure_init()) return 0;
@@ -943,6 +976,37 @@ static int layout_covers(const VkPipeState *s, const Layout *l, uint32_t fs_off,
return m < 0;
}
// The update template of a set whose bindings are b[0..n): one entry per binding, in order, over the packed array the
// replay fills from the set's first descriptor, a VkDescSlot per descriptor. The bind point follows the stages (compute
// layouts hold the compute stage only). VK_NULL_HANDLE when templates are off, for an empty set, for a push set
// without vkCmdPushDescriptorSetWithTemplateKHR or when creation fails: the replay then writes the set with write
// structs.
static VkDescriptorUpdateTemplate make_tpl(const VkDescriptorSetLayoutBinding *b, uint32_t n, VkDescriptorSetLayout sl,
int push, VkPipelineLayout pl, uint32_t set) {
if (!P.tpl || !n || (push && !P.tpl_push)) return VK_NULL_HANDLE;
VkDescriptorUpdateTemplateEntry e[MAX_BINDINGS];
size_t off = 0;
VkShaderStageFlags stages = 0;
for (uint32_t i = 0; i < n; i++) {
e[i] = (VkDescriptorUpdateTemplateEntry){b[i].binding, 0, b[i].descriptorCount, b[i].descriptorType, off, sizeof(VkDescSlot)};
off += sizeof(VkDescSlot) * b[i].descriptorCount;
stages |= b[i].stageFlags;
}
VkDescriptorUpdateTemplateCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO, .descriptorUpdateEntryCount = n,
.pDescriptorUpdateEntries = e,
.templateType = push ? VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR : VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET,
.descriptorSetLayout = sl,
.pipelineBindPoint = stages & VK_SHADER_STAGE_COMPUTE_BIT ? VK_PIPELINE_BIND_POINT_COMPUTE : VK_PIPELINE_BIND_POINT_GRAPHICS,
.pipelineLayout = pl, .set = set};
VkDescriptorUpdateTemplate t;
if (P.tpl_create(P.device, &ci, NULL, &t) != VK_SUCCESS) {
LOG_ONCE("vk_pipeline: descriptor update template creation failed: that set is written with write structs");
return VK_NULL_HANDLE;
}
return t;
}
// Finds or creates the layout of a binding list (sorted here); NULL on failure. Caller holds P.lock.
// Sorts a binding list by binding and hashes it into *h; the descriptor total, or -1 when a binding repeats.
static int64_t bindings_sort(Bindings *B, uint64_t *h) {
@@ -987,6 +1051,7 @@ static Layout *layout_get(Bindings *B) {
free(l);
return NULL;
}
l->tpl[0] = make_tpl(l->b, l->n, l->set, l->push, l->layout, 0);
l->next = *head;
*head = l;
P.stats.layouts++;
@@ -1044,6 +1109,8 @@ static Layout *layout_pair(Bindings *B, Bindings *F) {
free(l);
return NULL;
}
l->tpl[0] = make_tpl(l->b, n0, l->set, l->push, l->layout, 0);
l->tpl[1] = make_tpl(l->b + n0, n - n0, l->set1, l->push1, l->layout, 1);
l->next = *head;
*head = l;
P.stats.layouts++;
@@ -1116,6 +1183,7 @@ static Pipe *pipe_add(const void *key, size_t size, uint64_t h, VkPipeline pipel
p->info.set_layout1 = l ? l->set1 : VK_NULL_HANDLE;
p->info.push1 = l ? l->push1 : 0;
p->info.n_sets = l && l->set1 ? 2 : 1;
if (l) p->info.tpl[0] = l->tpl[0], p->info.tpl[1] = l->tpl[1];
p->lay = l;
Pipe **head = &pipes[h % PIPE_BUCKETS];
p->next = *head;
@@ -1388,10 +1456,15 @@ out:
// thread of the draw that needs it (no link-time optimisation); the optimised pipeline is the job pipe_miss queued
// (a monolithic build with the same modules), which replaces the fast one in the Pipe when done. Libraries and fast
// pipelines live until vk_pipeline_shutdown.
// Background build signalling, both with P.lock: work_cv wakes the pipeline workers, done_cv wakes threads waiting for a
// pipeline or a stage library to be published.
static CONDITION_VARIABLE work_cv = CONDITION_VARIABLE_INIT, done_cv = CONDITION_VARIABLE_INIT;
#define LIB_BUCKETS 1024
enum { LIB_VI, LIB_PR, LIB_FS, LIB_FO };
typedef struct Lib { struct Lib *next; uint64_t hash; VkPipeline pipe; uint32_t size; uint8_t pre, used, pad[2]; uint8_t key[]; } Lib;
// pre: built by the boot prebuild ("stage library prebuild" below); used: a draw's link found it
typedef struct Lib { struct Lib *next; uint64_t hash; VkPipeline pipe; uint32_t size; uint8_t pre, used, building, pad;
struct LibJob *job; uint8_t key[]; } Lib;
// pre: built by the boot prebuild ("stage library prebuild" below); used: a draw's link found it; building: a thread
// builds it or job holds it queued (pipe not set yet); entries live until libs_destroy
static Lib *libs[LIB_BUCKETS];
typedef struct { uint32_t kind, topo_class, attr_fmt[32], attr_off[32]; uint8_t attr_bind[32], rate[17], pad[3]; } ViKey;
@@ -1535,6 +1608,10 @@ static Lib *lib_find(uint64_t h, const void *key, uint32_t size) {
// Returns the entered handle. Caller holds P.lock.
static VkPipeline lib_put(uint64_t h, const void *key, uint32_t size, VkPipeline pipe) {
Lib *b = lib_find(h, key, size);
if (b && b->building) { // another thread's build publishes the entry
if (pipe) vkDestroyPipeline(P.device, pipe, NULL);
return VK_NULL_HANDLE;
}
if (b) {
if (pipe) vkDestroyPipeline(P.device, pipe, NULL);
return b->pipe;
@@ -1547,10 +1624,72 @@ static VkPipeline lib_put(uint64_t h, const void *key, uint32_t size, VkPipeline
return pipe;
}
// Shader libraries a waiting draw needs (lib_wait): queued here ahead of every pipeline job, so an idle worker builds
// each at once and the vertex and fragment libraries of one draw build in parallel. A job no worker took is built by
// the first thread that needs its library (lib_get, lib_wait). Under P.lock.
typedef struct LibJob { struct LibJob *next; Lib *b; int kind; Layout *l; VkPipeState s; GKey k; } LibJob;
static struct {
LibJob *head, *tail;
int idle; // workers asleep on work_cv
int running; // jobs workers are building
int on; // TOTK_PIPE_WAIT: 1 workers (default), 0 the drawing thread builds them (draw);
// set by vk_pipeline_async_enable, before the replay runs
uint64_t waits, queued, by_worker, stolen, timeouts;
} U;
// A building entry for key, NULL when out of memory. Caller holds P.lock and found none.
static Lib *lib_new(uint64_t h, const void *key, uint32_t size) {
Lib *b = calloc(1, sizeof *b + size);
if (!b) return NULL;
b->hash = h, b->size = size, b->building = 1;
memcpy(b->key, key, size);
b->next = libs[h % LIB_BUCKETS];
libs[h % LIB_BUCKETS] = b;
return b;
}
// Builds b (building, not queued) with P.lock released and publishes it; a failed build is entered as VK_NULL_HANDLE.
static VkPipeline lib_build(Lib *b, int kind, const VkPipeState *s, const GKey *k, Layout *l) {
VkPipelineLayout lay = kind == LIB_PR ? half_layout(l, 0) : kind == LIB_FS ? half_layout(l, 1) : VK_NULL_HANDLE;
VkPipeline pipe = VK_NULL_HANDLE;
if (lay || kind == LIB_VI || kind == LIB_FO) {
ReleaseSRWLockExclusive(&P.lock);
uint64_t t0 = qpc();
pipe = lib_create(kind, s, k, lay, b->key);
double ms = ms_since(t0);
AcquireSRWLockExclusive(&P.lock);
P.g.lib_n[kind]++, P.g.lib_ms[kind] += ms;
if (ms > P.g.lib_max[kind]) P.g.lib_max[kind] = ms;
}
if (!pipe && ++P.g.lib_fail <= 8) rt_log("vk_pipeline: gpl: stage library %d not built", kind);
b->pipe = pipe, b->building = 0;
WakeAllConditionVariable(&done_cv);
return pipe;
}
// Takes b's queued job out of the queue and builds it on this thread. Caller holds P.lock.
static void lib_steal(Lib *b) {
LibJob *j = b->job, **pp = &U.head, *prev = NULL;
while (*pp && *pp != j) prev = *pp, pp = &(*pp)->next;
if (*pp) *pp = j->next;
if (U.tail == j) U.tail = prev;
b->job = NULL;
lib_build(b, j->kind, &j->s, &j->k, j->l);
free(j);
}
static VkPipeline lib_get(int kind, const VkPipeState *s, const GKey *k, Layout *l, const void *key, uint32_t size,
int build, int *miss) {
uint64_t h = key_hash(key, size);
Lib *have = lib_find(h, key, size);
while (have && have->building) { // being built (wait) or queued (build it here)
if (!build) {
*miss = 1;
return VK_NULL_HANDLE;
}
if (have->job) U.stolen++, lib_steal(have);
else SleepConditionVariableSRW(&done_cv, &P.lock, INFINITE, 0);
}
if (have) {
if (have->pre && !have->used) have->used = 1, P.g.pre_used[kind]++;
return have->pipe;
@@ -1560,12 +1699,14 @@ static VkPipeline lib_get(int kind, const VkPipeState *s, const GKey *k, Layout
*miss = 1;
return VK_NULL_HANDLE;
}
Lib *b = lib_new(h, key, size); // entered first, so other threads wait for this build instead of repeating it
if (b) return lib_build(b, kind, s, k, l);
ReleaseSRWLockExclusive(&P.lock);
uint64_t t0 = qpc();
VkPipeline pipe = lib_create(kind, s, k, lay, key);
double ms = ms_since(t0);
AcquireSRWLockExclusive(&P.lock);
if ((have = lib_find(h, key, size))) { // another thread built it meanwhile
if ((have = lib_find(h, key, size)) && !have->building) { // another thread built it meanwhile
if (pipe) vkDestroyPipeline(P.device, pipe, NULL);
return have->pipe;
}
@@ -1586,6 +1727,26 @@ static void libs_destroy(void) {
}
}
// The four library keys of pipeline s (key k).
static void lib_keys(const VkPipeState *s, const GKey *k, ViKey *v, PrKey *pk, FsKey *fk, FoKey *ok) {
memset(v, 0, sizeof *v);
v->kind = LIB_VI, v->topo_class = k->topo_class;
memcpy(v->attr_fmt, k->attr_fmt, sizeof v->attr_fmt), memcpy(v->attr_off, k->attr_off, sizeof v->attr_off);
memcpy(v->attr_bind, k->attr_bind, sizeof v->attr_bind), memcpy(v->rate, k->rate, sizeof v->rate);
memset(pk, 0, sizeof *pk);
GfxShaderIo fin[32];
pk->kind = LIB_PR, pk->vs = s->vs, pk->clamp = k->clamp, pk->poly_mode = k->poly_mode, pk->minus_w = k->minus_w;
pk->missing = vs_union_missing(s, fin);
for (int i = 0; i < 32; i++)
if (pk->missing >> i & 1) pk->fin[i] = fin[i];
memset(fk, 0, sizeof *fk);
fk->kind = LIB_FS, fk->fs = s->fs, fk->dsb = k->dsb, fk->samples = k->samples, fk->a2c = k->a2c;
memset(ok, 0, sizeof *ok);
ok->kind = LIB_FO, ok->n_color = k->n_color, ok->samples = k->samples, ok->a2c = k->a2c, ok->logic_op = k->logic_op;
ok->blend_en = k->blend_en, ok->depth = k->depth, ok->stencil = k->stencil, ok->wmask = k->wmask;
memcpy(ok->color, k->color, sizeof ok->color), memcpy(ok->blend, k->blend, sizeof ok->blend);
}
// A fast-linked pipeline for s from its stage libraries. Caller holds P.lock.
// build 0 (drawing thread): links only when the shader libraries exist (vertex input and fragment output libraries,
// which hold no shader, are built here); *final 0 = not yet, a worker builds them. build 1 (worker): builds the
@@ -1597,25 +1758,10 @@ static VkPipeline gpl_link(const VkPipeState *s, const GKey *k, const Layout *lc
if (!l->set1 || k->fbl) return VK_NULL_HANDLE;
uint64_t t0 = qpc();
ViKey vk_;
memset(&vk_, 0, sizeof vk_);
vk_.kind = LIB_VI, vk_.topo_class = k->topo_class;
memcpy(vk_.attr_fmt, k->attr_fmt, sizeof vk_.attr_fmt), memcpy(vk_.attr_off, k->attr_off, sizeof vk_.attr_off);
memcpy(vk_.attr_bind, k->attr_bind, sizeof vk_.attr_bind), memcpy(vk_.rate, k->rate, sizeof vk_.rate);
PrKey pk;
memset(&pk, 0, sizeof pk);
GfxShaderIo fin[32];
pk.kind = LIB_PR, pk.vs = s->vs, pk.clamp = k->clamp, pk.poly_mode = k->poly_mode, pk.minus_w = k->minus_w;
pk.missing = vs_union_missing(s, fin);
for (int i = 0; i < 32; i++)
if (pk.missing >> i & 1) pk.fin[i] = fin[i];
FsKey fk;
memset(&fk, 0, sizeof fk);
fk.kind = LIB_FS, fk.fs = s->fs, fk.dsb = k->dsb, fk.samples = k->samples, fk.a2c = k->a2c;
FoKey ok;
memset(&ok, 0, sizeof ok);
ok.kind = LIB_FO, ok.n_color = k->n_color, ok.samples = k->samples, ok.a2c = k->a2c, ok.logic_op = k->logic_op;
ok.blend_en = k->blend_en, ok.depth = k->depth, ok.stencil = k->stencil, ok.wmask = k->wmask;
memcpy(ok.color, k->color, sizeof ok.color), memcpy(ok.blend, k->blend, sizeof ok.blend);
lib_keys(s, k, &vk_, &pk, &fk, &ok);
int miss = 0;
VkPipeline lib[4];
lib[1] = lib_get(LIB_PR, s, k, l, &pk, sizeof pk, build, &miss); // shader libraries first: no work when absent
@@ -1682,8 +1828,9 @@ static struct {
Job *head, *tail; // draws' misses
Job *low, *low_tail; // prebuild
uint32_t inflight; // queued or running, both queues
CONDITION_VARIABLE work, idle, done; // all with P.lock; done: a job finished
} A = {.work = CONDITION_VARIABLE_INIT, .idle = CONDITION_VARIABLE_INIT, .done = CONDITION_VARIABLE_INIT};
int low_running; // pool workers running a job taken from the low queue
CONDITION_VARIABLE idle; // with P.lock: no job queued or running
} A = {.idle = CONDITION_VARIABLE_INIT};
// ---- pipeline keys (build/v121/pipeline-keys.bin next to the pipeline cache)
// Every graphics pipeline built in a game run appends a KeyRec: the VkPipeState it was built from with the shader
@@ -1852,14 +1999,33 @@ static DWORD WINAPI pipe_worker(void *arg) {
int me = (int)(intptr_t)arg;
AcquireSRWLockExclusive(&P.lock);
for (;;) {
while (!A.head && !A.low) SleepConditionVariableSRW(&A.work, &P.lock, INFINITE, 0);
// With 3 or more workers one never takes low-queue work, so a waiting draw's library starts at once
// (not with TOTK_PIPE_WAIT=draw).
while (!U.head && !A.head && !(A.low && (A.threads < 3 || !U.on || A.low_running < A.threads - 1))) {
U.idle++;
SleepConditionVariableSRW(&work_cv, &P.lock, INFINITE, 0);
U.idle--;
}
if (U.head) { // a library a draw waits for
LibJob *lj = U.head;
if (!(U.head = lj->next)) U.tail = NULL;
lj->b->job = NULL;
if (A.prio_low[me]) A.prio_low[me] = 0, SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
U.by_worker++, U.running++;
lib_build(lj->b, lj->kind, &lj->s, &lj->k, lj->l); // releases P.lock while building
U.running--;
free(lj);
continue;
}
Job *j = A.head;
int lowrun = !j;
if (j) {
if (!(A.head = j->next)) A.tail = NULL;
} else {
j = A.low;
if ((A.low = j->next)) A.low->prev = NULL;
else A.low_tail = NULL;
A.low_running++;
}
j->next = j->prev = NULL;
j->worker = me;
@@ -1875,12 +2041,13 @@ static DWORD WINAPI pipe_worker(void *arg) {
j->p->fast_tried = 1;
if (f && !j->p->info.pipeline) {
j->p->fast = j->p->info.pipeline = f, P.g.wlinks++;
WakeAllConditionVariable(&A.done);
WakeAllConditionVariable(&done_cv);
j->low = 1, j->worker = -1, P.g.requeued++;
j->prev = NULL, j->next = A.low;
if (A.low) A.low->prev = j;
else A.low_tail = j;
A.low = j;
A.low_running -= lowrun;
continue;
} else if (f) {
vkDestroyPipeline(P.device, f, NULL);
@@ -1906,8 +2073,9 @@ static DWORD WINAPI pipe_worker(void *arg) {
K.promoted += j->demanded;
if (!--K.left && !K.loading) prebuild_report();
}
A.low_running -= lowrun;
if (!--A.inflight) WakeAllConditionVariable(&A.idle);
WakeAllConditionVariable(&A.done);
WakeAllConditionVariable(&done_cv);
if (pipe && (!j->prebuild || j->demanded)) perf_count(PERF_CNT_PIPE_ASYNC); // a draw waited for it
free(j);
}
@@ -1939,11 +2107,20 @@ static void workers_drain(void) {
free(j);
}
A.low = A.low_tail = NULL;
for (LibJob *j = U.head, *n; j; j = n) { // queued library jobs too: their entries read as failed builds
n = j->next;
j->b->job = NULL, j->b->building = 0;
free(j);
}
U.head = U.tail = NULL;
while (A.inflight) SleepConditionVariableSRW(&A.idle, &P.lock, INFINITE, 0);
while (U.running) SleepConditionVariableSRW(&done_cv, &P.lock, INFINITE, 0);
ReleaseSRWLockExclusive(&P.lock);
}
void vk_pipeline_async_enable(void) {
const char *w = cfg_get("TOTK_PIPE_WAIT");
U.on = !(w && !strcmp(w, "draw"));
const char *e = cfg_get("TOTK_PIPE_SYNC");
if (e && *e && strcmp(e, "0")) return;
A.on = 1;
@@ -1966,7 +2143,7 @@ static void promote(Pipe *p) {
if (A.tail) A.tail->next = j;
else A.head = j;
A.tail = j;
WakeConditionVariable(&A.work);
WakeConditionVariable(&work_cv);
} else if (A.prio_low[j->worker]) {
A.prio_low[j->worker] = 0;
SetThreadPriority(A.thread[j->worker], THREAD_PRIORITY_NORMAL);
@@ -2025,11 +2202,80 @@ static Pipe *pipe_miss(const VkPipeState *s, const GKey *k, uint64_t h, int mode
A.tail = j;
}
A.inflight++;
WakeConditionVariable(&A.work);
WakeConditionVariable(&work_cv);
*queued = 1;
return p;
}
// A job still in the draw queue goes to the head of the low queue: its pipeline has (or is about to have) a fast-linked
// one, so the optimised build waits behind other draws' misses. Caller holds P.lock.
static void job_lower(Job *j) {
if (!j || j->worker >= 0 || j->low) return;
if (A.head == j) {
if (!(A.head = j->next)) A.tail = NULL;
} else {
Job *q = A.head;
while (q && q->next != j) q = q->next;
if (!q) return;
if (!(q->next = j->next)) A.tail = q;
}
j->low = 1, P.g.requeued++;
j->prev = NULL, j->next = A.low;
if (A.low) A.low->prev = j;
else A.low_tail = j;
A.low = j;
}
// A draw waits for pipeline s: its missing pre-rasterization and fragment shader libraries go to the head of the
// worker queues (U), so they build in parallel on idle workers, and this thread sleeps until both are published or
// end (now_ms) passes. A job no worker took within 1 ms (all busy) is built here. Caller holds P.lock.
static void lib_wait(const VkPipeState *s, const GKey *k, Layout *l, double end) {
ViKey vk_;
PrKey pk;
FsKey fk;
FoKey ok;
lib_keys(s, k, &vk_, &pk, &fk, &ok);
U.waits++;
const void *key[2] = {&pk, &fk};
uint32_t size[2] = {sizeof pk, sizeof fk};
Lib *b[2];
int queued = 0;
for (int i = 0; i < 2; i++) {
uint64_t h = key_hash(key[i], size[i]);
if ((b[i] = lib_find(h, key[i], size[i]))) continue;
if (!half_layout(l, i)) return; // gpl_link reports it
LibJob *j = calloc(1, sizeof *j);
if (!j || !(b[i] = lib_new(h, key[i], size[i]))) {
free(j);
return;
}
*j = (LibJob){.b = b[i], .kind = i ? LIB_FS : LIB_PR, .l = l, .s = *s, .k = *k};
b[i]->job = j;
if (U.tail) U.tail->next = j;
else U.head = j;
U.tail = j;
U.queued++, queued++;
}
if (queued) WakeAllConditionVariable(&work_cv);
double steal_at = now_ms() + 1.0;
for (;;) {
if (!b[0]->building && !b[1]->building) return;
double now = now_ms();
if (now >= end) {
U.timeouts++;
return;
}
Lib *q = b[0]->job ? b[0] : b[1]->job ? b[1] : NULL;
if (q && (now >= steal_at || !U.idle)) {
U.stolen++;
lib_steal(q); // releases P.lock while building
continue;
}
double until = q && steal_at < end ? steal_at : end;
SleepConditionVariableSRW(&done_cv, &P.lock, (DWORD)(until - now) + 1, 0);
}
}
static VkPipeline graphics(const VkPipeState *s, const VkPipeInfo **info, int async, int *pending, uint32_t wait_ms) {
if (info) *info = NULL;
if (pending) *pending = 0;
@@ -2045,46 +2291,45 @@ static VkPipeline graphics(const VkPipeState *s, const VkPipeInfo **info, int as
} else {
p = pipe_miss(s, &k, h, async, &queued);
}
// A draw that would wait (wait_ms) builds the missing shader libraries itself and links (about 0.12 ms per library
// from the driver's disk cache): the workers may all be in optimised builds of earlier pipelines, and its job would
// queue behind them. Its job then only does the optimised build, from the low queue. Other draws leave the
// libraries to the job and are skipped meanwhile. TOTK_GPL_DRAW_BUILD=0 leaves them to the job too.
// A draw that would wait (wait_ms) gets its missing shader libraries built at once, the vertex and fragment ones in
// parallel on idle workers (lib_wait; about 0.12 ms per library from the driver's disk cache, 5-180 ms cold), and
// links them here: its own job may queue behind optimised builds of earlier pipelines. The job then only does the
// optimised build, from the low queue. Other draws leave the libraries to the job and are skipped meanwhile.
// TOTK_PIPE_WAIT=draw builds them on this thread one after the other instead, as before; TOTK_GPL_DRAW_BUILD=0
// leaves them to the job.
static int draw_build = -1;
if (draw_build < 0) {
const char *e = cfg_get("TOTK_GPL_DRAW_BUILD");
draw_build = !(e && !strcmp(e, "0"));
rt_log("vk_pipeline: a draw that waits for its pipeline %s", !draw_build ? "leaves its libraries to its job"
: U.on ? "has its libraries built in parallel on the workers, ahead of other jobs"
: "builds its libraries itself (TOTK_PIPE_WAIT=draw)");
}
double end = wait_ms ? now_ms() + wait_ms : 0;
if (p && p->building && P.gpl && !p->info.pipeline && !p->fast_tried && p->lay) {
int final, build = wait_ms != 0 && draw_build;
if (build) p->fast_tried = 1; // the job, if a worker takes it meanwhile, goes straight to the optimised build
int final = 0, build = wait_ms != 0 && draw_build;
uint64_t p0 = perf_begin(), t0 = qpc();
VkPipeline f = gpl_link(s, &k, p->lay, build, &final); // build 1 releases P.lock while building
if (build && U.on) {
job_lower(p->job); // its worker would only wait for the same libraries; it links them if this draw cannot
lib_wait(s, &k, (Layout *)p->lay, end); // releases P.lock while waiting
build = 0; // links here when both libraries exist (gpl_link build 0)
} else if (build) {
p->fast_tried = 1; // the job, if a worker takes it meanwhile, goes straight to the optimised build
}
VkPipeline f = p->info.pipeline ? VK_NULL_HANDLE : gpl_link(s, &k, p->lay, build, &final); // build 1 releases P.lock
perf_end(PERF_PIPE, p0);
if (build) {
if (wait_ms && draw_build) {
double ms = ms_since(t0);
P.g.dlinks++, P.g.dlink_ms += ms;
if (ms > P.g.dlink_max) P.g.dlink_max = ms;
if (ms > 5.0 && P.g.slow++ < 16) rt_log("vk_pipeline: gpl: a waiting draw built its libraries in %.1f ms", ms);
if (ms > 5.0 && P.g.slow++ < 16)
rt_log("vk_pipeline: gpl: a waiting draw %s its libraries in %.1f ms", U.on ? "got" : "built", ms);
}
if (final) p->fast_tried = 1;
if (f && !p->info.pipeline) {
p->fast = p->info.pipeline = f;
Job *j = p->job;
if (j && j->worker < 0 && !j->low) { // still in the draw queue: to the head of the low queue
if (A.head == j) {
if (!(A.head = j->next)) A.tail = NULL;
} else {
Job *q = A.head;
while (q && q->next != j) q = q->next;
if (q && !(q->next = j->next)) A.tail = q;
}
j->low = 1, P.g.requeued++;
j->prev = NULL, j->next = A.low;
if (A.low) A.low->prev = j;
else A.low_tail = j;
A.low = j;
}
WakeAllConditionVariable(&A.done);
job_lower(p->job);
WakeAllConditionVariable(&done_cv);
} else if (f) {
vkDestroyPipeline(P.device, f, NULL); // a worker published one while the lock was free
}
@@ -2092,9 +2337,9 @@ static VkPipeline graphics(const VkPipeState *s, const VkPipeInfo **info, int as
if (p && p->building && !p->info.pipeline) {
promote(p);
uint64_t p0 = wait_ms ? perf_begin() : 0; // the wait counts as the render thread's pipeline time
double end = wait_ms ? now_ms() + wait_ms : 0, now;
double now;
while (p->building && !p->info.pipeline && wait_ms && (now = now_ms()) < end)
SleepConditionVariableSRW(&A.done, &P.lock, (DWORD)(end - now) + 1, 0);
SleepConditionVariableSRW(&done_cv, &P.lock, (DWORD)(end - now) + 1, 0);
if (wait_ms) perf_end(PERF_PIPE, p0);
if (wait_ms && !p->building) perf_count(PERF_CNT_PIPE_WAIT);
}
@@ -2150,7 +2395,7 @@ static DWORD WINAPI warm_builder(void *arg) {
K.promoted += j->demanded;
K.left--;
if (!--A.inflight) WakeAllConditionVariable(&A.idle);
WakeAllConditionVariable(&A.done);
WakeAllConditionVariable(&done_cv);
free(j);
}
ReleaseSRWLockExclusive(&P.lock);
@@ -2249,7 +2494,7 @@ void vk_pipeline_prewarm(void) {
}
for (int i = 0; i < nt; i++) CloseHandle(t[i]);
AcquireSRWLockExclusive(&P.lock);
while (K.left) SleepConditionVariableSRW(&A.done, &P.lock, INFINITE, 0); // jobs a pool worker took
while (K.left) SleepConditionVariableSRW(&done_cv, &P.lock, INFINITE, 0); // jobs a pool worker took
rt_log("vk_pipeline: prewarm: %zu records in %s: %u pipelines built, %u failed, %u already built, %u dropped "
"(shader neither cached nor stored); shaders %.0f ms, pipelines %.0f ms on %d threads, total %.0f ms",
n, K.path, K.built, K.failed, known, dropped, t1 - t0, now_ms() - t1, nt, now_ms() - t0);
@@ -2754,6 +2999,11 @@ void vk_pipeline_save(void) {
P.g.link_max, (unsigned long long)P.g.link_fail, (unsigned long long)P.g.defer,
(unsigned long long)P.g.swaps, (unsigned long long)P.g.requeued, (unsigned long long)P.g.slow,
(unsigned long long)P.g.dlinks, P.g.dlinks ? P.g.dlink_ms / P.g.dlinks : 0.0, P.g.dlink_max);
if (U.waits || U.queued)
rt_log("vk_pipeline: gpl: library jobs for waiting draws: %llu queued, %llu built by a worker, %llu by the "
"thread that needed them (no worker free), %llu waits ended at their limit",
(unsigned long long)U.queued, (unsigned long long)U.by_worker, (unsigned long long)U.stolen,
(unsigned long long)U.timeouts);
static const char *const kn[4] = {"vertex input", "pre-rasterization", "fragment shader", "fragment output"};
for (int i = 0; i < 4; i++)
rt_log("vk_pipeline: gpl: %s libraries %llu (avg %.2f ms, max %.2f ms)", kn[i], (unsigned long long)P.g.lib_n[i],
@@ -2812,6 +3062,8 @@ void vk_pipeline_shutdown(void) {
for (int i = 0; i < LAYOUT_BUCKETS; i++) {
for (Layout *l = layouts[i], *n; l; l = n) {
n = l->next;
for (int k = 0; k < 2; k++)
if (l->tpl[k]) P.tpl_destroy(P.device, l->tpl[k], NULL);
vkDestroyPipelineLayout(P.device, l->layout, NULL);
for (int h = 0; h < 2; h++)
if (l->half[h]) vkDestroyPipelineLayout(P.device, l->half[h], NULL);
+179 -14
View File
@@ -42,10 +42,117 @@ static SRWLOCK st_lock = SRWLOCK_INIT;
static CONDITION_VARIABLE cv_work = CONDITION_VARIABLE_INIT; // a submission, a callback or stop
static CONDITION_VARIABLE cv_done = CONDITION_VARIABLE_INIT; // done advanced
static uint64_t issued, done; // newest submitted value; newest value whose side effects ran
static uint64_t reserved; // asynchronous replay: newest value handed out before its submission (>= issued)
static int stop;
static Pending *queue; // side effects in the order they were added
static int qhead, qn, qcap;
// ---- GPU time (TOTK_TRACE=perf): each submission part (vk_sync_split) writes a timestamp when it starts and when it
// ends, into a ring of query pairs. The completion thread reads a pair once the part's value is reached and adds the
// part's interval to the busy time, counting overlap with the previous part once. Off (one log line) when the queue
// family has no timestamp bits or the period is 0. Upload batches (vk_memory.c) are not timed.
#define TS_PAIRS 1024
static VkQueryPool ts_pool;
static int ts_state; // 0 not set up, 1 on, -1 off
static double ts_period; // ns per tick
static uint64_t ts_mask; // valid bits of a timestamp
static uint32_t ts_next; // submitting thread (sub_lock): next pair
static volatile uint8_t ts_busy[TS_PAIRS]; // written until the completion thread read the pair
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 LONG64 ts_busy_ns, ts_parts, ts_missed;
static void ts_setup(VkCtx *c) {
ts_state = -1;
if (!(rt_trace & RT_TRACE_PERF)) return;
uint32_t nq = 0;
vkGetPhysicalDeviceQueueFamilyProperties(c->phys, &nq, NULL);
VkQueueFamilyProperties *qp = calloc(nq ? nq : 1, sizeof *qp);
if (!qp) return;
vkGetPhysicalDeviceQueueFamilyProperties(c->phys, &nq, qp);
uint32_t bits = c->gfx_family < nq ? qp[c->gfx_family].timestampValidBits : 0;
free(qp);
float period = c->props.limits.timestampPeriod;
if (!bits || period <= 0) {
rt_log("vk: no GPU timestamps on this queue (valid bits %u, period %.3f ns): the perf line shows no GPU time", bits, period);
return;
}
VkQueryPoolCreateInfo qi = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, .queryType = VK_QUERY_TYPE_TIMESTAMP,
.queryCount = 2 * TS_PAIRS};
VkResult r = vkCreateQueryPool(c->device, &qi, NULL, &ts_pool);
if (r != VK_SUCCESS) {
rt_log("vk: timestamp query pool failed (%d): the perf line shows no GPU time", (int)r);
return;
}
ts_period = period;
ts_mask = bits >= 64 ? ~0ull : (1ull << bits) - 1;
ts_state = 1;
}
typedef struct { uint32_t pair; } TsRead;
static void ts_read_cb(void *arg) { // completion thread, in submission order
TsRead *t = arg;
VkCtx *c = vk_ctx();
uint64_t v[2];
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) {
uint64_t from = s > ts_last_end ? s : ts_last_end;
if (e > from) InterlockedAdd64(&ts_busy_ns, (LONG64)((double)(e - from) * ts_period));
if (e > ts_last_end) ts_last_end = e;
InterlockedIncrement64(&ts_parts);
} else InterlockedIncrement64(&ts_missed); // the counter wrapped
} else InterlockedIncrement64(&ts_missed);
__atomic_store_n(&ts_busy[t->pair], 0, __ATOMIC_RELEASE);
free(t);
}
// At the start of a command buffer (sub_lock held): the pair it times, -1 untimed.
static int ts_open(VkCommandBuffer cb) {
if (ts_state != 1) return -1;
uint32_t p = ts_next % TS_PAIRS;
if (__atomic_load_n(&ts_busy[p], __ATOMIC_ACQUIRE)) { InterlockedIncrement64(&ts_missed); return -1; }
ts_next++;
ts_busy[p] = 1;
vkCmdResetQueryPool(cb, ts_pool, 2 * p, 2);
vkCmdWriteTimestamp(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, ts_pool, 2 * p);
return (int)p;
}
// At its end, before vkEndCommandBuffer; fence: the value whose completion covers it.
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);
TsRead *t = malloc(sizeof *t);
if (!t) rt_fatal("vk: out of memory");
t->pair = (uint32_t)pair;
vk_completion_add(fence, ts_read_cb, t);
}
// The pair of the part being recorded.
static void ts_begin(VkCommandBuffer cb) { ts_cur = ts_open(cb); }
static void ts_end(VkCommandBuffer cb, uint64_t fence) {
ts_close(cb, ts_cur, fence);
ts_cur = -1;
}
// An empty submission records no command buffer: its pair is never written.
static void ts_drop(void) {
if (ts_cur < 0) return;
__atomic_store_n(&ts_busy[ts_cur], 0, __ATOMIC_RELEASE);
ts_cur = -1;
}
static void gpu_stats(PerfGpu *g) {
g->have_gpu = ts_state == 1;
g->gpu_ns = (uint64_t)InterlockedExchange64(&ts_busy_ns, 0);
g->gpu_parts = (uint64_t)InterlockedExchange64(&ts_parts, 0);
g->gpu_missed = (uint64_t)InterlockedExchange64(&ts_missed, 0);
g->have_vram = vk_mem_vram(&g->vram_used, &g->vram_budget, &g->vram_exact);
}
// Without the replay (T6c) only empty recordings ({OP_BEGIN, OP_END}) can be submitted.
WEAK int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t fence) {
(void)cb, (void)fence;
@@ -105,6 +212,8 @@ void vk_sync_start(void) {
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = c->gfx_family};
if (vkCreateCommandPool(c->device, &pi, NULL, &cmd_pool) != VK_SUCCESS) rt_fatal("vk: submission command pool");
if (!ts_state) ts_setup(c);
perf_gpu_stats = gpu_stats;
uint64_t now = 0;
vkGetSemaphoreCounterValue(c->device, c->timeline, &now);
if (issued > now) { // a new device after vk_shutdown: values keep increasing for the front end
@@ -115,46 +224,72 @@ void vk_sync_start(void) {
}
AcquireSRWLockExclusive(&st_lock);
issued = done = now;
if (reserved < now) reserved = now;
stop = 0;
ReleaseSRWLockExclusive(&st_lock);
vk_mem_note_submit(now);
completion_thread = CreateThread(NULL, 0, completion_main, c, 0, NULL);
if (!completion_thread) rt_fatal("vk: cannot start the completion thread (error %lu)", GetLastError());
__atomic_store_n(&started, 1, __ATOMIC_RELEASE);
vk_astc_mode(); // decides TOTK_ASTC and builds the ASTC decoder now, not at the first ASTC upload
}
ReleaseSRWLockExclusive(&start_lock);
}
// A command buffer the GPU no longer uses, claimed for value `fence`. Called with sub_lock held.
static CmdSlot *take_slot(VkCtx *c, uint64_t fence) {
// The index of a command buffer the GPU no longer uses, claimed for value `fence`. Called with sub_lock held; may move
// slots[].
static int claim_slot(VkCtx *c, uint64_t fence) {
uint64_t gpu = 0;
vkGetSemaphoreCounterValue(c->device, c->timeline, &gpu);
for (int i = 0; i < nslots; i++)
if (slots[i].fence <= gpu) {
slots[i].fence = fence;
cur_slot = i;
return &slots[i];
return i;
}
CmdSlot *n = realloc(slots, sizeof *n * (size_t)(nslots + 1));
if (!n) rt_fatal("vk: out of memory (command buffers)");
slots = n;
CmdSlot *s = &slots[nslots];
cur_slot = nslots;
VkCommandBufferAllocateInfo ai = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cmd_pool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1};
if (vkAllocateCommandBuffers(c->device, &ai, &s->cb) != VK_SUCCESS) rt_fatal("vk: command buffer allocation");
s->fence = fence;
nslots++;
return s;
return nslots++;
}
static CmdSlot *take_slot(VkCtx *c, uint64_t fence) {
cur_slot = claim_slot(c, fence);
return &slots[cur_slot];
}
// The copies the replay's first accesses to VRAM mirrors queued (vk_memory.c), in a command buffer of their own that is
// submitted right before the part that needs them and timed like a part; VK_NULL_HANDLE when there are none. After the
// part was recorded, sub_lock held; may move slots[].
static VkCommandBuffer prologue(VkCtx *c, uint64_t fence) {
if (!vk_mem_mirror_pending()) return VK_NULL_HANDLE;
int i = claim_slot(c, fence); // before reading slots: the claim may move it
VkCommandBuffer cb = slots[i].cb;
VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
vkBeginCommandBuffer(cb, &bi);
int pair = ts_open(cb);
vk_mem_mirror_prologue(cb);
ts_close(cb, pair, fence);
vkEndCommandBuffer(cb);
return cb;
}
VkCommandBuffer vk_sync_split(void) {
VkCtx *c = vk_ctx();
VkCommandBuffer cb = slots[cur_slot].cb;
ts_end(cb, cur_fence);
vkEndCommandBuffer(cb);
vk_upload_flush(0); // the uploads this part asked for precede it in queue order
VkCommandBufferSubmitInfo cbi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = cb};
VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = 1, .pCommandBufferInfos = &cbi};
VkCommandBuffer pro = prologue(c, cur_fence);
VkCommandBufferSubmitInfo cbi[2] = {{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = pro},
{.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};
vk_queue_lock();
VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE);
vk_queue_unlock();
@@ -164,13 +299,26 @@ VkCommandBuffer vk_sync_split(void) {
VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
vkBeginCommandBuffer(s->cb, &bi);
ts_begin(s->cb);
return s->cb;
}
// Asynchronous replay (vk_backend.c): the value the next queued submission or present will signal. Values are handed
// out in queue order and the replay thread submits them in that order, so each vk_sync_submit gets exactly its value.
uint64_t vk_sync_reserve(void) {
vk_sync_start();
AcquireSRWLockExclusive(&st_lock);
if (reserved < issued) reserved = issued;
uint64_t v = ++reserved;
ReleaseSRWLockExclusive(&st_lock);
return v;
}
uint64_t vk_sync_submit(const uint64_t *handles, int n) {
vk_sync_start();
VkCtx *c = vk_ctx();
AcquireSRWLockExclusive(&sub_lock);
vk_mem_dirty_submit(); // TOTK_TRACE=perf: what a mirror would upload before this submit
uint64_t fence = __atomic_load_n(&issued, __ATOMIC_ACQUIRE) + 1;
cur_fence = fence;
CmdSlot *s = take_slot(c, fence);
@@ -178,17 +326,25 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) {
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
uint64_t t0 = perf_begin();
vkBeginCommandBuffer(s->cb, &bi);
ts_begin(s->cb);
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);
else ts_drop();
vkEndCommandBuffer(s->cb);
perf_replay(t0);
// After the recording: the uploads it asked for (and every earlier one) precede it in queue order.
vk_upload_flush(0);
VkCommandBufferSubmitInfo cbi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = s->cb};
VkCommandBuffer pro = prologue(c, fence);
s = &slots[cur_slot];
VkCommandBufferSubmitInfo cbi[2] = {{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = pro},
{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = s->cb}};
VkSemaphoreSubmitInfo sig = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = c->timeline,
.value = fence, .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT};
VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = recorded ? 1 : 0,
.pCommandBufferInfos = &cbi, .signalSemaphoreInfoCount = 1, .pSignalSemaphoreInfos = &sig};
VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2,
.commandBufferInfoCount = (pro ? 1u : 0u) + (recorded ? 1u : 0u),
.pCommandBufferInfos = pro ? cbi : cbi + 1, .signalSemaphoreInfoCount = 1,
.pSignalSemaphoreInfos = &sig};
vk_queue_lock();
VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE);
vk_queue_unlock();
@@ -227,6 +383,7 @@ int vk_sync_fence_wait(uint64_t value, uint64_t timeout_ns) {
uint64_t p0 = perf_begin();
int ok = fence_wait_impl(value, timeout_ns);
perf_end(PERF_FENCE, p0);
perf_gpu_wait(p0);
return ok;
}
@@ -234,8 +391,8 @@ static int fence_wait_impl(uint64_t value, uint64_t timeout_ns) {
vk_sync_start();
AcquireSRWLockExclusive(&st_lock);
int ok;
if (value > issued) {
ok = 0; // never submitted: never reached (as in the null back end)
if (value > issued && value > reserved) {
ok = 0; // never submitted nor queued: never reached (as in the null back end)
} else {
uint64_t t0 = kern_now_ns();
while (done < value) {
@@ -256,6 +413,8 @@ static int fence_wait_impl(uint64_t value, uint64_t timeout_ns) {
uint64_t vk_sync_fence_done(void) { return __atomic_load_n(&done, __ATOMIC_ACQUIRE); }
uint64_t vk_sync_issued(void) { return __atomic_load_n(&issued, __ATOMIC_ACQUIRE); }
void vk_sync_finish(void) {
vk_sync_start();
vk_sync_fence_wait(__atomic_load_n(&issued, __ATOMIC_ACQUIRE), UINT64_MAX);
@@ -279,6 +438,12 @@ void vk_sync_shutdown(void) {
AcquireSRWLockExclusive(&sub_lock);
VkCtx *c = vk_ctx();
if (c && cmd_pool) vkDestroyCommandPool(c->device, cmd_pool, NULL);
perf_gpu_stats = NULL;
if (c && ts_pool) vkDestroyQueryPool(c->device, ts_pool, NULL);
ts_pool = VK_NULL_HANDLE;
ts_state = 0;
ts_last_end = 0;
memset((void *)ts_busy, 0, sizeof ts_busy);
cmd_pool = VK_NULL_HANDLE;
free(slots);
slots = NULL;
+228 -37
View File
@@ -1,5 +1,5 @@
// Vulkan textures of the M4 graphics layer: a VkImage cache keyed by storage
// GPU address plus descriptor, uploads through staging buffers with block-linear deswizzle and CPU ASTC decode,
// GPU address plus descriptor, uploads through staging buffers with block-linear deswizzle and ASTC decode (vk_astc.c),
// re-uploads when page write generations (vk_memory.c) show the storage written, ReadTexels / WriteTexels, the texture
// and sampler descriptor slots, and VkSamplers.
//
@@ -51,7 +51,11 @@ typedef struct Tex {
uint8_t gpu_dirty; // mirrored, and the GPU wrote the image after the last write-back to the storage
uint32_t gpu_gen; // page generation when the GPU write that set gpu_dirty was recorded
uint64_t gpu_seq; // order of that GPU write among all of them (gpu_writes)
uint64_t gpu_fence; // fence of the submission that made that write (0: not recorded in one)
uint64_t wr_fence; // any entry: newest submission recorded to write its image (render, copy, clear,
// storage binding); ReadTexels waits for it instead of draining the replay queue
uint8_t listed; // in owned[]
uint32_t wt_n; // WriteTexels into the storage since the last upload
} Tex;
// A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the
@@ -79,6 +83,8 @@ static int nsslots;
static nvn_hmap samplers = {.klen = sizeof(samp_par)};
static int nreleased;
static uint64_t n_uploads, n_reuploads;
// Re-uploads since the last perf line (tex_lock): all, and those of textures a WriteTexels wrote since their last upload.
static uint64_t ru_n, ru_bytes, ru_ns, ru_wt_n, ru_wt_bytes, ru_wt_ns;
// TOTK_TEX_ALIAS (default on; =0: a new image over live storage starts empty): a new descriptor over the storage of a
// live image that the GPU wrote gets that image's content (alias_copy). Counts since the last log line.
static int tex_alias = -1;
@@ -96,6 +102,7 @@ static uint64_t gpu_writes; // GPU writes into mirrored textures so far: Tex.gp
static uint32_t tex_gen = 1;
// Bumped with tex_gen and whenever a slot is registered: what vk_texture_by_id returns for any slot may have changed.
static uint32_t slots_gen = 1;
static uint32_t samp_gen = 1; // bumped whenever a sampler slot is registered: vk_sampler_by_id may answer otherwise
static uint64_t n_checks; // draw-binding storage checks (make_fresh_snap) since the last perf line
static void tex_gen_bump(void) {
@@ -465,45 +472,91 @@ static void shrink_rgb(int format, const uint8_t *src, uint8_t *dst, size_t n) {
for (size_t i = 0; i < n; i++, src += 4 * e, dst += 3 * e) memcpy(dst, src, 3 * e);
}
// TOTK_ASTC=check: the compute decoder's texels of n regions (gpu: the regions one after the other) against
// astc_decode_ldr of the same raw blocks (raw: the staging bytes the regions point into).
static void astc_check(const Tex *e, const uint8_t *raw, const VkAstcRegion *ar, int n, const uint8_t *gpu) {
static volatile long n_bad;
static uint64_t n_tex, n_texels, n_diff_tex;
uint32_t bw = e->f.astc_bw, bh = e->f.astc_bh;
uint64_t diff = 0, first = UINT64_MAX;
uint32_t want = 0, got = 0;
int first_k = 0;
for (int k = 0; k < n; k++) {
const VkAstcRegion *r = &ar[k];
size_t sb = (size_t)((r->w + bw - 1) / bw) * ((r->h + bh - 1) / bh) * 16, st = (size_t)r->w * r->h * 4;
uint8_t *cpu = malloc(st);
for (uint32_t z = 0; z < r->slices; z++, gpu += st) {
astc_decode_ldr(raw + r->src + z * sb, r->w, r->h, bw, bh, e->f.astc_srgb, cpu);
if (!memcmp(cpu, gpu, st)) continue;
for (size_t i = 0; i < st / 4; i++)
if (memcmp(cpu + 4 * i, gpu + 4 * i, 4)) {
if (first == UINT64_MAX) {
first = i, first_k = k;
memcpy(&want, cpu + 4 * i, 4), memcpy(&got, gpu + 4 * i, 4);
}
diff++;
}
}
free(cpu);
n_texels += (uint64_t)r->w * r->h * r->slices;
}
n_tex++;
if (diff) {
n_diff_tex++;
log_limited(&n_bad, "ASTC check: texture %#llx fmt %#x %ux%u: %llu texels differ, first region %d texel %llu "
"cpu %08x gpu %08x", (unsigned long long)e->pub.addr, e->t.d.format, bw, bh,
(unsigned long long)diff, first_k, (unsigned long long)first, want, got);
}
if (n_tex == 1 || !(n_tex & 63) || diff)
rt_log("vk: ASTC check: %llu textures, %llu texels compared, %llu textures differ", (unsigned long long)n_tex,
(unsigned long long)n_texels, (unsigned long long)n_diff_tex);
}
// Copies the whole storage into the image (every level and layer the image has).
static void upload(Tex *e) {
static volatile long n_bad;
static volatile long n_bad, n_undecoded;
const tex_t *t = &e->t;
const nvn_texdesc *d = &t->d;
int bw, bh, bpb = nvn_fmt_bpb(d->format, &bw, &bh);
if (!bpb || !t->mem) return;
uint64_t p0 = perf_begin();
uint64_t p0 = perf_begin(), u0 = kern_now_ns();
int linear = nvn_tex_is_linear(d), is3d = d->target == NVN_TEXTURE_TARGET_3D, one_d = e->pub.type == VK_IMAGE_TYPE_1D;
uint32_t levels = linear ? 1 : e->pub.levels, layers = linear ? 1 : e->pub.layers;
int astc = e->f.astc_bw != 0, rgb = e->f.expand_rgb;
int agpu = astc && vk_astc_gpu(), acheck = agpu && vk_astc_mode() == VK_ASTC_CHECK; // raw blocks to the GPU
uint32_t n = levels * layers, k = 0;
VkBufferImageCopy *rg = calloc(n, sizeof *rg);
VkDeviceSize total = 0;
VkAstcRegion *ar = agpu ? calloc(n, sizeof *ar) : NULL;
VkDeviceSize total = 0, decoded = 0;
for (uint32_t lv = 0; lv < levels; lv++)
for (uint32_t ly = 0; ly < layers; ly++) {
uint32_t w = mip(d->width, lv), h = one_d ? 1 : mip(d->height, lv), sl = is3d ? mip(d->depth, lv) : 1;
uint64_t texels = (uint64_t)w * h * sl, raw = (uint64_t)(w + bw - 1) / bw * bpb * ((h + bh - 1) / bh) * sl;
uint64_t out = astc ? texels * 4 : rgb ? texels * (uint64_t)bpb / 3 * 4 : raw;
uint64_t out = agpu ? raw : astc ? texels * 4 : rgb ? texels * (uint64_t)bpb / 3 * 4 : raw;
rg[k++] = (VkBufferImageCopy){.bufferOffset = total, .imageSubresource = {e->pub.aspect, lv, ly, 1},
.imageExtent = {w, h, sl}};
total += (out + 15) & ~15ull;
decoded += texels * 4;
}
VkCommandBuffer cb = vk_upload_begin();
VkBuffer sb;
uint8_t *stg = vk_staging(total, &sb, NULL);
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++) {
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;
uint8_t *dst = stg + rg[k].bufferOffset, *tmp = astc || rgb ? malloc(raw) : NULL;
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",
(unsigned long long)e->pub.addr, d->format, d->target, lv, ly);
memset(tmp ? tmp : dst, 0, raw);
}
if (astc) {
if (agpu) {
ar[k] = (VkAstcRegion){.src = rg[k].bufferOffset, .w = w, .h = h, .slices = sl, .level = lv, .layer = ly};
} else if (astc) {
uint64_t a0 = perf_begin();
for (uint32_t z = 0; z < sl; z++)
astc_decode_ldr_mt(tmp + (size_t)z * rb * rows, w, h, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb,
@@ -518,13 +571,49 @@ 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);
vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, n, rg);
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->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);
}
barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW);
vk_upload_end();
if (acheck) { // waits for the GPU
vk_upload_flush(1);
if (checked) {
vmaInvalidateAllocation(vk_ctx()->vma, ralloc, 0, VK_WHOLE_SIZE);
astc_check(e, raw_copy, ar, (int)n, rmap);
}
vmaDestroyBuffer(vk_ctx()->vma, rbuf, ralloc);
}
free(raw_copy);
free(ar);
free(rg);
if (e->uploaded) n_reuploads++, perf_count(PERF_CNT_TEX_REUP);
else n_uploads++, perf_count(PERF_CNT_TEX_UP);
uint64_t dt = kern_now_ns() - u0;
if (e->uploaded) {
n_reuploads++, perf_count(PERF_CNT_TEX_REUP);
ru_n++, ru_bytes += t->L.size, ru_ns += dt;
if (e->wt_n) {
ru_wt_n++, ru_wt_bytes += t->L.size, ru_wt_ns += dt;
if (rt_trace & RT_TRACE_PERF)
rt_log("perf: frame %llu: re-upload after %u WriteTexels: texture %#llx fmt %#x %ux%u, %.1f KiB, %.3f ms",
(unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), e->wt_n,
(unsigned long long)e->pub.addr, d->format, d->width, d->height, t->L.size / 1024.0, dt / 1e6);
}
} else {
n_uploads++, perf_count(PERF_CNT_TEX_UP);
}
e->wt_n = 0;
e->uploaded = 1;
e->n_up++;
perf_end(PERF_TEX, p0);
@@ -719,6 +808,11 @@ const VkTexImage *vk_texture_get(const tex_t *t) {
return e ? &e->pub : NULL;
}
// Small render targets the CPU reads without NVN's invalidate (vk_texture_write_back_small); small_pending: one may be
// dirty.
enum { WB_SMALL = 64 << 10 };
static int small_pending;
void vk_texture_gpu_written(const tex_t *t) {
if (!t || !t->mem) return;
vk_exec_frame_written(t);
@@ -727,10 +821,14 @@ void vk_texture_gpu_written(const tex_t *t) {
if (e) {
make_fresh(e); // a partial render keeps what the storage held before
e->pub.gpu_owned = 1;
uint64_t f = vk_exec_recording_fence();
if (f > e->wr_fence) e->wr_fence = f;
if (e->pub.mirrored) {
e->gpu_dirty = 1;
storage_rewritten(e, 0, &e->gpu_gen); // CPU writes up to here are older than this render
e->gpu_seq = ++gpu_writes;
e->gpu_fence = vk_exec_recording_fence();
if (e->t.L.size <= WB_SMALL) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE);
own_add(e);
}
}
@@ -832,6 +930,7 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) {
void vk_texture_lock(void) { lock(); }
void vk_texture_unlock(void) { unlock(); }
uint32_t vk_texture_slots_gen(void) { return slots_gen; }
uint32_t vk_sampler_gen(void) { return __atomic_load_n(&samp_gen, __ATOMIC_RELAXED); }
const VkTexImage *vk_texture_by_id(int id, view_t *view, int *has_view) {
lock();
@@ -880,6 +979,8 @@ int vk_texture_slot_swz(int id, uint8_t swz[4]) {
return ok;
}
uint32_t vk_texture_epoch(void) { return __atomic_load_n(&submit_epoch, __ATOMIC_RELAXED); }
void vk_texture_new_submit(void) {
lock();
if (!++submit_epoch) submit_epoch = 1;
@@ -891,6 +992,10 @@ void vk_texture_new_submit(void) {
(unsigned long long)f * 60, (unsigned long long)n_alias_copied, (unsigned long long)n_alias_upload,
(unsigned long long)n_alias_uninit);
n_alias_copied = n_alias_upload = n_alias_uninit = 0;
rt_log("perf: frame %llu: texture re-uploads %llu (%.1f MiB, %.2f ms), after WriteTexels %llu (%.1f MiB, %.2f ms)",
(unsigned long long)f * 60, (unsigned long long)ru_n, ru_bytes / 1048576.0, ru_ns / 1e6,
(unsigned long long)ru_wt_n, ru_wt_bytes / 1048576.0, ru_wt_ns / 1e6);
ru_n = ru_bytes = ru_ns = ru_wt_n = ru_wt_bytes = ru_wt_ns = 0;
uint64_t ck, calls, untr;
vk_mem_snap_stats(&ck, &calls, &untr);
rt_log("perf: frame %llu: binding storage checks %llu (texture %llu), write-watch snapshot queries %llu, untracked %llu",
@@ -922,29 +1027,18 @@ static int region_of(const tex_t *t, const view_t *v, const NVNcopyRegion *r, ui
return 1;
}
// Pool bytes <-> caller rows through a linear copy of the level (MNG nvn_mtl.m:10121-10132, 10158-10170).
// Pool bytes <-> caller rows of the region alone (nvn_texels_box). MNG nvn_mtl.m:10121-10132, 10158-10170 go through a
// linear copy of the whole level; for a small region of a large texture that cost tens of milliseconds while the guest
// thread held its virtual core and the caller the texture lock (monsters2 replay, frames 765-790).
static int storage_texels(const tex_t *t, const Region *g, const NVNcopyRegion *r, uint8_t *mem, int write) {
const nvn_texdesc *d = &t->d;
uint32_t lw = mip(d->width, g->level), lh = d->target == 0 || d->target == 3 ? 1 : mip(d->height, g->level);
uint32_t lz = g->is3d ? mip(d->depth, g->level) : 1;
size_t lp = (size_t)((lw + g->bw - 1) / g->bw) * (size_t)g->bpb, lr = (lh + g->bh - 1) / (uint32_t)g->bh;
if (g->x0 + g->wb > lp || g->y0 + g->hb > lr || (g->is3d && (uint32_t)(r->zoffset + g->nz) > lz)) return 0;
uint8_t *lin = malloc(lp * lr * lz);
if (!lin) rt_fatal("vk: out of memory for texels");
int ok = 1;
for (int z = 0; z < g->nz && ok; z++) {
for (int z = 0; z < g->nz; z++) {
unsigned layer = g->is3d ? 0 : g->layer0 + (unsigned)r->zoffset + (unsigned)z;
if ((!g->is3d || z == 0) && nvn_deswizzle(d, g->level, layer, t->mem, lin, lp)) { ok = 0; break; }
uint8_t *slice = lin + (g->is3d ? (size_t)(r->zoffset + z) * lp * lr : 0);
for (size_t y = 0; y < g->hb; y++) {
uint8_t *p = slice + (g->y0 + y) * lp + g->x0, *q = mem + (size_t)z * g->img + y * g->row;
if (write) memcpy(p, q, g->wb);
else memcpy(q, p, g->wb);
}
if (write && (!g->is3d || z == g->nz - 1) && nvn_swizzle(d, g->level, layer, lin, lp, t->mem)) ok = 0;
uint32_t slice = g->is3d ? (uint32_t)(r->zoffset + z) : 0;
if (nvn_texels_box(&t->d, g->level, layer, (uint32_t)g->x0, (uint32_t)g->y0, slice, (uint32_t)g->wb,
(uint32_t)g->hb, t->mem, mem + (size_t)z * g->img, g->row, !write))
return 0;
}
free(lin);
return ok;
return 1;
}
static int read_aspect(const Tex *e, VkImageAspectFlags *a) {
@@ -972,9 +1066,9 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t
}
if (g->level >= e->pub.levels) return 0;
uint32_t w = (uint32_t)r->width, h = (uint32_t)r->height;
int rgb = e->f.expand_rgb, astc = e->f.astc_bw != 0;
size_t texels = (size_t)w * h, ib = astc ? 4 : rgb ? (size_t)g->bpb / 3 * 4 : 0;
size_t slice = ib ? texels * ib : g->wb * g->hb;
int rgb = e->f.expand_rgb, astc = e->f.astc_bw != 0, agpu = astc && write && vk_astc_gpu();
size_t texels = (size_t)w * h, ib = agpu ? 0 : astc ? 4 : rgb ? (size_t)g->bpb / 3 * 4 : 0;
size_t slice = ib ? texels * ib : g->wb * g->hb; // agpu: the raw blocks, decoded by vk_astc_record
VkBuffer sb;
VmaAllocation sa;
uint8_t *stg = vk_staging(slice * (size_t)g->nz, &sb, &sa);
@@ -983,7 +1077,9 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t
uint8_t *s = stg + slice * (size_t)z;
const uint8_t *m = mem + (size_t)z * g->img;
if (write) {
if (astc) {
if (agpu) {
for (size_t y = 0; y < g->hb; y++) memcpy(s + y * g->wb, m + y * g->row, g->wb);
} else if (astc) {
uint8_t *tight = malloc(g->wb * g->hb);
for (size_t y = 0; y < g->hb; y++) memcpy(tight + y * g->wb, m + y * g->row, g->wb);
uint64_t a0 = perf_begin();
@@ -1005,7 +1101,21 @@ static int image_texels(Tex *e, const Region *g, const NVNcopyRegion *r, uint8_t
if (write) {
vmaFlushAllocation(c->vma, sa, 0, VK_WHOLE_SIZE);
barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT);
vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)g->nz, rg);
int done = 0;
if (agpu) {
VkAstcRegion *ar = calloc((size_t)g->nz, sizeof *ar);
for (int z = 0; z < g->nz; z++)
ar[z] = (VkAstcRegion){.src = slice * (size_t)z, .w = w, .h = h, .slices = 1, .level = rg[z].imageSubresource.mipLevel,
.layer = rg[z].imageSubresource.baseArrayLayer, .offset = rg[z].imageOffset};
uint64_t a0 = perf_begin();
done = vk_astc_record(cb, sb, slice * (size_t)g->nz, ar, g->nz, e->f.astc_bw, e->f.astc_bh, e->f.astc_srgb,
e->pub.image, aspect, VK_NULL_HANDLE);
perf_end(PERF_ASTC, a0);
free(ar);
}
if (!agpu) vkCmdCopyBufferToImage(cb, sb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, (uint32_t)g->nz, rg);
else if (!done) log_limited(&n_astc, "WriteTexels of ASTC texture %#llx not decoded on the GPU: left as it was",
(unsigned long long)e->pub.addr);
barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW);
} else {
barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT);
@@ -1090,6 +1200,7 @@ void vk_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegion *r, co
log_limited(&n_bad, "WriteTexels: region outside level %u of texture %#llx", g.level, (unsigned long long)e->pub.addr);
vk_guest_written((uintptr_t)t->mem, t->L.size);
if (e->pub.gpu_owned) image_texels(e, &g, r, (uint8_t *)src, 1);
else if (e->uploaded) e->wt_n++;
} else if (e) {
image_texels(e, &g, r, (uint8_t *)src, 1);
}
@@ -1133,6 +1244,28 @@ static int owned_dirty(uint64_t addr, uint64_t size, Tex **hit) {
return n;
}
// A storage image may be written by the shaders of the submission being recorded (texture lock held, vk_exec_draw.c
// tex_info). Its storage is not marked GPU-owned: only the write fence for ReadTexels.
void vk_texture_storage_bound_locked(const VkTexImage *img) {
Tex *e = (Tex *)img; // pub is the first member
uint64_t f = vk_exec_recording_fence();
if (f > e->wr_fence) e->wr_fence = f;
}
uint64_t vk_texture_last_write(const tex_t *t) {
lock();
Tex *e = entry_of(t);
uint64_t f = e ? e->wr_fence : 0;
unlock();
return f;
}
size_t vk_texture_texel_span(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint64_t row, uint64_t img) {
Region g;
if (!region_of(t, v, r, row, img, &g)) return 0;
return (size_t)(g.nz - 1) * g.img + (g.hb - 1) * g.row + g.wb;
}
int vk_texture_cpu_stale(uint64_t addr, uint64_t size) {
if (!size || !__atomic_load_n(&nowned, __ATOMIC_ACQUIRE)) return 0;
AcquireSRWLockShared(&owned_lock);
@@ -1141,6 +1274,21 @@ int vk_texture_cpu_stale(uint64_t addr, uint64_t size) {
return dirty;
}
// The newest submission recorded to write a GPU-written mirrored texture overlapping [addr, addr+size); 0: none.
uint64_t vk_texture_dirty_fence(uint64_t addr, uint64_t size) {
if (!size || !__atomic_load_n(&nowned, __ATOMIC_ACQUIRE)) return 0;
lock();
uint64_t f = 0;
for (int i = owned_lower(addr > max_owned ? addr - max_owned : 0); i < nowned && owned[i]->pub.addr < addr + size; i++) {
const Tex *e = owned[i];
if (!e->gpu_dirty || e->pub.addr + e->t.L.size <= addr) continue;
if (e->gpu_fence > f) f = e->gpu_fence;
if (e->wr_fence > f) f = e->wr_fence;
}
unlock();
return f;
}
// The image is the newest content only while nothing wrote the storage since the GPU render: TotK finalizes grass maps,
// reuses their storage for new data and memcpys that data on, and writing the old image back first handed the copy
// a stale map or stale resource bytes (wrong textures in the distance). Every hit is checked before any is written
@@ -1168,6 +1316,48 @@ void vk_texture_write_back(uint64_t addr, uint64_t size) {
unlock();
}
// TotK's grass system renders a 128x128 RGBA8 map of cut and trampled grass into a CPU-cached pool every frame and its
// CPU code reads the pool bytes without nvnMemoryPoolInvalidateMappedRange (cut reactions: blades, insects, the cut
// itself; build/v121-bloom NOTES.md). On the Switch the bytes are there once the frame's fence has passed. So when the
// CPU waits for a fence, mirrored targets up to WB_SMALL bytes rendered by work up to that fence go back to their
// storage, unless the CPU rewrote it since. Larger targets still go back on an invalidate, memcpy or ReadTexels.
int vk_texture_small_dirty(void) { return __atomic_load_n(&small_pending, __ATOMIC_ACQUIRE); }
void vk_texture_write_back_small(uint64_t fence) {
lock();
__atomic_store_n(&small_pending, 0, __ATOMIC_RELAXED);
AcquireSRWLockShared(&owned_lock);
int n = 0, later = 0;
for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && owned[i]->t.L.size <= WB_SMALL;
Tex **hit = n ? malloc(sizeof *hit * (size_t)n) : NULL;
if (n && !hit) rt_fatal("vk: out of memory for a texture write-back");
n = 0;
for (int i = 0; i < nowned; i++) {
Tex *e = owned[i];
if (!e->gpu_dirty || e->t.L.size > WB_SMALL) continue;
if (e->gpu_fence > fence) later = 1;
else hit[n++] = e;
}
ReleaseSRWLockShared(&owned_lock);
if (later) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE);
int k = 0;
for (int i = 0; i < n; i++) {
uint32_t now;
if (storage_rewritten(hit[i], hit[i]->gpu_gen, &now) == 1) hit[i]->gpu_dirty = 0;
else hit[k++] = hit[i];
}
for (int i = 1; i < k; i++)
for (int j = i; j > 0 && hit[j - 1]->gpu_seq > hit[j]->gpu_seq; j--) {
Tex *x = hit[j];
hit[j] = hit[j - 1], hit[j - 1] = x;
}
for (int i = 0; i < k; i++) write_back(hit[i]);
for (int i = 0; i < k; i++) perf_count(PERF_CNT_WB);
for (int i = k; i < n; i++) perf_count(PERF_CNT_WB_SKIP);
free(hit);
unlock();
}
// ---------------------------------------------------------------- samplers (LAS textures.c:778-844)
static VkCompareOp compare_op(uint32_t f) {
switch (f) {
@@ -1275,6 +1465,7 @@ void vk_sampler_register(int id, const samp_par *s) {
sslots[id].p = *s;
sslots[id].s = VK_NULL_HANDLE;
sslots[id].used = 1;
samp_gen++;
unlock();
}
+79
View File
@@ -0,0 +1,79 @@
"""End-to-end run of the installer in dev mode with the game files named in config.local.toml.
Reads input.base, the 1.2.1 entry of input.updates (the one build/extracted/v121 was extracted from, else the only
one) and input.keys, and passes them to `python -m installer` started the way the release launcher starts it: the
embeddable Python (build/py314-embed) with this checkout on sys.path. The paths are never printed. Events go to
<events> (JSON lines); stdout gets each step's state and time, warnings and errors.
python tests/installer_e2e.py --dest build/install-test --lang en-US [--events FILE] [--no-inputs] [-- extra args]
--no-inputs leaves the game files out (update mode on an existing folder).
"""
import argparse
import json
import subprocess
import sys
import tomllib
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
EMBED = ROOT / 'build' / 'py314-embed' / 'py' / 'python.exe'
BOOT = ('import runpy, sys; sys.path.insert(0, sys.argv.pop(1)); '
"runpy.run_module('installer', run_name='__main__', alter_sys=True)")
def game_inputs():
config = tomllib.loads((ROOT / 'config.local.toml').read_text(encoding='utf-8'))['input']
updates = [Path(u) for u in config.get('updates', [])]
manifest = ROOT / 'build' / 'extracted' / 'v121' / 'extraction-manifest.json'
update = None
if manifest.is_file():
source = json.loads(manifest.read_text())['source']
update = next((u for u in updates if str(u.resolve()) == source), None)
if update is None and len(updates) == 1:
update = updates[0]
if update is None:
sys.exit('installer_e2e: cannot tell which input.updates entry is 1.2.1')
return ['--base', config['base'], '--update', str(update), '--keys', config['keys']]
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--dest', type=Path, required=True)
ap.add_argument('--lang')
ap.add_argument('--events', type=Path)
ap.add_argument('--no-inputs', action='store_true')
ap.add_argument('extra', nargs='*')
a = ap.parse_args()
python = EMBED if EMBED.is_file() else Path(sys.executable)
command = [str(python), '-c', BOOT, str(ROOT), '--dest', str(a.dest.resolve())]
if a.lang:
command += ['--lang', a.lang]
if not a.no_inputs:
command += game_inputs()
command += a.extra
events = a.events or a.dest.resolve().parent / f'{a.dest.name}-events.jsonl'
with open(events, 'w', encoding='utf-8') as log:
proc = subprocess.Popen(command, stdout=subprocess.PIPE, text=True, encoding='utf-8', cwd=ROOT)
for line in proc.stdout:
log.write(line)
log.flush()
try:
e = json.loads(line)
except ValueError:
print('non-JSON line on stdout:', line.rstrip())
continue
if e['type'] == 'step' and e['state'] != 'started':
extra = f'{e["seconds"]:.1f} s' if 'seconds' in e else e.get('reason', '')
print(f'[{e["t"]:8.1f}] {e["step"]:8} {e["state"]} {extra}', flush=True)
elif e['type'] == 'step':
print(f'[{e["t"]:8.1f}] {e["step"]:8} started', flush=True)
elif e['type'] in ('warning', 'error', 'plan', 'end'):
print(f'[{e["t"]:8.1f}] {json.dumps({k: v for k, v in e.items() if k != "t"})}', flush=True)
elif e['type'] == 'start':
print(f'[{e["t"]:8.1f}] start mode={e["mode"]} release={e["release"]}', flush=True)
return proc.wait()
if __name__ == '__main__':
sys.exit(main())
+409
View File
@@ -0,0 +1,409 @@
"""The installer window: event model, form, catalogs, cancel, a self-closing Tk run and one end-to-end run.
.venv-py314\\Scripts\\python tests\\installer_gui_test.py
The end-to-end test runs the real CLI in --verify mode on a finished install (every step skips) and folds its events
through the same code the window uses (the fixture's step keys are recomputed for the checkout under test, because the keys fingerprint recomp/, tools/ and runtime/). The install is build/inst-gui/e2e-install: hard links to build/install-test, so
nothing there is written, except the state file and totk.ini, which a run rewrites and which are copied. The run hashes
the 17 GB RomFS; it is skipped when the hash differs from the expected one in all three tries. On the developer machine
(RAM with a bad bit) a buffered read of a file sometimes returns flipped bits from the page cache while the file on
disk is intact (an unbuffered read of install-test's romfs.bin gives the expected hash), so such a skip is the machine,
not the installer. It is also skipped when build/install-test is absent.
"""
import io
import json
import os
import queue
import re
import shutil
import subprocess
import sys
import tempfile
import time
import unittest
from pathlib import Path
from types import SimpleNamespace
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
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)
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']}
def fold(*events):
m = Model()
for e in events:
m.apply(e)
return m
class ParseTest(unittest.TestCase):
def test_event_line(self):
self.assertEqual(parse_line('{"type": "step", "step": "check", "state": "started"}\n')['step'], 'check')
def test_noise_is_not_an_event(self):
for line in ('', 'Traceback (most recent call last):', '{broken', '[1, 2]', '{"no_type": 1}', '{"type": 3}'):
self.assertIsNone(parse_line(line), line)
def test_unicode_survives(self):
e = parse_line(json.dumps({'type': 'warning', 'id': 'x', 'params': {'p': 'Привет'}}, ensure_ascii=False))
self.assertEqual(e['params']['p'], 'Привет')
class ModelTest(unittest.TestCase):
def test_steps_follow_the_events(self):
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])
self.assertEqual(m.current, 'check')
m.apply({'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.0})
self.assertEqual(m.states['check'], DONE)
self.assertIsNone(m.current)
m.apply({'type': 'step', 'step': 'extract', 'state': 'skipped', 'reason': 'current'})
self.assertEqual(m.states['extract'], SKIPPED)
def test_progress_fraction(self):
m = fold(START, {'type': 'step', 'step': 'extract', 'state': 'started'},
{'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 5, 'total': 20, 'unit': 'bytes'})
self.assertEqual((m.current, m.phase, m.fraction()), ('extract', 'copy', 0.25))
m.apply({'type': 'progress', 'step': 'extract', 'phase': 'merge'})
self.assertIsNone(m.fraction()) # a phase without size is indeterminate
m.apply({'type': 'progress', 'step': 'extract', 'phase': 'copy', 'done': 30, 'total': 20})
self.assertEqual(m.fraction(), 1.0)
def test_a_new_step_resets_progress(self):
m = fold(START, {'type': 'progress', 'step': 'check', 'phase': 'inputs', 'done': 1, 'total': 2},
{'type': 'step', 'step': 'extract', 'state': 'started'})
self.assertIsNone(m.phase)
self.assertIsNone(m.fraction())
def test_error_marks_the_step(self):
m = fold(START, {'type': 'step', 'step': 'check', 'state': 'started'},
{'type': 'error', 'id': 'check.disk_space', 'step': 'check', 'params': {'path': 'D:\\T'}},
{'type': 'end', 'result': 'failed', 'seconds': 2})
self.assertEqual(m.states['check'], FAILED)
self.assertEqual(m.error[0], 'check.disk_space')
self.assertEqual(m.result, 'failed')
def test_space_text(self):
m = fold({'type': 'space', 'dest': 'D:\\T', 'need': 36_400_000_000, 'free': 100_000_000_000, 'temp': 'C:\\t',
'temp_need': 0, 'temp_free': 5, 'same_disk': True})
text = m.need_text(Catalog('en'))
self.assertIn('36.4', text)
self.assertIn('100.0', text)
self.assertEqual(Model().need_text(Catalog('en')), '')
m = fold({'type': 'space', 'dest': 'D:\\T', 'need': 20_000_000_000, 'free': 1, 'temp': 'C:\\t',
'temp_need': 2_000_000_000, 'temp_free': 3, 'same_disk': False})
self.assertIn('C:\\t', m.need_text(Catalog('en')))
def test_warnings_collect(self):
m = fold(START, {'type': 'warning', 'id': 'warn.lang_needs_fonts', 'params': {'lang': 'ko'}})
self.assertEqual(m.warnings, [('warn.lang_needs_fonts', {'lang': 'ko'})])
def test_finish_without_end_event(self):
m = fold(START, {'type': 'step', 'step': 'lift', 'state': 'started'})
m.finish(-1073741510)
self.assertEqual(m.result, 'failed')
self.assertEqual(m.states['lift'], FAILED)
self.assertEqual(m.error[0], 'ui.process_died')
def test_finish_after_cancel(self):
m = fold(START, {'type': 'step', 'step': 'lift', 'state': 'started'}, {'type': 'end', 'result': 'cancelled'})
m.finish(3)
self.assertEqual(m.result, 'cancelled')
self.assertIsNone(m.error)
self.assertEqual(m.states['lift'], PENDING) # not finished; the next run resumes here
def test_exit_code_3_alone_is_cancelled(self):
m = Model()
m.finish(3)
self.assertEqual(m.result, 'cancelled')
def test_unknown_event_is_ignored(self):
m = fold(START, {'type': 'future_thing', 'x': 1})
self.assertEqual(m.states['check'], PENDING)
class RunStatesTest(unittest.TestCase):
OK = [START, {'type': 'plan', 'run': ['check', 'extract'], 'skip': {'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': '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}]
FAILED = [START, {'type': 'step', 'step': 'check', 'state': 'started'}, {'type': 'step', 'step': 'check', 'state': 'done'},
{'type': 'step', 'step': 'extract', 'state': 'started'},
{'type': 'error', 'id': 'extract.romfs_mismatch', 'step': 'extract', 'params': {}},
{'type': 'warning', 'id': 'warn.lang_needs_fonts', 'params': {'lang': 'ko'}},
{'type': 'end', 'result': 'failed', 'seconds': 1}]
def test_ok_run_has_only_done_and_skipped_steps(self):
m = fold(*self.OK)
m.finish(0)
self.assertEqual(m.result, 'ok')
self.assertIsNone(m.error)
self.assertEqual({m.states[s] for s in m.steps}, {DONE, SKIPPED})
def test_a_new_run_keeps_nothing_of_the_failed_one(self):
m = fold(*self.FAILED)
m.finish(1)
self.assertEqual(m.states['extract'], FAILED)
for e in self.OK: # the same model object fed a second run (the window makes a new one, but even then)
m.apply(e)
m.finish(0)
self.assertEqual({m.states[s] for s in m.steps}, {DONE, SKIPPED})
self.assertIsNone(m.error)
self.assertEqual(m.warnings, [])
self.assertEqual(m.result, 'ok')
class FormTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.dir = Path(self.tmp.name)
for name in ('base.nsp', 'upd.nsp', 'prod.keys'):
(self.dir / name).write_bytes(b'x')
def tearDown(self):
self.tmp.cleanup()
def form(self, **kw):
values = dict(base=str(self.dir / 'base.nsp'), update=str(self.dir / 'upd.nsp'), keys=str(self.dir / 'prod.keys'),
lang='en-US', dest=str(self.dir / 'out'))
values.update(kw)
return Form(**values)
def test_complete_form(self):
self.assertEqual(self.form().problems(), [])
def test_empty_form(self):
self.assertEqual(Form().problems(), ['ui.need_dest', 'ui.need_base', 'ui.need_update', 'ui.need_keys',
'ui.need_lang'])
def test_missing_files(self):
self.assertEqual(self.form(base=str(self.dir / 'no.nsp'), keys=str(self.dir / 'no.keys')).problems(),
['ui.missing_base', 'ui.missing_keys'])
def test_unknown_language(self):
self.assertEqual(self.form(lang='xx').problems(), ['ui.need_lang'])
def test_command(self):
cmd = self.form().command('py.exe', root=ROOT, temp=self.dir / 't', verify=True)
self.assertEqual(cmd[:3], ['py.exe', '-m', 'installer'])
for flag in ('--base', '--update', '--keys', '--lang', '--dest', '--root', '--temp', '--verify'):
self.assertIn(flag, cmd)
self.assertEqual(cmd[cmd.index('--lang') + 1], 'en-US')
def test_installed_folder_needs_only_itself(self):
out = self.dir / 'out'
statefile.save(self._mkdir(out), statefile.empty('r0') | {'lang': 'ru'})
form = Form(dest=str(out))
self.assertTrue(form.update_mode())
self.assertEqual(form.problems(), [])
cmd = form.command('py.exe')
self.assertEqual(cmd, ['py.exe', '-m', 'installer', '--dest', str(out)])
def test_installed_folder_still_checks_given_files(self):
out = self.dir / 'out'
statefile.save(self._mkdir(out), statefile.empty('r0'))
self.assertEqual(Form(dest=str(out), base=str(self.dir / 'no.nsp')).problems(), ['ui.missing_base'])
def test_broken_state_is_a_new_install(self):
out = self._mkdir(self.dir / 'out')
(out / statefile.NAME).write_text('{nope', encoding='utf-8')
self.assertFalse(Form(dest=str(out)).update_mode())
@staticmethod
def _mkdir(path):
path.mkdir(parents=True, exist_ok=True)
return path
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_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 += ['ui.state_' + n for n in ('started', 'done', 'skipped', 'pending', 'running', 'failed')]
for lang in ('en', 'ru'):
cat.set_language(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', 'Корейский')):
cat = Catalog(lang)
self.assertIn(korean, cat('warn.lang_needs_fonts', lang='ko'))
self.assertNotIn(' ko ', cat('warn.lang_needs_fonts', lang='ko'))
self.assertNotIn('extract', cat('state.output_changed', step='extract'))
for word in ('base', 'update', 'keys'):
self.assertIn(cat('param.input.' + word), cat('check.keys_wrong', input=word))
self.assertNotIn('en-US', cat('check.lang_unknown', lang='xx', known='ja, en-US'))
self.assertEqual(Catalog('en')('check.lang_unknown', lang='xx', known='ja, en-US'),
"Unknown language 'xx'. Known: Japanese, English (US).")
def test_fallback_and_params(self):
cat = Catalog('ru')
self.assertIn('36.4', cat('ui.log_space', need='36.4', free='1.0'))
self.assertEqual(cat('no.such.id'), 'no.such.id')
self.assertIn('a, b', cat('check.release_incomplete', paths=['a', 'b']))
cat.set_language('de') # unknown language falls back to English
self.assertEqual(cat.lang, 'en')
def test_describe_and_amount(self):
cat = Catalog('en')
self.assertEqual(amount(cat, 6e9, 16e9, 'bytes'), '6.0 / 16.0 GB')
self.assertEqual(amount(cat, 1, 9, 'shaders'), '1 / 9 shaders')
self.assertEqual(amount(cat, 1, 9, 'widgets'), '1 / 9 widgets')
line = describe_event(cat, {'type': 'step', 'step': 'check', 'state': 'done', 'seconds': 1.5})
self.assertIn('1.5 s', line)
self.assertEqual(describe_event(cat, {'type': 'start'}), '')
def test_windows_language_is_a_known_value(self):
self.assertIn(windows_ui_language(), ('en', 'ru'))
BREAK_CHILD = 'import time\nprint("ready", flush=True)\ntime.sleep(60)\n'
class CancelTest(unittest.TestCase):
def test_ctrl_break_ends_the_process_group(self):
p = Process([sys.executable, '-c', BREAK_CHILD])
try:
self.assertEqual(p.lines.get(timeout=20).strip(), 'ready')
t0 = time.monotonic()
p.cancel()
code = None
while code is None and time.monotonic() - t0 < 20:
code = p.poll()
time.sleep(0.05)
self.assertIsNotNone(code, 'the child ignored Ctrl+Break')
self.assertNotEqual(code, 0)
finally:
p.kill()
class SmokeTest(unittest.TestCase):
def test_every_screen_in_both_languages(self):
try:
import tkinter # noqa: F401
except ImportError:
self.skipTest('no tkinter')
r = subprocess.run([sys.executable, '-m', 'installer.gui', '--selftest'], cwd=ROOT, timeout=120,
capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr[-2000:])
def make_fixture():
"""build/inst-gui/e2e-install: links to install-test (see the module docstring), with the step keys of the state
set to the ones this checkout computes, so the install counts as current for it."""
if not (FIXTURE / statefile.NAME).is_file():
shutil.rmtree(FIXTURE, ignore_errors=True)
for folder, _dirs, files in os.walk(INSTALLED):
target = FIXTURE / Path(folder).relative_to(INSTALLED)
target.mkdir(parents=True, exist_ok=True)
for name in files:
src, dst = Path(folder) / name, target / name
if name in COPIED and target == FIXTURE:
shutil.copy2(src, dst)
else:
os.link(src, dst)
layout = layouts.find(ROOT)
state = statefile.load(FIXTURE)
options = SimpleNamespace(base=None, update=None, keys=None, lang=None)
ctx = Context(layout, FIXTURE, ROOT / 'build' / 'inst-gui' / 'e2e-temp', Reporter(out=io.StringIO()), options, state=state)
ctx.steps = all_steps()
ctx.plan()
for step_id, record in state['steps'].items():
record['key'] = ctx.keys[step_id]
statefile.save(FIXTURE, state)
return FIXTURE
@unittest.skipUnless((INSTALLED / statefile.NAME).is_file(), 'build/install-test is absent')
class EndToEndTest(unittest.TestCase):
@staticmethod
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)
model, stray = Model(), []
t0, code = time.monotonic(), None
while code is None and time.monotonic() - t0 < 600:
try:
line = p.lines.get(timeout=0.2)
except queue.Empty:
line = ''
if line:
event = parse_line(line)
if event:
model.apply(event)
elif line.strip():
stray.append(line)
code = p.poll()
p.kill()
model.finish(code)
if not any(w[0] == 'state.output_changed' for w in model.warnings):
break
return code, model, stray, time.monotonic() - t0
def test_verify_run_skips_every_step(self):
form = Form(dest=str(make_fixture()))
self.assertTrue(form.update_mode())
code, model, stray, elapsed = self.run_cli(form, ROOT / 'build' / 'inst-gui' / 'e2e-temp', tries=3)
print(f'\nend-to-end: exit {code}, result {model.result}, {elapsed:.1f} s, states {model.states}')
if any(w[0] == 'state.output_changed' for w in model.warnings):
self.skipTest('the RomFS hash read wrong three times (page cache of the faulty RAM, see the module docstring)')
self.assertEqual(code, 0, stray[-5:] or model.error)
self.assertEqual(model.result, 'ok')
self.assertIn(model.mode, ('resume', 'update'))
self.assertEqual({s: model.states[s] for s in model.steps if s not in ('check', 'cleanup')},
{s: SKIPPED for s in model.steps if s not in ('check', 'cleanup')})
self.assertIsNotNone(model.space)
self.assertEqual(stray, [])
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)
p = Process(cmd, cwd=ROOT)
model, t0, code, cancelled = Model(), time.monotonic(), None, False
while code is None and time.monotonic() - t0 < 120:
try:
event = parse_line(p.lines.get(timeout=0.2))
except queue.Empty:
event = None
if event:
model.apply(event)
if event.get('phase') == 'verify_romfs' and not cancelled:
cancelled = True
p.cancel()
code = p.poll()
p.kill()
model.finish(code)
self.assertTrue(cancelled)
self.assertEqual((code, model.result), (3, 'cancelled'))
self.assertIsNone(model.error)
if __name__ == '__main__':
unittest.main(verbosity=2)
+264
View File
@@ -0,0 +1,264 @@
"""installer/ bookkeeping with fake steps: fresh install, resume after a failure or cancel, transient steps, update
with a newer release, totk.ini and the event stream. No game files, no tools; stdlib only.
Run: python tests/installer_test.py
"""
import io
import json
import re
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from installer import ini, runner, state # noqa: E402
from installer.events import Reporter # noqa: E402
from installer.layout import Layout # noqa: E402
class Fake(runner.Step):
"""A step whose output is <dir>/<id>.out holding its key; dir is the temp folder for a transient step."""
def __init__(self, id, transient=False, always=False, previous=None):
self.id, self.transient, self.always, self.previous = id, transient, always, previous
def out(self, ctx):
return (ctx.temp if self.transient else ctx.dest) / f'{self.id}.out'
def key(self, ctx):
upstream = ctx.keys[self.previous] if self.previous else ''
return f'{self.id}:{ctx.options.inputs.get(self.id, "v1")}:{upstream}'
def current(self, ctx, record):
p = self.out(ctx)
return p.is_file() and p.read_text() == ctx.keys[self.id]
def verify(self, ctx, record):
ctx.options.verified.append(self.id)
return ctx.options.verify_ok.get(self.id, True)
def run(self, ctx):
o = ctx.options
o.ran.append(self.id)
if self.id in ctx.forced:
o.forced.append(self.id)
if self.id == 'check':
ctx.verify_kept(self.id)
failure = o.fail.pop(self.id, None)
if failure == 'error':
raise runner.InstallError('fake.failed', step=self.id)
if failure == 'cancel':
raise KeyboardInterrupt
if self.id == 'cleanup':
for p in ctx.temp.glob('*.out'):
p.unlink()
return {}
p = self.out(ctx)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(ctx.keys[self.id])
return {'note': self.id}
def fake_steps():
return [Fake('check', always=True), Fake('extract'), Fake('lift', transient=True, previous='extract'),
Fake('compile', previous='lift'), Fake('prepare', previous='compile'), Fake('cleanup', always=True)]
class Bed:
"""A dest and temp folder plus one run of the fake steps per call."""
def __init__(self, base):
self.dest, self.temp = base / 'dest', base / 'temp'
self.temp.mkdir()
def run(self, release='1.0', inputs=None, fail=None, verify_ok=None, verify=False):
layout = Layout(root=Path('.'), dev=True, release=release, python=Path('python'), tools=Path('tools'),
recomp=Path('recomp'), patches=Path('p'), expected=Path('e'), llvm=Path('llvm'),
deps=Path('deps'), runtime=Path('rt'), runtime_lib=Path('lib'), ninja=Path('ninja'),
hactool=Path('hactool'), xlate=Path('xlate'))
options = SimpleNamespace(inputs=inputs or {}, fail=dict(fail or {}), verify_ok=verify_ok or {}, ran=[],
verified=[], forced=[])
out = io.StringIO()
ctx = runner.Context(layout, self.dest, self.temp, Reporter(out=out), options, state=state.load(self.dest),
verify=verify)
mode = ctx.mode
result = runner.run_steps(fake_steps(), ctx)
events = [json.loads(line) for line in out.getvalue().splitlines()]
return SimpleNamespace(result=result, ran=options.ran, verified=options.verified, forced=options.forced,
events=events, mode=mode,
state=state.load(self.dest))
@staticmethod
def skipped(r):
return {e['step']: e['reason'] for e in r.events if e['type'] == 'step' and e['state'] == 'skipped'}
ALL = ['check', 'extract', 'lift', 'compile', 'prepare', 'cleanup']
class RunnerTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.bed = Bed(Path(self.tmp.name))
def tearDown(self):
self.tmp.cleanup()
def test_fresh_install_runs_everything_and_records_it(self):
r = self.bed.run()
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'install', ALL))
self.assertEqual(set(r.state['steps']), {'extract', 'lift', 'compile', 'prepare'})
self.assertEqual(r.state['steps']['compile']['note'], 'compile')
self.assertEqual(r.state['release'], '1.0')
started = [e['step'] for e in r.events if e['type'] == 'step' and e['state'] == 'started']
done = [e['step'] for e in r.events if e['type'] == 'step' and e['state'] == 'done']
self.assertEqual((started, done), (ALL, ALL))
def test_rerun_skips_finished_steps_and_unneeded_transient(self):
self.bed.run()
r = self.bed.run()
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'resume', ['check', 'cleanup']))
self.assertEqual(self.bed.skipped(r), {'extract': 'current', 'lift': 'not_needed', 'compile': 'current',
'prepare': 'current'})
self.assertEqual(r.verified, []) # same release: no full checks
def test_resume_after_failure(self):
r = self.bed.run(fail={'compile': 'error'})
self.assertEqual((r.result, r.ran), ('failed', ['check', 'extract', 'lift', 'compile']))
error = [e for e in r.events if e['type'] == 'error'][0]
self.assertEqual((error['id'], error['step'], error['params']), ('fake.failed', 'compile', {'step': 'compile'}))
self.assertEqual(set(r.state['steps']), {'extract', 'lift'})
r = self.bed.run()
# lift's output is still in the temp folder (no cleanup ran), so only compile and later run.
self.assertEqual((r.result, r.mode, r.ran), ('ok', 'resume', ['check', 'compile', 'prepare', 'cleanup']))
def test_resume_after_cancel_with_transient_output_gone(self):
r = self.bed.run(fail={'prepare': 'cancel'})
self.assertEqual(r.result, 'cancelled')
self.assertNotIn('prepare', r.state['steps'])
for p in self.bed.temp.glob('*.out'):
p.unlink()
r = self.bed.run()
self.assertEqual(r.ran, ['check', 'prepare', 'cleanup'])
self.assertEqual(self.bed.skipped(r)['lift'], 'not_needed')
def test_failed_check_writes_nothing(self):
r = self.bed.run(fail={'check': 'error'})
self.assertEqual(r.result, 'failed')
self.assertFalse(self.bed.dest.exists())
def test_removed_output_runs_again(self):
self.bed.run()
(self.bed.dest / 'compile.out').unlink()
r = self.bed.run()
self.assertEqual(r.ran, ['check', 'lift', 'compile', 'cleanup'])
def test_update_with_new_lifter_keeps_extraction(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1', inputs={'lift': 'v2'})
self.assertEqual((r.result, r.mode), ('ok', 'update'))
self.assertEqual(r.ran, ['check', 'lift', 'compile', 'prepare', 'cleanup'])
self.assertEqual(r.verified, ['extract']) # kept from another release: full check
self.assertEqual(r.state['steps']['extract']['release'], '1.0')
self.assertEqual(r.state['steps']['compile']['release'], '1.1')
self.assertEqual(r.state['release'], '1.1')
def test_update_that_changes_nothing_skips_the_rebuild(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1')
self.assertEqual(r.ran, ['check', 'cleanup'])
self.assertEqual(r.verified, ['extract', 'compile', 'prepare'])
def test_update_with_changed_kept_files_extracts_again(self):
self.bed.run(release='1.0')
r = self.bed.run(release='1.1', verify_ok={'extract': False})
self.assertEqual(r.ran, ['check', 'extract', 'cleanup'])
self.assertEqual(r.forced, ['extract']) # the step knows not to trust what it left behind
warnings = [e for e in r.events if e['type'] == 'warning']
self.assertEqual([(w['id'], w['params']) for w in warnings], [('state.output_changed', {'step': 'extract'})])
def test_verify_option_checks_same_release(self):
self.bed.run()
r = self.bed.run(verify=True)
self.assertEqual(r.verified, ['extract', 'compile', 'prepare'])
def test_state_errors(self):
self.bed.dest.mkdir()
(self.bed.dest / state.NAME).write_text('{not json')
with self.assertRaises(state.StateError) as e:
state.load(self.bed.dest)
self.assertEqual(e.exception.id, 'state.unreadable')
(self.bed.dest / state.NAME).write_text(json.dumps({'format': 99, 'steps': {}}))
with self.assertRaises(state.StateError) as e:
state.load(self.bed.dest)
self.assertEqual(e.exception.id, 'state.newer_format')
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'
def test_example_line_is_activated(self):
out = ini.set_language(self.EXAMPLE, 'ru')
self.assertIn('\nlang = ru\n', out)
self.assertNotIn('# lang', out)
self.assertIn('# locale = en-US', out)
def test_active_line_is_rewritten_and_the_rest_kept(self):
text = '[display]\nvk_present = fifo\n[language]\n LANG = ja \nfps60 = 0\n'
out = ini.set_language(text, 'de')
self.assertEqual(out, '[display]\nvk_present = fifo\n[language]\n lang = de\nfps60 = 0\n')
self.assertEqual(ini.get_language(out), 'de')
def test_missing_section_is_added(self):
self.assertEqual(ini.set_language('fps60 = 0\n', 'fr'), 'fps60 = 0\n\n[language]\nlang = fr\n')
self.assertEqual(ini.set_language('[language]\nlocale = de-DE\n', 'fr'),
'[language]\nlang = fr\nlocale = de-DE\n')
def test_write_keeps_user_lines(self):
with tempfile.TemporaryDirectory() as d:
d = Path(d)
example = d / 'totk.ini.example'
example.write_text(self.EXAMPLE)
path = ini.write(d / '', 'en-GB', example)
self.assertEqual(ini.get_language(path.read_text()), 'en-GB')
path.write_text(path.read_text().replace('# vk_present = mailbox', 'vk_present = fifo'))
ini.write(d, 'ja', example)
text = path.read_text()
self.assertIn('vk_present = fifo', text)
self.assertEqual(ini.get_language(text), 'ja')
class EventsTest(unittest.TestCase):
def test_lines_are_json_and_the_log_is_english(self):
with tempfile.TemporaryDirectory() as d:
out = io.StringIO()
rep = Reporter(out=out, log_path=Path(d) / 'x.log')
rep.step('check', 'started')
rep.progress('extract', 'copy', 0, 10, 'bytes')
rep.progress('extract', 'copy', 5, 10, 'bytes') # throttled
rep.progress('extract', 'copy', 10, 10, 'bytes') # last one always goes out
rep.error('check.disk_space', {'path': 'D:\\x', 'need_gb': 40, 'free_gb': 3}, step='check')
rep.close()
events = [json.loads(line) for line in out.getvalue().splitlines()]
self.assertEqual([e['type'] for e in events], ['step', 'progress', 'progress', 'error'])
self.assertEqual([e.get('done') for e in events[1:3]], [0, 10])
self.assertTrue(all('t' in e for e in events))
log = (Path(d) / 'x.log').read_text(encoding='utf-8')
self.assertIn('40 GB needed, 3 GB free', log)
self.assertIn('[check.disk_space]', log)
def test_catalog_has_every_id_the_code_uses(self):
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())}
self.assertEqual(sorted(i for i in used if i not in catalog), [])
if __name__ == '__main__':
unittest.main()
+124 -22
View File
@@ -7,6 +7,8 @@ recompiled only when it, a header it includes or its flags change.
.venv\\Scripts\\python tools\\build_game.py --modules subsdk0 # one module (links if it has no calls into others)
.venv\\Scripts\\python tools\\build_game.py --jobs 8
.venv\\Scripts\\python tools\\build_game.py --out build/v121-t2 # a separate build directory
.venv\\Scripts\\python tools\\build_game.py --toolchain toolchain/llvm-mingw-<version>/llvm-mingw-<version>-ucrt-x86_64 \\
--out build/v121-llvmmingw # llvm-mingw instead of MSYS2 UCRT64
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
@@ -34,6 +36,16 @@ Compile jobs share a memory budget (MEM_BUDGET, default the smaller of 50 GiB an
big machine does not run out of memory on the largest chunks. Each phase reports its wall time and the peak memory
of its processes (Windows job object accounting: committed private bytes).
--ninja PATH names the Ninja to run; without MSYS2 the bin/ninja.exe of the release folder (tools/make_release.py) is used.
--toolchain DIR builds with an llvm-mingw tree (DIR/bin/clang.exe) instead of MSYS2 UCRT64 clang. Ninja still comes
from MSYS2. The libraries and headers MSYS2 supplies (Vulkan, SDL3, Opus, libvpx, VMA) then come from --deps DIR
(default toolchain/deps-mingw: include/, lib/ with the import libraries and libvpx.a, bin/ with the DLLs), and the C++
runtime is libc++ from llvm-mingw. Such a build never reuses another build's generated objects.
Split build. --make-runtime-lib compiles only runtime/ into <out>/libtotkrt.a (what CI prebuilds). --runtime-lib PATH
then compiles only the generated C and the install-time files (modules.c and INSTALL_SIDE, which bake the player's
game hashes in) and links them with that library. The library holds no game-derived data.
Standard library only.
"""
import argparse
@@ -49,6 +61,7 @@ import time
from pathlib import Path
import romfs_index
from build_shaders import stale as stale_shaders
from check_content import GENERATED_MARKER
ROOT = Path(__file__).resolve().parent.parent
@@ -64,9 +77,19 @@ SDL3_DLLS = [UCRT / 'SDL3.dll', UCRT / 'libiconv-2.dll', UCRT / 'libopus-0.dll']
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']
LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '-Wl,--Map=$builddir/totk.map'] # the map feeds tools/prof_report.py
LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '"-Wl,--Map=$builddir/totk.map"'] # the map feeds tools/prof_report.py
LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-lwinmm', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end of M4; libvpx static (movies)
VMA_INCLUDE = UCRT.parent / 'include' / 'vma' # vk_mem_alloc.h; MSYS2 package vulkan-memory-allocator
# --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', '-lwinmm', '-lvulkan-1', '-lSDL3', '-lopus',
'-l:libvpx.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a']
DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw'
INSTALL_SIDE = {'romfs.c'} # runtime sources that include romfs_config.h: compiled per install, not in the library
LIB_NAME = 'libtotkrt.a'
AR = UCRT / 'llvm-ar.exe'
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', 'v112': '1.1.2', 'v121': '1.2.1', 'v143': '1.4.3'}
@@ -96,6 +119,17 @@ def flag_path(p):
return below_root(p).as_posix()
def cmd_arg(s):
"""s for a Ninja command line: Ninja-escaped, and quoted when it holds a space (a user or folder name), as clang and
lld split their Windows command line at unquoted spaces."""
s = s.replace('$', '$$')
return f'"{s}"' if ' ' in s else s
def cmd_path(p):
return cmd_arg(flag_path(p))
def read_layout(gen):
"""[(name, base, size)] from layout.json (same format as recomp/layout.py read(), without its NSO dependency)."""
data = json.loads((gen / 'layout.json').read_text())
@@ -125,10 +159,10 @@ def chunk_files(gen, mod):
def write_if_changed(path, text):
"""Keep the mtime of an unchanged file so Ninja does not rebuild from it."""
if path.exists() and path.read_text() == text:
if path.exists() and path.read_text(encoding='utf-8', errors='replace') == text:
return
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, newline='\n')
path.write_text(text, encoding='utf-8', newline='\n') # Ninja and clang read UTF-8; paths may be non-ASCII
def c_string(s):
@@ -139,9 +173,10 @@ RUNTIME = ROOT / 'runtime' # --runtime: a copy of runtime/ lets a task build w
def runtime_sources(skip=()):
"""Every runtime/*.c, *.cpp, platform/*.c, the in-game menu (ui/*.cpp) and Dear ImGui except the names in skip, so
a task adds a file without editing this script."""
pats = ('*.c', '*.cpp', 'platform/*.c', 'ui/*.cpp', 'third_party/imgui/*.cpp', 'third_party/imgui/backends/*.cpp')
"""Every runtime/*.c, *.cpp, platform/*.c, *.rc, the in-game menu (ui/*.cpp) and Dear ImGui except the names in skip,
so a task adds a file without editing this script."""
pats = ('*.c', '*.cpp', 'platform/*.c', '*.rc', 'ui/*.cpp', 'third_party/imgui/*.cpp',
'third_party/imgui/backends/*.cpp')
return sorted(p for pat in pats for p in RUNTIME.glob(pat) if p.name not in skip)
@@ -182,21 +217,24 @@ def module_glue(layout, linked, hashes, paths):
return '\n'.join(out)
def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False):
def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False, runtime_lib=None, make_lib=False):
"""build.ninja text, and the object lists per module. With prebuilt the generated objects already exist in
out/obj and are link inputs without a compile edge."""
inc = f'-I {flag_path(RUNTIME)} -I {flag_path(gen)}'
# romfs_config.h, vk_mem_alloc.h, imgui.h
rt_inc = f'{inc} -I {flag_path(out)} -I {flag_path(VMA_INCLUDE)} -I {flag_path(RUNTIME / "third_party" / "imgui")}'
out/obj and are link inputs without a compile edge. runtime_lib: a prebuilt library replaces the runtime objects
except INSTALL_SIDE. make_lib: build only that library, from the runtime objects except INSTALL_SIDE."""
inc = f'-I {cmd_path(RUNTIME)} -I {cmd_path(gen)}'
vma = f'{cmd_path(DEPS / "include")} -I {cmd_path(DEPS / "include" / "vma")}' if DEPS else cmd_path(VMA_INCLUDE)
imgui = cmd_path(RUNTIME / 'third_party' / 'imgui')
rt_inc = f'{inc} -I {cmd_path(out)} -I {vma} -I {imgui}' # romfs_config.h, vk_mem_alloc.h (+ deps headers), imgui.h
libs = [f'-L {cmd_path(DEPS / "lib")}'] + LLVM_LINK_LIBS if DEPS else LINK_LIBS
lines = [
'# Written by tools/build_game.py; do not edit.',
'ninja_required_version = 1.10',
f'builddir = {ninja_path(out)}',
f'cc = {CLANG.as_posix()}',
f'cc = {cmd_arg(CLANG.as_posix())}',
f'genflags = {" ".join(GEN_FLAGS)} {inc}',
f'rtflags = {" ".join(RT_FLAGS)} {rt_inc}',
f'ldflags = {" ".join(LINK_FLAGS)}',
f'libs = {" ".join(LINK_LIBS)}',
f'libs = {" ".join(libs)}',
'',
'pool gen',
f' depth = {pool_depth}',
@@ -213,6 +251,10 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False):
' depfile = $out.d',
' description = CC $in',
'',
'rule rc',
f' command = {cmd_arg(WINDRES.as_posix())} -I {cmd_path(RUNTIME)} -O coff $in -o $out',
' description = RC $in',
'',
'rule link',
' command = $cc $ldflags @$out.rsp $libs -o $out',
' rspfile = $out.rsp',
@@ -230,14 +272,35 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False):
lines.append(' pool = gen')
objs[mod].append(o)
rt = []
for src in runtime_sources(skip) + [out / 'modules.c']:
sources = runtime_sources(skip)
if make_lib:
sources = [p for p in sources if p.name not in INSTALL_SIDE]
elif runtime_lib:
sources = [p for p in sources if p.name in INSTALL_SIDE] + [out / 'modules.c']
else:
sources += [out / 'modules.c']
for src in sources:
o = runtime_object(out, src)
lines.append(f'build {ninja_path(o)}: cc_rt {ninja_path(src)}')
lines.append(f'build {ninja_path(o)}: {"rc" if src.suffix == ".rc" else "cc_rt"} {ninja_path(src)}')
rt.append(o)
objs['runtime'] = rt
every = [o for v in objs.values() for o in v]
exe = out / 'totk.exe'
lines += ['', f'build {ninja_path(exe)}: link ' + ' '.join(ninja_path(o) for o in every),
if make_lib:
exe = out / LIB_NAME
lines += ['', 'rule ar', f' command = {cmd_arg(AR.as_posix())} rcs $out $in', ' description = AR $out', '', f'build {ninja_path(exe)}: ar ' + ' '.join(ninja_path(o) for o in every),
'build objects: phony ' + ' '.join(ninja_path(o) for o in every), f'default {ninja_path(exe)}', '']
return '\n'.join(lines), objs, exe
rule = 'link'
if runtime_lib:
# Whole archive: the weak probe stubs in gfx_stubs.c would otherwise satisfy references to the probes before the
# linker looks into the library, and the real probes would stay out of totk.exe.
rule = 'link_lib'
lines += ['', 'rule link_lib',
f' command = $cc $ldflags @$out.rsp -Wl,--whole-archive {cmd_path(runtime_lib)} -Wl,--no-whole-archive $libs -o $out',
' rspfile = $out.rsp', ' rspfile_content = $in_newline', ' description = LINK $out']
lines += ['', f'build {ninja_path(exe)}: {rule} ' + ' '.join(ninja_path(o) for o in every) +
(f' | {ninja_path(runtime_lib)}' if runtime_lib else ''),
'build objects: phony ' + ' '.join(ninja_path(o) for o in every), f'default {ninja_path(exe)}', '']
return '\n'.join(lines), objs, exe
@@ -251,7 +314,7 @@ def main_objects(gen, main_out, rt_dir, mods):
return None, f'{flag_path(main_ninja)} not found'
if gen != (ROOT / 'generated' / main_out.name).resolve():
return None, 'generated C directory is not the main one'
lines = main_ninja.read_text().splitlines()
lines = main_ninja.read_text(encoding='utf-8').splitlines()
sources = dict(l[6:].split(': cc_gen ') for l in lines if l.startswith('build ') and ': cc_gen ' in l)
flags = next((l for l in lines if l.startswith('genflags = ')), '')
if not flags.startswith('genflags = ' + ' '.join(GEN_FLAGS) + ' -I '):
@@ -339,7 +402,7 @@ def read_manifest(path):
"""{object path below obj/: (sha256, size, mtime_ns)}; empty when the file is missing."""
man = {}
if path.is_file():
for line in path.read_text().splitlines():
for line in path.read_text(encoding='utf-8').splitlines():
sha, size, mtime, rel = line.split(' ', 3)
man[rel] = (sha, int(size), int(mtime))
return man
@@ -518,6 +581,16 @@ def main():
ap.add_argument('--romfs', type=Path, help="totk.exe's default RomFS directory with romfs.bin and index.json "
'(default build/extracted/<version>/romfs)')
ap.add_argument('--profile', type=Path, help="totk.exe's default profile directory (default build/profile)")
ap.add_argument('--toolchain', type=Path, help='llvm-mingw directory (with bin/clang.exe) to build with instead of '
'MSYS2 UCRT64 clang')
ap.add_argument('--ninja', type=Path, help='ninja.exe to build with (default: MSYS2 UCRT64, else bin/ninja.exe of the '
'release folder)')
ap.add_argument('--deps', type=Path, help=f'with --toolchain: include/, lib/ and bin/ of Vulkan, SDL3, Opus, libvpx '
f'and VMA (default {DEFAULT_DEPS.relative_to(ROOT).as_posix()})')
ap.add_argument('--make-runtime-lib', action='store_true',
help=f'build only <out>/{LIB_NAME} from runtime/ (no generated C, no totk.exe)')
ap.add_argument('--runtime-lib', type=Path, help=f'link against this {LIB_NAME} instead of compiling runtime/ '
f'({", ".join(sorted(INSTALL_SIDE))} and modules.c still compile)')
ap.add_argument('--expected', type=Path,
help='expected hashes (default tools/data/expected-<display version>.json)')
args = ap.parse_args()
@@ -534,6 +607,19 @@ def main():
sys.exit(f'no expected hashes known for {args.version}; pass --expected')
expected = ROOT / 'tools' / 'data' / f'expected-v{DISPLAY_VERSIONS[args.version]}.json'
hashes = json.loads(Path(expected).read_text())['exefs_sha256']
global CLANG, AR, WINDRES, NINJA, TOOLCHAIN_BIN, DEPS
if args.ninja:
NINJA = args.ninja.resolve()
elif not NINJA.is_file() and (ROOT / 'bin' / 'ninja.exe').is_file():
NINJA = ROOT / 'bin' / 'ninja.exe' # the release folder (tools/make_release.py)
dlls = SDL3_DLLS
if args.toolchain:
TOOLCHAIN_BIN = (args.toolchain / 'bin').resolve()
CLANG = TOOLCHAIN_BIN / 'clang.exe'
AR = TOOLCHAIN_BIN / 'llvm-ar.exe'
WINDRES = TOOLCHAIN_BIN / 'llvm-windres.exe'
DEPS = (args.deps or DEFAULT_DEPS).resolve()
dlls = sorted((DEPS / 'bin').glob('*.dll'))
for tool in (CLANG, NINJA):
if not tool.is_file():
sys.exit(f'{tool} not found: install MSYS2 UCRT64 clang, lld and ninja (docs/toolchain-msys2-python.md)')
@@ -564,11 +650,18 @@ def main():
print(f'build_game: warning: no {paths["romfs"] / "index.json"}; totk.exe will have no RomFS', file=sys.stderr)
global RUNTIME
RUNTIME = (args.runtime or RUNTIME).resolve()
stale = stale_shaders(RUNTIME)
if stale:
sys.exit(f'{stale[0]}: its SPIR-V header no longer matches; run python tools/build_shaders.py')
skip = {s for s in args.skip_runtime.split(',') if s}
main_out = (ROOT / 'build' / args.version).resolve()
pairs = None
if args.full_build:
if args.make_runtime_lib:
mods, linked, why = {}, [], ''
elif args.full_build:
why = '--full-build'
elif args.toolchain:
why = f'--toolchain {flag_path(args.toolchain)}'
elif out == main_out:
why = ''
else:
@@ -581,10 +674,14 @@ def main():
unlink_shared(out, mods)
if why:
print(f'build_game: compiling all generated C: {why}')
text, objs, exe = ninja_file(gen, out, mods, jobs, skip, pairs is not None)
runtime_lib = args.runtime_lib.resolve() if args.runtime_lib else None
if runtime_lib and not runtime_lib.is_file():
sys.exit(f'{runtime_lib} not found: build it with --make-runtime-lib')
text, objs, exe = ninja_file(gen, out, mods, jobs, skip, pairs is not None, runtime_lib,
args.make_runtime_lib)
write_if_changed(out / 'build.ninja', text)
env = dict(os.environ, PATH=str(UCRT) + os.pathsep + os.environ.get('PATH', ''))
env = dict(os.environ, PATH=str(TOOLCHAIN_BIN or UCRT) + os.pathsep + os.environ.get('PATH', ''))
base = [str(NINJA), '-f', str(out / 'build.ninja'), '-j', str(jobs)]
nfiles = sum(len(v) for v in mods.values())
print(f'build_game: {nfiles} generated files in {len(linked)} modules ({", ".join(linked)}), {jobs} jobs', flush=True)
@@ -599,13 +696,18 @@ def main():
verify_objects(out, objs, pairs is not None, main_out, pairs, base, env)
if args.compile_only:
return
if args.make_runtime_lib:
exe.unlink(missing_ok=True) # ar rcs would keep members of deleted sources
code, dt, peak_one, _ = run_measured(base + [flag_path(exe)], env)
print(f'link: {dt:.1f} s, peak {mib(peak_one)}')
if code:
print(f'build_game: link failed, {flag_path(exe)} is the old one (a running totk.exe from this dir locks it)',
file=sys.stderr)
sys.exit(code)
for dll in SDL3_DLLS: # dynamic SDL3 (zlib) and its libiconv (LGPL, unmodified DLL), found next to totk.exe
if args.make_runtime_lib:
print(f'{below_root(exe)}: {mib(exe.stat().st_size)}')
return
for dll in dlls: # dynamic SDL3 (zlib) and its libiconv (LGPL, unmodified DLL), found next to totk.exe
shutil.copy2(dll, exe.parent / dll.name)
sections, image = pe_sections(exe)
print(f'{below_root(exe)}: file {mib(exe.stat().st_size)}, '
+82
View File
@@ -0,0 +1,82 @@
"""Compile the runtime's own GLSL shaders (runtime/shaders/*.comp) 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[]`
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).
python tools/build_shaders.py [--check]
"""
import argparse
import hashlib
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
BIN = Path(r'C:\msys64\ucrt64\bin')
TARGET = 'vulkan1.1'
def source_hash(src):
return hashlib.sha256(src.read_bytes().replace(b'\r\n', b'\n')).hexdigest() # the same on a CRLF checkout
def header_of(src, words):
name = src.stem
lines = [f'// Generated by tools/build_shaders.py from runtime/shaders/{src.name}; do not edit.',
f'// source sha256 {source_hash(src)}',
'#pragma once',
'#include <stdint.h>',
f'static const uint32_t {name}_spv[] = {{']
for i in range(0, len(words), 8):
lines.append(' ' + ', '.join(f'0x{w:08x}' for w in words[i:i + 8]) + ',')
lines.append('};')
return '\n'.join(lines) + '\n'
def header_path(src):
return src.parent.parent / f'{src.stem}_spv.h'
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')):
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)
return out
def compile_one(src):
with tempfile.TemporaryDirectory() as tmp:
spv = Path(tmp) / 'out.spv'
subprocess.run([str(BIN / 'glslc.exe'), '-O', f'--target-env={TARGET}', '-fshader-stage=compute',
'-o', str(spv), str(src)], check=True)
subprocess.run([str(BIN / 'spirv-val.exe'), f'--target-env', TARGET, str(spv)], check=True)
data = spv.read_bytes()
words = [int.from_bytes(data[i:i + 4], 'little') for i in range(0, len(data), 4)]
header_path(src).write_text(header_of(src, words), encoding='utf-8', newline='\n')
print(f'{header_path(src).relative_to(ROOT)}: {len(words)} words')
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--check', action='store_true', help='only report headers that do not match their source')
ap.add_argument('--runtime', type=Path, default=ROOT / 'runtime')
args = ap.parse_args()
if args.check:
bad = stale(args.runtime)
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')):
compile_one(src)
return 0
if __name__ == '__main__':
sys.exit(main())
+7 -1
View File
@@ -47,7 +47,9 @@ def list_files(staged: bool) -> list[Path]:
return [Path(p) for p in out.decode("utf-8").split("\0") if p]
def problems_for(path: Path) -> list[str]:
def problems_for(path: Path, allow_large: bool = False) -> list[str]:
"""allow_large: files over MAX_BYTES (toolchain binaries in a release folder) are checked for the generated marker
only, not refused."""
found = []
if path.suffix.lower() in BLOCKED_SUFFIXES:
found.append(f"blocked file type {path.suffix}")
@@ -56,6 +58,10 @@ def problems_for(path: Path) -> list[str]:
if not path.is_file():
return found
size = path.stat().st_size
if size > MAX_BYTES and allow_large:
if GENERATED_MARKER.encode() in path.read_bytes():
found.append("generated from game code")
return found
if size > MAX_BYTES:
found.append(f"{size // 1024} KiB, over the {MAX_BYTES // 1024} KiB limit")
return found
+123 -29
View File
@@ -9,7 +9,9 @@ Steps, each skipped when its verified output already exists:
Steps 1 and 3 run for every file in `input.updates`. Each update's version is identified by the SHA-256 of its `main`,
not by its file name. Output: build/extracted/v0/{exefs,romfs} and build/extracted/v<version>/{exefs,romfs}.
Runs under .venv-py314. Key values are read by the tools from the keys file and never printed.
--base, --update, --keys and --work name the files and the output directory instead of the config (the installer
imports the functions instead). Runs under .venv-py314. Key values are read by the tools from the keys file and never
printed.
"""
import argparse
import hashlib
@@ -39,12 +41,33 @@ LONG_HEX = re.compile(r'[0-9A-Fa-f]{32,}')
# say nothing about the data. The IVFC levels and the image hash decide.
UNSIGNED = re.compile(r'\s*(?:Fixed-Key Signature|NPDM Signature) \(FAIL\)')
HACTOOL_ERROR = re.compile(r'\(FAIL\)|\(INVALID\)|Failed to|Error:')
# hactool lines of a hash check that failed: a RomFS level, a section's superblock or its hash table.
HACTOOL_HASH_FAIL = re.compile(r'^\s*(?:Level \d|Superblock Hash|Hash Table) \(FAIL\)', re.M)
# Errors of the vendored extractors (tools/vendor/macnativegames) that mean a game file's data does not match its own
# hashes or ends early: the file is damaged or was not copied completely. DAMAGE finds them in a script's log.
DAMAGE_TEXT = re.compile(r'hash mismatch|hash-table mismatch|FS header hash|IVFC level \d+ block \d+ failed|Truncated|'
r'out of bounds|Section bounds')
DAMAGE = re.compile(rf'^\w*Error: (.*(?:{DAMAGE_TEXT.pattern}).*)$', re.M)
class DamagedFile(ValueError):
"""A game file whose data fails its hash checks or ends early. path: that file; detail: the check that failed."""
def __init__(self, path, detail):
self.path, self.detail = Path(path), detail
super().__init__(f'{self.path} is damaged or incomplete ({detail}). Copy that file again from its source '
f'(or dump it again) and retry.')
def say(message):
print(message, flush=True)
def shown(path):
"""path relative to the repository when it is below it (log lines), else as given."""
return path.relative_to(ROOT) if path.is_relative_to(ROOT) else path
def sha256(path):
with path.open('rb') as f:
return hashlib.file_digest(f, 'sha256').hexdigest()
@@ -62,10 +85,25 @@ def version_dir(expected):
return 'v0' if expected['version'] == 0 else 'v' + expected['display_version'].replace('.', '')
def run_logged(command, log):
with log.open('w') as out:
code = subprocess.run([str(c) for c in command], stdout=out, stderr=subprocess.STDOUT).returncode
require(code == 0, f'{Path(command[1]).name} failed; see {log.relative_to(ROOT)}')
# The embeddable Python (a ._pth file) does not put a script's directory on sys.path, so a vendored script that imports
# its siblings fails when started by path. This stub adds the directory, then runs the script as __main__.
RUN_SCRIPT = ('import os, runpy, sys; p = sys.argv[1]; sys.path.insert(0, os.path.dirname(p)); sys.argv = sys.argv[1:]; '
"runpy.run_path(p, run_name='__main__')")
def run_logged(command, log, source=None):
"""Run a vendored script with its output in log. source: the game file it reads; a failed hash check or a short
read of that file raises DamagedFile."""
args = [str(c) for c in command]
env = dict(os.environ, PYTHONIOENCODING='utf-8') # its messages may name non-ASCII paths
with log.open('wb') as out:
code = subprocess.run([args[0], '-c', RUN_SCRIPT, *args[1:]], stdout=out, stderr=subprocess.STDOUT,
env=env).returncode
if code and source:
damage = DAMAGE.search(log.read_text(encoding='utf-8', errors='replace'))
if damage:
raise DamagedFile(source, damage.group(1).strip())
require(code == 0, f'{Path(command[1]).name} failed; see {shown(log)}')
def exefs_problems(exefs, expected):
@@ -84,7 +122,7 @@ def extract_exefs(game, keys, out, logs, *, update):
shutil.rmtree(exefs)
out.mkdir(parents=True, exist_ok=True)
command = [sys.executable, VENDOR / 'extract_exefs.py', game, '--keys', keys, '--output', exefs]
run_logged(command + (['--update'] if update else []), logs / f'extract_exefs-{out.name}.log')
run_logged(command + (['--update'] if update else []), logs / f'extract_exefs-{out.name}.log', source=game)
return exefs
@@ -117,21 +155,18 @@ def check_romfs(romfs, expected):
say(f' RomFS {expected["display_version"]}: {report["romfs_size"]:,} bytes, {len(report["files"]):,} files, hash matches')
def extract_base_romfs(out, keys, logs):
def extract_base_romfs(out, keys, logs, base=None):
romfs = out / 'romfs'
if romfs.exists():
shutil.rmtree(romfs)
run_logged([sys.executable, VENDOR / 'extract_romfs.py', '--keys', keys,
'--manifest', out / 'extraction-manifest.json', '--output', romfs], logs / 'extract_romfs-v0.log')
'--manifest', out / 'extraction-manifest.json', '--output', romfs], logs / 'extract_romfs-v0.log',
source=base)
def merge_update_romfs(base, update, keys_path, hactool, output, logs):
"""extract_update_romfs.merge() without its Darwin-only bounded view of the base: the base Program NCA is copied out.
Needs room for both Program NCAs (about 17 GB together) and the merged image (about 17 GB).
"""
if output.exists():
shutil.rmtree(output)
def program_ncas(base, update, keys_path):
"""(tickets, [(source, record, header)] of the base and the update Program NCA). Raises ValueError for a file
that is no TotK base game or update, or keys that cannot open it."""
keys = load_keys(keys_path)
tickets = {}
inputs = []
@@ -146,6 +181,52 @@ def merge_update_romfs(base, update, keys_path, hactool, output, logs):
rights = header[0x230:0x240]
require(rights == bytes(16) or rights in tickets, 'Missing common ticket')
inputs.append((source, record, header))
return tickets, inputs
def merge_space(base, update, keys_path):
"""Bytes merge_update_romfs needs free next to its output, beyond the merged image itself: the two Program NCAs."""
_, inputs = program_ncas(base, update, keys_path)
return inputs[0][1]['size'] + inputs[1][1]['size']
MERGE_PHASES = {'copy': ' copying the base and update Program NCAs', 'merge': ' merging with hactool',
'diagnose': ' a hash check failed: checking the base Program NCA alone',
'index': ' hashing and indexing the merged image'}
def run_hactool(command, environment):
"""(exit code, stdout and stderr with long hex strings removed). hactool prints UTF-8 (its manifest)."""
completed = subprocess.run(command, env=environment, capture_output=True, encoding='utf-8', errors='replace')
return completed.returncode, LONG_HEX.sub('<hex removed>', completed.stdout + completed.stderr)
def damaged_merge_input(hactool, hactool_keys, environment, base, update, base_nca, log_path):
"""The DamagedFile for a merge whose hash checks failed. The merged RomFS mixes data of both files, so hactool
verifies the base Program NCA alone: if it fails, the base game file is damaged, else the update file is. The
output is appended to log_path."""
code, log = run_hactool([str(hactool), '-y', '--disablekeywarns', '--suppresskeys', '-k', str(hactool_keys),
str(base_nca)], environment)
with log_path.open('a', encoding='utf-8') as f:
f.write(f'\n---- base Program NCA alone (exit code {code}) ----\n{log}')
checked = '\n'.join(line for line in log.splitlines() if not UNSIGNED.match(line))
if HACTOOL_HASH_FAIL.search(checked):
return DamagedFile(base, f'its game data fails the hash check; see {shown(log_path)}')
if all(re.search(rf'Level {i} \(GOOD\)', log) for i in range(6)):
return DamagedFile(update, f'its game data fails the hash check; see {shown(log_path)}')
return ValueError(f'the game data fails the hash check: the base game file {base} or the update file {update} is '
f'damaged or incomplete. Copy them again from their source and retry; see {shown(log_path)}')
def merge_update_romfs(base, update, keys_path, hactool, output, logs, phase=lambda name: say(MERGE_PHASES[name])):
"""extract_update_romfs.merge() without its Darwin-only bounded view of the base: the base Program NCA is copied out.
Needs room for both Program NCAs (about 17 GB together) and the merged image (about 17 GB). phase(name) is called
as each part of MERGE_PHASES starts.
"""
if output.exists():
shutil.rmtree(output)
tickets, inputs = program_ncas(base, update, keys_path)
sections = [i for i in range(4) if inputs[1][2][0x400 + i * 512 + 4] == 4]
require(len(sections) == 1, 'Expected one BKTR section')
need = inputs[0][1]['size'] + inputs[1][1]['size'] + 18 * (1 << 30)
@@ -160,25 +241,29 @@ def merge_update_romfs(base, update, keys_path, hactool, output, logs):
f.write(f'{rights.hex()} = {key.hex()}\n')
hactool_keys = tmp / 'hactool.keys'
hactool_prod_keys(keys_path, hactool_keys)
say(' copying the base and update Program NCAs')
phase('copy')
base_nca, update_nca = tmp / 'base.nca', tmp / 'update.nca'
for (source, record, _), target in zip(inputs, (base_nca, update_nca)):
copy_range(source, record_offset(record), record['size'], target)
try:
copy_range(source, record_offset(record), record['size'], target)
except ValueError as e: # the file ends inside its Program NCA
raise DamagedFile(source, str(e)) from None
result = tmp / 'result'
result.mkdir()
say(' merging with hactool')
phase('merge')
command = [str(hactool), '-x', '-y', '--disablekeywarns', '--suppresskeys', '-k', str(hactool_keys),
f'--basenca={base_nca}', f'--section{sections[0]}={result / "romfs.bin"}', str(update_nca)]
environment = dict(os.environ, MNG_TITLE_KEYS=str(title_keys))
completed = subprocess.run(command, env=environment, capture_output=True, text=True, errors='replace')
log = LONG_HEX.sub('<hex removed>', completed.stdout + completed.stderr)
log_path.write_text(log)
code, log = run_hactool(command, environment)
log_path.write_text(log, encoding='utf-8')
checked = '\n'.join(line for line in log.splitlines() if not UNSIGNED.match(line))
require(completed.returncode == 0 and not HACTOOL_ERROR.search(checked),
f'hactool merge failed; see {log_path.relative_to(ROOT)}')
if HACTOOL_HASH_FAIL.search(checked):
phase('diagnose')
raise damaged_merge_input(hactool, hactool_keys, environment, base, update, base_nca, log_path)
require(code == 0 and not HACTOOL_ERROR.search(checked), f'hactool merge failed; see {shown(log_path)}')
require(all(re.search(rf'Level {i} \(GOOD\)', log) for i in range(6)), 'hactool did not verify all six IVFC levels')
image = result / 'romfs.bin'
say(' hashing and indexing the merged image')
phase('index')
files = index_romfs(image)
report = dict(status='pass', romfs_size=image.stat().st_size, romfs_sha256=sha256(image), files=files,
integrity='BKTR merge; all six IVFC levels verified')
@@ -190,12 +275,21 @@ def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--config', type=Path, default=ROOT / 'config.local.toml')
parser.add_argument('--hactool', type=Path, default=ROOT / 'toolchain' / 'hactool' / 'hactool.exe')
parser.add_argument('--base', type=Path, help='base game (default: input.base of the config)')
parser.add_argument('--update', type=Path, action='append',
help='an update NSP, repeatable (default: input.updates of the config)')
parser.add_argument('--keys', type=Path, help='prod.keys (default: input.keys of the config)')
parser.add_argument('--work', type=Path, help='output directory with extracted/ and logs/ (default: work.dir of '
'the config, else build)')
args = parser.parse_args()
config = tomllib.loads(args.config.read_text(encoding='utf-8'))
base, keys = Path(config['input']['base']), Path(config['input']['keys'])
updates = config['input'].get('updates', [])
work = ROOT / config.get('work', {}).get('dir', 'build')
config = {}
if not (args.base and args.keys and args.update and args.work):
config = tomllib.loads(args.config.read_text(encoding='utf-8'))
given = config.get('input', {})
base, keys = args.base or Path(given['base']), args.keys or Path(given['keys'])
updates = args.update or given.get('updates', [])
work = args.work or ROOT / config.get('work', {}).get('dir', 'build')
extracted, logs = work / 'extracted', work / 'logs'
logs.mkdir(parents=True, exist_ok=True)
versions = load_expected()
@@ -212,7 +306,7 @@ def main():
report_npdm(base_out / 'exefs', v0)
if not romfs_verified(base_out / 'romfs', v0):
say(' extracting RomFS (17 GB)')
extract_base_romfs(base_out, keys, logs)
extract_base_romfs(base_out, keys, logs, base)
check_romfs(base_out / 'romfs', v0)
for update in updates:
+509
View File
@@ -0,0 +1,509 @@
"""Assemble the release folder of docs/installer.md ("Release layout") from pinned downloads.
.venv-py314\\Scripts\\python tools\\make_release.py --out build/release/TotK-Setup [--zip build/release/TotK-Setup.zip]
.venv-py314\\Scripts\\python tools\\make_release.py --out build/release/TotK-Setup --prove
Needs Python 3.14 (tarfile reads the MSYS2 .zst packages), uv on PATH and network access on the first run. Downloads are
cached in toolchain/release-dl/ and checked against the sha256 in PINS; a mismatch stops the build. The folder holds:
python/ python.org embeddable 3.14.9 + the packages of tools/requirements-installer.txt + tkinter (the Tcl/Tk 9.0
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 (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 git describe of the checkout (else the short hash); the installer's release string
TotK-Setup.exe installer/launcher/launcher.c built with llvm/ (-mwindows): starts python/pythonw.exe -m installer.gui
--prove compiles generated/v121 and links build/release-check/totk.exe with nothing but the folder's python/, llvm/,
runtime/ and bin/ (PATH holds the Windows system directory and those), then runs the four probes there. It needs a
generated/v121 and build/extracted/v121 from the usual game build (they are read, nothing is written to them).
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.
"""
import argparse
import hashlib
import json
import os
import shutil
import struct
import subprocess
import sys
import tarfile
import time
import urllib.request
import zipfile
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import check_content # noqa: E402
ROOT = Path(__file__).resolve().parent.parent
MAIN_ROOT = (ROOT / 'build').resolve().parent # build/, generated/, toolchain/ and refs/ are junctions in a worktree
DL = MAIN_ROOT / 'toolchain' / 'release-dl'
WORK = MAIN_ROOT / 'toolchain' / 'release-work' # unpacked downloads
MSYS2 = 'https://repo.msys2.org/mingw/ucrt64/'
SDL_VERSION = '3.4.18'
LLVM_VERSION = '20261020'
MNG_COMMIT = 'b75be8cfa704df3701e86130c8ced268e79d6b11' # MacNativeGames, the shader list (refs/MacNativeGames)
# name: (url, sha256)
PINS = {
'python-embed': ('https://www.python.org/ftp/python/3.14.9/python-3.14.9-embed-amd64.zip',
'3c01d7cfc666f746a6f064f60fb2156ab0ec464d3d738ab796371a46a9bb24a2'),
'python-installer': ('https://www.python.org/ftp/python/3.14.9/python-3.14.9-amd64.exe',
'a247a0b612244a803f507b39fb7121e3602e4160d59c3b99d98e735289c53394'),
'llvm-mingw': (f'https://github.com/mstorsjo/llvm-mingw/releases/download/{LLVM_VERSION}/'
f'llvm-mingw-{LLVM_VERSION}-ucrt-x86_64.zip',
'fd5462b7f5d93b2ff981fcd3f9c7e427e47f3203c64ea91c27bcc444eebd74cd'),
'sdl3-dll': (f'https://github.com/libsdl-org/SDL/releases/download/release-{SDL_VERSION}/SDL3-{SDL_VERSION}-win32-x64.zip',
'75c2c0fc74e7d1206aaedc22893a35887332f4f85f5c8b74fddc10d177985f2c'),
'sdl3-devel': (f'https://github.com/libsdl-org/SDL/releases/download/release-{SDL_VERSION}/SDL3-devel-{SDL_VERSION}-mingw.tar.gz',
'049b9dd711e3fb3f8d84feb998dfec54a75e17cf539c4a80ff4d058d839771cf'),
'ninja': ('https://github.com/ninja-build/ninja/releases/download/v1.13.2/ninja-win.zip',
'07fc8261b42b20e71d1720b39068c2e14ffcee6396b76fb7a795fb460b78dc65'),
'opus': (MSYS2 + 'mingw-w64-ucrt-x86_64-opus-1.6.1-2-any.pkg.tar.zst',
'bb114bcf26225f50f874497ebfd6b8224fff2a20bda3f8ee45cd07ce5f02657b'),
'libvpx': (MSYS2 + 'mingw-w64-ucrt-x86_64-libvpx-1.17.0-1-any.pkg.tar.zst',
'8ff6919dbb88ea26ce28d22fd81d6f0d6e200cf56a4dd81ffc2d33124fe98024'),
'libvpx-src': ('https://github.com/webmproject/libvpx/archive/refs/tags/v1.17.0.tar.gz', # LICENSE and PATENTS only
'1020f184046187baa2985dbde38e0691f49c44088bca7a1842b0236c6081dc0a'),
'vulkan-headers': (MSYS2 + 'mingw-w64-ucrt-x86_64-vulkan-headers-1~1.4.363.0-1-any.pkg.tar.zst',
'be602b2657cace531316e78b1eda4087e8f7f84ebb877f08fba198a2db06406a'),
'vulkan-loader': (MSYS2 + 'mingw-w64-ucrt-x86_64-vulkan-loader-1~1.4.363.0-1-any.pkg.tar.zst', # import library only
'065076fe13d39ab97eea4740596426d5ae741b5044e48e1c9495d04835c92dbe'),
'vma': (MSYS2 + 'mingw-w64-ucrt-x86_64-vulkan-memory-allocator-3.4.0-1-any.pkg.tar.zst',
'44c81162337915afecda3185f66fd98268a54454580b2717080c9dfef57fe781'),
}
# The component MSI inside the python.org installer that carries Tcl/Tk and tkinter (Burn names its payloads a0..a27).
TCLTK_PAYLOAD = ('a12', 'ac87609db3f715404dd41a989c2f1706ea1512f3d86217f938d11c63af8eb505')
SDL3_DLL_SHA256 = '2b4b128d8d510cc2d442693cf848c050d700d43dc667afe9e5fd82b0570d667e' # SDL3.dll inside the SDL zip
# llvm-mingw: what clang and lld need to compile and link totk.exe for x86_64-w64-mingw32 (dynamic LLVM, libc++, compiler-rt).
LLVM_BIN = {'clang.exe', 'clang++.exe', 'clang-23.exe', 'ld.lld.exe', 'llvm-ar.exe', 'llvm-ranlib.exe', 'llvm-nm.exe',
'llvm-objdump.exe', 'llvm-objcopy.exe', 'llvm-strip.exe', 'llvm-readobj.exe', 'llvm-dlltool.exe',
'llvm-rc.exe', 'llvm-cvtres.exe', 'llvm-windres.exe', 'llvm-wrapper.exe', 'mingw32-common.cfg',
'x86_64-w64-windows-gnu.cfg', 'x86_64-pc-windows-gnu.cfg', 'x86_64-w64-mingw32-clang.exe',
'x86_64-w64-mingw32-clang++.exe', 'libLLVM-23.dll', 'libclang-cpp.dll', 'libc++.dll', 'libunwind.dll',
'libwinpthread-1.dll', 'libffi-8.dll'}
LLVM_SKIP_DIRS = {'aarch64-w64-mingw32', 'armv7-w64-mingw32', 'i686-w64-mingw32', 'busybox', 'python', 'share'}
LLVM_SKIP_SUFFIXES = {'.idl', '.tlb'}
# tools/ files the installer steps run (and what they import), copied from the repo.
TOOLS_FILES = ['build_game.py', 'check_content.py', 'extract.py', 'make_romfs_patches.py', 'prepare_shaders.py',
'romfs_index.py', 'shader_census.py']
TOOLS_DIRS = ['vendor/macnativegames', 'data'] # data/: expected hashes
RUNTIME_SUFFIXES = {'.h', '.inc'}
PROBES = ['--probe-kernel', '--probe-vkexec', '--probe-nvn-tex', '--probe-shader']
def sha256_file(path):
h = hashlib.sha256()
with open(path, 'rb') as f:
for block in iter(lambda: f.read(1 << 20), b''):
h.update(block)
return h.hexdigest()
def fetch(name):
"""The pinned download, from the cache or the network, checked against its sha256."""
url, want = PINS[name]
DL.mkdir(parents=True, exist_ok=True)
path = DL / Path(url.split('?')[0]).name.replace('%7E', '~')
if name == 'libvpx-src':
path = DL / 'libvpx-1.17.0.tar.gz'
if not path.is_file():
print(f'download {url}')
with urllib.request.urlopen(url) as r, open(path, 'wb') as f:
shutil.copyfileobj(r, f)
got = sha256_file(path)
if got != want:
sys.exit(f'{path}: sha256 {got}, pinned {want}')
return path
def unpack(name, member_filter=None):
"""Extract a pinned archive into toolchain/release-work/<name> (once) and return that directory."""
dest = WORK / name
stamp = dest / '.unpacked'
if stamp.is_file():
return dest
shutil.rmtree(dest, ignore_errors=True)
dest.mkdir(parents=True)
path = fetch(name)
if path.suffix == '.zip':
with zipfile.ZipFile(path) as z:
z.extractall(dest)
else:
with tarfile.open(path) as t:
t.extractall(dest, filter='data')
stamp.write_text('')
return dest
def copy_tree(src, dst, skip=lambda rel: False):
"""Copy src into dst file by file (sorted), leaving out what skip(relative posix path) names."""
for p in sorted(src.rglob('*')):
rel = p.relative_to(src)
if p.is_dir() or skip(rel.as_posix()):
continue
(dst / rel).parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(p, dst / rel)
def clean_env(*path_dirs):
"""A process environment without MSYS2 and the user's tools: the Windows system directory and path_dirs only."""
system = os.environ.get('SystemRoot', r'C:\Windows')
keep = ('SYSTEMROOT', 'SYSTEMDRIVE', 'TEMP', 'TMP', 'USERPROFILE', 'LOCALAPPDATA', 'APPDATA', 'COMSPEC', 'PATHEXT',
'NUMBER_OF_PROCESSORS') # os.environ keys are upper case on Windows
env = {k: v for k, v in os.environ.items() if k.upper() in keep}
env['PATH'] = os.pathsep.join([*(str(Path(d)) for d in path_dirs), rf'{system}\System32', system])
return env
def run(cmd, **kw):
print('+', ' '.join(str(c) for c in cmd), flush=True)
return subprocess.run([str(c) for c in cmd], check=True, **kw)
def mib(n):
return f'{n / (1 << 20):.1f} MiB'
def dir_size(p):
return sum(f.stat().st_size for f in Path(p).rglob('*') if f.is_file())
# ---- python/ -------------------------------------------------------------------------------------------------------
def tcltk_files():
"""Directory holding the python.org installer's tcltk payload, unpacked (not installed) into toolchain/release-work."""
dest = WORK / 'tcltk'
if (dest / 'DLLs' / '_tkinter.pyd').is_file():
return dest
exe = fetch('python-installer')
data = exe.read_bytes()
cabs = []
pos = data.find(b'MSCF\0\0\0\0')
while pos >= 0:
cabs.append((struct.unpack_from('<I', data, pos + 8)[0], pos))
pos = data.find(b'MSCF\0\0\0\0', pos + 1)
size, pos = max(cabs) # the attached container holds all component MSIs
shutil.rmtree(WORK / 'tcltk-src', ignore_errors=True)
src = WORK / 'tcltk-src'
src.mkdir(parents=True)
(src / 'container.cab').write_bytes(data[pos:pos + size])
name, want = TCLTK_PAYLOAD
expand = Path(os.environ.get('SystemRoot', r'C:\Windows')) / 'System32' / 'expand.exe'
run([expand, '-F:' + name, src / 'container.cab', src], stdout=subprocess.DEVNULL)
got = sha256_file(src / name)
if got != want:
sys.exit(f'{src / name}: sha256 {got}, pinned {want} (not the tcltk component of python 3.14.9)')
msi = src / (name + '.msi')
(src / name).rename(msi)
shutil.rmtree(dest, ignore_errors=True)
# Administrative image: copies the MSI's files to TARGETDIR, installs nothing and shows no window.
run(['msiexec', '/a', msi, '/qn', f'TARGETDIR={dest}'])
if not (dest / 'DLLs' / '_tkinter.pyd').is_file():
sys.exit(f'{dest}: no _tkinter.pyd after msiexec /a')
return dest
def build_python(out):
py = out / 'python'
with zipfile.ZipFile(fetch('python-embed')) as z:
z.extractall(py)
(py / 'python314._pth').write_text('python314.zip\n.\nLib/site-packages\n..\n../tools\n../recomp\n', encoding='utf-8')
# The embeddable Python ignores the script directory and PYTHONPATH once a ._pth file exists, so the folder's own code
# (installer/, and tools/*.py and recomp/*.py importing their siblings) has to be on the list.
env = dict(os.environ, UV_CACHE_DIR=str(MAIN_ROOT / 'toolchain' / 'uv-cache'))
run(['uv', 'pip', 'install', '--target', py / 'Lib' / 'site-packages', '--python', sys.executable,
'--python-platform', 'windows', '--python-version', '3.14', '--only-binary', ':all:',
'-r', ROOT / 'tools' / 'requirements-installer.txt'], env=env)
shutil.rmtree(py / 'Lib' / 'site-packages' / 'bin', ignore_errors=True) # numpy/cffi console scripts
tk = tcltk_files()
for dll in ('_tkinter.pyd', 'tcl90.dll', 'tcl9tk90.dll', 'zlib1.dll'):
shutil.copy2(tk / 'DLLs' / dll, py / dll)
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; '
'print("python ok: Tcl", tkinter.Tcl().eval("info patchlevel"), "numpy", numpy.__version__)'],
env=clean_env(py), capture_output=True, text=True)
if check.returncode:
sys.exit('bundled python check failed:\n' + check.stderr)
print(check.stdout.strip())
# ---- llvm/ ---------------------------------------------------------------------------------------------------------
def build_llvm(out):
src = unpack('llvm-mingw') / f'llvm-mingw-{LLVM_VERSION}-ucrt-x86_64'
dst = out / 'llvm'
def skip(rel):
top = rel.split('/')[0]
if top in LLVM_SKIP_DIRS:
return True
if top == 'bin':
return rel not in {f'bin/{n}' for n in LLVM_BIN}
if top == 'lib': # lib/clang/<major>/include and the x86_64 Windows builtins; no sanitizers, no other targets
if rel.startswith('lib/clang/') and '/include/' in rel:
return False
return not rel.endswith('/lib/windows/libclang_rt.builtins-x86_64.a')
return Path(rel).suffix.lower() in LLVM_SKIP_SUFFIXES or rel.startswith('include/ddk/')
copy_tree(src, dst, skip)
return dst
# ---- bin/ and deps ---------------------------------------------------------------------------------------------------
def build_bin(out):
b = out / 'bin'
b.mkdir(parents=True, exist_ok=True)
for f in ('totk_xlate.exe', 'totk_xlate.id'):
shutil.copy2(MAIN_ROOT / 'build' / 'xlate' / f, b / f)
shutil.copy2(MAIN_ROOT / 'toolchain' / 'hactool' / 'hactool.exe', b / 'hactool.exe')
dll = unpack('sdl3-dll') / 'SDL3.dll'
if sha256_file(dll) != SDL3_DLL_SHA256:
sys.exit(f'{dll}: not the pinned SDL3.dll')
shutil.copy2(dll, b / 'SDL3.dll')
shutil.copy2(unpack('opus') / 'ucrt64' / 'bin' / 'libopus-0.dll', b / 'libopus-0.dll')
shutil.copy2(unpack('ninja') / 'ninja.exe', b / 'ninja.exe')
def build_deps(out):
"""runtime/deps: the headers and import libraries build_game.py --deps expects (include/, lib/)."""
d = out / 'runtime' / 'deps'
(d / 'include').mkdir(parents=True, exist_ok=True)
(d / 'lib').mkdir(parents=True, exist_ok=True)
sdl = unpack('sdl3-devel') / f'SDL3-{SDL_VERSION}' / 'x86_64-w64-mingw32'
copy_tree(sdl / 'include', d / 'include')
shutil.copy2(sdl / 'lib' / 'libSDL3.dll.a', d / 'lib')
for pkg, dirs, libs in (('opus', ['opus'], ['libopus.dll.a']), ('libvpx', ['vpx'], ['libvpx.a']),
('vulkan-headers', ['vulkan', 'vk_video'], []), ('vulkan-loader', [], ['libvulkan-1.dll.a']),
('vma', ['vma'], [])):
root = unpack(pkg) / 'ucrt64'
for inc in dirs:
copy_tree(root / 'include' / inc, d / 'include' / inc)
for lib in libs:
shutil.copy2(root / 'lib' / lib, d / 'lib')
def collect_licenses(out):
lic = out / 'licenses'
lic.mkdir(parents=True, exist_ok=True)
pairs = [
(unpack('opus') / 'ucrt64' / 'share' / 'licenses' / 'opus' / 'COPYING', 'opus-COPYING.txt'),
(unpack('vulkan-headers') / 'ucrt64' / 'share' / 'licenses' / 'vulkan-headers' / 'LICENSE', 'vulkan-headers-LICENSE.txt'),
(unpack('vulkan-loader') / 'ucrt64' / 'share' / 'licenses' / 'vulkan-loader' / 'LICENSE', 'vulkan-loader-LICENSE.txt'),
(unpack('vma') / 'ucrt64' / 'share' / 'licenses' / 'VulkanMemoryAllocator' / 'LICENSE', 'vulkan-memory-allocator-LICENSE.txt'),
(unpack('sdl3-dll') / 'LICENSE.txt', 'SDL3-LICENSE.txt'),
(MAIN_ROOT / 'toolchain' / 'hactool' / 'LICENSE', 'hactool-LICENSE.txt'),
(MAIN_ROOT / 'refs' / 'MacNativeGames' / 'LICENSE', 'MacNativeGames-LICENSE.txt'),
]
for src, name in pairs:
shutil.copy2(src, lic / name)
with tarfile.open(fetch('libvpx-src')) as t: # the MSYS2 package carries no licence text; the notice needs both
for member, name in (('libvpx-1.17.0/LICENSE', 'libvpx-LICENSE.txt'), ('libvpx-1.17.0/PATENTS', 'libvpx-PATENTS.txt')):
(lic / name).write_bytes(t.extractfile(member).read())
for name in ('LICENSE.TXT',):
shutil.copy2(out / 'llvm' / name, lic / 'llvm-mingw-LICENSE.txt')
# ---- runtime/, recomp/, tools/, data/ ---------------------------------------------------------------------------------
def runtime_lib(out, llvm):
"""Build libtotkrt.a with the folder's llvm/ and copy it, the headers and romfs.c into runtime/."""
build = MAIN_ROOT / 'build' / 'release-check' / 'rtlib'
gen = MAIN_ROOT / 'generated' / 'v121'
run([sys.executable, ROOT / 'tools' / 'build_game.py', '--make-runtime-lib', '--toolchain', llvm,
'--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 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)
shutil.copy2(p, rt / p.relative_to(ROOT / 'runtime'))
shutil.copy2(ROOT / 'runtime' / 'romfs.c', rt / 'romfs.c') # build_game.py INSTALL_SIDE
def build_sources(out):
copy_tree(ROOT / 'recomp', out / 'recomp', lambda r: not r.endswith(('.py', '.txt')) or '__pycache__' in r)
for f in TOOLS_FILES:
(out / 'tools').mkdir(exist_ok=True)
shutil.copy2(ROOT / 'tools' / f, out / 'tools' / f)
for d in TOOLS_DIRS:
copy_tree(ROOT / 'tools' / d, out / 'tools' / d, lambda r: '__pycache__' in r or r.endswith('.md'))
(out / 'data').mkdir(exist_ok=True)
text = subprocess.run(['git', '-C', MAIN_ROOT / 'refs' / 'MacNativeGames', 'show',
f'{MNG_COMMIT}:games/totk/assets/shader_list.tsv'], check=True, capture_output=True).stdout
(out / 'data' / 'shader_list.tsv').write_bytes(text)
copy_tree(ROOT / 'installer', out / 'installer', lambda r: '__pycache__' in r or r.startswith('launcher/'))
shutil.copy2(ROOT / 'totk.ini.example', out / 'totk.ini.example')
shutil.copy2(ROOT / 'THIRD_PARTY_NOTICES.md', out / 'THIRD_PARTY_NOTICES.md')
(out / 'VERSION').write_text(version() + '\n', encoding='utf-8')
def version():
"""git describe of the checkout, else its short hash."""
for args in (['describe', '--tags', '--always'], ['rev-parse', '--short', 'HEAD']):
r = subprocess.run(['git', '-C', ROOT, *args], capture_output=True, text=True)
if r.returncode == 0 and r.stdout.strip():
return r.stdout.strip()
sys.exit('no git version: not a git checkout')
# ---- TotK-Setup.exe --------------------------------------------------------------------------------------------------
def c_wide(text):
"""A C wide string literal of text: ASCII as is, everything else as a universal character name."""
bs = chr(92)
out = []
for ch in text:
if ch in (bs, '"'):
out.append(bs + ch)
elif 32 <= ord(ch) < 127:
out.append(ch)
elif ord(ch) < 0x10000:
out.append(f'{bs}u{ord(ch):04x}')
else:
sys.exit(f'launcher text: {ch!r} is outside the BMP')
return 'L"' + ''.join(out) + '"'
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'):
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])}')
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('\n'.join(lines) + '\n', encoding='ascii')
def build_launcher(out, llvm):
work = MAIN_ROOT / 'build' / 'release-check' / 'launcher'
launcher_header(work / 'launcher_text.h')
run([llvm / 'bin' / 'clang.exe', '-O2', '-mwindows', '-Wall', '-Wl,-s', '-I', work,
ROOT / 'installer' / 'launcher' / 'launcher.c', '-o', out / 'TotK-Setup.exe'], env=clean_env(llvm / 'bin'))
# ---- checks ----------------------------------------------------------------------------------------------------------
def check_folder(out):
"""check_content over every file of the release folder; a hit stops the build."""
bad = []
for p in sorted(out.rglob('*')):
if p.is_file():
bad += [f'{p.relative_to(out)}: {problem}' for problem in check_content.problems_for(p, allow_large=True)]
if bad:
sys.exit('game-derived content in the release folder:\n' + '\n'.join(bad))
print(f'check_content: {sum(1 for p in out.rglob("*") if p.is_file())} files, no hit')
def check_bundle(out):
"""The folder's own python runs the installer without game files and the window's selftest."""
chk = MAIN_ROOT / 'build' / 'release-check' / 'bundle'
shutil.rmtree(chk, ignore_errors=True)
env = clean_env(out / 'python')
py = out / 'python' / 'python.exe'
r = subprocess.run([py, '-m', 'installer', '--lang', 'en-US', '--dest', chk / 'install', '--temp', chk / 'temp'],
cwd=out, env=env, capture_output=True, text=True, timeout=600)
events = [json.loads(line) for line in r.stdout.splitlines() if line.startswith('{')]
errors = [e['id'] for e in events if e['type'] == 'error']
started = any(e['type'] == 'start' and e.get('release') == (out / 'VERSION').read_text().strip() for e in events)
steps = [e['phase'] for e in events if e['type'] == 'progress']
if errors != ['check.inputs_needed'] or not started or 'inputs' not in steps:
sys.exit(f'bundle check: exit {r.returncode}, errors {errors}, release read {started}\n{r.stdout[-2000:]}\n{r.stderr[-2000:]}')
print(f'bundle check: the installer ran the host check from the folder and stopped at the game files (exit {r.returncode})')
r = subprocess.run([py, '-m', 'installer.gui', '--selftest'], cwd=out, env=env, capture_output=True, text=True, timeout=300)
if r.returncode:
sys.exit(f'bundle check: window selftest exit {r.returncode}\n{r.stdout[-2000:]}\n{r.stderr[-2000:]}')
print('bundle check: the window built every screen in both languages and closed')
def sizes(out):
rows = [(p.name if p.is_dir() else p.name, dir_size(p) if p.is_dir() else p.stat().st_size) for p in sorted(out.iterdir())]
for name, n in rows:
print(f' {name:26} {mib(n):>12}')
print(f' {"total":26} {mib(sum(n for _, n in rows)):>12}')
def write_zip(out, path):
path.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED, compresslevel=6) as z:
for p in sorted(out.rglob('*')):
if p.is_file():
info = zipfile.ZipInfo((Path(out.name) / p.relative_to(out)).as_posix(), (1980, 1, 1, 0, 0, 0))
info.compress_type = zipfile.ZIP_DEFLATED
info.external_attr = 0o644 << 16
z.writestr(info, p.read_bytes(), compresslevel=6)
print(f'{path}: {mib(path.stat().st_size)}')
def prove(out):
"""Compile generated/v121 and link totk.exe with the folder's tools only, then run the probes next to it."""
out = out.resolve()
gen = MAIN_ROOT / 'generated' / 'v121'
ext = MAIN_ROOT / 'build' / 'extracted' / 'v121'
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',
'--ninja', out / 'bin' / 'ninja.exe', '--gen', gen, '--out', chk, '--exefs', ext / 'exefs', '--romfs', ext / 'romfs',
'--profile', chk / 'profile'], env=env)
print(f'prove: build {time.time() - t0:.0f} s')
for dll in ('SDL3.dll', 'libopus-0.dll'):
shutil.copy2(out / 'bin' / dll, chk / dll)
failed = []
for probe in PROBES:
r = subprocess.run([str(chk / 'totk.exe'), probe], cwd=chk, env=clean_env(chk), capture_output=True, text=True,
timeout=900)
tail = (r.stdout.strip().splitlines() or [''])[-1]
print(f'{probe}: exit {r.returncode}: {tail}')
if r.returncode:
failed.append(probe)
if failed:
sys.exit(f'probes failed: {failed}')
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
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')
args = ap.parse_args()
out = args.out.resolve()
if args.prove:
prove(out)
return
if out.exists():
shutil.rmtree(out)
out.mkdir(parents=True)
build_python(out)
llvm = build_llvm(out)
build_bin(out)
build_deps(out)
runtime_lib(out, llvm)
build_sources(out)
build_launcher(out, llvm)
collect_licenses(out)
check_folder(out)
check_bundle(out)
sizes(out)
if args.zip:
write_zip(out, args.zip.resolve())
if __name__ == '__main__':
main()
+78 -1
View File
@@ -1,6 +1,9 @@
"""Condense the TOTK_TRACE=perf and TOTK_TRACE=sched lines of a game log over a frame window.
python tools/perf_summary.py build/v121/logs/fight.log 1560 2600
`mem: dirty` lines (vk_memory.c dirty census) are summarised per pool class (CPU_CACHED, CPU_UNCACHED, other): what
a device-local mirror of buffer-bound pools would upload per frame and per submit, FlushMappedRange use, mirror size.
"""
import re
import statistics
@@ -8,10 +11,56 @@ import sys
PERF = re.compile(r'perf: frame (\d+): interval avg ([\d.]+) ms max ([\d.]+) ms \(([\d.]+) fps\); render thread blocked: '
r'pacing (\d+)%.*?pipeline build (\d+)%.*?texture (\d+)%')
GPU = re.compile(r'perf: frame (\d+): gpu avg (?:([\d.]+) ms|n/a).*?fence wait per frame: render thread ([\d.]+) ms, '
r'other threads ([\d.]+) ms; vram (?:([\d.]+) of ([\d.]+) MiB|n/a).*?ram working set ([\d.]+) MiB, '
r'private ([\d.]+) MiB; bound: (\w+)')
SCHED = re.compile(r'sched: frame (\d+): (.*)')
THREAD = re.compile(r'sched: (render )?(0x[0-9a-f]+) (p\d+ m\d+) run (\d+)% core (\d+)% host (\d+)% sleep (\d+)% '
r'sync (\d+)% cv (\d+)% lock (\d+)% addr (\d+)% cpu (\d+)% blocks (\d+) preempted (\d+)')
RWAIT = re.compile(r'sched: render wait (\d): (.*)')
DIRTY = re.compile(r'mem: dirty frame (\d+): (.*)')
DIRTY_FRAMES = re.compile(r'mem: dirty frames KiB [\w/]+:(.*)')
MEMCLASS = re.compile(r'mem: (class .*)')
CLASSES = ('cached', 'uncached', 'other')
def dirty_fields(text):
"""`key a/b/c` and `key x` pairs of a `mem: dirty frame` line -> {key: [floats]}."""
return {k: [float(x) for x in v.split('/')] for k, v in re.findall(r'(\w+) ([\d.]+(?:/[\d.]+)*)(?=[ ;]|$)', text)}
def p95(v):
v = sorted(v)
return v[min(len(v) - 1, int(0.95 * len(v)))] if v else 0.0
def dirty_summary(lines, frames):
"""lines: [(frame, fields)] of the window; frames: per-frame eager KiB triples."""
if not lines:
return
n = sum(f['frames'][0] for _, f in lines)
def avg(key, i): # frame-weighted mean of a per-frame value
return sum(f[key][i] * f['frames'][0] for _, f in lines if key in f) / n
for i, c in enumerate(CLASSES):
per = [t[i] / 1024 for t in frames]
print(f'SUMMARY dirty {c}: eager MiB/frame avg {avg("eager_mib", i):.3f} p95 {p95(per):.3f} '
f'max {max(per, default=0):.3f}; lazy avg {avg("lazy_mib", i):.3f}; first use avg {avg("first_mib", i):.3f}; '
f'FlushMappedRange pool {avg("flush_pool_calls", i):.1f} calls {avg("flush_pool_mib", i):.3f} MiB, '
f'buffer {avg("flush_buf_calls", i):.1f} calls {avg("flush_buf_mib", i):.3f} MiB per frame')
tot = [sum(t) / 1024 for t in frames]
subs = avg('submits_per_frame', 0)
last = lines[-1][1]
print(f'SUMMARY dirty all: eager MiB/frame avg {sum(avg("eager_mib", i) for i in range(3)):.3f} p95 {p95(tot):.3f} '
f'max {max(tot, default=0):.3f}; submits/frame {subs:.2f}; per submit avg '
f'{sum(f["avg_mib"][0] * f["frames"][0] for _, f in lines) / n:.3f} MiB max '
f'{max(f["max_mib"][0] for _, f in lines):.3f} MiB')
mirror = ', '.join(f'{c} {int(last["live_pools"][i])} pools {last["live_mib"][i]:.1f} MiB (ever '
f'{int(last["ever_pools"][i])} / {last["ever_mib"][i]:.1f} MiB)' for i, c in enumerate(CLASSES))
print(f'SUMMARY mirror at frame {lines[-1][0]}: {mirror}; untracked {last["untracked_mib"][0]:.1f} MiB')
print(f'SUMMARY dirty census cost: {avg("queries_per_frame", 0):.1f} write-watch queries, '
f'{avg("scan_ms_per_frame", 0):.3f} ms per frame')
def main():
@@ -19,16 +68,37 @@ def main():
sys.exit(__doc__)
lo = int(sys.argv[2]) if len(sys.argv) > 2 else 1560
hi = int(sys.argv[3]) if len(sys.argv) > 3 else 10**9
fr, fps, mx = None, [], []
fr, fps, mx, gpu, vram, ram, bound = None, [], [], [], [], [], {}
dirty, dframes, dlast = [], [], None
for line in open(sys.argv[1], encoding='utf-8', errors='replace'):
if line.startswith('run_game:'):
print(line.rstrip())
elif m := MEMCLASS.search(line):
print(m[1])
elif m := DIRTY.search(line):
dlast = int(m[1])
if lo < dlast <= hi:
dirty.append((dlast, dirty_fields(m[2])))
elif m := DIRTY_FRAMES.search(line):
if dlast is not None and lo < dlast <= hi:
dframes += [tuple(float(x) for x in t.split('/')) for t in m[1].split()]
elif m := PERF.search(line):
f = int(m[1])
if lo < f <= hi:
fps.append(float(m[4]))
mx.append(float(m[3]))
print(f'perf {f}: {m[4]} fps max {m[3]} pacing {m[5]}% pipe {m[6]}% tex {m[7]}%')
elif m := GPU.search(line):
f = int(m[1])
if lo < f <= hi:
if m[2]:
gpu.append(float(m[2]))
if m[5]:
vram.append((float(m[5]), float(m[6])))
ram.append((float(m[7]), float(m[8])))
bound[m[9]] = bound.get(m[9], 0) + 1
print(f' gpu {f}: {m[2] or "n/a"} ms, fence wait render {m[3]} other {m[4]} ms, '
f'vram {m[5] or "n/a"} MiB, ram ws {m[7]} private {m[8]} MiB, bound {m[9]}')
elif m := SCHED.search(line):
fr = int(m[1])
if lo < fr <= hi:
@@ -41,6 +111,13 @@ def main():
if fps:
print(f'SUMMARY frames {lo}-{hi}: mean fps {statistics.mean(fps):.1f}, min window fps {min(fps):.1f}, '
f'max frame {max(mx):.1f} ms')
if ram:
g = f'gpu mean {statistics.mean(gpu):.2f} ms' if gpu else 'gpu n/a'
v = f'vram max {max(u for u, _ in vram):.0f} of {vram[-1][1]:.0f} MiB' if vram else 'vram n/a'
b = ', '.join(f'{k} {n}' for k, n in sorted(bound.items()))
print(f'SUMMARY gpu: {g}, {v}, ram ws max {max(w for w, _ in ram):.0f} MiB, '
f'private max {max(p for _, p in ram):.0f} MiB; bound windows: {b}')
dirty_summary(dirty, dframes)
if __name__ == '__main__':
+17 -5
View File
@@ -16,10 +16,12 @@ pipeline once, so the GPU driver's own disk cache holds them (vk_pipeline.c "sta
a 12-core machine (inventory 1, translation 3, library builds 0.6) and 1.3 GB of shader cache; the seed files can be
deleted afterwards.
Game-derived output stays in the build directory (ignored). The list is read from refs/ at preparation time; nothing
of it is copied into the repository. Needs Python 3.14 (compression.zstd), so run it in .venv-py314:
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; 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:
.venv-py314\\Scripts\\python tools\\prepare_shaders.py build\\v121 [--version v121] [--list <tsv>] [--no-run]
[--romfs DIR --exefs DIR] # instead of build\\extracted\\<version>
"""
import argparse
import hashlib
@@ -50,9 +52,16 @@ CONTROL = 0x880
ANS_REC = 4 + 32 + 32 + 147 + 1
BUNDLED_LIST = ROOT / 'tools' / 'data' / 'shader_list.tsv' # the installer archive carries a copy (no git, no refs/)
if not BUNDLED_LIST.is_file():
BUNDLED_LIST = ROOT / 'data' / 'shader_list.tsv' # release folder layout (tools/make_release.py)
def read_list(path):
if path:
return Path(path).read_text(encoding='utf-8')
if BUNDLED_LIST.is_file():
return BUNDLED_LIST.read_text(encoding='utf-8')
return subprocess.run(['git', '-C', str(ROOT / 'refs' / 'MacNativeGames'), 'show', ':'.join(LIST_REF)],
check=True, capture_output=True, encoding='utf-8').stdout
@@ -85,7 +94,8 @@ def answers(deps, stage, control):
"""GfxShaderAnswers in gfx_shader.c pack_answers layout (147 bytes) from the list's deps JSON."""
tex, img, fmt, attr, fout = bytearray(32), bytearray(32), bytearray(32), bytearray(32), bytearray(8)
for slot, t in deps.get('tex', {}).items():
tex[int(slot)] = TEX.index(t.removesuffix('_depth'))
t = t.removesuffix('_depth')
tex[int(slot)] = TEX.index('2d' if t == '*' else t) # "*": any kind, translated as the plain 2D it stands for
for slot, o in deps.get('img', {}).items():
img[int(slot)] = TEX.index(o['type'])
fmt[int(slot)] = FMT.index(o['fmt']) if o.get('fmt') in FMT else 0
@@ -111,13 +121,15 @@ def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('build', type=Path, help='build directory holding totk.exe (build\\v121)')
ap.add_argument('--version', default='v121', help='build/extracted/<version>')
ap.add_argument('--list', help='a shader_list.tsv instead of the one in refs/MacNativeGames')
ap.add_argument('--romfs', type=Path, help='RomFS directory (default build/extracted/<version>/romfs)')
ap.add_argument('--exefs', type=Path, help='ExeFS directory (default build/extracted/<version>/exefs)')
ap.add_argument('--list', help='a shader_list.tsv instead of tools/data/shader_list.tsv or refs/MacNativeGames')
ap.add_argument('--no-run', action='store_true', help='write the seed files only')
a = ap.parse_args()
t0 = time.monotonic()
variants, prio = parse_list(read_list(a.list))
extracted = ROOT / 'build' / 'extracted' / a.version
shaders, _ = inventory(extracted / 'romfs', extracted / 'exefs')
shaders, _ = inventory(a.romfs or extracted / 'romfs', a.exefs or extracted / 'exefs')
t1 = time.monotonic()
progs, recs, skipped = {}, [], {'not in this version': 0, 'stage': 0}
for key, deps in variants:
+55 -2
View File
@@ -1,10 +1,14 @@
"""Report for a TOTK_PROFILE sampling profile (runtime/prof_win.c): top functions and the guest/runtime/outside split.
usage: prof_report.py <profile> <totk.map> [--top 40]
usage: prof_report.py <profile> <totk.map> [--top 40] [--chains 30]
The map is the lld one from the link (build/<version>/totk.map); its addresses are RVAs, the profile header carries the
runtime image base, so ASLR does not matter. Symbols in the map are labels without sizes: a Rip belongs to the nearest
symbol at or below it.
A profile taken with TOTK_PROFILE_RENDER=1 or TOTK_PROFILE_CHAINS=1 also has call chains: the report then adds the inclusive time per
function (a function counted once per chain), and for each module outside the exe the exe functions that call into it
and the longest common chains down to it.
"""
import argparse
import bisect
@@ -28,7 +32,7 @@ def load_map(path):
def load_profile(path):
prof = {'rips': [], 'modules': [], 'threads': [], 'samples': 0, 'base': 0}
prof = {'rips': [], 'modules': [], 'threads': [], 'stacks': [], 'samples': 0, 'base': 0}
for line in Path(path).read_text().splitlines():
p = line.split(None, 3)
if not p:
@@ -43,20 +47,67 @@ def load_profile(path):
prof['modules'].append((int(p[1], 16), int(p[2], 16), p[3]))
elif p[0] == 'rip':
prof['rips'].append((int(p[1], 16), int(p[2])))
elif p[0] == 'stack':
f = line.split()
prof['stacks'].append((int(f[1]), [int(x, 16) for x in f[2:]]))
return prof
def report_chains(prof, name_of, in_exe, top):
stacks = prof['stacks']
total = sum(n for n, _ in stacks) or 1
print(f'\ncall chains: {total} samples in {len(stacks)} chains (render thread, or every thread with TOTK_PROFILE_CHAINS)')
incl = collections.Counter()
for n, chain in stacks:
for f in set(name_of(r) for r in chain):
incl[f] += n
print(f'\ninclusive, top {top} (% of chain samples)')
for f, n in incl.most_common(top):
print(f' {100 * n / total:6.2f}% {n:8d} {f}')
# Per outside module: the innermost exe frame of each chain whose leaf is in that module
by_mod = collections.defaultdict(collections.Counter)
paths = collections.defaultdict(collections.Counter)
for n, chain in stacks:
if in_exe(chain[0]):
continue
mod = name_of(chain[0]).split('!')[0]
i = next((k for k, r in enumerate(chain) if in_exe(r)), None)
if i is None:
by_mod[mod]['(no exe frame in the chain)'] += n
continue
by_mod[mod][name_of(chain[i])] += n
paths[mod][' < '.join(name_of(r) for r in chain[i:i + 4])] += n
for mod, c in sorted(by_mod.items(), key=lambda kv: -sum(kv[1].values())):
m = sum(c.values())
print(f'\n{mod}: {100 * m / total:.2f}% of chain samples; exe callers')
for f, n in c.most_common(12):
print(f' {100 * n / total:6.2f}% {n:8d} {f}')
for pth, n in paths[mod].most_common(6):
print(f' {100 * n / total:6.2f}% {pth}')
def main():
ap = argparse.ArgumentParser()
ap.add_argument('profile')
ap.add_argument('map')
ap.add_argument('--top', type=int, default=40)
ap.add_argument('--chains', type=int, default=30, help='inclusive functions listed from the call chains')
a = ap.parse_args()
prof = load_profile(a.profile)
addrs, names = load_map(a.map)
base = prof['base']
exe = next((m for m in prof['modules'] if m[0] == base), None)
exe_end = base + exe[1] if exe else base
def in_exe(rip):
return base <= rip < exe_end
def name_of(rip):
if in_exe(rip):
i = bisect.bisect_right(addrs, rip - base) - 1
return names[i] if i >= 0 else '?'
m = next((m for m in prof['modules'] if m[0] <= rip < m[0] + m[1]), None)
return f'{Path(m[2]).name}!+{rip - m[0]:#x}' if m else 'unknown'
total = sum(n for _, n in prof['rips']) or 1
funcs = collections.Counter()
cats = collections.Counter()
@@ -83,6 +134,8 @@ def main():
print('\nthreads (samples, % of them inside the exe)')
for tid, n, ie in sorted(prof['threads'], key=lambda t: -t[1])[:12]:
print(f' tid {tid:6d} {n:8d} {100 * ie / max(n, 1):5.1f}% in exe')
if prof['stacks']:
report_chains(prof, name_of, in_exe, a.chains)
if __name__ == '__main__':
+7
View File
@@ -0,0 +1,7 @@
# 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.
numpy==2.5.4
lz4==4.4.5
cryptography==50.0.2
cffi==2.1.1
pycparser==3.11
+18
View File
@@ -12,6 +12,11 @@
# Fix for physics that bounce at high frame rates.
# highfps_fix = 1
[performance]
# A thread waiting for a virtual core interrupts the owner when it runs game code (0 = off, the old scheduling).
# async_preempt = 1
# Replay NVN submissions on their own thread, at most one frame behind the game (1 = on).
# replay_async = 0
[display]
# Presentation mode: mailbox (default when the driver offers it, vsync otherwise), fifo (vsync) or immediate.
# vk_present = mailbox
@@ -43,6 +48,19 @@
# rumble_strength = 130
# rumble_curve = 0.6
# rumble_triggers = 0
# Mouse camera sensitivity (0 to 10; 0 = no mouse camera and no cursor capture).
# mouse_sens = 1
# Remap buttons, one line per Switch button: bind_<A B X Y L R ZL ZR PLUS MINUS LEFT UP RIGHT DOWN LCLICK RCLICK> =
# comma separated pad:<name> and key:<name> entries, or none. A line replaces that button's defaults (pad and keyboard);
# buttons without a line keep them. Invalid entries are logged and skipped.
# pad names: south east west north l r zl zr start back lstick rstick dpad_left dpad_up dpad_right dpad_down
# key names: a-z 0-9 space tab lshift lctrl up down left right mouse_l mouse_r mouse_m
# Defaults: A = pad:east, key:E. B = pad:south, key:lshift. X = pad:north, key:space. Y = pad:west, key:mouse_l.
# A pad that reports A/B and X/Y swapped (some GuliKit models): swap them back, keyboard keys stay.
# bind_A = pad:south, key:E
# bind_B = pad:east, key:lshift
# bind_X = pad:west, key:space
# bind_Y = pad:north, key:mouse_l
[files]
# Folder with RomFS mod files that replace or add game files. Default: none.