acphal: ACP message queue and background XMA ring decoding with the Rust XMA2 decoder (RT-ACPHAL-XMA2) #404

Manually merged
github merged 10 commits from gh-pr/404/head into gh-pr/404/base 2026-11-26 08:58:14 +00:00
Owner

@⁠Andretuta on GitHub · 2026-11-24 04:44 UTC · original
ubiquitarianism:feat/acphal-xma2-decoder → main · +1518 −120 · 10 files · 10 commits · merged 2026-11-24 13:44 UTC

Claimed in the coordination issue as: RT-ACPHAL-XMA2 (#14)

Depends on: #357

What and why

  • Replaces the acphal.dll stub with an ACP command and message queue and a worker that fills SHAPE XMA PCM rings. The worker follows packet skips and refill order, updates shared fields atomically, and bounds each pass to the ring. Repeated enables preserve queued PCM for the same format and reset it when the format changes.
  • CMake builds the Rust decoder in runtime/xma_decoder at configure time through Cargo. The shim outputs silence based on each packet's frame count when Cargo is missing, the build fails, or the direct cl.exe fallback is used. Windows runtime CI explicitly requires the decoder.
  • The hardware constructor keeps the first frame and leaves start skipping to the XMA context. The decoder consumes 2048-byte packets, so read and loop points are rounded to packet boundaries. A loop plays its end packet in full and restarts with a fresh decoder at the start packet. The loop-end subframe is ignored.
  • Each worker uses its own std::jthread stop token. A worker memory failure reports an error for the affected voice. SHAPE allocation publishes a complete table or returns E_OUTOFMEMORY. Release detaches the worker, then frees the process-wide arrays without writing through the opaque HAL handle.
  • APU mappings stay stable after explicit heap configuration. Each voice reuses contiguous PCM storage while consuming its packets.

Tested

  • MSVC 19.44.35230 and Windows SDK 10.0.26100 built and linked gta6rt and acphal_test in Debug and Release with Rust. A separate CMake configuration built acphal_test with the silence fallback in both build types. CTest passed in all four configurations, with 170 checks per normal run with Rust, 166 with silence, and 14 for the separate non-cached-only case in each configuration.
  • MSYS2 Clang 23.1.3 passed three normal runs and the non-cached-only case in both decoder modes. Synthetic tests cover exact PCM block counts, non-silent Rust output across loop rewinds and refills, mono and stereo at all four rates, ring bounds, and allocation failure. In MSVC Release with the silence fallback, a six-frame input required 1 C++ worker allocation, down from 208 on snapshot 4e9407a40.
  • Cargo and rustc 1.97.1 passed formatting and all 10 Rust tests on both x86_64-pc-windows-msvc and x86_64-pc-windows-gnullvm, including a first-frame test for the hardware constructor. Locked release builds succeeded on both targets.
  • Python 3.14.2 and pytest 9.0.2 passed 204 tests and 200 subtests, with 3 skipped. The game-data check passed.
  • With an empty offline Cargo cache and an older archive present, the optional CMake build selected the silence fallback. The configuration requiring Rust failed as expected.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — audio decoder & runtime shim only)

🤖 Generated with Claude Code

