Water light on, PS4 pads, F1 menu saves at once, long-session memory, memory switches

Rendering and memory (GPL-3.0 code ported in; credits in the source):
- Water: 64-lane compute programs on 32-lane waves keep each wave
  together through loops and set up shared memory from the first
  thread, so the water's tiled light no longer leaves dark blocks. It
  runs by default now (VICTORY_WATER_LIGHT=0 leaves it out; an empty
  value, an earlier version's saved OFF, runs it). Also cloud tiles and
  the screen probes' ray activation.
- Placed views: their padding cleared as before; folding a buffer
  view's first element into its address is VICTORY_PLACED_FOLD=1, off:
  with it on, clothing and caps swapped to other variants for single
  frames in the boat cutscene.
- Texture sweep: textures whose copy was let go leave the looks until
  used again. Static samplers made first, in one order. Small buffers
  made ahead of the first 3D frame.
- play mode: VICTORY_TEXSPK_PASS_MS=8, VICTORY_PASSES_HOT=1,
  VICTORY_HELPER_PRIORITY=1.
- Memory switches, all off: VICTORY_SLOT_EVICT, VICTORY_ALLOCATOR_TRIM,
  VICTORY_BUDGET_PACK (and VICTORY_LIST_POOL_IDLE_S).

Input: DualShock 4 (0x05C4, 0x09CC, 0x0BA0 adapter) read over HID, USB
and Bluetooth, beside the DualSense; PlayStation pads rumble, and stop
when the game ends (issue #352).

F1 menu: every change is saved at once; it saves to
victory-settings.txt in VICTORY_HOME or %LOCALAPPDATA%\victory however
the game was started (no more "NO SETTINGS FILE").

Warm-up start: its 30 s count from the first frame of 100 draws or
more, not from the start: waiting at the title screen restarted it too
early and the next run still sat at 14 fps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
trustno1-coderandClaude Opus 5.5 committed 2026-11-27 15:03:27 -08:00
1 parent df2daafeb1
commit 64d4931b3d
44 files changed
+4664 -952

No files matched your search

+1
View File
@@ -34,6 +34,7 @@ windows-sys = { workspace = true, features = [
"Win32_System_Power",
"Win32_System_ProcessStatus",
"Win32_System_Threading",
"Win32_UI_Input",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_Input_XboxController",
"Win32_UI_WindowsAndMessaging",
+25 -3
View File
@@ -629,16 +629,38 @@ fn scheduler() -> Option<&'static Scheduler> {
})
.as_ref()?;
STARTED.call_once(|| {
let workers = std::thread::available_parallelism().map_or(4, |n| n.get());
// As many workers as processors (`VICTORY_DSTORAGE_WORKERS=<n>`: that many), below
// normal priority (`VICTORY_DSTORAGE_PRIORITY=<-2..2>`, 0: normal, as before
// 2026-11-27). While a scene streams in, all of them unpack at once: in a crowded
// cutscene (timeline tl5, 176 s into a new game, 62 ms a frame) 16 workers ran 10 ms
// a frame each beside 8 listing threads of 20 ms, and the threads a frame waits for
// stood "woken, not running yet" for 9.5 ms of each frame's chain. Below them in
// priority the workers take the processors nobody of the frame wants. The process
// runs above normal (`runtime::ahead_of_other_programs`), so they still come before
// other programs' threads.
let number = |name: &str| std::env::var(name).ok().and_then(|v| v.trim().parse::<i32>().ok());
let workers = match number("VICTORY_DSTORAGE_WORKERS") {
Some(n) if n > 0 => n as usize,
_ => std::thread::available_parallelism().map_or(4, |n| n.get()),
};
let priority = number("VICTORY_DSTORAGE_PRIORITY").map_or(-1, |p| p.clamp(-2, 2));
let started = (0..workers)
.filter(|i| {
std::thread::Builder::new()
.name(format!("dstorage {i}"))
.spawn(|| work(scheduler))
.spawn(move || {
#[cfg(windows)]
if priority != 0 {
use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadPriority};
// SAFETY: this thread's own priority.
unsafe { SetThreadPriority(GetCurrentThread(), priority) };
}
work(scheduler)
})
.is_ok()
})
.count();
tracing::info!(workers = started, "dstorage: workers");
tracing::info!(workers = started, priority, "dstorage: workers (VICTORY_DSTORAGE_WORKERS, VICTORY_DSTORAGE_PRIORITY)");
});
Some(scheduler)
}
+1 -1
View File
@@ -85,7 +85,7 @@ pub fn mount_at(letter: char, root: &Path) {
/// `root` as UTF-16, absolute, without trailing separators (a drive root
/// keeps its own). An empty root is the current directory, as it is to a
/// path joined to it: it is the title's folder when the title is run by its
/// file name alone (`victory run GTA6.exe`).
/// file name alone (`victory run game.exe`).
fn root_wide(root: &Path) -> Vec<u16> {
let root = if root.as_os_str().is_empty() {
Path::new(".")
+26 -7
View File
@@ -1,5 +1,7 @@
//! `gameinput.dll`: input devices from the host. Gamepads come from XInput
//! (host slots 0-3); while none is connected, the keyboard drives one
//! (host slots 0-3) and, in the slots XInput leaves empty, from HID
//! ([`playstation`]: DualShock 4, DualSense, DualSense Edge); while none is
//! connected, the keyboard drives one
//! stand-in gamepad (`FALLBACK_BUTTONS`). The keyboard is a device of its own
//! too. A poll thread turns host state into readings (a new one per change)
//! and runs device callbacks on connect and disconnect.
@@ -21,9 +23,20 @@ use super::xasync::{E_INVALIDARG, E_NOTIMPL};
use super::xgameruntime::{HRESULT, S_OK};
use crate::{HleExport, HleModule};
// PS5 pads (DualSense, DualSense Edge) read straight from HID, USB or Bluetooth (from AnyXboxX's VIctory copy).
// PS5 pads (DualSense, DualSense Edge) read straight from HID, USB or Bluetooth (from AnyXboxX's VIctory copy);
// PS4 pads (DualShock 4) and rumble from FrancescoGelosoEterea/VIscosity (d8db7a0).
#[cfg(any(windows, test))]
mod dualsense;
mod playstation;
/// Stops the motors of the PlayStation pads read over HID, before the
/// process ends (XInput pads stop by themselves when it does).
pub(crate) fn stop_rumble() {
#[cfg(windows)]
if playstation::stop_rumble() {
// Their readers send it with their next report.
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
/// The documented errors.
pub const DEVICE_DISCONNECTED: HRESULT = 0x838A_0001_u32 as i32;
@@ -1266,14 +1279,15 @@ mod host {
Some(super::apply_mouse_sample(&mut self.mouse, delta, buttons, menu_open))
}
fn pad(&mut self, slot: usize) -> Option<PadState> {
// XInput first; HID PS5 pads take the slots XInput leaves free (`VICTORY_DUALSENSE`: 0 off, 1 both).
// XInput first; HID PlayStation pads (PS4 and PS5) take the slots XInput leaves free
// (`VICTORY_DUALSENSE`: 0 off, 1 both).
if slot == 0 {
let xinput = std::array::from_fn(|slot| self.xinput(slot));
self.pads = super::dualsense::slots(
super::dualsense::mode(),
self.pads = super::playstation::slots(
super::playstation::mode(),
xinput,
&mut self.hid_slots,
super::dualsense::pads,
super::playstation::pads,
);
}
#[allow(unused_mut)]
@@ -1321,6 +1335,11 @@ mod host {
}
fn rumble(&mut self, slot: usize, low: u16, high: u16) {
// A slot a HID pad has is one XInput left empty.
if let Some(id) = self.hid_slots[slot] {
super::playstation::rumble(id, low, high);
return;
}
let speeds = XINPUT_VIBRATION {
wLeftMotorSpeed: low,
wRightMotorSpeed: high,
@@ -1,670 +0,0 @@
//! PlayStation 5 pads (DualSense, DualSense Edge) read straight from HID:
//! they are not XInput devices, so without a remapper (Steam Input,
//! DS4Windows) XInput never lists them. Reports can be USB or Bluetooth. A scan
//! thread looks for new pads every [`SCAN`]; each pad has a reader thread
//! that keeps its latest state until the pad goes away. By default XInput
//! has priority over HID, since a remapper can present the same pad twice.
use super::{
A, B, DOWN, LEFT, LEFT_SHOULDER, LEFT_STICK, MENU, PadState, RIGHT, RIGHT_SHOULDER,
RIGHT_STICK, UP, VIEW, X, Y,
};
#[cfg(windows)]
const SONY: u16 = 0x054C;
/// DualSense, DualSense Edge.
#[cfg(windows)]
const PRODUCTS: [u16; 2] = [0x0CE6, 0x0DF2];
/// The D-pad hat (low nibble of the first button byte): 0 up, clockwise in
/// eighths, 8 and above centred.
fn hat(value: u8) -> u32 {
match value & 0xF {
0 => UP,
1 => UP | RIGHT,
2 => RIGHT,
3 => RIGHT | DOWN,
4 => DOWN,
5 => DOWN | LEFT,
6 => LEFT,
7 => LEFT | UP,
_ => 0,
}
}
/// Face buttons in the first button byte, the rest in the second and third
/// (the PS button and the mute button have no console pad button).
const BUTTONS: [(usize, u8, u32); 11] = [
(0, 0x10, X), // square
(0, 0x20, A), // cross
(0, 0x40, B), // circle
(0, 0x80, Y), // triangle
(1, 0x01, LEFT_SHOULDER),
(1, 0x02, RIGHT_SHOULDER),
(1, 0x10, VIEW), // create
(1, 0x20, MENU), // options
(1, 0x40, LEFT_STICK),
(1, 0x80, RIGHT_STICK),
(2, 0x02, VIEW), // touchpad click
];
/// The pad in one input report, `None` for a report of another kind.
/// `bluetooth`: the pad is connected over Bluetooth, where the full report
/// is 0x31 (one byte further in) and 0x01 is the short report the pad sends
/// before it was asked for the full one.
pub(super) fn parse(report: &[u8], bluetooth: bool) -> Option<PadState> {
// Offsets of the sticks (LX LY RX RY), the triggers (L2 R2) and the
// three button bytes.
let (sticks, triggers, buttons) = match (report.first()?, bluetooth) {
(0x01, false) => (1, 5, 8),
(0x31, true) => (2, 6, 9),
(0x01, true) => (1, 8, 5),
_ => return None,
};
if report.len() < sticks + 4 || report.len() < triggers + 2 || report.len() < buttons + 3 {
return None;
}
let bytes = &report[buttons..buttons + 3];
// 0 to 255 with 128 centred; down is positive on the pad, up here.
let axis = |v: u8| ((f32::from(v) - 127.5) / 127.5).clamp(-1.0, 1.0);
Some(PadState {
buttons: BUTTONS
.iter()
.filter(|&&(byte, mask, _)| bytes[byte] & mask != 0)
.fold(hat(bytes[0]), |all, &(_, _, bit)| all | bit),
left_trigger: f32::from(report[triggers]) / 255.0,
right_trigger: f32::from(report[triggers + 1]) / 255.0,
left_x: axis(report[sticks]),
left_y: -axis(report[sticks + 1]),
right_x: axis(report[sticks + 2]),
right_y: -axis(report[sticks + 3]),
})
}
/// The Bluetooth address in a pairing feature report (0x09, the address at
/// 1..7 on both transports), `None` when it is too short or all zeros.
fn mac(pairing: &[u8]) -> Option<[u8; 6]> {
let mac: [u8; 6] = pairing.get(1..7)?.try_into().ok()?;
(mac != [0; 6]).then_some(mac)
}
#[cfg(windows)]
pub(super) use host::pads;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Mode {
Off,
Auto,
Both,
}
#[cfg(windows)]
pub(super) fn mode() -> Mode {
static MODE: std::sync::LazyLock<Mode> =
std::sync::LazyLock::new(|| match std::env::var("VICTORY_DUALSENSE").as_deref() {
Ok("0") => Mode::Off,
Ok("1") => Mode::Both,
_ => Mode::Auto,
});
*MODE
}
/// XInput does not identify the corresponding physical HID device. Prefer
/// it in automatic mode. If both sources are requested, retain the slot of
/// every connected HID pad unless that slot has been claimed by XInput.
pub(super) fn slots(
mode: Mode,
mut xinput: [Option<PadState>; super::PADS],
assigned: &mut [Option<u64>; super::PADS],
hid: impl FnOnce() -> Vec<(u64, PadState)>,
) -> [Option<PadState>; super::PADS] {
if mode == Mode::Off || mode == Mode::Auto && xinput.iter().any(Option::is_some) {
assigned.fill(None);
return xinput;
}
let pads = hid();
for (slot, owner) in xinput.iter_mut().zip(assigned.iter_mut()) {
if slot.is_none()
&& let Some((_, state)) = pads.iter().find(|(id, _)| Some(*id) == *owner)
{
*slot = Some(*state);
} else {
*owner = None;
}
}
for (id, state) in pads {
if assigned.contains(&Some(id)) {
continue;
}
let Some(slot) = xinput.iter().position(Option::is_none) else {
break;
};
xinput[slot] = Some(state);
assigned[slot] = Some(id);
}
xinput
}
#[cfg(windows)]
mod host {
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{LazyLock, Mutex, PoisonError};
use std::time::Duration;
use windows_sys::Win32::Devices::DeviceAndDriverInstallation::{
DIGCF_DEVICEINTERFACE, DIGCF_PRESENT, SP_DEVICE_INTERFACE_DATA,
SP_DEVICE_INTERFACE_DETAIL_DATA_W, SetupDiDestroyDeviceInfoList,
SetupDiEnumDeviceInterfaces, SetupDiGetClassDevsW, SetupDiGetDeviceInterfaceDetailW,
};
use windows_sys::Win32::Devices::HumanInterfaceDevice::{
HIDD_ATTRIBUTES, HIDP_CAPS, HIDP_STATUS_SUCCESS, HidD_FreePreparsedData,
HidD_GetAttributes, HidD_GetFeature, HidD_GetHidGuid, HidD_GetPreparsedData, HidP_GetCaps,
};
use windows_sys::Win32::Foundation::{
CloseHandle, ERROR_IO_PENDING, GENERIC_READ, GENERIC_WRITE, GetLastError, HANDLE,
INVALID_HANDLE_VALUE, WAIT_TIMEOUT,
};
use windows_sys::Win32::Storage::FileSystem::{
CreateFileW, FILE_FLAG_OVERLAPPED, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
ReadFile,
};
use windows_sys::Win32::System::IO::{
CancelIoEx, GetOverlappedResult, GetOverlappedResultEx, OVERLAPPED,
};
use windows_sys::Win32::System::Threading::CreateEventW;
use super::{PRODUCTS, PadState, SONY, mac, parse};
/// How often to look for newly connected pads.
const SCAN: Duration = Duration::from_secs(2);
/// A pad in full-report mode sends reports all the time (hundreds a
/// second); this long without one and we ask it whether it is still
/// there. Over Bluetooth Windows can keep the device after the pad is
/// switched off or out of range, and a read then waits for ever.
const QUIET_MS: u32 = 500;
struct Pad {
/// This connection's: a pad that reconnects gets a new one.
id: u64,
path: Vec<u16>,
/// The pad's Bluetooth address, the same over USB and Bluetooth:
/// one pad seen through both is opened once.
mac: Option<[u8; 6]>,
/// `None` until the first report, and while the pad is silent.
state: Option<PadState>,
}
/// The open pads, in the order they were found.
static PADS: Mutex<Vec<Pad>> = Mutex::new(Vec::new());
fn lock() -> std::sync::MutexGuard<'static, Vec<Pad>> {
PADS.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Paths found to be a pad already open over its other transport, with
/// its address: not opened again on every scan (two feature reports
/// and a log line each time) while that pad stays open.
static TWINS: Mutex<Vec<(Vec<u16>, [u8; 6])>> = Mutex::new(Vec::new());
/// The open pads' latest states with their connection ids, oldest
/// connection first, without the pads that are silent or yet to report;
/// the first call starts looking for them.
pub(in super::super) fn pads() -> Vec<(u64, PadState)> {
static STARTED: LazyLock<()> = LazyLock::new(|| {
std::thread::spawn(|| {
loop {
scan();
std::thread::sleep(SCAN);
}
});
});
LazyLock::force(&STARTED);
lock()
.iter()
.filter_map(|pad| Some((pad.id, pad.state?)))
.collect()
}
/// Windows sizes each report buffer for its top-level HID collection.
/// The calibration and pairing reports use the same feature buffer.
struct Reports {
input: Vec<u8>,
feature: Vec<u8>,
}
impl Reports {
fn for_device(handle: HANDLE) -> Option<Self> {
let mut data = 0;
// SAFETY: the device is open and the output is writable.
if !unsafe { HidD_GetPreparsedData(handle, &mut data) } {
return None;
}
let mut caps = HIDP_CAPS::default();
// SAFETY: `data` was obtained above and is released after its last use.
let status = unsafe { HidP_GetCaps(data, &mut caps) };
unsafe { HidD_FreePreparsedData(data) };
if status != HIDP_STATUS_SUCCESS
|| caps.InputReportByteLength == 0
|| caps.FeatureReportByteLength == 0
{
return None;
}
Some(Self {
input: vec![0; usize::from(caps.InputReportByteLength)],
feature: vec![0; usize::from(caps.FeatureReportByteLength)],
})
}
}
/// Feature report `id` into `buffer` (its first byte), `false` when the
/// pad did not answer. A round trip to the pad, so over Bluetooth it also
/// tells whether the pad is still there.
fn feature(handle: HANDLE, id: u8, buffer: &mut [u8]) -> bool {
buffer.fill(0);
buffer[0] = id;
// SAFETY: the buffer is as long as we say.
unsafe { HidD_GetFeature(handle, buffer.as_mut_ptr().cast(), buffer.len() as u32) }
}
/// Asks for the calibration report: it switches a pad on Bluetooth to
/// full reports (0x31), again after it reconnected; over USB it changes
/// nothing. `false` when the pad did not answer.
fn full_reports(handle: HANDLE, buffer: &mut [u8]) -> bool {
feature(handle, 0x05, buffer)
}
/// Opens the pads not open yet, each with a reader thread.
fn scan() {
let paths = hid_paths();
{
let pads = lock();
// A twin whose pad went away is opened again: over USB, say,
// after the Bluetooth link dropped with the cable still in.
TWINS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.retain(|(path, mac)| {
paths.contains(path) && pads.iter().any(|pad| pad.mac == Some(*mac))
});
}
for path in paths {
// Cheap filter before opening anything: Sony's vendor id is in
// the path, as `vid_054c` (USB) or `vid&0002054c` (Bluetooth).
let lower = String::from_utf16_lossy(&path).to_ascii_lowercase();
if !lower.contains("054c") {
continue;
}
if lock().iter().any(|pad| pad.path == path)
|| TWINS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.any(|(twin, _)| *twin == path)
{
continue;
}
let Some((handle, mut reports)) = open(&path) else {
continue;
};
let bluetooth = lower.contains("{00001124-");
// A paired pad that is switched off can still be listed over
// Bluetooth: it does not answer.
if !full_reports(handle, &mut reports.feature) {
tracing::debug!(path = %lower, "DualSense listed but not answering");
// SAFETY: just opened, nothing else has it.
unsafe { CloseHandle(handle) };
continue;
}
let mac = if feature(handle, 0x09, &mut reports.feature) {
mac(&reports.feature)
} else {
None
};
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
let id = {
let mut pads = lock();
let twin = mac.is_some() && pads.iter().any(|pad| pad.mac == mac);
(!twin).then(|| {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
pads.push(Pad {
id,
path: path.clone(),
mac,
state: None,
});
id
})
};
let Some(id) = id else {
tracing::info!(path = %lower, bluetooth, "DualSense already open over the other transport");
if let Some(mac) = mac {
TWINS
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push((path, mac));
}
// SAFETY: just opened, nothing else has it.
unsafe { CloseHandle(handle) };
continue;
};
tracing::info!(path = %lower, bluetooth, "DualSense connected");
let raw = handle as usize;
if let Err(error) = std::thread::Builder::new()
.name("DualSense reader".into())
.spawn(move || read(raw as HANDLE, id, bluetooth, reports))
{
lock().retain(|pad| pad.id != id);
// SAFETY: just opened, and no reader has it.
unsafe { CloseHandle(handle) };
tracing::warn!(%error, "could not start a DualSense reader");
}
}
}
/// Reads reports until the pad goes away, then forgets it.
fn read(handle: HANDLE, id: u64, bluetooth: bool, mut reports: Reports) {
let set = |state: Option<PadState>| {
if let Some(pad) = lock().iter_mut().find(|pad| pad.id == id) {
pad.state = state;
}
};
// SAFETY: an unnamed manual-reset event; no security attributes.
let event = unsafe { CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()) };
let why = 'pad: loop {
if event.is_null() {
break 'pad "no event";
}
// SAFETY: plain data; only the event is set.
let mut overlapped: OVERLAPPED = unsafe { std::mem::zeroed() };
overlapped.hEvent = event;
let mut got = 0;
// SAFETY: the buffer and `overlapped` outlive the read: every way
// out below waits for it to end first.
let started = unsafe {
ReadFile(
handle,
reports.input.as_mut_ptr(),
reports.input.len() as u32,
std::ptr::null_mut(),
&mut overlapped,
)
};
// SAFETY: plain query.
if started == 0 && unsafe { GetLastError() } != ERROR_IO_PENDING {
break 'pad "read failed";
}
loop {
// SAFETY: the read started above.
let done =
unsafe { GetOverlappedResultEx(handle, &overlapped, &mut got, QUIET_MS, 0) };
if done != 0 {
if got == 0 {
break 'pad "read nothing";
}
break;
}
// SAFETY: plain query.
if unsafe { GetLastError() } != WAIT_TIMEOUT {
break 'pad "read failed";
}
// Silent: whatever was held is not held any more, and the
// pad may be gone without the read failing.
set(None);
if !full_reports(handle, &mut reports.feature) {
// SAFETY: cancel the read and wait for it to end, so
// nothing writes into the input buffer once we return.
unsafe {
CancelIoEx(handle, &overlapped);
GetOverlappedResult(handle, &overlapped, &mut got, 1);
}
break 'pad "not answering";
}
}
if let Some(state) = parse(&reports.input[..got as usize], bluetooth) {
set(Some(state));
}
};
tracing::info!(why, bluetooth, "DualSense disconnected");
lock().retain(|pad| pad.id != id);
// SAFETY: ours, and nothing uses them any more.
unsafe {
if !event.is_null() {
CloseHandle(event);
}
CloseHandle(handle);
}
}
/// The pad at `path` opened for reading, `None` when it is something else
/// or cannot be opened.
fn open(path: &[u16]) -> Option<(HANDLE, Reports)> {
let mut name = path.to_vec();
name.push(0);
// SAFETY: a terminated path; no security attributes or template.
let handle = unsafe {
CreateFileW(
name.as_ptr(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null(),
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
std::ptr::null_mut(),
)
};
if handle == INVALID_HANDLE_VALUE {
return None;
}
let mut attributes = HIDD_ATTRIBUTES {
Size: size_of::<HIDD_ATTRIBUTES>() as u32,
..Default::default()
};
// SAFETY: an open device and room for its attributes.
let ours = unsafe { HidD_GetAttributes(handle, &mut attributes) }
&& attributes.VendorID == SONY
&& PRODUCTS.contains(&attributes.ProductID);
if ours && let Some(reports) = Reports::for_device(handle) {
return Some((handle, reports));
}
// SAFETY: just opened.
unsafe { CloseHandle(handle) };
None
}
/// The device paths of the HID devices present, without terminators.
fn hid_paths() -> Vec<Vec<u16>> {
let mut paths = Vec::new();
// SAFETY: plain queries into buffers we own; the list is destroyed
// before returning.
unsafe {
let mut guid = std::mem::zeroed();
HidD_GetHidGuid(&mut guid);
let set = SetupDiGetClassDevsW(
&guid,
std::ptr::null(),
std::ptr::null_mut(),
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE,
);
if set == INVALID_HANDLE_VALUE as isize {
return paths;
}
for index in 0.. {
let mut interface = SP_DEVICE_INTERFACE_DATA {
cbSize: size_of::<SP_DEVICE_INTERFACE_DATA>() as u32,
..std::mem::zeroed()
};
if SetupDiEnumDeviceInterfaces(set, std::ptr::null(), &guid, index, &mut interface)
== 0
{
break;
}
let mut size = 0;
SetupDiGetDeviceInterfaceDetailW(
set,
&interface,
std::ptr::null_mut(),
0,
&mut size,
std::ptr::null_mut(),
);
if size == 0 {
continue;
}
// u64s keep the detail's alignment.
let mut buffer = vec![0u64; (size as usize).div_ceil(8)];
let detail = buffer
.as_mut_ptr()
.cast::<SP_DEVICE_INTERFACE_DETAIL_DATA_W>();
(*detail).cbSize = size_of::<SP_DEVICE_INTERFACE_DETAIL_DATA_W>() as u32;
if SetupDiGetDeviceInterfaceDetailW(
set,
&interface,
detail,
size,
std::ptr::null_mut(),
std::ptr::null_mut(),
) == 0
{
continue;
}
let start = std::ptr::addr_of!((*detail).DevicePath).cast::<u16>();
let room = (size as usize
- std::mem::offset_of!(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath))
/ 2;
let wide = std::slice::from_raw_parts(start, room);
let end = wide.iter().position(|&c| c == 0).unwrap_or(room);
paths.push(wide[..end].to_vec());
}
SetupDiDestroyDeviceInfoList(set);
}
paths
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn automatic_input_prefers_the_remapper_in_any_slot_and_falls_back_to_hid() {
let physical = PadState {
buttons: A,
..PadState::default()
};
let remapped = PadState {
buttons: Y,
..PadState::default()
};
let mut assigned = [None; super::super::PADS];
let none = [None; super::super::PADS];
let xinput = [None, None, Some(remapped), None];
assert_eq!(
slots(Mode::Auto, none, &mut assigned, || vec![(1, physical)])[0],
Some(physical)
);
assert_eq!(
slots(Mode::Auto, xinput, &mut assigned, || panic!(
"HID not needed"
)),
xinput
);
assert_eq!(
slots(Mode::Off, none, &mut assigned, || panic!("HID disabled")),
none
);
assert_eq!(
slots(Mode::Auto, none, &mut assigned, || vec![(2, physical)])[0],
Some(physical)
);
}
#[test]
fn a_surviving_hid_pad_keeps_its_slot_through_disconnect_and_reconnect() {
let a = PadState {
buttons: A,
..PadState::default()
};
let b = PadState {
buttons: B,
..PadState::default()
};
let x = PadState {
buttons: X,
..PadState::default()
};
let xinput = [None, Some(x), None, None];
let mut assigned = [None; super::super::PADS];
assert_eq!(
slots(Mode::Both, xinput, &mut assigned, || vec![(7, a), (8, b)]),
[Some(a), Some(x), Some(b), None]
);
assert_eq!(
slots(Mode::Both, xinput, &mut assigned, || vec![(8, b)]),
[None, Some(x), Some(b), None]
);
assert_eq!(
slots(Mode::Both, xinput, &mut assigned, || vec![(8, b), (9, a)]),
[Some(a), Some(x), Some(b), None]
);
}
#[test]
fn the_pairing_report_holds_the_address() {
let mut pairing = [0u8; 20];
pairing[0] = 0x09;
pairing[1..7].copy_from_slice(&[1, 2, 3, 4, 5, 6]);
assert_eq!(mac(&pairing), Some([1, 2, 3, 4, 5, 6]));
// a pad that answered with nothing cannot be told apart
assert_eq!(mac(&[0x09; 1]), None);
assert_eq!(mac(&[0x09, 0, 0, 0, 0, 0, 0]), None);
}
#[test]
fn a_usb_report_reads_as_a_pad() {
let mut report = [0u8; 64];
report[0] = 0x01;
report[1..5].copy_from_slice(&[0, 255, 128, 128]); // left stick: left, down
report[5] = 255; // L2
report[8] = 0x20 | 2; // cross, D-pad right
report[9] = 0x20 | 0x01; // options, L1
let pad = parse(&report, false).unwrap();
assert_eq!(pad.buttons, A | RIGHT | MENU | LEFT_SHOULDER);
assert_eq!(
(pad.left_x, pad.left_y, pad.left_trigger),
(-1.0, -1.0, 1.0)
);
assert!(pad.right_x.abs() < 0.01 && pad.right_y.abs() < 0.01);
}
#[test]
fn bluetooth_reports_read_one_byte_further_in() {
let mut report = [0u8; 78];
report[0] = 0x31;
report[2..6].copy_from_slice(&[128, 0, 255, 128]); // left up, right right
report[7] = 255; // R2
report[9] = 0x80 | 8; // triangle, D-pad centred
let pad = parse(&report, true).unwrap();
assert_eq!(pad.buttons, Y);
assert_eq!(
(pad.left_y, pad.right_x, pad.right_trigger),
(1.0, 1.0, 1.0)
);
// the same bytes over USB are not a pad report
assert_eq!(parse(&report, false), None);
}
#[test]
fn the_short_bluetooth_report_reads_too() {
let mut report = [0u8; 78];
report[0] = 0x01;
report[1..5].copy_from_slice(&[128; 4]);
report[5] = 0x40 | 4; // circle, D-pad down
report[7] = 0x02; // touchpad click
report[8] = 128; // L2 halfway
let pad = parse(&report, true).unwrap();
assert_eq!(pad.buttons, B | DOWN | VIEW);
assert!((pad.left_trigger - 0.5).abs() < 0.01);
}
}
File diff suppressed because it is too large. Load diff
@@ -501,6 +501,8 @@ pub fn wait_for_previous() {
}
fn end() {
// A pad left rumbling would run on until it is switched off.
crate::modules::gameinput::stop_rumble();
// Compiled host pipelines kept for the next run.
#[cfg(windows)]
victory_gpu::pipeline::save_pipeline_cache();
+3 -3
View File
@@ -188,7 +188,7 @@ pub extern "system" fn XMemVirtualAlloc(
if !at.is_null() && asked & 0x400 != 0 {
victory_gpu::title_memory_write_combined(at as usize, size);
}
// (Grand Theft Auto VI asks for its 32-bit heap with kind 0x42000,
// (the game asks for its 32-bit heap with kind 0x42000,
// without the 64 KiB page bits: not `placeholder` above, though the host
// makes a placeholder of it all the same.)
if !at.is_null() && constrained && kind & MEM_COMMIT == 0 && flags & ADDRESS_32BIT != 0 {
@@ -1151,7 +1151,7 @@ fn port_on() -> bool {
}
/// The title's reservations of 64 KiB pages for 32-bit graphics memory:
/// base and bytes (one, of 185 MB, in Grand Theft Auto VI).
/// base and bytes (one, of 185 MB, in the game).
static LOW_RESERVATIONS: Mutex<Vec<(usize, usize)>> = Mutex::new(Vec::new());
/// Pages given another place in their section so far by [`arrange`], and
@@ -1165,7 +1165,7 @@ static ARRANGED: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
/// reservation), so that the reservation becomes one stretch of the section
/// and the GPU side can open it as one heap.
///
/// Grand Theft Auto VI takes all the pages of its 185 MB heap in one call
/// The game takes all the pages of its 185 MB heap in one call
/// and maps its five pieces in another order than their addresses (run
/// port1-1: "Scattered"). Only fresh slices are exchanged: never mapped,
/// never written (no commit bit), so every page still reads as zeros and