Audio: refill XMA rings from a decoded reserve on the title's thread #1878

Open
beamdotdev wants to merge 1 commit from upstream-pr/1880 into main
beamdotdev commented 2026-11-25 00:00:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by beamdotdev on 2026-11-25T00:00:49Z.


Audio was choppy: XMA rings ran dry under load.

Why

  • The XMA decoding thread ran at normal priority, while the title's mixer that consumes its output is time-critical; the title's workers took the CPU from it.
  • A block the title freed (read-advance command) was refilled only after the decoding thread was scheduled and had decoded the next frame.
  • A voice whose inputs both ran out stayed silent until re-enabled, so a stream refilled a little late dropped out.

What

  • Each voice keeps two frames (~21 ms) decoded ahead of its ring. ADVANCE_XMA_READ copies from that reserve into the freed blocks at once, on the title's thread; the decoding thread then tops the reserve up.
  • The decoding thread runs THREAD_PRIORITY_TIME_CRITICAL. It works only while a ring has room or a reserve is short, so it cannot hog a core.
  • When new input arrives after a voice ran out, it resumes from a cold decoder with the error status cleared.
  • A partial block waits for more input rather than being padded with silence, except once the input has run out.
  • imdct_half's pre/post rotations are computed once per transform size instead of per channel and subframe (bit-identical output).

