Skip to content

kernelx: the finest system timer for the title (choppy audio, #260) - #300

Merged
theaustinayers merged 1 commit into
xcl-dev:mainfrom
AAigars:kernelx-timer-resolution
Nov 24, 2026
Merged

theaustinayers merged 1 commit into
xcl-dev:mainfrom
AAigars:kernelx-timer-resolution

Conversation

@AAigars

@AAigars AAigars commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #260.

The choppy sound in the retail title is not the audio processor: the game mixes on the CPU and plays through Windows spatial audio (ISpatialAudioClient, two static objects, 48 kHz float), and that stream was fed on time but with sound in only 34 % of its 10 ms passes. The mixer sleeps between 5.33 ms chunks, and without a finer timer Windows wakes it on its 15.6 ms tick, so it made a third of real time (5.33 / 15.6 = 34 %). XCL now asks for the finest timer Windows offers (0.5 ms) at start, and opts out of the Windows 11 power throttling that drops it while the window is hidden. With it every pass has sound.

What is in it

  • kernelx::fine_timer (crates/xcl/src/hle/kernelx.rs): NtSetTimerResolution to the finest resolution NtQueryTimerResolution reports, SetProcessInformation(ProcessPowerThrottling, IGNORE_TIMER_RESOLUTION), one timer: log line; test fine_timer_makes_short_sleeps_short (20 × Windows Sleep(1), average under 5 ms: 15.4 ms without the fix, fails; passes with it)
  • called from host_main after hle::init (crates/xcl/src/host.rs)

Tested

  • NVIDIA GeForce RTX 4070, driver 32.0.16.2101; AMD Ryzen 7 9800X3D, 32 GB; Windows 11 Pro build 26200; Rust 1.96.1; release
  • cargo test --workspace --release -- --test-threads=4: 652 passed, 0 failed, 40 ignored (SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware – no wave64)
  • Retail title (<package id> 1.0.2.1), XCL_NETWORK=0: the menu's sound is continuous instead of choppy. Measured with temporary logging on the game's spatial audio stream (not in this PR): before, sound in 172 of 500 passes per 5 s, evenly spread; after, 500 of 500 once the sound starts.

Regression run

main:

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: NVIDIA GeForce RTX 4070
title: 83d1f0b1
seconds: 60
exit: 0x0
frames shown: 1487 (PresentX 1487)
presents per 2 s (last 3): 60 61 59
device removals: 0
crashes: 0
peak commit: 14673 MB

PR:

xcl-smoke v1
commit: 9507d05496be7bd87ca769d95cdfec705a3bd6cf
gpu: NVIDIA GeForce RTX 4070
title: 83d1f0b1
seconds: 60
exit: 0x0
frames shown: 1482 (PresentX 1482)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 14667 MB

5 frames fewer over 60 s (0.3 %) at the same rate at the end; one run each, so within run-to-run noise.

Not tested

  • AMD GPUs; Windows 10 (where the power-throttling opt-out does not exist and the call fails harmlessly)
  • other titles (Minecraft, Unicorn Overlord, Fortnite, XDefiant)
  • debug build
  • power use with the finer timer (laptops)

AI use

  • Claude Code, Claude Opus 5.5: found the cause (temporary logging on the game's audio calls), wrote the code, the test and this description; I read the diff, ran the tests and the regression runs, and listened to the game before and after.

…#260)

Windows wakes a sleeping thread on its 15.6 ms tick unless the process
asks for a finer timer. The retail game's audio mixer sleeps between
5.33 ms chunks, so it made one chunk per tick, a third of real time:
measured on its spatial audio stream, sound in 34 % of the device's
10 ms passes and silence in the rest. With the finest timer (0.5 ms),
every pass has sound. Windows 11 keeps it while the window is hidden
only when the process opts out of power throttling.

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

Copy link
Copy Markdown

Independent native Windows/NVIDIA validation of #300 at 9507d05496be7bd87ca769d95cdfec705a3bd6cf (clean checkout).

Tested

  • Ryzen 5 3600, RTX 5060 Ti (NVIDIA 621.14 / 32.0.16.2114), 32 GB RAM; Windows 11 Pro 10.0.26300; Rust 1.100.0 MSVC, MSVC 19.44, SDK 10.0.26100.0, DXC 1.9.2602.17. Debug build, an environment listed as not tested in the PR description.
  • cargo build --workspace: exit 0; one pre-existing fetch_update deprecation warning.
  • cargo test --workspace -- --test-threads=4 --nocapture: exit 0; 652 passed, 0 failed, 40 ignored over 17 result blocks (lib: 612 passed, 0 failed, 20 ignored). The synthetic-title end-to-end test passes.
  • fine_timer_makes_short_sleeps_short passes. Log: was 15.62 ms, now 0.50 ms; kept while the window is hidden: yes.

Separate before/after probe, using the actual PR function
A small local Rust executable depends on this checkout's xcl library. In one fresh process, it measures three batches of 20 calls to Windows Sleep(1), calls xcl::hle::kernelx::fine_timer() once, and measures three more batches. Instant measures each batch; no std::thread::sleep, no title is loaded. It was run after the workspace tests ended, using cargo run --offline --manifest-path <local-probe>/Cargo.toml.

batch mean Sleep(1), before mean Sleep(1), after
1 8.279205 ms 1.476785 ms
2 7.283230 ms 1.541250 ms
3 7.638460 ms 1.572365 ms

This reproduces a shorter wait after the timer request on this host. The before values are the measured 7-8 ms, not assumed to equal the queried 15.62 ms system resolution. This does not measure audio quality, mixer continuity, or game performance.

SKIPPED / Not tested
Four explicit skips: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64); test_title_draws_with_a_root_signature_under_the_debug_layer (no d3d12SDKLayers.dll); uploads_start_from_each_subresources_state (no D3D12 debug layer, 0x887A002D). No XCL_SAMPLE variables were set. No retail title/smoke blocks, no listening test, no verification with an occluded/minimized title window, no power-use measurement, AMD, Wine or Windows 10. The opt-out API's reported success is not an independent check of hidden-window behavior.

AI use
Codex wrote the local probe, executed the build/tests/probe and summarized the actual logs. Exact deployed model identifier is not exposed in this session. No runtime changes or game data published.

@mosin311aa

Copy link
Copy Markdown

Can confirm this fixes the choppy audio problem on my RTX 4070 Super system, no idea if this is the best solution though

@mastrr9

mastrr9 commented Nov 23, 2026 •

Copy link
Copy Markdown

Independent static code & Win32 API audit of 9507d0549 (code inspection only — cargo test not run on macOS; no game files):

Summary: Clean and matches Windows API semantics

  • PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION (kernelx.rs:1293-1299): Setting ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION with StateMask = 0 matches Microsoft Learn for opting out of Windows 11's occluded/background timer-resolution throttling, and ignoring a FALSE return on Windows 10/Wine degrades gracefully.
  • NtQueryTimerResolution / NtSetTimerResolution (kernelx.rs:1272-1273, 1301-1307): Signatures ((u32, u8, *mut u32) -> i32) and parameter ordering (coarsest, finest, current) match ntdll, and the Windows kernel automatically releases per-process NtSetTimerResolution requests on process exit/crash.

Two small notes

  1. Closing keyword (Fixes #260): Since xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread #260 was updated to track both the 15.6 ms timer tick and xapu returning silence for Opus packets (xapu: decode the title's Opus packets and play them on the PC (retail title's sound) #294), changing Fixes #260. to Part of #260. avoids auto-closing xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread #260 before xapu: decode the title's Opus packets and play them on the PC (retail title's sound) #294 lands.
  2. Connection to xapu: decode the title's Opus packets and play them on the PC (retail title's sound) #294 (ISpatialAudioClient): Your finding that the title mixes on the CPU and outputs through ISpatialAudioClient directly answers xapu: decode the title's Opus packets and play them on the PC (retail title's sound) #294's open question ("Where the title's final mix goes is not found yet") — worth checking whether xapu: decode the title's Opus packets and play them on the PC (retail title's sound) #294's xaudio2::console_pcm direct playback double-plays decoded xapu audio on Windows once xapu writes real PCM into the title's buffer.

(AI-assisted — Gemini 4 Neon)


Update: Windows 11 / RTX 5080 test run on 9507d0549

Tested

  • Hardware / OS / toolchain: NVIDIA GeForce RTX 5080 (driver 617.42), AMD Ryzen 7 9800X3D, Windows 11 25H2 (build 26200.9711), rustc 1.100.0 (MSVC), debug build in a clean worktree at 9507d05496be7bd87ca769d95cdfec705a3bd6cf.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 652 passed, 0 failed, 40 ignored (xcl lib: 612 passed, 0 failed, 20 ignored; all e2e/GPU suites passed).
  • SKIPPED GPU tests (2, wave32-only adapter): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.
  • hle::kernelx::tests::fine_timer_makes_short_sleeps_short passed.

Not tested

  • Retail title / scripts/smoke.ps1 run on this machine (test suite only).
  • AMD / Intel GPUs.

AI use: test run orchestrated and reported with Antigravity (Gemini 4 Neon) on the hardware listed above.

@ryosen0

ryosen0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Data point on the intro slow stretch (our machine: RTX 3060 Ti 8 GB, Ryzen 7 9800X3D, driver 32.0.16.1742): the retail title's artwork intro runs at ~4.4-8.6 fps for roughly t=45-165 s, then a steady 30 fps. We measured the same dip with and without this PR + #294 in the stack (same graphics PRs otherwise, XCL_NETWORK=0, XCL_ACTIVITY=1):

So the dip is not the audio path on this hardware - noting it so nobody chases the timer / Opus work for it. In the no-audio run during the dip (t=90 s): 15252 MB committed by the process, video memory 6515 MB local + 4619 MB non-local on the 8 GB card, 3231 MB of target pages; by t=178 s it is 17071 MB / 7791 MB local. DXC compiles are all cache hits there (dxil_cache 25 hits, 0 misses). It looks like memory pressure and streaming, not compilation.

Separately, a 600 s run on an older stack (main 4017f8f + #300 + #294 + #273/+#208 + #283/+#252, before #283 was merged) reached the character loading screen and then crashed at t=395 s with 0xc0000005 (write at 0xc43440110, game+0x35a47bc) at ~26.6 GB of commit - past the loading screen, so probably unrelated to this PR.

AI use: GPT-6.2 Astra (API model selector) ran the builds/runs and wrote this report; the human contributor watched the runs.

@emirduran707

Copy link
Copy Markdown
Contributor

First AMD run: the sound plays cleanly with this PR.

Setup: AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; release. Build: main a6f5f7a + this PR (9507d05) + #440 (be722a7, so the intro video runs at speed) + #435 (a94e151), merged locally. Same retail title as #260, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_FRAME_DUMP=60, A pressed by hand on the first menu, a person listening.

  • Log: timer: finest resolution for the title (was 1.00 ms, now 0.50 ms; kept while the window is hidden: yes).
  • Heard: the sound is continuous through the intro video and the loading screen (it was choppy on the same machine without this PR).
  • Measured from the xapu: … Opus packets decoded lines: 50 packets a second from the video on (960 frames each, so real time at 48 kHz), 100 a second once a second stream starts. Without this PR, on main + Textures: linear levels are detiled a row at a time #440 only, the same stretch decoded about 17 packets a second (~0.34 of real time).

Not tested / not attributed: the run ended at 161 s with 0xc0000005 in title code (+0x23cf683, write at address 0) while the world streamed in, at 27 GB of process commit, with no E_OUTOFMEMORY logged. With three PRs in the build and no longer baseline run on this machine I cannot tie it to any of them. NVIDIA, other titles.

AI use: Claude Code with Claude Opus 5.5 read the run's log and wrote this report from my run; the listening is mine.

@Chrysana

Copy link
Copy Markdown

Test report for #300 at 9507d05 on AMD RX 9070 XT, merged onto current main 508a0c7 (local merge 1a47cac, clean, not pushed). A person listened to each run.

Setup: AMD Radeon RX 9070 XT 16 GB, driver 32.0.32015.2008, Ryzen 7 9800X3D, 31 GB RAM, Windows 11 25H2 build 26200.9974, rustc 1.100.0 x86_64-pc-windows-msvc, release builds. Retail title 1.0.2.1. XCL_NETWORK=0 XCL_RUN_SECONDS=60 XCL_ACTIVITY=1 XCL_PRESS_A_AT=14,17,21 xcl run <folder>, one run per build.

build what was heard
main 508a0c7 slow, crackly sound throughout
main + #300 the "screen narration" prompt plays at full speed and clean; no game sound after it while the intro video plays at ~5 fps (542 → 555 frames in 60 s)
main + #300 + #451 (1f282c7, local merge e6daa0e) the intro video and its sound play together at full speed; one short stutter in the middle (1507 frames)
  • The log line from this PR: timer: finest resolution for the title (was 1.00 ms, now 0.50 ms; kept while the window is hidden: yes). Another program had already set the system timer to 1 ms; the title's audio was choppy anyway.
  • report.json's xapu counters are the same in all three runs (23 Opus packets, 21768 frames, about 0.45 s). So the intro's sound doesn't go through xapu. It comes from the title's CPU mix, and it seems to need the video running at speed.
  • So, on its own, this PR fixes the speed of the sound, but the intro stays silent until the video runs at speed. With D3D12.X: faster title texture uploads (reused upload memory, faster detiling) #451 (or another video speed-up) the two together give continuous sound. That may matter for Fixes #260 if this merges first.

Not tested: the test suite and smoke.ps1 on this PR (none of the above is a regression run); gameplay past the intro; the cause of the single stutter; Windows 10.

AI use: Claude Code with Claude Opus 5.5 built the merges, ran the title and read the logs and report.json, and wrote this report. The listening was done by me.

@Kalmarv

Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Regression run for this PR's head on Windows.

Tested

  • Windows 11 Pro 25H2 (build 26200), AMD Ryzen 9 9950X, 32 GB RAM, NVIDIA GeForce RTX 5070 Ti (driver 610.74), Rust 1.99.0, release builds (cargo build --release -p xcl) from clean checkouts
  • main: 0e946cfc68244edee1fba6e0425371f687a05ccd (a commit on main with no code change from this PR's base 9ab7d76); PR head: 9507d05496be7bd87ca769d95cdfec705a3bd6cf, no uncommitted changes
  • scripts/smoke.ps1 -Game <title folder> -Seconds 60 on a retail title, main first, after one 60 s warm-up run of main; both sides start from the same learned coherent-window size
  • PR 1534 frames vs main 1420 (108 %), peak commit 14728 vs 14728 MB (100 %); 0 crashes and 0 device removals on both sides, both exited 0x0
  • The change is reached: the PR's run logged timer: finest resolution for the title (was 1.00 ms, now 0.50 ms; kept while the window is hidden: yes); main's run has no timer line
  • By ear: earlier runs of main on this machine crackled; this PR sounded normal in both of its runs (the pair above and one more 60 s run). One listener, no measurement.

Regression run

main:

xcl-smoke v1
commit: 0e946cfc68244edee1fba6e0425371f687a05ccd
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1420 (PresentX 1420)
presents per 2 s (last 3): 53 50 48
device removals: 0
crashes: 0
peak commit: 14728 MB

PR:

xcl-smoke v1
commit: 9507d05496be7bd87ca769d95cdfec705a3bd6cf
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1534 (PresentX 1535)
presents per 2 s (last 3): 61 59 60
device removals: 0
crashes: 0
peak commit: 14728 MB

Not tested

  • Audio measured: no capture or analysis of the output stream, only the impression above
  • A run with the window hidden
  • cargo test --workspace (not run for this report)
  • AMD GPUs, other titles

🤖 Claude Opus 5.5

@theaustinayers
theaustinayers merged commit cb868b2 into xcl-dev:main Nov 24, 2026
1 check passed
burber added a commit to burber/xcl that referenced this pull request Nov 24, 2026
…pload reuse)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
burber added a commit to burber/xcl that referenced this pull request Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ryosen0 added a commit to ryosen0/xcl that referenced this pull request Nov 24, 2026
theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 24, 2026
…l#486, ivall#574, ivall#602, ivall#634, ivall#636

Upstream's texture detiling and uploads replace the fork's (ivall#440 and the threaded detile); its disk cache of driver blobs (ivall#634) replaces the fork's first version.

Co-Authored-By: Claude Fable 5.2 <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.

xapu answers Opus decode with silence (no sound), and the default 15.6 ms timer step starves the title's audio thread

9 participants