**@⁠Andretuta** on GitHub · 2026-11-24 04:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/404) `ubiquitarianism:feat/acphal-xma2-decoder` → `main` · +1518 −120 · 10 files · 10 commits · merged 2026-11-24 13:44 UTC Claimed in the coordination issue as: `RT-ACPHAL-XMA2` (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) Depends on: #357 ### What and why - Replaces the `acphal.dll` stub with an ACP command and message queue and a worker that fills SHAPE XMA PCM rings. The worker follows packet skips and refill order, updates shared fields atomically, and bounds each pass to the ring. Repeated enables preserve queued PCM for the same format and reset it when the format changes. - CMake builds the Rust decoder in `runtime/xma_decoder` at configure time through Cargo. The shim outputs silence based on each packet's frame count when Cargo is missing, the build fails, or the direct `cl.exe` fallback is used. Windows runtime CI explicitly requires the decoder. - The hardware constructor keeps the first frame and leaves start skipping to the XMA context. The decoder consumes 2048-byte packets, so read and loop points are rounded to packet boundaries. A loop plays its end packet in full and restarts with a fresh decoder at the start packet. The loop-end subframe is ignored. - Each worker uses its own `std::jthread` stop token. A worker memory failure reports an error for the affected voice. SHAPE allocation publishes a complete table or returns `E_OUTOFMEMORY`. Release detaches the worker, then frees the process-wide arrays without writing through the opaque HAL handle. - APU mappings stay stable after explicit heap configuration. Each voice reuses contiguous PCM storage while consuming its packets. ### Tested - MSVC 19.44.35230 and Windows SDK 10.0.26100 built and linked `gta6rt` and `acphal_test` in Debug and Release with Rust. A separate CMake configuration built `acphal_test` with the silence fallback in both build types. CTest passed in all four configurations, with 170 checks per normal run with Rust, 166 with silence, and 14 for the separate non-cached-only case in each configuration. - MSYS2 Clang 23.1.3 passed three normal runs and the non-cached-only case in both decoder modes. Synthetic tests cover exact PCM block counts, non-silent Rust output across loop rewinds and refills, mono and stereo at all four rates, ring bounds, and allocation failure. In MSVC Release with the silence fallback, a six-frame input required 1 C++ worker allocation, down from 208 on snapshot `4e9407a40`. - Cargo and rustc 1.97.1 passed formatting and all 10 Rust tests on both `x86_64-pc-windows-msvc` and `x86_64-pc-windows-gnullvm`, including a first-frame test for the hardware constructor. Locked release builds succeeded on both targets. - Python 3.14.2 and pytest 9.0.2 passed 204 tests and 200 subtests, with 3 skipped. The game-data check passed. - With an empty offline Cargo cache and an older archive present, the optional CMake build selected the silence fallback. The configuration requiring Rust failed as expected. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — audio decoder & runtime shim only) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Add portable C++20 XMA2 packet parser and WMA Pro frame decoder
  (runtime/src/xma2_decoder.{h,cpp}, following FFmpeg wmaprodec.c /
  wmaprodata.h LGPL-2.1-or-later under Leonida's GPL-3.0-or-later)
  with cross-platform xma2_decoder_test.
- Implement IAcpHal command/message queue protocol (REGISTER_MESSAGES,
  UNREGISTER_MESSAGES, LOAD_FLOWGRAPH, COMMAND_COMPLETED, DISCONNECTED,
  queue-full dropped counters, and APU notification word writes) in
  runtime/shims/acphal.cpp.
- Wire a lazily-started, cleanly-joined background XMA2 decoder thread
  pumping enabled SHAPE_XMA_CONTEXT output rings in APU memory.
stop_worker set a shared stopping_ flag, moved the thread out and joined it
after releasing worker_mutex_. A wake in that window reset stopping_ and
started a second worker, so the old one kept running and the join could hang.
Each worker is now a std::jthread with its own stop_token (waits use
condition_variable_any with the token), so a stop only reaches its own thread.

acphal_test races 20000 context alloc/release rounds against ENABLE_XMA on a
second thread (the unfixed code hung there intermittently).

AI-assisted. Tested: acphal_test 115 checks, 0 failures, repeated runs
(clang 23 / mingw x64).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the hand-written C++ XMA2/WMA Pro decoder with the Rust decoder
already on main. runtime/CMakeLists.txt builds it at configure time when
cargo is on PATH and links it into gta6rt and acphal_test
(LEONIDA_HAVE_XMA2_DECODER); without cargo, if that build fails, or with
the cl.exe fallback, XMA voices output silence for each packet's frame
count.

The ring engine decodes whole 2048-byte packets: the read offset and the
loop points are used per packet, so a loop plays its end packet in full
and restarts with a fresh decoder at the packet holding the loop start.
The C ABI gains leonida_xma2_decoder_create_hardware (no first-frame drop,
no 64-sample start skip), since the context carries its own skip.

Tested: acphal_test 129 checks (silence) and 125 checks (Rust decoder,
gnullvm build), 0 failures, 3 runs each, clang 23 / mingw x64; cargo fmt
and cargo test (gnullvm); pytest build/docs suites 204 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:58:12 +00:00
github manually merged commit 713eec5031 into gh-pr/404/base 2026-11-26 08:58:14 +00:00
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 14:10 UTC · original

Post-merge check of main d0da7dade on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo, cargo 1.98.0): the XMA2 decoder builds and links, the full build succeeds, and full ctest is 304/306 with the same two pre-existing failures as before (d3d12_execute_indirect_validation, d3d12_depth_resolve). The new acphal_noncached_only passes. Without cargo on PATH, configure prints "cargo not found" and skips the decoder, and acphal_test still passes 166/166.

