Skip to content

XAudio2: honor requested alignment for 32-bit APU addresses in ApuAlloc - #1470

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:acphal/apu-alloc-alignment
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:acphal/apu-alloc-alignment

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

ApuAlloc(va, apu, size, alignment, flags) aligned the host VA via alloc_aligned(size, alignment), but Apu::address / Apu::find_free_range hardcoded 4 KiB (0x1000) alignment for the returned 32-bit APU address. After any earlier smaller allocation advanced APU.next off a larger boundary, a subsequent ApuAlloc requesting alignment > 0x1000 (e.g. 0x20000) returned a misaligned APU address.

This threads alignment through Apu::address_aligned and Apu::find_free_range, aligning candidate starts and overlap skips up to alignment.max(1).checked_next_power_of_two()?.max(0x1000) with checked arithmetic.

What is in it

  • crates/xcl/src/hle/acphal.rs: add align parameter to Apu::find_free_range, add Apu::address_aligned called by ApuAlloc, and add unit test apu_alloc_honors_alignment_for_apu_address (verified failing before the fix with apu % 0x20000 == 0x1000).

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 acphal: 8 passed, 0 failed (apu_alloc_honors_alignment_for_apu_address 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: 7fa09f37b99fc8feef6a063f4f4d07cbb50b73cb
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1643 (PresentX 1643)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14605 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/acphal.rs, wrote the fix and unit test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.

ApuAlloc aligned the host VA via alloc_aligned(size, alignment), but
Apu::address / Apu::find_free_range hardcoded 4 KiB (0x1000) alignment
for the returned 32-bit APU address. After any smaller allocation
advanced `APU.next` off a larger power-of-two boundary, subsequent
ApuAlloc calls with `alignment > 0x1000` returned a misaligned APU
address.

Pass `alignment` through `Apu::address_aligned` and `find_free_range`,
aligning both candidate starts and overlap skips up to
`alignment.max(1).next_power_of_two().max(0x1000)` with checked math.

Assisted-by: OpenCode (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 7fa09f3 (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: apu_alloc_honors_alignment_for_apu_address.
  • 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

Copy link
Copy Markdown
Contributor

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!

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

amd-win test report for 7fa09f37 (exact head 7fa09f37b99fc8feef6a063f4f4d07cbb50b73cb) on AMD Radeon RX 7800 XT

Tested

  • AMD Radeon RX 7800 XT 16 GB (RDNA3), driver 32.0.31035.1003; Ryzen 7 5700X3D; Windows 11 build 26200; rustc 1.100.0 MSVC; DXC 1.8.2502.11 (Windows SDK 10.0.26100); debug build for the tests, release build for the smoke runs
  • The PR head exactly as pushed (its base is older than current main; the merged tree was not tested). Merges cleanly into origin/main 55d64c27 (git merge-tree)
  • Focused: cargo test -p xcl --lib acphal: 8 passed, 0 failed (includes the new apu_alloc_honors_alignment_for_apu_address)
  • Full suite, cargo test --workspace -- --test-threads=4: 771 passed, 0 failed, 41 ignored; no SKIPPED lines printed

Regression run (own test title tests/testtitle, 60 s, release; same title and time for both. The test title exits with code 2 by design and shows no frames, so crashes: 1 is its normal exit and is the same on both)
PR head:

xcl-smoke v1
commit: 7fa09f37b99fc8feef6a063f4f4d07cbb50b73cb
gpu: AMD Radeon RX 7800 XT
title: testgame
seconds: 60
exit: 0x2
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 
device removals: 0
crashes: 1
peak commit: n/a MB

main (b7aa9f3):

xcl-smoke v1
commit: b7aa9f3bd212088e0d927439284dc5ed325ce3c7
gpu: AMD Radeon RX 7800 XT
title: testgame
seconds: 60
exit: 0x2
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 
device removals: 0
crashes: 1
peak commit: n/a MB

Not tested

  • No retail title: this is a run of our own test title only, so the smoke run does not reach the code this PR changes and says nothing about frames or frame time
  • Other GPUs (NVIDIA / Intel), wave64-only paths on other adapters, and the merged tree on current main

AI use

  • Run and written up by Claude Code (Claude Sonnet 5.5) from real runs on the hardware named above, for oni-swr. No code was changed.

@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 29, 2026
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check claim
head: 7fa09f3

@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 9dfbe3e
head: 7fa09f3
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2886 passed, 1 failed, 51 ignored; failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, which fails on the base alone too (base: 2885 passed, 1 failed, 51 ignored, 28 SKIPPED lines). 28 SKIPPED lines, as on the base. The new test hle::acphal::tests::apu_alloc_honors_alignment_for_apu_address passed, as did the existing acphal tests.
review: no blocking finding; one file, every hunk read. ApuAlloc aligned the host address to the requested alignment but handed out a 32-bit APU address aligned to a page only. find_free_range now takes the alignment (rounded up to a power of two, at least 0x1000), aligns the first candidate and each candidate after an overlap with checked arithmetic, and ApuAlloc passes its alignment; the other caller keeps page alignment through address. For alignments up to a page the candidates are the same as before. One thing to know: a larger alignment can leave gaps below the aligned address; they are found again on the wrap to APU_BASE. Not tested by me: a title that asks for an alignment above a page.
performance: none to speak of.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@tiagonleal

tiagonleal commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 9dfbe3e
head: 7fa09f3
merge: clean
build: cargo build --workspace -> ok (also cargo build --release -p xcl)
tests: cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4 -> 2886 passed, 1 failed, 51 ignored; SKIPPED: 24 lines, the same as on the base; failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, which fails the same way on the base alone (2885 passed, 1 failed, 51 ignored; #3513 fixes it). hle::acphal::tests::apu_alloc_honors_alignment_for_apu_address ran and passed. With ApuAlloc calling the old page-aligned address again, it fails at acphal.rs:1328 (APU address 0x20000-aligned: left 4096, right 0); file restored after.
review: one file, whole diff read. find_free_range (acphal.rs:221) rounds the alignment up to a power of two and to at least a page (acphal.rs:225), the same rounding alloc_aligned uses for the host address (acphal.rs:276), so the two agree for odd values too. Candidate starts and overlap skips are aligned with checked adds; an overflow ends the search and ApuAlloc returns E_OUTOFMEMORY after freeing the host memory (acphal.rs:317-320), as before. ApuMapVirtualAddress still uses page alignment through address. What the title asks for, measured: with XCL_TRACE_APU=1 on the base and on the merge, the title makes three ApuAlloc calls in its menus, with alignments 0x100, 0x800 and 0x800, and gets the same APU addresses on both (0x10000000, 0x10006000, 0x10229000). No alignment above a page is requested there, so the changed path was not reached by the title.
performance: scripts/smoke.ps1, 60 s of a retail title, release: 1653 frames and 17995 MB peak commit against 1659 frames and 17322 MB on the base (today's base run after one warm-up). The traced pair: 1673 frames, 18272 MB against 1670 frames, 18142 MB on the base. No device removal, no crash in any of the four. Audio initialised as on the base: the same acphal and xapu lines (ApuCreateHeap, DSP connected with 128 streams, audio processor connected, xapu: decoder connected with 256 streams).
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB (617.42, wave32), Ryzen 7 9800X3D, 32 GB, Windows 11 26300, rustc 1.100.0 / Claude Code (Claude Opus 5.5 ran the build, suite, tests and smoke, reviewed the diff against the merged source and wrote this)

  • Tiago

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 7fa09f3

#939 makes the same find_free_range change, rounding candidates to the requested alignment, which is passed from ApuAlloc. #939 also changes the ApuMapVirtualAddress / ApuIsVirtualAddressValid ABI, which this PR does not touch. If #939 is merged as is, this PR is superseded; if its ABI change is not taken, this is the alignment fix on its own. The change is not on main.
No problems found in the diff: align_up is checked, non-power-of-two alignments round up, and apu_alloc_honors_alignment_for_apu_address checks the APU address and the VA.

Recommendation: keep open.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH ready-to-merge Has 2 independent safe merge-checks on current head (#3041) stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants