fix(input): XInput is read on a thread of its own, so players with no controller no longer hitch every second #2124

Merged
HumanBean merged 2 commits from HumanBean/Longshot:fix/xinput-off-game-thread into main 2026-11-21 21:15:00 +00:00
Owner

Summary

Players report a better frame rate with a controller connected. On Windows, with no controller SDL reads, the game's
input read (pads_read in host/nn_hid.c) asked XInput for its pads itself, on the game's thread. Each second it
asked all four empty slots again, all in the same read. XInputGetState on an empty slot is slow (it looks for the
device again: a millisecond or more, longer with Bluetooth or a virtual pad's driver installed). Players on a keyboard
and mouse got a hitch every second. Plugging in a controller SDL reads turns the XInput path off, so the hitch went
away.

XInput is now read on a thread of its own (xinput_main): every 2 ms while an XInput pad is there, every 100 ms while
none is, nothing while SDL has the pads. pads_read only takes its last reading. The reading itself
(xinput_poll) is the old loop, moved unchanged. Linux is unaffected in practice: its XInputGetState is a stub.

On main: Windows, no controller plugged in, keyboard and mouse. The frame-time graph (or a Tracy capture) should show
a spike on the game's thread once a second, in pads_read. Plug in a pad and it goes.

Read first: xinput_poll, xinput_main and xinput_read in host/nn_hid.c, and the call in pads_read.

Testing

Tested: not run in the game: the person I work for asked to open it with --force and leave the in-game testing
to the Test Farm. This PC is Linux, where the bug does not happen. No checks were run on this PC either (left to CI).

Still to test:

  • Windows, any GPU: keyboard and mouse only, no controller plugged in. Stand still in Kokiri Forest for 60 s and
    record frame times on main and on this PR. Expecting the once-a-second spike on main to be gone here
    (@Beanstalk windows)
  • Windows, an Xbox pad read through XInput (HOS_NO_SDL=1): buttons, both sticks and the dead zone work, and the pad
    is found when plugged in mid-game. Expecting the same as main (@Beanstalk windows, or by hand)
  • Linux, a controller and the keyboard: input as on main (@Beanstalk linux)
  • CI: tests/test_xinput_thread.py fails on main (the game's thread asks XInput) and passes here, and the changed
    test_pads_after_menu.py and test_stick_dead_zone.py still pass

Measurements

Verdict: not measured yet. The numbers need a Windows PC with no controller (Test Farm, above).

Setup: Windows, keyboard and mouse only; frame times over 60 s standing still, main against this PR.

Before: main @ b62932a96 After: this PR @ a34b94a2
1 % low fps, frame-time spikes per minute

Risks

  • XInput input on Windows (an Xbox pad SDL doesn't read, or HOS_XINPUT=1) is now up to 2 ms older than before: the
    thread's reading, then pads_read's own 2 ms cache. That's the same as SDL's pads, which have their own thread too.
  • One more thread, waking every 100 ms while no XInput pad is there, and not at all once SDL has the pads.
  • A revert is enough. Nothing changes for players after updating (no option, no save, no cache).

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says. (not run: Windows-only bug,
    opened with --force on request; the Test Farm runs it)
  • mise run test passes (any failure that also fails on main is named). (not run on this PC; waiting on CI)
  • Measured against main, or Measurements says why there is nothing to measure. (needs Windows: Test Farm)
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A fix: a game-free test that fails on main and passes here. (written as its own first commit; not run either
    way yet, CI will)
  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. (not built here; CI)
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

## Summary Players report a better frame rate with a controller connected. On Windows, with no controller SDL reads, the game's input read (`pads_read` in `host/nn_hid.c`) asked XInput for its pads itself, on the game's thread. Each second it asked all four empty slots again, all in the same read. `XInputGetState` on an empty slot is slow (it looks for the device again: a millisecond or more, longer with Bluetooth or a virtual pad's driver installed). Players on a keyboard and mouse got a hitch every second. Plugging in a controller SDL reads turns the XInput path off, so the hitch went away. XInput is now read on a thread of its own (`xinput_main`): every 2 ms while an XInput pad is there, every 100 ms while none is, nothing while SDL has the pads. `pads_read` only takes its last reading. The reading itself (`xinput_poll`) is the old loop, moved unchanged. Linux is unaffected in practice: its `XInputGetState` is a stub. On `main`: Windows, no controller plugged in, keyboard and mouse. The frame-time graph (or a Tracy capture) should show a spike on the game's thread once a second, in `pads_read`. Plug in a pad and it goes. Read first: `xinput_poll`, `xinput_main` and `xinput_read` in `host/nn_hid.c`, and the call in `pads_read`. ## Testing **Tested:** not run in the game: the person I work for asked to open it with `--force` and leave the in-game testing to the Test Farm. This PC is Linux, where the bug does not happen. No checks were run on this PC either (left to CI). **Still to test:** - Windows, any GPU: keyboard and mouse only, no controller plugged in. Stand still in Kokiri Forest for 60 s and record frame times on `main` and on this PR. Expecting the once-a-second spike on `main` to be gone here (@Beanstalk windows) - Windows, an Xbox pad read through XInput (`HOS_NO_SDL=1`): buttons, both sticks and the dead zone work, and the pad is found when plugged in mid-game. Expecting the same as `main` (@Beanstalk windows, or by hand) - Linux, a controller and the keyboard: input as on `main` (@Beanstalk linux) - CI: `tests/test_xinput_thread.py` fails on `main` (the game's thread asks XInput) and passes here, and the changed `test_pads_after_menu.py` and `test_stick_dead_zone.py` still pass ## Measurements **Verdict:** not measured yet. The numbers need a Windows PC with no controller (Test Farm, above). Setup: Windows, keyboard and mouse only; frame times over 60 s standing still, `main` against this PR. | | Before: main @ b62932a96 | After: this PR @ a34b94a2 | |---|---|---| | 1 % low fps, frame-time spikes per minute | | | ## Risks - XInput input on Windows (an Xbox pad SDL doesn't read, or `HOS_XINPUT=1`) is now up to 2 ms older than before: the thread's reading, then `pads_read`'s own 2 ms cache. That's the same as SDL's pads, which have their own thread too. - One more thread, waking every 100 ms while no XInput pad is there, and not at all once SDL has the pads. - A revert is enough. Nothing changes for players after updating (no option, no save, no cache). ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. *(not run: Windows-only bug, opened with `--force` on request; the Test Farm runs it)* - [ ] `mise run test` passes (any failure that also fails on `main` is named). *(not run on this PC; waiting on CI)* - [ ] Measured against `main`, or Measurements says why there is nothing to measure. *(needs Windows: Test Farm)* - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [ ] A fix: a game-free test that fails on `main` and passes here. *(written as its own first commit; not run either way yet, CI will)* - [x] A check under `tests/` that reads changed source is updated, not weakened. - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. *(not built here; CI)* - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With no controller SDL reads, pads_read asked XInputGetState on the game's
thread, all four empty slots again each second in one read. The check
compiles production's pads_read with a fake XInput and fails while the
game's thread asks it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(input): XInput is read on a thread of its own, so players with no controller no longer hitch every second
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request) Has been skipped
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 9s
CI / Workflow lint (actionlint) (pull_request) Successful in 18s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 18s
Third-party parts / As their notes say (pull_request) Successful in 19s
CI / Website build and checks (pull_request) Failing after 46s
beanstalk/approval can't be tested as asked: see the farm's comment
PR description / Description follows the template (pull_request_target) Failing after 21s
a34b94a2e9
XInputGetState on an empty slot is slow on Windows (it looks for the
device again). With no controller SDL reads, pads_read asked all four
empty slots each second, in one read on the game's thread: a hitch every
second on a keyboard and mouse, which a controller plugged in took away.

xinput_main now reads XInput (every 2 ms while a pad is there, every
100 ms while none is, nothing while SDL has the pads) and pads_read takes
its last reading. The checks that cut pads_read take the new code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@Beanstalk windows: with no controller plugged in (keyboard and mouse only), stand still in Kokiri Forest for 60 s and record frame times on main and on this PR. On main I expect a frame-time spike once a second; here it should be gone. Then, if the PC has an Xbox pad, set HOS_NO_SDL=1, plug the pad in mid-game, and check its buttons and both sticks work as they do on main.

@Beanstalk windows: with no controller plugged in (keyboard and mouse only), stand still in Kokiri Forest for 60 s and record frame times on main and on this PR. On main I expect a frame-time spike once a second; here it should be gone. Then, if the PC has an Xbox pad, set HOS_NO_SDL=1, plug the pad in mid-game, and check its buttons and both sticks work as they do on main.
Member

Beanstalk test farm · ⛔ can't be tested as asked

Commit a34b94a · approved by HumanBean (untrusted) · dashboard

Not run: the farm can't test this as asked, so nothing is built or run:

The requested comparison requires a verified controller-free Windows PC and physical Xbox pad hot-plugging, neither of which the unattended farm can arrange; HOS_NO_SDL is also prohibited. The alternative can compare stationary frame times on Windows but cannot verify the device setup or physical input.

⚠️ Note: The frame-time probe reports summary metrics, not the once-per-second spike pattern, so it cannot establish that the periodic hitch is gone.

What it could test instead: The alternative plan measures 60 seconds of stationary frame times in Kokiri Forest on Windows, comparing three starts each of main and the PR. It fails on a crash or a 15% regression in 99th-percentile frame time. The unattended farm cannot ensure that controllers are disconnected or perform the requested Xbox pad hot-plug test.

A member of BigBeans can comment @Beanstalk <new words> to ask for something else, or @Beanstalk test it anyway to run the plan regardless.

<!-- beanstalk:sticky --> ### Beanstalk test farm · ⛔ can't be tested as asked Commit `a34b94a` · approved by HumanBean (untrusted) · [dashboard](https://beanstalk.magicbean.dev/prs/2124) **Not run: the farm can't test this as asked**, so nothing is built or run: > The requested comparison requires a verified controller-free Windows PC and physical Xbox pad hot-plugging, neither of which the unattended farm can arrange; HOS_NO_SDL is also prohibited. The alternative can compare stationary frame times on Windows but cannot verify the device setup or physical input. ⚠️ **Note:** The frame-time probe reports summary metrics, not the once-per-second spike pattern, so it cannot establish that the periodic hitch is gone. What it could test instead: The alternative plan measures 60 seconds of stationary frame times in Kokiri Forest on Windows, comparing three starts each of main and the PR. It fails on a crash or a 15% regression in 99th-percentile frame time. The unattended farm cannot ensure that controllers are disconnected or perform the requested Xbox pad hot-plug test. A member of `BigBeans` can comment `@Beanstalk <new words>` to ask for something else, or `@Beanstalk test it anyway` to run the plan regardless.
HumanBean changed title from WIP: fix(input): XInput is read on a thread of its own, so players with no controller no longer hitch every second to fix(input): XInput is read on a thread of its own, so players with no controller no longer hitch every second 2026-11-21 21:14:55 +00:00
HumanBean deleted branch fix/xinput-off-game-thread 2026-11-21 21:15:01 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2124
No description provided.