Testing

  • cargo test -p victory-hle audio and cargo test -p victory-formats xma pass; clippy is clean on both crates.
  • xma_contexts_decode_into_their_output_rings now also covers the refill from the reserve on read-advance and resuming after input runs out.
  • Not yet checked by ear in a title. VICTORY_AUDIO_CAPTURE (diagnostics build) can record before/after.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5482457090 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1880). Originally posted by [beamdotdev](https://github.com/beamdotdev) on 2026-11-25T00:00:49Z. --- Audio was choppy: XMA rings ran dry under load. ## Why - The XMA decoding thread ran at normal priority, while the title's mixer that consumes its output is time-critical; the title's workers took the CPU from it. - A block the title freed (read-advance command) was refilled only after the decoding thread was scheduled and had decoded the next frame. - A voice whose inputs both ran out stayed silent until re-enabled, so a stream refilled a little late dropped out. ## What - Each voice keeps two frames (~21 ms) decoded ahead of its ring. `ADVANCE_XMA_READ` copies from that reserve into the freed blocks at once, on the title's thread; the decoding thread then tops the reserve up. - The decoding thread runs `THREAD_PRIORITY_TIME_CRITICAL`. It works only while a ring has room or a reserve is short, so it cannot hog a core. - When new input arrives after a voice ran out, it resumes from a cold decoder with the error status cleared. - A partial block waits for more input rather than being padded with silence, except once the input has run out. - `imdct_half`'s pre/post rotations are computed once per transform size instead of per channel and subframe (bit-identical output). ## Testing - `cargo test -p victory-hle audio` and `cargo test -p victory-formats xma` pass; clippy is clean on both crates. - `xma_contexts_decode_into_their_output_rings` now also covers the refill from the reserve on read-advance and resuming after input runs out. - Not yet checked by ear in a title. `VICTORY_AUDIO_CAPTURE` (diagnostics build) can record before/after. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Marvisak commented 2026-11-25 02:06:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:37:37Z.


Automated triage for the maintainers.

The reserve-and-refill part (fill called from ADVANCE_XMA_READ, plus the decoding thread topping up two frames ahead) is a reasonable way to keep XMA rings from running dry. However, the PR also changes what the emulated XMA context does, and gives no evidence for it. In decode in crates/victory-hle/src/modules/audio.rs, a voice whose input ran out now resumes when new input arrives, from a cold decoder, with context dword 2 zeroed (context.set(2, 0)). Enabling only clears the error bits (set_field(2, 26, 6, 0) in Xma::enable), so the comment "as enabling does" does not match the code, and the read offset is reset too. A partial block now waits for more input instead of being padded. Whether the hardware resumes a context that ran out, and from where, is the kind of runtime behaviour that needs a reference: the title's own handling of that state, or public documentation. The description also says the change has not been heard in a title. To move forward: give evidence for the resume semantics (or split them out, so the reserve and the thread priority can be judged alone), and attach a before/after VICTORY_AUDIO_CAPTURE comparison or a listening report from the title.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149766886 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1880#issuecomment-7149766886). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:37:37Z. --- <!-- victory-triage v1 head=3de75792f89cd2e10ece1f5db2d66a8742bb5394 verdict=needs-work at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The reserve-and-refill part (`fill` called from `ADVANCE_XMA_READ`, plus the decoding thread topping up two frames ahead) is a reasonable way to keep XMA rings from running dry. However, the PR also changes what the emulated XMA context does, and gives no evidence for it. In `decode` in `crates/victory-hle/src/modules/audio.rs`, a voice whose input ran out now resumes when new input arrives, from a cold decoder, with context dword 2 zeroed (`context.set(2, 0)`). Enabling only clears the error bits (`set_field(2, 26, 6, 0)` in `Xma::enable`), so the comment "as enabling does" does not match the code, and the read offset is reset too. A partial block now waits for more input instead of being padded. Whether the hardware resumes a context that ran out, and from where, is the kind of runtime behaviour that needs a reference: the title's own handling of that state, or public documentation. The description also says the change has not been heard in a title. To move forward: give evidence for the resume semantics (or split them out, so the reserve and the thread priority can be judged alone), and attach a before/after `VICTORY_AUDIO_CAPTURE` comparison or a listening report from the title. --- _Generated by [Claude Code](https://claude.ai/code)_
beamdotdev commented 2026-11-25 02:06:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by beamdotdev on 2026-11-25T02:05:53Z.


Rebased onto main (8fb0b28). The IMDCT rotation cache is dropped from this PR: main's own (#172) covers it. One test from main that builds a Voice now uses the renamed field. victory-hle tests: 142 pass; the one failure, dstorage::a_page_list_filled_after_its_request_starts, fails on main too.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150313659 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1880#issuecomment-7150313659). Originally posted by [beamdotdev](https://github.com/beamdotdev) on 2026-11-25T02:05:53Z. --- Rebased onto main (8fb0b28). The IMDCT rotation cache is dropped from this PR: main's own (#172) covers it. One test from main that builds a Voice now uses the renamed field. victory-hle tests: 142 pass; the one failure, dstorage::a_page_list_filled_after_its_request_starts, fails on main too.
catminou4 commented 2026-11-25 02:17:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:17:16Z.

Review: COMMENTED


Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff.

XMA ring refill from a decoded reserve on the title's thread — moves decode work off the audio callback path; sensible if the reserve bounds are right. Windows-only verification needed.

<!-- greyfinch-sync:Marvisak/VIctory:review:6050698791 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1880#pullrequestreview-6050698791). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:17:16Z. **Review:** COMMENTED --- Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff. XMA ring refill from a decoded reserve on the title's thread — moves decode work off the audio callback path; sensible if the reserve bounds are right. Windows-only verification needed.
Desamodeth commented 2026-11-26 21:45:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:45:32Z.

Review: COMMENTED


Independent Windows Release CPU-only benchmark and PCM/concurrency verification of exact head 3de75792 merged onto main (63bcca43), compared head-to-head against main and #2788 (2314c249) (Ryzen 7 9800X3D, rustc 1.98.1 MSVC, synthetic XMA streams only, no GPU/WARP; full table in the #2788 review).

  • Correctness: cargo test --locked -p victory-hle --lib modules::audio:: (8 passed), clippy -D warnings, and cargo xtask policy pass cleanly. Sequential stereo/looping-mono PCM is 100% bit-exact with main, and 80 concurrent stereo streams had 0 mismatches / 0 stalls.
  • Measured benefit (reserve refill): In a 16-voice burst, 100% of ADVANCE_XMA_READ calls leave the ring immediately full from the 2-frame reserve (vs 0% on main / #2788), and total uncontended 16×16-block adv+pump throughput is essentially unchanged (337.26 vs 341.26 µs/round).
  • Measured trade-offs vs main and #2788 (10 interleaved forward+reverse runs):
    1. Contended ADVANCE_XMA_READ still blocks on the shared voice lock while pump() decodes ring + reserve frames: same-voice mid-decode p50/p99/max is 18.90 / 29.30 / 88.75 µs (vs 22.50 / 33.35 / 42.05 µs on main and 0.10 / 0.15 / 0.20 µs on #2788); 16-voice burst p95/max is 16.90 / 59.80 µs (vs 12.60 / 35.75 µs on main and 0.10 / 0.25 µs on #2788).
    2. Uncontended ADVANCE_XMA_READ is 42.3× slower on the mixer thread: 1,466.20 ns/call vs 34.70 ns/call on main (34.95 ns on #2788) due to synchronous fill() copying blocks via heap_bytes + VecDeque::pop_front.
    3. 4.0× higher live heap per active stereo voice: 131,072 B live / 133,159 B peak for 16 active stereo voices vs 32,768 B / 34,855 B on main and #2788 (+6,144 B per active stereo voice because the 2-frame reserve pushes VecDeque capacity to 4,096 i16 = 8 KiB; drops to 0 B on disable).

AI-assisted with Gemini 4 Neon

<!-- greyfinch-sync:Marvisak/VIctory:review:6080813419 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1880#pullrequestreview-6080813419). Originally posted by [Desamodeth](https://github.com/Desamodeth) on 2026-11-26T21:45:32Z. **Review:** COMMENTED --- Independent Windows Release CPU-only benchmark and PCM/concurrency verification of exact head `3de75792` merged onto `main` (`63bcca43`), compared head-to-head against `main` and #2788 (`2314c249`) (Ryzen 7 9800X3D, `rustc 1.98.1` MSVC, synthetic XMA streams only, no GPU/WARP; full table in [the #2788 review](https://github.com/Marvisak/VIctory/pull/2788#pullrequestreview-6080813231)). - **Correctness:** `cargo test --locked -p victory-hle --lib modules::audio::` (**8 passed**), `clippy -D warnings`, and `cargo xtask policy` pass cleanly. Sequential stereo/looping-mono PCM is **100% bit-exact** with `main`, and 80 concurrent stereo streams had **0 mismatches / 0 stalls**. - **Measured benefit (reserve refill):** In a 16-voice burst, **100%** of `ADVANCE_XMA_READ` calls leave the ring immediately full from the 2-frame reserve (vs 0% on `main` / #2788), and total uncontended 16×16-block `adv+pump` throughput is essentially unchanged (`337.26` vs `341.26 µs/round`). - **Measured trade-offs vs `main` and #2788** (10 interleaved forward+reverse runs): 1. **Contended `ADVANCE_XMA_READ` still blocks on the shared voice lock** while `pump()` decodes ring + reserve frames: same-voice mid-decode p50/p99/max is **18.90 / 29.30 / 88.75 µs** (vs `22.50 / 33.35 / 42.05 µs` on `main` and **0.10 / 0.15 / 0.20 µs** on #2788); 16-voice burst p95/max is **16.90 / 59.80 µs** (vs `12.60 / 35.75 µs` on `main` and **0.10 / 0.25 µs** on #2788). 2. **Uncontended `ADVANCE_XMA_READ` is 42.3× slower on the mixer thread:** `1,466.20 ns/call` vs `34.70 ns/call` on `main` (`34.95 ns` on #2788) due to synchronous `fill()` copying blocks via `heap_bytes` + `VecDeque::pop_front`. 3. **4.0× higher live heap per active stereo voice:** `131,072 B` live / `133,159 B` peak for 16 active stereo voices vs `32,768 B` / `34,855 B` on `main` and #2788 (**+6,144 B per active stereo voice** because the 2-frame reserve pushes `VecDeque` capacity to 4,096 `i16` = 8 KiB; drops to `0 B` on disable). *AI-assisted with Gemini 4 Neon*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1880:upstream-pr/1880'
git switch 'upstream-pr/1880'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1880'
git switch 'upstream-pr/1880'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1880'
git switch 'upstream-pr/1880'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1880'
git switch 'main'
git merge --squash 'upstream-pr/1880'
git switch 'main'
git merge --ff-only 'upstream-pr/1880'
git switch 'main'
git merge 'upstream-pr/1880'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!1878
No description provided.