Skip to content

XAudio2: GetState names the oldest queued buffer when the host gives none - #890

Open
shortside wants to merge 1 commit into
xcl-dev:mainfrom
shortside:xaudio2/getstate-queued-context
Open

shortside wants to merge 1 commit into
xcl-dev:mainfrom
shortside:xaudio2/getstate-queued-context

Conversation

@shortside

@shortside shortside commented Nov 24, 2026 •

Copy link
Copy Markdown

Under Wine (CrossOver 26.3), GetState on a decoded (XMA / Opus) source voice that has buffers queued but has not started returns BuffersQueued = 2 and pCurrentBufferContext = NULL; Windows names the first queued buffer. hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime checks for that and fails under Wine (left: 0, right: 12291 = 0x3003). Reported in #258 (comment).

XCL submits its decoded buffers with a non-null token as the context, so a null current context with buffers queued means the host did not say. When that happens, GetState now names the oldest of the voice's buffers that has not ended. When the host returns a context, the mapping is unchanged, so this does nothing on Windows.

What is in it

  • crates/xcl/src/hle/xaudio2.rs: DecodedBuffer gets a submission number (NEXT_BUFFER_SEQ); get_state falls back to the oldest live buffer when the host's current context is null and BuffersQueued != 0. +16 / -2, one commit.

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, Rosetta 2; CrossOver 26.3 (Wine 11.0), Windows 10 bottle, D3DMetal; DXC 1.9.2609. Cross-built for x86_64-pc-windows-msvc with clang-cl / lld-link 23.1.6 and an xwin SDK 10.0.26100, Rust 1.100.0, debug. The test binary was run under Wine; main is beaa245, the head is this PR's commit (the runs were made before a comment-only edit to the new comment).
  • xcl-<hash>.exe hle::xaudio2:: --test-threads=1, 3 runs each: main 8 passed, 1 failed (this test) every run; head 9 passed, 0 failed every run.
  • hle:: with --skip ::gpu_ on the head (skipping the 3 tests below that crash this host on main too): 278 passed, 3 failed, 2 ignored; this test passes. The 3 failures fail on main as well.
  • Whole lib, non-GPU (--skip ::gpu_ --test-threads=1, restarted past each crash), main vs head: 571 / 5 / 20 and 572 / 5 / 20 (passed / failed / ignored); no other test changed outcome apart from two that are flaky on this host (cpu_writes::tests::writes_land_after_the_queue failed once on main and passed on reruns; vram::tests::d32s8_targets_take_their_own_size_of_video_memory crashed in the main run and not in the head's). In this whole-lib run the head still fails this test, later on: after GetState it gets no buffer callbacks within the test's 3 s (STARTED 0). It passes alone and in the hle:: run above; the whole-lib run took 636 s under Rosetta, so I read it as the audio thread being late, not confirmed.
  • Crashed on this host in both main and head (as reported in macOS (Apple Silicon, CrossOver + D3DMetal): the newer retail game reaches its first-run menus at 30 fps #258): bind::tests::create_list_reset_and_clear_state_track_initial_pso_on_d3d12, title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, vram::tests::d32s8_targets_take_their_own_size_of_video_memory.

Tested by others (on head 76d2ff7)

Regression run

  • From my Mac: MacBook Pro M5 Pro (24 GB), macOS 27.2.1, CrossOver 26.3 (Wine 11.0), D3DMetal. scripts/smoke.ps1 -Seconds 60 under PowerShell 7 on macOS, the retail title, release builds cross-built for x86_64-pc-windows-msvc, each from a clean tree of the commit, each on a fresh data root, one after the other. The script starts xcl.exe through a small wrapper that runs it under Wine and rewrites only the Log: / Report: lines to macOS paths; XCL_DXC_THREADS=4 for both runs. The blocks are as printed.
  • What they show and what they do not: on this host main does not draw (OpenExistingHeapFromAddress … Not implemented, macOS (Apple Silicon, CrossOver + D3DMetal): the newer retail game reaches its first-run menus at 30 fps #258), so both runs sit at 0 frames for the 60 s and end cleanly; XCL's log has no XAudio2 line before that point, so the change is not reached. It is a check that nothing is worse (no crash, no device removal, the same frames), not a run of the change. gpu: is D3DMetal's adapter name, and peak commit reads 0 MB under Wine, so the commit limit cannot be compared here. A Windows pair would say more.

main:

xcl-smoke v1
commit: b9b6ea62080cc62fc19e212033372d01c6214e5c
gpu: AMD Compatibility Mode
title: Retail game
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: 76d2ff732e8af140a40570f7de38289a16861380
gpu: AMD Compatibility Mode
title: Retail game
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • The whole suite on Windows or a PC GPU (only the hle::xaudio2:: tests ran on Windows, above); the GPU tests were not run for this PR.
  • No retail title, no audible output.
  • The e2e test title's XAudio2 check (XMA2 GetState: ctx=0x0, queued=1 under Wine, noted by @ClosetPie107).

AI use

  • Claude Code with Claude Opus 5.5: found the cause, wrote the change and this description, and ran the tests above on my machine.

…none

Under Wine (CrossOver 26.3), GetState on a decoded (XMA / Opus) source
voice that has buffers queued but has not started returns
BuffersQueued = 2 and pCurrentBufferContext = NULL. Windows names the
first queued buffer there, and xma2_source_voice_decodes_and_manages_lifetime
checks for it (left 0, right 0x3003).

XCL submits its decoded buffers with a non-null token as the context, so
a null current context with buffers queued means the host did not say.
Each decoded buffer now carries its submission number, and GetState
names the oldest one that has not ended. When the host returns a context,
the mapping is unchanged, so Windows is not affected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ettehn

ettehn commented Nov 24, 2026

Copy link
Copy Markdown

Native Windows x64 verification on 76d2ff7: all 9 XAudio2 unit tests passed with system XAudio2 2.9 (cargo test -p xcl --lib hle::xaudio2:: -- --test-threads=1 --nocapture). Both source-voice lifetime tests ran to completion, including the Opus path using Windows Media Foundation’s MSOpusDecoder. Neither took the missing-device/decoder skip.

I also exercised the fallback separately. In a local test build I verified that the native GetState result had two queued buffers and a non-null current context, then cleared only that context immediately after the host call. The fallback returned the first submitted context for both codecs: 0x1001 for Opus and 0x3003 for XMA2. Both tests passed their subsequent playback callback and lifetime assertions. This second run simulates the missing host context on Windows; Wine and retail playback were outside this check.

OpenAI Codex (GPT-6.2 Astra) assisted with verification.

@ClosetPie107

Copy link
Copy Markdown

Tested on Linux / RADV, and it also fixes the Opus variant of the same test.

Tested

  • AMD Radeon RX 9070 (RADV GFX1201, Mesa 26.3.1), Ryzen 5 9600X, CachyOS, kernel 7.3.5. Proton Experimental's Wine (experimental-11.0-20261112), vkd3d-proton v3.1 + Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257's patch, DXVK v3.1.1-209, DXC 1.9.2609.5, opus.dll (libopus) from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257's build-opus.sh through XCL_OPUS_DIR.
  • This PR's head 76d2ff7 (base beaa245) against main 1ec4c98, both release builds for x86_64-pc-windows-gnu, Rust 1.96.0.
  • xcl-<hash>.exe hle::xaudio2:: --test-threads=1 under Wine, 3 runs each:
    • main: 7 passed, 2 failed every run: xma2_source_voice_decodes_and_manages_lifetime (left: 0, right: 12291) and opus_source_voice_decodes_and_manages_lifetime (left: 0, right: 4097, the same GetState context check).
    • head: 9 passed, 0 failed every run.
  • The Opus test only reaches that check when an Opus decoder is there. That's probably why the description and the other Wine reports only name the XMA2 one.

Not tested: the whole suite on the head, the e2e test title's XAudio2 check (XMA2 GetState: ctx=0x0, queued=1), and Windows.

AI use: T3 Code (Claude Opus 5.5) built both, ran the tests and wrote this comment. The runs are real, on the hardware above, and I checked the report before posting.

@crastinator crastinator left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Linux with Proton's Wine, in addition to the CrossOver run in the description: same failure on main, fixed by this PR.

Tested (head 76d2ff7, and merged onto main 1ec4c98: no conflicts)

  • AMD Radeon RX 6700 XT (RDNA2, gfx1031, RADV NAVI22, Mesa 26.3.1), Ryzen 7 5700X, Linux 7.3.5 (CachyOS)
  • Wine: Proton Experimental 11.0-20261112 (its files/bin/wine, kernel write watches), with its own vkd3d-proton (stock, not the #257 patch) and DXVK v3.1.1-209 dxgi; VKD3D_CONFIG=ignore_shared_fence; DXC 1.9.2609
  • Cross-built on Linux for x86_64-pc-windows-msvc (clang-cl / lld-link 23.1.5, xwin SDK 10.0.26100), Rust 1.99.1, debug; the lib test binary run under Wine
  • On main 1ec4c98, hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime fails here too, with left: 0, right: 12291 at xaudio2.rs:1599. That is Proton's Wine giving a null pCurrentBufferContext, the same as CrossOver.
  • hle::xaudio2:: --test-threads=1, 3 runs each: head 76d2ff7 9 passed / 0 failed every time. Merged onto 1ec4c98, also 9 / 0 every time. main: 8 passed / 1 failed.

Not tested

  • Windows (where the host gives a context and the new path should not be taken), a retail title, audible output.

AI use

  • Claude Code (Claude Opus 5.5) built the branches, ran the tests and wrote this report; I checked the results.

Update (merged onto main 930e8b4, vkd3d-proton v3.1 + #257 patch + #775's dxil-spirv, opus.dll from #257's build-opus.sh)

  • hle::xaudio2:: 9 passed / 0 failed in 3 runs, now including opus_source_voice_decodes_and_manages_lifetime, which only reaches the check with an Opus decoder. The e2e test title's XAudio2 check passes as well.
  • One gap: without opus.dll, which is a stock Wine/Proton setup since there is no MSOpusDecoder.dll there, the e2e test title still fails with Opus GetState: ctx=0x0, queued=1. The Opus voice then takes the silence fallback in submit_source_buffer. That voice is not in DECODED_VOICES, so get_state never reaches the new fallback, and the host's null context passes through. This might be out of scope here, but it should be named somewhere, since #257 makes libopus optional.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 datapoint.

Merged state: head 76d2ff7 onto main 077ce40, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 707 passed, 9 failed, 41 ignored; failures are the known Wine-lane set from my #42 rows plus this rig's title-textures flake family (see #718). GetState oldest-buffer naming green.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@vakaobr

vakaobr commented Nov 24, 2026

Copy link
Copy Markdown

Test report from macOS (CrossOver/Wine), head 76d2ff7 applied to main 9aec53d, with the cause on this host. Part of #258.

Under CrossOver 26.3 (Wine 11.0), GetState on a source voice that has queued buffers but has not started returns pCurrentBufferContext = NULL. In upstream FAudio, 24.01 to 26.03 only name a buffer once it has started, and 26.04 names queued_buffers[0], as Windows does; Wine 11.0 bundles FAudio 25.12 (I did not check the FAudio inside the CrossOver binary). The XMA2 lifetime test's left: 0, right: 12291 is that context (0x3003), not a sample count: the decode asserts before it pass. A temporary probe of the host's raw GetState showed ctx=0x0 queued=2 before Start and XCL's token after the first OnBufferStart.

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, Rosetta 2, CrossOver 26.3.0.39832; Rust 1.100.0, x86_64-pc-windows-gnu (mingw-w64 GCC 16.2.0), debug.
  • The 9 hle::xaudio2:: tests, one per process (--exact <name> --test-threads=1), 3 runs each: main 8 passed, 1 failed (xma2_source_voice_decodes_and_manages_lifetime), 0 ignored; with this PR 9 passed, 0 failed, 0 ignored.
  • opus_source_voice_decodes_and_manages_lifetime returns early in all runs ("MSOpusDecoder.dll not available"), so Opus under Wine is not covered here.
  • Mutation: with max_by_key instead of min_by_key in the fallback, the XMA2 test fails with 0x4004 (CTX_BAD).

Not tested

  • Windows, the whole suite, GPU tests, Opus under Wine, plain PCM voices (the fallback covers decoded voices only), any title.

AI use

  • Claude Code with Claude Opus 5.5 found the cause, added and removed the probe, ran the tests above on this machine and wrote this report.

@mikebuckets171

Copy link
Copy Markdown

Verified on a second host, on the gnullvm target: the failure reproduces on three different main commits and on the Mac port branch, and your commit fixes it on today's main.

Host: MacBook Pro M5 Pro (48 GB), macOS 27.2.1, CrossOver 26 with D3DMetal, Rosetta 2, Rust 1.100.0, x86_64-pc-windows-gnullvm with LLVM-MinGW 20261117 (clang 23.1.6), --release --locked, test binaries under Wine, one test per process (--exact <full test path> --nocapture --test-threads=1), a fresh data root for each run. hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime:

tree result
1ec4c98 (this PR's stated base) FAILED 2/2 - panicked at crates/xcl/src/hle/xaudio2.rs:1599:13: assertion left == right failed, left: 0, right: 12291, exit 0x65
beaa245 (the base the description ran on) FAILED 2/2 - identical
6efdabc (today's main) FAILED 3/3 - identical
mac-on-main-wip db065d8 (the Mac port branch) FAILED 2/2 - identical
6efdabc + this PR's commit 76d2ff7 cherry-picked (clean, +16 / -2) ok 4/4, 0.29-0.32 s each, exit 0x0

What the numbers are, since they read like a sample count and are not: the failing assert is assert_eq!({ st.current_buffer_context } as usize, CTX_VALID) at xaudio2.rs:1599, with const CTX_VALID: usize = 0x3003 = 12291 - so left: 0, right: 12291 is GetState's current-buffer token, exactly as your description says. Everything before it passes on this host: both submits return S_OK, stats().xma_buffers_decoded is +1 and xma_decode_errors +1 (lines 1593-1594), buffers_queued == 2 (1598). The assert sits before start(voice, 0, 0) at 1603, so what reads 0 is which buffer GetState names as current for a decoded voice with two buffers submitted and not yet started - not the XMA2 decode.

Two things worth having on the record:

  • Not a toolchain artifact. The same failure appears on the gnullvm / LLVM-MinGW build here and on the clang-cl + xwin MSVC build the description used, and on the Mac port branch as well as on main - so the fix is not papering over a cross-build difference.
  • The commit still applies to current main. Cherry-picked onto 6efdabc it applies cleanly (xaudio2.rs, +16 / -2, no conflicts) even though the PR's base is 1ec4c98, and the PR reports mergeable: true. So the pair above describes main today against your change, not against a stale base.

One methodological note, in case anyone else is about to run this pair: my first attempt at a "clean main" run looked like a pass, and it was a mistake of mine - git checkout -- . restores the working tree from the index, so a staged cherry-pick stayed in the sources while HEAD still read as the base commit. Re-run in a worktree whose working tree was verified clean (I checked for the patch's NEXT_BUFFER_SEQ marker in the file), and the base fails as above. Worth stating because "it passed here" would otherwise send someone down a dead end.

Host: macOS 27.2.1 (Apple silicon, M5 Pro), CrossOver 26, D3DMetal; local inference unloaded and the bottle's wineserver stopped for each run. AI use: GPT-6 Luna built every tree, ran the tests and drafted this comment; the counts, line numbers and codes are from those runs.

@pablogonz12

Copy link
Copy Markdown
Contributor

Test report on RDNA4 / Wine: 76d2ff7 merged onto main 9e3831e (clean merge). It fixes the XMA2 check of the e2e test title, and shows a second GetState gap that this change does not reach.

Tested

  • AMD Radeon RX 9070 XT (RADV, Mesa 26.3.1), Ryzen 9 9900X, CachyOS; Wine GE-Proton11-14, vkd3d-proton v3.1, DXC 1.9.2609.5; release, x86_64-pc-windows-msvc cross-built with cargo-xwin (static CRT), no Windows build.
  • cargo test -p xcl --lib binary under Wine, --test-threads=4, with --skip jumps_to_outer_loop_heads_are_structured (it aborts the whole binary here, main tip: jumps_to_outer_loop_heads_are_structured kills the whole lib test binary on Wine + RDNA3 (exit status 5), bisected to b8b1f49 #1246): 722 passed, 4 failed, 21 ignored. The four: gpu_f64_roots, gpu_f64_math_random, gpu_nonfused_mad_and_mac_prevent_fma_contraction, gpu_depth_buffer_created_for_unordered_access_is_placed. None is XAudio2; hle::xaudio2 alone: 9 passed.
  • tests/e2e.rs test_title_runs_and_game_folder_is_untouched, run alone:
    • main: [FAIL] XAudio2: ... XMA2 GetState: ctx=0x0, queued=1.
    • with this PR: the XMA2 check passes; the next check fails: [FAIL] XAudio2: ... Opus GetState: ctx=0x0, queued=1. The log of that run says Opus: decoder = none (MSOpusDecoder.dll not available; no opus.dll ...) and Opus decoder unavailable ... falling back to silence. The voice is then not one of the decoded voices (it takes the XMA silence path of submit_source_buffer), so get_state has no token to map and Wine returns a null context. The get_state change here only runs for voices in DECODED_VOICES. A host with libopus would take the decoded path; I did not run that.
    • The same run also fails the user32 ... not cloaked check (e2e: the console screen may be hidden where DWM cannot cloak it #1462 / user: hide title console window when DWM cloaking is unavailable (#175) #794), unrelated.

Not tested

  • Windows; Wine with libopus available; the retail title.

AI use

  • Written with Claude (Anthropic) assistance; run by me on the hardware above.

@shortside

Copy link
Copy Markdown
Author

@pablogonz12 thanks, that is the run this PR was missing (the test title does not get that far on my Mac). The second gap is real and wider than the Opus voice: under Wine any voice XCL does not decode reads that way before it starts, a plain PCM voice too. It is in #1698, stacked on this one, so this commit and the runs against it stay as they are. If you have a moment, the test title's Opus GetState check on your host (Wine without libopus) is the one thing I cannot run for it.

@crastinator

Copy link
Copy Markdown

@pablogonz12 matches what I see on RDNA2 under Wine (my review above, the Update section). The one case you didn't run, with libopus: given an opus.dll from #257's build-opus.sh (XCL_OPUS_DIR), the Opus voice takes the decoded path. The e2e test title's XAudio2 check then passes completely with this PR, and hle::xaudio2:: is 9/9 with opus_source_voice_decodes_and_manages_lifetime included (76d2ff7 merged onto main 930e8b4, RX 6700 XT, Mesa 26.3.1, 3 runs). So what's left is only the silence fallback for an Opus voice when there's no decoder (stock Wine/Proton).

@pablogonz12

Copy link
Copy Markdown
Contributor

On the merged main 689f2db the unit test this touches is red on AMD/RADV/Wine: hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime fails at crates/xcl/src/hle/xaudio2.rs:1599, GetState returns current_buffer_context = 0 where the test wants the playing buffer's 0x3003, both in the full run and run alone (--exact). RX 9070 XT, RADV/Mesa 26.3.1, Wine GE-Proton11-14, x86_64-pc-windows-msvc release with +crt-static.

At your head 76d2ff7 the same test passes here (722 passed, 4 failed, 21 ignored, the four being the known ones), so the test that main carries today is green with this PR and red without it.

Written with Claude (Anthropic) assistance; tested by me on the hardware listed.

@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (76d2ff7)

Verified the clean, detached PR #890 head 76d2ff732e8af140a40570f7de38289a16861380.

Hardware / environment

  • STOR-WINLAB, Windows x64 build 26200, AuthenticAMD Family 23 Model 113 Stepping 0.
  • rustc 1.100.0 (0ef1bc386 2026-11-09) (x86_64-pc-windows-msvc), DXC 1.9.2609.5 (01b62ad47), Cargo test profile with debug info and incremental compilation disabled.
  • Both commands ran directly in PowerShell outside Invoke-ArcTest.ps1. No Arc GPU mutex was acquired. The focused tests use no D3D12 or GPU and require a working Windows XAudio2 output endpoint.

Commands and results

  • cargo test --workspace --no-run exited 0 and built the workspace test executables.
  • cargo test -p xcl --lib source_voice_decodes_and_manages_lifetime -- --nocapture ran with XCL_ADAPTER=NONE_CPU_ONLY and RUST_TEST_NOCAPTURE=1, and exited 0. Cargo reported 2 passed, 0 failed, 0 ignored and 691 filtered out. The log contained 0 uppercase SKIPPED markers and 2 explicit skipping: no audio device messages. Counting those early returns as skips gives 0 behavioral passes and 2 skipped.

Observed behavior

The PR changes runtime code and adds or modifies no tests. The filter selected the existing XMA2 and Opus source-voice tests. Both created a system XAudio2 2.9 engine, then received 0x80070490 from CreateMasteringVoice and returned before submitting buffers. The checks for two queued buffers and the oldest buffer's original context therefore did not run.

Not tested

The full GPU workspace suite and game smoke runs were omitted. The XMA2 and Opus GetState assertions and the Wine/CrossOver null-context fallback remain unverified.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr890-focused.log and published.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@shortside

Copy link
Copy Markdown
Author

@pablogonz12 thanks for the run. The Opus case you hit behind this one is the gap #1698 covers (it is stacked on this PR); a run of that on your setup would be very welcome.

@pablogonz12

Copy link
Copy Markdown
Contributor

Ran the stacked head on Linux here.

Not tested: the full lib suite on this head, and any game run. Scope is the xaudio2 module. Written with Claude assistance.

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.

10 participants