Pipeline workers follow the CPU; one pad that never disconnects
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
2d12b19052
commit
ffdde86ef6
2 files changed
+116
-9
No files matched your search
@@ -292,12 +292,28 @@ fn next() -> Arc<Pending> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the workers (half the logical processors, 2 to 8) on first use.
|
||||
/// How many workers make pipelines: `asked` (`VICTORY_PIPELINE_WORKERS`, 1 to
|
||||
/// 64), else two thirds of the logical processors, 2 to 16. They run below the
|
||||
/// title's own threads, and a cold cache queues hundreds at once.
|
||||
fn worker_count(asked: Option<usize>, processors: usize) -> usize {
|
||||
match asked {
|
||||
Some(n) => n.clamp(1, 64),
|
||||
None => (processors * 2 / 3).clamp(2, 16),
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the workers ([`worker_count`]) on first use.
|
||||
fn workers() -> bool {
|
||||
static RUNNING: OnceLock<bool> = OnceLock::new();
|
||||
*RUNNING.get_or_init(|| {
|
||||
let mut running = false;
|
||||
let workers = std::thread::available_parallelism().map_or(2, |n| (n.get() / 2).clamp(2, 8));
|
||||
let workers = worker_count(
|
||||
std::env::var("VICTORY_PIPELINE_WORKERS")
|
||||
.ok()
|
||||
.and_then(|v| v.trim().parse().ok()),
|
||||
std::thread::available_parallelism().map_or(2, |n| n.get()),
|
||||
);
|
||||
tracing::info!(workers, "pipeline workers");
|
||||
for i in 0..workers {
|
||||
let spawned = std::thread::Builder::new()
|
||||
.name(format!("victory-pipelines-{i}"))
|
||||
@@ -349,6 +365,15 @@ fn drained() {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn worker_count_follows_the_processors_and_the_switch() {
|
||||
assert_eq!(worker_count(None, 1), 2);
|
||||
assert_eq!(worker_count(None, 24), 16);
|
||||
assert_eq!(worker_count(None, 64), 16);
|
||||
assert_eq!(worker_count(Some(0), 24), 1);
|
||||
assert_eq!(worker_count(Some(20), 24), 20);
|
||||
}
|
||||
|
||||
/// A first use queues the pipeline and, within the budget, gets the
|
||||
/// worker's answer (here: no host pipeline for no shaders).
|
||||
#[test]
|
||||
|
||||
@@ -137,6 +137,32 @@ fn fallback(keys: &[KeyState]) -> PadState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Two pads as one: buttons of both, and of each axis and trigger the one
|
||||
/// pushed further.
|
||||
fn merged(a: PadState, b: PadState) -> PadState {
|
||||
let further = |x: f32, y: f32| if y.abs() > x.abs() { y } else { x };
|
||||
PadState {
|
||||
buttons: a.buttons | b.buttons,
|
||||
left_trigger: further(a.left_trigger, b.left_trigger),
|
||||
right_trigger: further(a.right_trigger, b.right_trigger),
|
||||
left_x: further(a.left_x, b.left_x),
|
||||
left_y: further(a.left_y, b.left_y),
|
||||
right_x: further(a.right_x, b.right_x),
|
||||
right_y: further(a.right_y, b.right_y),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the title sees one pad that is always there, fed by the keyboard
|
||||
/// and by every host pad (on by default; `VICTORY_ONE_PAD=0` shows each host
|
||||
/// pad as a device of its own, and the stand-in only while there is none).
|
||||
/// The title keeps to the pad it started with: with the stand-in taken away
|
||||
/// when a pad was switched on, it showed its controller-disconnected screen
|
||||
/// until the pad was off again. It also keeps a pad the host lists twice (its
|
||||
/// own slot and a remapper's) from being two players.
|
||||
fn one_pad() -> bool {
|
||||
std::env::var_os("VICTORY_ONE_PAD").is_none_or(|v| v != "0")
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct Table<const N: usize>([*const (); N]);
|
||||
// SAFETY: function addresses only.
|
||||
@@ -202,7 +228,7 @@ impl Device {
|
||||
root_id: id,
|
||||
supported_input: kind(index),
|
||||
// Low and high frequency.
|
||||
rumble_motors: if index < PADS { 0x3 } else { 0 },
|
||||
rumble_motors: if index <= FALLBACK { 0x3 } else { 0 },
|
||||
..Default::default()
|
||||
};
|
||||
Device {
|
||||
@@ -333,6 +359,8 @@ pub struct Hub {
|
||||
/// Apart from the state: XInput can be slow, and the title reads
|
||||
/// readings every frame.
|
||||
source: Mutex<Box<dyn Source>>,
|
||||
/// The title sees one pad ([`one_pad`]).
|
||||
one_pad: bool,
|
||||
state: Mutex<State>,
|
||||
/// A device callback returned.
|
||||
returned: Condvar,
|
||||
@@ -356,7 +384,7 @@ fn now() -> u64 {
|
||||
}
|
||||
|
||||
impl Hub {
|
||||
fn new(source: Box<dyn Source>) -> &'static Hub {
|
||||
fn new(source: Box<dyn Source>, one_pad: bool) -> &'static Hub {
|
||||
let hub = Box::into_raw(Box::new(Hub {
|
||||
object: Object {
|
||||
vtable: &INPUT,
|
||||
@@ -364,6 +392,7 @@ impl Hub {
|
||||
},
|
||||
devices: std::array::from_fn(Device::new),
|
||||
source: Mutex::new(source),
|
||||
one_pad,
|
||||
state: Mutex::new(State {
|
||||
slots: Default::default(),
|
||||
callbacks: Vec::new(),
|
||||
@@ -384,7 +413,7 @@ impl Hub {
|
||||
|
||||
/// A hub over the host's input, polled from now on.
|
||||
fn start(source: Box<dyn Source>) -> &'static Hub {
|
||||
let hub = Hub::new(source);
|
||||
let hub = Hub::new(source, one_pad());
|
||||
hub.poll();
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
@@ -406,7 +435,7 @@ impl Hub {
|
||||
let pads: [_; PADS] = std::array::from_fn(|slot| source.pad(slot));
|
||||
(pads, source.keys())
|
||||
};
|
||||
let events = self.state().poll(&self.devices, pads, keys);
|
||||
let events = self.state().poll(&self.devices, self.one_pad, pads, keys);
|
||||
self.deliver(events);
|
||||
}
|
||||
|
||||
@@ -449,11 +478,18 @@ impl State {
|
||||
fn poll(
|
||||
&mut self,
|
||||
devices: &[Device; DEVICES],
|
||||
one_pad: bool,
|
||||
pads: [Option<PadState>; PADS],
|
||||
keys: Vec<KeyState>,
|
||||
) -> Vec<Event> {
|
||||
let now = now();
|
||||
let mut events = std::mem::take(&mut self.pending);
|
||||
if one_pad {
|
||||
let pad = pads.into_iter().flatten().fold(fallback(&keys), merged);
|
||||
self.update(devices, FALLBACK, Some(Value::Pad(pad)), now, &mut events);
|
||||
self.update(devices, KEYBOARD, Some(Value::Keys(keys)), now, &mut events);
|
||||
return events;
|
||||
}
|
||||
let any_pad = pads.iter().any(Option::is_some);
|
||||
for (slot, pad) in pads.into_iter().enumerate() {
|
||||
self.update(devices, slot, pad.map(Value::Pad), now, &mut events);
|
||||
@@ -915,7 +951,8 @@ extern "system" fn device_status(this: *const Device) -> u32 {
|
||||
extern "system" fn set_rumble(this: *const Device, params: *const [f32; 4]) {
|
||||
// SAFETY: one of our devices.
|
||||
let device = unsafe { &*this };
|
||||
if device.index >= PADS {
|
||||
let one_pad = device.hub().one_pad;
|
||||
if device.index >= PADS && !(one_pad && device.index == FALLBACK) {
|
||||
return;
|
||||
}
|
||||
let [low, high, ..] = if params.is_null() {
|
||||
@@ -925,7 +962,17 @@ extern "system" fn set_rumble(this: *const Device, params: *const [f32; 4]) {
|
||||
unsafe { *params }
|
||||
};
|
||||
let speed = |level: f32| (level.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16;
|
||||
lock(&device.hub().source).rumble(device.index, speed(low), speed(high));
|
||||
let mut source = lock(&device.hub().source);
|
||||
if one_pad {
|
||||
// The one pad is every host pad.
|
||||
for slot in 0..PADS {
|
||||
if source.pad(slot).is_some() {
|
||||
source.rumble(slot, speed(low), speed(high));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
source.rumble(device.index, speed(low), speed(high));
|
||||
}
|
||||
}
|
||||
extern "system" fn unsupported<const SLOT: usize>(_: *const c_void) -> HRESULT {
|
||||
tracing::warn!(
|
||||
@@ -1302,8 +1349,12 @@ mod tests {
|
||||
}
|
||||
|
||||
fn fake() -> (&'static Hub, Arc<Mutex<FakeHost>>) {
|
||||
fake_with(false)
|
||||
}
|
||||
|
||||
fn fake_with(one_pad: bool) -> (&'static Hub, Arc<Mutex<FakeHost>>) {
|
||||
let host = Arc::new(Mutex::new(FakeHost::default()));
|
||||
let hub = Hub::new(Box::new(Fake(Arc::clone(&host))));
|
||||
let hub = Hub::new(Box::new(Fake(Arc::clone(&host))), one_pad);
|
||||
hub.poll();
|
||||
(hub, host)
|
||||
}
|
||||
@@ -1523,6 +1574,37 @@ mod tests {
|
||||
assert_eq!(current(hub, GAMEPAD_KIND, KEYBOARD), Err(READING_NOT_FOUND));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_host_pad_feeds_the_one_pad_which_never_goes() {
|
||||
let (hub, host) = fake_with(true);
|
||||
let log = Log::default();
|
||||
register(hub, GAMEPAD_KIND, 2, &log);
|
||||
assert_eq!(*log.lock().unwrap(), [(FALLBACK, STATUS_CONNECTED, 0)]);
|
||||
let pad = |hub: &Hub| match &hub.state().slots[FALLBACK].history.back().unwrap().value {
|
||||
Value::Pad(p) => *p,
|
||||
Value::Keys(_) => unreachable!(),
|
||||
};
|
||||
host.lock().unwrap().keys = vec![key(b'D')];
|
||||
hub.poll();
|
||||
assert_eq!(pad(hub).left_x, 1.0);
|
||||
// A pad is switched on, in any slot: its input arrives, beside the
|
||||
// keyboard's, and no device comes or goes.
|
||||
host.lock().unwrap().pads[2] = Some(PadState { buttons: A, left_y: -0.5, ..Default::default() });
|
||||
hub.poll();
|
||||
let p = pad(hub);
|
||||
assert_eq!((p.buttons, p.left_x, p.left_y), (A, 1.0, -0.5));
|
||||
let device: *const Device = &hub.devices[FALLBACK];
|
||||
set_rumble(device, &[1.0, 0.0, 0.0, 0.0]);
|
||||
assert_eq!(host.lock().unwrap().rumble, [(2, u16::MAX, 0)]);
|
||||
// And off again.
|
||||
host.lock().unwrap().pads[2] = None;
|
||||
host.lock().unwrap().keys.clear();
|
||||
hub.poll();
|
||||
assert_eq!(pad(hub), PadState::default());
|
||||
assert_eq!(log.lock().unwrap().len(), 1);
|
||||
assert!(!hub.state().slots[2].connected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_enumeration_waits_for_the_poll_and_unregistering_stops_callbacks() {
|
||||
let (hub, host) = fake();
|
||||
|
||||
Reference in new issue
Block a user