Skip to content

nvn/present: native HDR10 output for Ocarina of Time - #1

Closed
lypiut wants to merge 1 commit into
mainfrom
feature/oot-native-hdr
Closed

lypiut wants to merge 1 commit into
mainfrom
feature/oot-native-hdr

Conversation

@lypiut

@lypiut lypiut commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Native HDR10 with one SDR/HDR upscaling setting

Branch feature/oot-native-hdr, draft PR. Head 87a553c5e22e3ecee6b3e498026fd1d7cdc0ebe3; reviewed from 1af842919. No production code changed in the final documentation pass.

1. Summary

Ocarina of Time (Switch 2) renders HDR itself, but nxnative previously always answered SDR so the game never drew its HDR picture. This PR answers HDR status from the game's window screen, presents the game's RGB10A2 / BT.2100 PQ window unchanged in a PQ CAMetalLayer, and makes the existing Upscaling → Standard / MetalFX choice work in both SDR and native HDR10. Standard (bilinear) remains the default; MetalFX is an explicit player choice. No new launcher setting, second renderer, ScreenCaptureKit or production CAEDRMetadata.

2. Architecture

Game RGB10A2 / BT.2020 / PQ  ->  optional headroom shoulder  ->  RGB10A2 / BT.2100 PQ drawable  ->  macOS HDR display
  • Capability: potential EDR headroom > 1.0 on the window's own NSScreen (Apple's documented capability test), with a 500 ms hold so moving between screens does not flip the layer. Current headroom drives the game's peak and the shoulder. No display allowlist or model-name check.

  • VI handshake: output format 1, 100-nit paper white, peak = paper white × current headroom in 10-nit steps with a one-second hold, capped at 1600. The game then switches its own window to format 0x3d (RGB10A2). The PQ layer is kept until two consecutive non-PQ presents; the second one requests exactly one main-thread reevaluation.

  • Present: the normal HDR-into-PQ case is a byte-for-byte passthrough (fs_hdr mode 0) — no PQ-to-linear round trip and no second tone map. Transition frames (PQ→SDR, SDR→PQ) convert via BT.709 with hue-preserving highlight compression. The shoulder engages only when the game's declared output target exceeds what the screen can currently show.

  • Upscaling: one player setting, DM_PRESENT_UPSCALE=off|metalfx:

    Upscaling SDR Native HDR10
    Standard (default) existing bilinear existing PQ bilinear
    MetalFX existing perceptual MetalFX existing linear-light HDR MetalFX (PQ decode → RGBA16Float → HDR scaler → single PQ encode + shoulder)

    A PQ source never enters the SDR perceptual scaler; a failed decode never feeds PQ codes to the HDR scaler. Decode/scaler failures fall back to bilinear. DM_HDR_UPSCALE is only an HDR-only diagnostic override (unset follows the player, off/hdrfx force a mode and are logged).

Authoritative detail: Native HDR10 presentation. Game-specific evidence: Ocarina of Time note.

3. Design decisions

  • Paper white 100 nits maps the game's diffuse white to the screen's ordinary SDR white. 203 nits (BT.2408) made the whole picture about twice as bright as SDR in upstream testing.
  • Dynamic headroom, not a fixed peak: the game receives the current EDR headroom and already adapts its HDR output to it. Its declared extended range follows min(reported peak, 3.5 × paper white) — Ocarina's own policy, not a universal SDK rule — and is an authored output target, not a maximum pixel value or HDR10 mastering metadata. The host shoulder is therefore normally inactive at steady state and protects mainly transitional headroom changes or a fixed peak above current headroom.
  • Native PQ retained; CAEDRMetadata not adopted. An isolated experiment compared native PQ, linear EDR without metadata and linear EDR with metadata. The linear path was numerically validated and checked with real RGBA16Float drawables, but no visible image-quality benefit or advantage over native PQ was demonstrated; it added about 0.2 ms of host GPU time at 5K, doubled drawable pixel storage, left compositor cost unmeasured and had no defensible mastering-metadata source. This is an evidence-based decision, not a claim that Apple's API is inherently unsuitable. Full research remains on experiment/caedrmetadata; the conclusions are in the game note.
  • MetalFX stays opt-in in both colour modes; the fork owner accepted its extra ~2 ms display-command-buffer GPU time at 5K. The two scalers keep separate GPU resources and colour-processing modes.
  • Earlier review feedback, fixed: (1) a failed PQ decode could leave use = &g_fx_hdr and feed PQ codes to fs_hdr_fx — now any failure selects bilinear and MetalFX is never reported unless decoding ran; (2) the HDR→SDR exit could leave the PQ layer until an unrelated display event — the second SDR present now requests one main-thread reevaluation; (3) the shoulder was skipped in mode 4 exactly when limited headroom needed it — removed, with six headroom pairs covered; (4) GPU timing accepted zero/invalid samples — now rejects timeouts, errors and non-finite durations.
  • Resource lifetime: decode textures are replaced only on a size/format change after in-flight presents finish; mtl_hdr_fx_reset() releases decode texture, scaler and refusal cache together; pipeline caches are keyed by the acquired drawable's format; decode argument tables are per allocator slot. The 0.25 s headroom poll stops when HDR is off.

Squashed to a single commit (87a553c5) on request; the previous 29 commits and the full research remain on experiment/caedrmetadata. Authored as lypiut <lypiut@gmail.com>. Prior art: PR #1176, PR #1249, PR #3009.

