Skip to content

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

Merged
ubiquitarianism merged 10 commits into
mainfrom
feat/acphal-xma2-decoder
Nov 24, 2026
Merged

ubiquitarianism merged 10 commits into
mainfrom
feat/acphal-xma2-decoder

Conversation

@Andretuta

@Andretuta Andretuta commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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

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

…orker

- 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.
Andretuta and others added 5 commits November 24, 2026 03:05
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 <noreply@anthropic.com>
…ailable

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 <noreply@anthropic.com>
demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
…dd portable XMA2/WMA-Pro decoder, ACP message queue and background XMA ring decoding (RT-ACPHAL-XMA2) [Andretuta]

# Conflicts:
#	runtime/shims/acphal.cpp
#	scripts/build_runtime.ps1
@Andretuta Andretuta changed the title acphal: add portable XMA2/WMA-Pro decoder, ACP message queue and background XMA ring decoding (RT-ACPHAL-XMA2) acphal: ACP message queue and background XMA ring decoding with the Rust XMA2 decoder (RT-ACPHAL-XMA2) Nov 24, 2026
@ubiquitarianism
ubiquitarianism merged commit d0da7da into main Nov 24, 2026
8 of 15 checks passed
@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Post-merge check of main d0da7da 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).

demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
Brings 58 PRs integration did not have (among them ProjectLeonida#359 ProjectLeonida#360 ProjectLeonida#402 ProjectLeonida#497 ProjectLeonida#557 ProjectLeonida#558
ProjectLeonida#560 ProjectLeonida#561 ProjectLeonida#572 ProjectLeonida#573 ProjectLeonida#574 ProjectLeonida#577) and the newer heads of ProjectLeonida#401 and ProjectLeonida#404. 15 files
conflicted; resolved by hand:

- frame_pacer.h, d3d12_xs_device.h/.cpp: ProjectLeonida#402's pacer (per-event ticks,
  issue_token_locked) now issues from ProjectLeonida#414's shared FrameTokenSource, so a token
  from the device-side WaitFrameEventX is one the pacer accepts (towade's ProjectLeonida#14
  finding: without it every PresentX is refused). The dead frame_next_token and
  issued_ counters are dropped. Queue slot 32 stays ProjectLeonida#414's PresentX; ProjectLeonida#402's
  presentation-handler routes serve slot 30 or LEONIDA_XS_PRESENT_X_SLOT.
  ProjectLeonida#402's GetFrameStatisticsX route moves into ProjectLeonida#385's route_xbox_device_slot.
- acphal.cpp/acphal_test.cpp: upstream's reworked SHAPE allocation and release;
  the release writes nothing through its first argument, as ProjectLeonida#405 requires; both
  sides' tests kept. Upstream's ProjectLeonida#401/ProjectLeonida#404 heads remove lifecycle.cpp and the C++
  xma2_decoder.cpp (replaced by runtime/xma_decoder), so build_runtime.ps1's
  cl.exe list drops them and keeps ProjectLeonida#549's and ProjectLeonida#400's files.
- vulkan_backend.h, statistics_sum.comp, its SPIR-V header, d3d12_mesh_test,
  replay_query_edges_test, d3d12_vulkan_device.cpp, docs: ProjectLeonida#577's clipping
  counter on top of ProjectLeonida#540, which both sides carry; the Timestamps sentence keeps
  integration's corrected wording (c33ce87).
- xs_proxy_mock_test.cpp: both sides' cases; pr_conflict_label.py keeps the
  guarded reconfigure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

3 participants