Skip to content

feat(mkw): support NUP / v917504 builds, fix macOS private-tex NaN tint, v917504 VP9 codec and 2K/1080p toggle - #2394

Open
Ting2024 wants to merge 1 commit into
ly05010419:mainfrom
Ting2024:feat/mkw-nup-v917504
Open

Ting2024 wants to merge 1 commit into
ly05010419:mainfrom
Ting2024:feat/mkw-nup-v917504

Conversation

@Ting2024

Copy link
Copy Markdown

Summary

This PR adds `.nup` container import and runtime support for **Mario Kart World v917504**
(`0400c3f00006e000`), fixes the macOS full-screen green 3D LUT corruption and Grand Prix
race-entry VP9 codec crash, and exposes a 2K Docked / 1080p Handheld switch in the macOS
settings panel. All changes are self-contained under `games/mkw/`.

### Changes

1. **Switch 2 `.nup` import & `v917504` build/rewrite support**
   - Copied the MIT `nup` reader from `games/oot/third_party/nup` into
   `games/mkw/third_party/nup` (documented in `games/mkw/tools/SOURCE_NOTES.md`) and added
   `games/mkw/scripts/import_nup.py` to extract verified ExeFS modules and pack a
   16-byte-aligned `romfs.bin` into the work folder.
   - Updated `games/mkw/scripts/build_mkw.py` to accept `.nup` inputs alongside `.nsp` /
   `--dump`, materialize symlinked shared sources (outside `games/mkw`) cleanly into the work
   tree, rewrite multi-module builds (`main`, `subsdk*`, `sdk`), and generate
   `build/mkw_game_version.h` so `TOTK_GAME_VERSION` (`games/mkw/runtime/game_version.h`)
   reports the imported content version.
   - Updated `games/mkw/third_party/d2r/patch_nso.py` and `structck.py` for `v917504` code
   patterns and multi-module relocation/rewriting.

2. **`v917504` runtime, HLE & NVN2 additions**
   - `games/mkw/runtime/loader.c`: support weak symbol interposition fallback when resolving
   rewritten module imports.
   - `games/mkw/third_party/d2r_gfx/gfx/nvn/hle_nvn.c`: bump MKW NVN2 minor version integer
   (`[0x01] = 39`, required by `v917504`), and implement `nvnNativeImageInitialize` /
   `nvnNativeImageFinalize` / `nvnTextureInitializeFromNativeImage` backed by host-visible
   `NVNmemoryPool`s.
   - `games/mkw/runtime/gfx_bridge.c`: add `nn::gs::sdk::CheckGraphicsSystemInitialized`.
   - `games/mkw/runtime/frontend.m`: stub Switch 2 `nn::vi` HDR / VRR queries and metadata
   setters (HDR reported off).
   - `games/mkw/runtime/input.c`: add `Jcms` and `Nmouse` stubs needed at startup.
   - `games/mkw/runtime/offline_host.c`: stub offline `nn::chat` functions and handle
   `camera:ctx` IPC (`cmd 3 -> 0xdc4` no camera connected, `cmd 21 ->` never-signaled
   availability event, deduplicated logging for other unhandled commands).

3. **Fix full-screen neon-green 3D LUT corruption on macOS (`nvn_mtl.m`)**
   - On macOS (tested on macOS 27.2.1 / Apple M1 Max), newly allocated `MTLStorageModePrivate`
   color textures can contain non-zero sentinel bytes (`0xffff 0000 ffff ffff`, which decodes as
   `-NaN, 0.0, -NaN, -NaN` in `RGBA16Float`).
   - MKW renders its two `32x32x32` `RGBA16Float` 3D color-grading LUTs in 8-MRT slices with
   `blend_en = 0xfe` (`SrcAlpha / OneMinusSrcAlpha`, `alpha = 1.0`) without clearing them first;
   blending `c1..c7` against sentinel `-NaN` turns R/B/A into `qNaN`, tinting the entire 3D
   scene bright neon green while keeping G valid.
   - Added `pf_color_bpp()` and `tex_zero_new()` in
   `games/mkw/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m` using a single resident 1 MiB zero
   `MTLBuffer` to zero-initialize newly created non-mirrored GPU-only uncompressed color
   textures before use (`TOTK_ZERO_NEW_TEX=0` disables it).

4. **Fix Grand Prix race-entry crash (`codec_host.c`)**
   - `v917504` uses updated Switch 2 SDK `nn::codec` VP9 symbols and
   `nn::gs::TryWaitNativeSync`, so `subsdk0` previously fell through to `sdk.elf`'s original
   `InitializeVp9DecoderByTransferMemory` and hit `Break`.
   - Added host overrides for `MakeDecodeImagePreference` (`ImageTilingMode` variant), 5-arg
   `InitializeDecoderImagePool`, RGBA `CalculateDecoderImagePoolBuffer`,
   `ReadDecoderOutputImageRgba` (NV12 -> RGBA8888), `InitializeVp9DecoderByTransferMemory` /
   `ByRemoteMemory` (`0x260` struct zeroing), 1-arg `GetVp9DecoderSubmissionReadyEvent`, 8-arg
   `GetVp9DecoderOutputBufferProperties`, and `nn::gs::TryWaitNativeSync`.

5. **In-game 2K Docked ↔ 1080p Handheld switch**
   - Wired `operation-mode.txt` persistence (`runtime/runtime.h`, `runtime/services.c`) into
   `games/mkw/runtime/settings_panel.h` (`Resolution (from the next launch)` row in the key-`1`
   panel) so players can toggle between **2K (2560×1440 Docked)** and **1080p (1920×1080
   Handheld)** when `TOTK_OPERATION_MODE` is not pinned via environment variables.