Two supply-chain notes now that CMake builds this by default and CI requires it (LEONIDA_REQUIRE_XMA2_DECODER=ON, runtime.yml:260):

  1. The only dependency, xma, is a git dependency on a personal repo created 2026-11-21 (runtime/xma_decoder/Cargo.toml:13, rev 27122f7). Git sources get no checksum in Cargo.lock, so the commit hash is the only integrity check. If that repo is deleted or force-pushed, every cold build loses the decoder and CI fails. I read the pinned crate: it has no build.rs, no proc-macros, no unsafe, and no network or file access outside its tests, so it's fine today. Vendoring it, with cargo vendor or a fork under this org, would make the build reproducible. CMake runs cargo with --locked but not --offline (runtime/CMakeLists.txt:112-145), so a cold configure fetches from GitHub.
  2. Linking the Rust staticlib pulls Rust std's networking into gta6rt.exe: the cargo build now has a static WS2_32.dll import (getaddrinfo, connect, sendto, WSASocketW…) plus USERENV and bcryptprimitives, which the no-cargo build doesn't. The title can't reach them through the shim tables, so this isn't a leak, but it contradicts the "gta6rt needs no static ws2_32.lib link dependency" note in the ws2_offline.cpp header. That note, or the docs, should mention it.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 14:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/404#issuecomment-7133956076) Post-merge check of main d0da7dade on Windows 11 / RTX 4070 SUPER (VS 2022, Ninja, RelWithDebInfo, cargo 1.98.0): the XMA2 decoder builds and links, the full build succeeds, and full ctest is 304/306 with the same two pre-existing failures as before (d3d12_execute_indirect_validation, d3d12_depth_resolve). The new `acphal_noncached_only` passes. Without cargo on PATH, configure prints "cargo not found" and skips the decoder, and `acphal_test` still passes 166/166. Two supply-chain notes now that CMake builds this by default and CI requires it (`LEONIDA_REQUIRE_XMA2_DECODER=ON`, runtime.yml:260): 1. The only dependency, `xma`, is a git dependency on a personal repo created 2026-11-21 (`runtime/xma_decoder/Cargo.toml:13`, rev 27122f7). Git sources get no checksum in Cargo.lock, so the commit hash is the only integrity check. If that repo is deleted or force-pushed, every cold build loses the decoder and CI fails. I read the pinned crate: it has no build.rs, no proc-macros, no `unsafe`, and no network or file access outside its tests, so it's fine today. Vendoring it, with `cargo vendor` or a fork under this org, would make the build reproducible. CMake runs cargo with `--locked` but not `--offline` (`runtime/CMakeLists.txt:112-145`), so a cold configure fetches from GitHub. 2. Linking the Rust staticlib pulls Rust std's networking into gta6rt.exe: the cargo build now has a static `WS2_32.dll` import (getaddrinfo, connect, sendto, WSASocketW…) plus USERENV and bcryptprimitives, which the no-cargo build doesn't. The title can't reach them through the shim tables, so this isn't a leak, but it contradicts the "gta6rt needs no static ws2_32.lib link dependency" note in the `ws2_offline.cpp` header. That note, or the docs, should mention it. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.