Audio: XMA decoding keeps up while frames are slow (MMCSS thread, the mixer never waits for a decode) #2788
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2788
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2788"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@0xkn0wh3r3 on GitHub · 2026-11-25 20:23 UTC · original
0xkn0wh3r3:audio/steady-under-slow-frames→main· +226 −68 · 1 files · 6 commits · openAudio could cut out while world frames take about a second each. VIctory doesn't drive the audio device: the title mixes on its own time-critical thread and writes to the host's WASAPI. Our part is XMA decoding, on one thread, into output rings of at most 31 blocks of 256 bytes (about 83 ms of mono at 48 kHz).
Two things on our side could let a ring run dry while frames are slow. One commit for each, both in
crates/victory-hle/src/modules/audio.rs. A third commit, resuming a voice whose input ran out, was dropped after review (reverted in2314c249) and is left to #1880.Changes
1. The decoding thread is scheduled like a host audio engine's (
raise_priority, called when the "victory XMA decoder" thread starts).AvSetMmThreadCharacteristicsW). If that fails it falls back toTHREAD_PRIORITY_TIME_CRITICAL, thenTHREAD_PRIORITY_HIGHEST.XMA decoder: MMCSS "Pro Audio", orXMA decoder: no MMCSSwith the error and the priority).2. The read-advance command never waits for a decode (
Slot,pump,fill_block,advance_read).pumpheld while it decoded until the ring was full.voice: the decoder, its samples and its trace;ring: only the read block and "writable", which both sides change, plus the take counts.advance_readtakes onlyring.pumpholdsvoiceone block at a time (at most one frame's decode), so enable and disable wait for one frame at most.fill_blocktakesringonly after writing a block, to decide whether the ring is full.Overlap with other PRs
pump, so they will conflict textually.pumpanddecodefor other bugs; there is no functional overlap, but #2444 and #2709 rewrite the samepumploop, so expect rebase conflicts.Tests
read_advance_does_not_wait_for_decodingholds the voice lock as a decode does and sends command 18 from another thread.cargo test -p victory-hle: lib 153 passed, 0 failed; the other test binaries pass.cargo clippy -p victory-hle --all-targets -- -D warnings: clean, with and without--features diagnostics.Not verified
VICTORY_LOG=victory_hle::modules::audio=debug), will show what happened.@kotekompotek on GitHub · 2026-11-25 20:40 UTC · original
Right threading model: MMCSS 'Pro Audio' with the graceful fallback chain (time-critical -> normal) is the correct way to keep XMA decoding ahead of the mixer on busy systems, and decoupling the mixer's read from the in-flight decode (ring + never-wait) kills the stall. Ended voices resuming on new input (ended=false in decode) fixes the real silence-after-stream-restart bug. Non-Windows stub keeps the build portable.
@oni-swr on GitHub · 2026-11-25 20:58 UTC · original
WARP/CPU verification of head
b9bf1b6afa2b18811ed8e3141432245cead0a808Environment: Windows 11 x64, rustc/cargo 1.98.1, WARP/CPU only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), no physical GPU, no game files. DXIL validation with Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11. PR head merged cleanly onto origin/main
13f91caee2.Tests (
cargo test --no-fail-fast -p victory-hle)origin/mainbaseline: lib 146 passed, plusfidelityfx1 andno_dll_planting1; 0 failed.fidelityfx1,no_dll_planting1; 0 failed (+2:read_advance_does_not_wait_for_decoding,a_voice_that_ran_out_resumes_on_new_input). The two new tests passed on 5 consecutive extra runs (no flakiness seen).Negative control (each applied alone, then reverted)
advance_readalso takes the voice lock (the old behaviour):read_advance_does_not_wait_for_decodingfails (after its 5 s timeout), the other test passes.a_voice_that_ran_out_resumes_on_new_inputfails, the other passes.Lint:
cargo clippy --all-targets -- -D warningsclean.cargo fmt --check: no new hunks (audio.rs3 on main, 2 with the PR). Note:cargo fmt --checkis already red onorigin/mainhere (891 hunks, none from these PRs), so I compared per-file hunk counts against that baseline.Review: read the full diff. The split into a
voicelock (held per block) and aringlock (taken after it, never during a decode) is consistent, and the lock order is respected indisableandfill_block; reading "writable" without the ring lock is sound because only the decoder clears it and the read-advance only sets it. Not covered here: the MMCSS / thread-priority path (raise_priority) is not exercised by any test, and I ran no audio device. No game files, dumps, shader names, or retail hashes.AI disclosure: this verification was done by Claude Code (Claude Sonnet 5.5).
@fuzeofficlal on GitHub · 2026-11-25 21:05 UTC · original
Reviewed and tested
b9bf1b6aon Windows 11 (rustc 1.98.1MSVC) againstmain(13f91ca).Concurrency bug found in
fill_block(audio.rs:588-599): empty ring can be marked full and permanently stall a voiceIn commit
b15cc7a(Slot { voice, ring }),fill_blockpublishes the updatedwriteblock before acquiringring:If the mixer thread consumes that newly written block between lines 589 and 591 and calls
advance_read,advance_readmovesreadforward to equalwrite(leaving the ring empty) and setswritable = 1. Whenfill_blockthen acquiresringat line 591, it observeswrite == readand clearswritable = 0, marking an empty ring as full and stalling the voice indefinitely (the mixer seesread == writeso it will never issue anotherADVANCE_XMA_READ).Moving
let _ring = lock(ring);just above line 589 (still afterdecode(), so mixeradvance_readstill never blocks on decoding) closes that window completely.Minor note on commit
b9bf1b6(resuming ended voices,audio.rs:605-618)When an ended voice resumes on new valid input,
voice.endedandvoice.decoderare reset, but any leftover partial-block samples invoice.pcm(< 128) or unconsumedvoice.skipfrom the previous stream are kept instead of cleared/re-read fromword 1[26:24]. Worth double-checking whether a resumed voice should start with a freshpcm/skip.Verification & merge compatibility
cargo test --locked -p victory-hle(3 consecutive runs): 3/3 passed (148 passed, 0 failedunit +fidelityfx+no_dll_planting, including both new tests).cargo clippy --locked -p victory-hle --all-targets -- -D warnings(default &--features diagnostics): 0 warnings;cargo xtask policy: 184 files ok.13f91ca,#2708,#2714; conflicts inaudio.rswith#2709and#2710.@0xkn0wh3r3 on GitHub · 2026-11-25 21:35 UTC · original
@fuzeofficlal thanks, that window is real. Fixed in
36d80c3:fill_blocknow takes the ring lock before it moves the write block, still after the decode, so a read-advance still never waits for decoding. A read-advance can't land between publishing the block and the full check any more.On the resumed voice:
pcm: what's left there is the ended stream's flushed tail, which would have played had the title refilled in time, so it's kept.skip: a leftover skip can only survive a stream that ended before its first frames were decoded. Re-reading word 1[26:24] on resume would treat a late refill as a new stream start. I don't know which the hardware does, so it's unchanged.cargo test -p victory-hle(148 lib, including both new tests), clippy and policy pass. @oni-swr @kotekompotek thanks for the WARP run, the negative controls and the review.@Marvisak on GitHub · 2026-11-25 23:20 UTC · original · edited 2026-11-26 02:19 UTC
Automated triage for the maintainers.
Merge candidate (recommending a look, not an approval). Thanks for dropping commit 3: the diff since the last review (
6b73b07..2314c24) only removes the resume-an-ended-voice path (Voice::new,resumes, the word-2 reset), sodecodebehaves as onmainfor ended voices and that case is left to #1880. What remains incrates/victory-hle/src/modules/audio.rs: the per-context lock split (Slotwithvoiceandring;advance_readtakes onlyring,pumpholdsvoiceone block at a time, lock order voice then ring), withread_advance_does_not_wait_for_decodingcovering it, andraise_priorityregistering the decoder thread with MMCSS "Pro Audio" and falling back to time-critical/highest, which changes host scheduling only. No Cargo.toml change. It merges cleanly and CIcheckis passing. Only one of this and #1880's priority change should land.Generated by Claude Code
@0xkn0wh3r3 on GitHub · 2026-11-26 00:01 UTC · original
Thanks. Commit 3, resuming an ended voice, is reverted in
2314c249, and that case is left to #1880, which is now credited in the description. The PR is now the lock split and the decoding thread's scheduling only.@Dukestar69 on GitHub · 2026-11-26 03:50 UTC · original · approved these changes
Tested merged onto
origin/main(63bcca4) on Windows 11 x64:cargo test -p victory-hle --lib modules::audio::: 9 passed, 0 failed (including newread_advance_does_not_wait_for_decoding).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@futurepro62 on GitHub · 2026-11-26 13:42 UTC · original
NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, oni-swr, since none of them names a GPU run. Tested exact head
2314c24(on its base842b0cb).Tested
dxil.dllfrom Windows SDK 10.0.26100 (the loader's default), debug.cargo build --workspace --locked, thencargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.read_advance_does_not_wait_for_decoding.--skipleaves out tests that fail on main63bcca4on this card too, not because of this PR:heap_direct_texture_reads_mark_their_heap_indexends thevictory-shadertest process with0xc0000005(#2571), so without the skip the rest of that crate never reports;dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_viewsand the twostride_testshit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317);gathers_at_a_level_match_the_hardwarefails on lane 0 (#1744).Not tested
AI use
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.