## Testing & Verification

- **Hardware / OS**: MacBook Pro, Apple M1 Max (32 GB), macOS 27.2.1.
- **Game build tested**: Mario Kart World `v917504` (`Mario Kart World
[0400C3F00006E000][v917504].nup`), built with `scripts/build_mkw.py` and verified with `codesign
--verify --strict --verbose=2`.
- **Live macOS gameplay verification**:
  - Verified normal colors across the loading map, 3D title screen, menus, and in-race flyover
  (raw 3D LUT dump verified `0` NaNs across all 32 `z` slices).
  - Verified entering Grand Prix (Mushroom Cup / Mario Bros. Circuit) through VP9 video playback
  (`[codec] VP9 2560x1440`, 1423 frames decoded + `EOS`) into active 3D race rendering (frame
  2061, no crash).
  - Verified `TOTK_OPERATION_MODE=handheld` outputs `1920x1080` (`WindowCrop 0,0 1920x1080`).
  - Note: first-time shader translation and pipeline compilation still stutter until cached, and
  known upstream vehicle/ramp flickering remains unchanged.
- **Automated checks & compile**:
  - `make VERSION=mkw binary` on this branch (`feat/mkw-nup-v917504`): compiled cleanly with
  `-Wall -Wextra -Werror`.
  - `.githooks/pre-commit`: passed.
  - `python -m unittest discover -s games/mkw/tests -v`: 8/8 passed.
  - `python -m unittest tests.test_one_folder_rule tests.test_launcher_nup
  tests.test_launcher_identify tests.test_settings_panel -v`: 45 ran, 0 failed, 3 skipped
  (requires external `node` / `SHADER_INBOX_DIR`).
- **iOS**: Not built or tested (`games/mkw` is macOS-only).

…P9 codec and 2K/1080p toggle

- Add Switch 2 NUP container import (scripts/import_nup.py, third_party/nup copied from games/oot/third_party/nup) and v917504 multi-module rewrite/build support in build_mkw.py, patch_nso.py, structck.py, and game_version.h.
- Handle v917504 runtime requirements: loader.c weak interposition fallback, NVN2 minor 39 + nvnNativeImage* HLE in hle_nvn.c, nn::gs::sdk::CheckGraphicsSystemInitialized in gfx_bridge.c, nn::vi HDR/VRR stubs in frontend.m, Nmouse/Jcms stubs in input.c, and offline nn::chat + camera:ctx IPC in offline_host.c.
- Fix full-screen neon-green 3D LUT corruption on macOS (nvn_mtl.m): zero-initialize newly allocated GPU-only private color textures (TOTK_ZERO_NEW_TEX=0 to disable) so unblended/partial-MRT 3D LUT slices do not blend against sentinel -NaN pixels.
- Fix Grand Prix race-entry Break in codec_host.c by overriding v917504 nn::codec VP9 APIs (ImageTilingMode preference, 5-arg pool init, RGBA readback, ByTransferMemory/ByRemoteMemory 0x260 struct size, 1-arg submission event, 8-arg properties) and nn::gs::TryWaitNativeSync.
- Expose 2K Docked (2560x1440) / 1080p Handheld (1920x1080) switch in MKW Settings Panel (key 1, persisted via operation-mode.txt, applies on next launch).
@ly05010419

Copy link
Copy Markdown
Owner

Thanks for this — NUP input and v917504 are very welcome. No game data, no translator change and no new downloads, all good. A few things have to change before we can merge it:

  1. Pin v917504 like 1.0.0. Add games/mkw/assets/expected-v917504.json with the SHA-256 of every ExeFS module, romfs.bin and the rewritten modules, and check against it on the NUP path. Today step_code_nup compares the ExeFS with the manifest it just extracted from the same NUP (build_mkw.py around line 266), reuses rewritten modules when structck passes, and pick_package accepts any version, so any MKW NUP builds and the result cannot be matched to a known input. Unknown versions should be refused.
  2. The NaN tint. Zero-filling every new GPU-only colour texture (tex_zero_new) assumes the pool bytes under it are zero. On NVN a texture's contents are the bytes of its memory pool, so the faithful fix is to initialise the new Metal texture from those bytes. Please either show the pool bytes of the LUT that tinted (they should be zero if zero-fill is right) or initialise from the pool. Also please measure the cost: one run with TOTK_ZERO_NEW_TEX on and one off (load time and long frames), and say whether the synchronous wait when the texture is made off the submit thread (nvn_mtl.m around line 4087) can stall.
  3. 1.0.0. The NVN minor version (16 → 39 in hle_nvn.c) and the weak-symbol resolution order in loader.c change 1.0.0 too. Please say whether 1.0.0 was replayed after this; if not, keep them per version.
  4. structck now accepts a first instruction that only moves registers as call setup. Please list the call sites that need it and why they are safe.
  5. Tests. A small synthetic NUP for import_nup.py (good package, wrong version or title, bad checksum), the pinned-hash check on the NUP path (a wrong hash must fail), the loader accepting 2–8 modules by name, and the zero-fill slice arithmetic (3D, arrays, large textures).
  6. Docs. games/mkw/README.md still says 1.0.0 NSP only; SOURCE_NOTES.md should say scripts/import_nup.py is adapted from games/oot/scripts/import_nup.py; and the version string should follow the 1.0.0 format (it ends up in Info.plist and TOTK_GAME_VERSION).

Once these are in and CI passes we will merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants