XAudio2: reset XMA2 bit reservoir and skip orphan continuations after packet loss #1450

Open
b3IKK wants to merge 1 commit from upstream-pr/1460 into main
b3IKK commented 2026-11-24 16:27:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T16:27:15Z.


In xma2::StreamDecoder::decode_packet_step, when packet_loss was set (at stream start, after flush(), or after a decode error) and a continuation packet arrived with num_bits_prev_frame > 0:

  • save_bits(..., append = true) unconditionally wrote up to 16,352 discarded bits into self.reservoir and advanced reservoir.bit_pos.
  • if self.packet_loss zeroed num_saved_bits and cleared packet_loss without calling reservoir.reset().
  • On the next packet's num_bits_prev_frame, save_bits appended at the stale reservoir.bit_pos while decode_frame read bits 0..num_saved_bits from the previous packet's discarded bytes, decoding a phantom frame and corrupting overlap-add state.

This only appends/decodes num_bits_prev_frame when !self.packet_loss && self.num_saved_bits > 0 (otherwise skipping those bits with pgb.skip_bits), and resets self.reservoir and self.num_saved_bits whenever decode_frame consumes the reservoir or packet_loss is cleared.

What is in it

  • crates/xcl/src/hle/xma2/mod.rs: skip orphan continuation bits when there is no valid saved prefix; reset reservoir and num_saved_bits on frame consumption and packet-loss recovery; add unit test packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame (verified failing before the fix with reservoir.bit_pos == 16352).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 771 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib xma2: 10 passed, 0 failed (packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame failed before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 08175680d04a8b5fec8066bace3e3c43602d915f
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14596 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited hle/xma2/mod.rs, wrote the fix and unit test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5476156802 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T16:27:15Z. --- In `xma2::StreamDecoder::decode_packet_step`, when `packet_loss` was set (at stream start, after `flush()`, or after a decode error) and a continuation packet arrived with `num_bits_prev_frame > 0`: - `save_bits(..., append = true)` unconditionally wrote up to 16,352 discarded bits into `self.reservoir` and advanced `reservoir.bit_pos`. - `if self.packet_loss` zeroed `num_saved_bits` and cleared `packet_loss` without calling `reservoir.reset()`. - On the next packet's `num_bits_prev_frame`, `save_bits` appended at the stale `reservoir.bit_pos` while `decode_frame` read bits `0..num_saved_bits` from the previous packet's discarded bytes, decoding a phantom frame and corrupting overlap-add state. This only appends/decodes `num_bits_prev_frame` when `!self.packet_loss && self.num_saved_bits > 0` (otherwise skipping those bits with `pgb.skip_bits`), and resets `self.reservoir` and `self.num_saved_bits` whenever `decode_frame` consumes the reservoir or `packet_loss` is cleared. **What is in it** - `crates/xcl/src/hle/xma2/mod.rs`: skip orphan continuation bits when there is no valid saved prefix; reset `reservoir` and `num_saved_bits` on frame consumption and packet-loss recovery; add unit test `packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame` (verified failing before the fix with `reservoir.bit_pos == 16352`). **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 771 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`) - `cargo test -p xcl --lib xma2`: 10 passed, 0 failed (`packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame` failed before the fix) **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 08175680d04a8b5fec8066bace3e3c43602d915f gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1648 (PresentX 1648) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14596 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `hle/xma2/mod.rs`, wrote the fix and unit test, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
futurepro62 commented 2026-11-24 17:06:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T16:55:57Z.


Full-suite run, since this PR has no test report yet. Tested exact head 0817568 (on its base b7aa9f3).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 771 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 771 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7138429194 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7138429194). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T16:55:57Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `0817568` (on its base `b7aa9f3`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **771 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `packet_loss_continuation_does_not_desync_reservoir_or_decode_phantom_frame`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **771 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 19:07:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:52:55Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7141283317 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7141283317). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T18:52:55Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
S1m0ne1997 commented 2026-11-25 16:12:04 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by S1m0ne1997 on 2026-11-25T15:16:31Z.


Test report for 08175680d04a8b5fec8066bace3e3c43602d915f (this PR's head), for #1607.

Tested

  • NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, rustc 1.100.0 (0ef1bc386 2026-11-09) x86_64-pc-windows-msvc, debug.
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4 at the head: 771 passed, 0 failed, 41 ignored.
  • SKIPPED (2): gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).

Regression run (retail title, scripts/smoke.ps1 -Seconds 60, release; main built from the commit in its block, this PR from its head with no changes to crates/, both run with the scripts/smoke.ps1 of main, which hashes the name of the game folder)

main:

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16384 MB

this PR:

xcl-smoke v1
commit: 08175680d04a8b5fec8066bace3e3c43602d915f
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1666 (PresentX 1666)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18013 MB

Not tested

  • AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only.

AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.

<!-- greyfinch-sync:ivall/xcl:comment:7166581085 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7166581085). Originally posted by [S1m0ne1997](https://github.com/S1m0ne1997) on 2026-11-25T15:16:31Z. --- Test report for `08175680d04a8b5fec8066bace3e3c43602d915f` (this PR's head), for #1607. **Tested** - NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, `rustc 1.100.0 (0ef1bc386 2026-11-09)` `x86_64-pc-windows-msvc`, debug. - `cargo build --workspace`, then `cargo test --workspace -- --test-threads=4` at the head: **771 passed, 0 failed, 41 ignored**. - SKIPPED (2): `gpu_geometry_wave32_prefix_count_over_a_mask` (no wave64), `gpu_wave32_program_in_wave64_hardware` (no wave64). **Regression run** (retail title, `scripts/smoke.ps1 -Seconds 60`, release; `main` built from the commit in its block, this PR from its head with no changes to `crates/`, both run with the `scripts/smoke.ps1` of `main`, which hashes the name of the game folder) main: ``` xcl-smoke v1 commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324 gpu: NVIDIA GeForce RTX 4090 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1599 (PresentX 1599) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 16384 MB ``` this PR: ``` xcl-smoke v1 commit: 08175680d04a8b5fec8066bace3e3c43602d915f gpu: NVIDIA GeForce RTX 4090 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1666 (PresentX 1666) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 18013 MB ``` **Not tested** - AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only. *AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.*
github-actions[bot] commented 2026-11-29 20:42:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:42:50Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7262298737 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7262298737). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T20:42:50Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:50:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:50:22Z.


Triage (#1607) – head 0817568

No other open PR changes XMA2 packet-loss continuations. Main changed this block since (b4484bc8): it decodes a continuation only when it is complete (continuation_complete) and keeps an incomplete one buffered.
Problem: in xma2/mod.rs:decode_packet_step this PR decodes after appending and then calls reservoir.reset() / num_saved_bits = 0, even when the continuation runs past the packet. A frame that spans three packets is then decoded incomplete and its remainder is skipped, and main's test incomplete_continuation_remains_buffered_for_the_next_packet would fail. The rebase must keep continuation_complete and reset only after a complete decode. The skipping of orphan continuations after packet loss (no saved frame start) is not on main.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302192703 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7302192703). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:50:22Z. --- **Triage (#1607)** – head 0817568 No other open PR changes XMA2 packet-loss continuations. Main changed this block since (b4484bc8): it decodes a continuation only when it is complete (`continuation_complete`) and keeps an incomplete one buffered. Problem: in `xma2/mod.rs:decode_packet_step` this PR decodes after appending and then calls `reservoir.reset()` / `num_saved_bits = 0`, even when the continuation runs past the packet. A frame that spans three packets is then decoded incomplete and its remainder is skipped, and main's test `incomplete_continuation_remains_buffered_for_the_next_packet` would fail. The rebase must keep `continuation_complete` and reset only after a complete decode. The skipping of orphan continuations after packet loss (no saved frame start) is not on main. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
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/1460:upstream-pr/1460'
git switch 'upstream-pr/1460'

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/1460'
git switch 'upstream-pr/1460'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1460'
git switch 'upstream-pr/1460'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1460'
git switch 'main'
git merge --squash 'upstream-pr/1460'
git switch 'main'
git merge --ff-only 'upstream-pr/1460'
git switch 'main'
git merge 'upstream-pr/1460'
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
xcl-dev/xcl!1450
No description provided.