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

Open
github-bot wants to merge 1 commit from github-bot/gh-1460 into main AGit
Owner

@⁠b3IKK on GitHub · 2026-11-24 16:27 UTC · original
b3IKK:xma2/reservoir-reset-on-packet-loss → main · +57 −4 · 1 files · 1 commits · open

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.
**@⁠b3IKK** on GitHub · 2026-11-24 16:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/1460) `b3IKK:xma2/reservoir-reset-on-packet-loss` → `main` · +57 −4 · 1 files · 1 commits · open 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.
In StreamDecoder::decode_packet_step, when `packet_loss` was set (e.g.
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.

Only append/decode `num_bits_prev_frame` when `!self.packet_loss &&
self.num_saved_bits > 0` (otherwise skip the bits), and reset `reservoir`
and `num_saved_bits` whenever `decode_frame` consumes the reservoir or
`packet_loss` is cleared.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:54:31 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 16:55 UTC · original · edited 2026-11-24 17:04 UTC

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.
**@⁠futurepro62** on GitHub · 2026-11-24 16:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7138429194) · edited 2026-11-24 17:04 UTC 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 18:52 UTC · original

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!

**@⁠DavidM1911** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7141283317) 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!
Author
Owner

@⁠S1m0ne1997 on GitHub · 2026-11-25 15:16 UTC · original

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.

**@⁠S1m0ne1997** on GitHub · 2026-11-25 15:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/1460#issuecomment-7166581085) 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.*
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1460/head:github-bot/gh-1460'
git switch 'github-bot/gh-1460'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1460'
git switch 'github-bot/gh-1460'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1460'
git switch 'github-bot/gh-1460'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1460'
git switch 'main'
git merge --squash 'github-bot/gh-1460'
git switch 'main'
git merge --ff-only 'github-bot/gh-1460'
git switch 'main'
git merge 'github-bot/gh-1460'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.