4. Validation

  • make -C dm/host GAME=0400486024DEC000 dmhost — success; dm/host/tests run — pass.
  • check_t_hdr — 251 checks, 0 failed (capability thresholds, independent headroom, exit sequence, concurrent state).
  • check_t_present_crop — 322 checks in each of five runs, 0 failed (four-way Standard/MetalFX × SDR/HDR selection matrix, fault injection and recovery, six roll-off pairs, crop/scale, alpha, timing validity).
  • check_t_hdr_mfx — both RGB10A2→RGB10A2 and RGBA16Float→RGBA16Float HDR descriptors accepted.
  • Settings/docs tests — 31 tests, 0 failed. Full Python tool sweep (266 files): 264 pass with skips; test_triage_report and test_usage fail only in fixtures that call git commit (global commit.gpgsign=true needs the 1Password signer on this Mac) — environment, not this change.
  • Prior history: Python lint 295 baseline / 0 new; clang-tidy 181 = baseline, 0 above/below; full graphics suite 283 summaries with four upstream baseline failures on clean 73f23d41 plus one t_counter_clock timing flake that passed 7/7 isolated reruns; TSan/ASan/UBSan builds of t_hdr clean.
  • Synthetic GPU medians (2× scaling, flat PQ, 12 presents, 1080p→5K): bilinear 0.03–0.62 ms, HDR MetalFX 0.25–6.11 ms across resolutions; run-to-run variance is large, so these are indicative, not gameplay benchmarks.
  • regress.py impact upstream/main..HEAD: BOTW, Scarlet, Tears of the Kingdom via NVN present; Hades via a broad rule. check --games not run: harness root, game builds and new-chain shader caches unavailable.

Live play (user-driven, 2026-11-22, M4 Pro / Studio Display XDR, commit 176c171b; no automated inputs, no save/asset/cache changes):

session frame p50 (warm) display command buffer p50 notes
SDR Standard (DM_HDR=0, DM_PRESENT_UPSCALE=off) 16.3–19.9 ms 0.259 ms layer SDR, VI peak 0
SDR MetalFX (DM_HDR=0, DM_PRESENT_UPSCALE=metalfx) 16.4 ms 1.736 ms perceptual, 2560×1440→5K
HDR Standard (DM_HDR=auto, DM_PRESENT_UPSCALE=off) 16.6 ms 0.290–0.303 ms headroom 1.2→16, format 1, peak 1600, paper white 100
HDR MetalFX (DM_HDR=auto, DM_PRESENT_UPSCALE=metalfx) 16.6–17.2 ms 2.299–2.314 ms linear-light RGBA16Float 2560×1440→5K

5. Known limitations / remaining work

  • Standard remains the default; MetalFX is the player's explicit quality option in both SDR and HDR. A scene-matched four-way A/B (kokiri-still / field-still, ≥3 runs each) with matched save, render scale, drawable, refresh rate and warm caches is still needed to confirm image quality.
  • Live fullscreen/brightness/EDR transitions, third-party HDR displays, Core Animation compositing and physical luminance remain unverified; no composited or physical-nit claim is made.
  • Nintendo's next-frame metadata calls are stored in atomics without explicit frame association; values change rarely and no artefact was observed. Enum tables for gamut/tone-mapping/Bézier metadata and any private mastering-metadata API remain unknown.
  • No ScreenCaptureKit or new screenshot infrastructure was added; the red/green rectangles in some captures were reported as screenshot-conversion artefacts, not gameplay defects.

6. Screenshots (same Kokiri-village view, 5120×2880)

SDR Standard SDR MetalFX
SDR bilinear SDR MetalFX
HDR Standard HDR linear-light MetalFX
HDR bilinear HDR MetalFX

@lypiut
lypiut force-pushed the feature/oot-native-hdr branch from d89e8ee to c3dcc2e Compare November 22, 2026 07:56
Ocarina of Time (Switch 2) renders HDR itself but nxnative always answered SDR, so its HDR picture never appeared. Answer vi::QueryDisplayHdrStatus from the game's window screen and present the game's RGB10A2 / BT.2100 PQ window unchanged in a PQ CAMetalLayer. Potential EDR headroom > 1.0 enables HDR; current headroom sets the reported peak (paper white x headroom, 10-nit steps with a one-second hold, capped at 1600) and a headroom shoulder compresses only highlights the game declares above what the screen can show. The PQ layer is kept until two non-PQ presents plus one main-thread reevaluation; transition frames convert via BT.709 with hue-preserving compression. CAEDRMetadata stays off: an isolated linear-EDR experiment found no visible benefit, higher host GPU cost, doubled drawable storage, unmeasured compositor cost and no mastering-metadata source.

The existing Upscaling row (DM_PRESENT_UPSCALE=off|metalfx) is now the single player choice for SDR and HDR: Standard keeps bilinear and PQ bilinear; MetalFX keeps the perceptual SDR and linear-light HDR MetalFX scalers with separate resources. DM_HDR_UPSCALE is only a logged HDR-only diagnostic override. A failed PQ decode never feeds PQ codes to the HDR scaler, MetalFX is never reported unless decoding ran, and failures fall back to bilinear.

Includes review fixes: a bounded capability settle timer, a one-shot HDR-to-SDR exit, a mode-4 shoulder compatible with MetalFX, valid GPU timing samples, and transient recovery and sharpness tests. Validation: t_hdr (251 checks), t_present_crop (322 checks x5), t_hdr_mfx, host tests and docs/settings tests pass; live four-way GPU timings and the CAEDRMetadata and VI calibration findings are recorded in the game note. Four comparison screenshots and upstream references (PRs seedds#1176, seedds#1249, seedds#3009) are preserved. Squashed from 29 commits of PR #1; full research remains on experiment/caedrmetadata.
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.

1 participant