Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions d3d12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ measurements and what is still wrong in the picture are in the [top-level README
| A pad's press waiting up to 8 ms for its reading | `LEONIDA_PAD_QUICK` (on; 0 off): the input thread looks every millisecond at XInput's packet numbers of the pads it has and at the Sony readers' count of new readings, and polls at once when one moved; keyboard and mouse stay at every 8 ms (gameinput module tests) | `crates/leonida-hle/src/modules/gameinput.rs`, `gameinput/hid.rs` | voidopcode/vilocity 3eda937 |
| The world slower than the wall clock under 15 frames a second (the title's game step stops at 1/15 s a frame) | `LEONIDA_SIM_MAX_STEP_MS=100`, `125`, `200` or `250` (unset: the title's own): the two loads of the step's ceiling (+0x2226045, +0x2226833 in 1.0.2.1) read another constant of the image that holds the value asked for; all places checked first (test `the_game_step_loads_point_at_the_ceiling_asked_for`, the retail executable's check) | `crates/leonida-loader/src/hooks.rs` | kylepelham/El1xr 9372c47f (game time 0.97-0.98 of the wall clock there, 0.89-0.92 without) |
| With the 60 fps patch the scripts told 60 frames a second, from which the title halves a count it hands them | Always with the 60 fps patch: the function that tells the scripts the frame rate answers 59 (test `the_scripts_are_told_fifty_nine`) | `crates/leonida-loader/src/vipatch.rs` | trustno1-coder/VIctory-NVIDIA PR #65 and kylepelham/El1xr `sim_target.rs` |
| Textures wait for room that render targets never use | `LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS` (0, off: the reserve of `LEONIDA_CARD_ROOM_FOR_TARGETS_MB` is kept for the run, as before; 20000 is the value measured upstream): once the world's render targets hold half of the reserve and have not grown by 64 MiB for that many ms, nothing is kept for targets any more, and the texture queue gets the rest back; 64 MiB of growth brings the reserve back. Where the targets level off at 2.1-2.9 GB, 160-990 MB stayed reserved for targets that never came while thousands of textures waited (test `room_for_targets_is_given_up_once_they_settle`; upstream's 5-second log line of the room test is not part of this change) | `crates/leonida-gpu/src/resource.rs` | readmet3xt-lab, trustno1-coder/VIctory-NVIDIA `21b040da` |
| No trainer menu | `LEONIDA_MENU` (on; 0 off): F4 (or RB + d-pad Left) opens an in-game menu beside the F1 settings and the F3 panel; arrows or numpad 8/2/4/6 move, Enter or 5 selects, Backspace or 0 goes back; `LEONIDA_MENU_OPEN=1` starts it open. DLSS sharpness, the frame-rate cap and the FPS panel (the renderer's own settings, shared with the F1 menu), screenshots, frame recording and the title's clock rate work; script features (god mode, vehicles, weather, ...) wait for the title's natives. Built and unit-tested, not yet tried in the running title (`docs/mod-menu.md`) | `crates/leonida-menu`, `crates/leonida-gpu/src/host/trainer.rs` | Leonida's own; the classic single-player trainer layout, no code or art from one |
| No Lua mods | `LEONIDA_LUA` (on; 0 off, no thread): Lua 5.4 mods in `mods\<name>\` (`mod.json` with `"script": "main.lua"`, `LEONIDA_MODS` names the folder) run in a sandbox on a thread of their own, with the `leonida` API (version 1): frame, key and menu events, an overlay, the live settings (frame cap, DLSS sharpness, panel), pages in the F4 menu under Mods (and Reload Lua mods), keys held for the title, and checked reads, writes and pattern scans of the title's memory; a failing mod is switched off alone. `mods\example_lua` ships disabled. Not the title's own script functions (god mode, vehicles, weather: none can be called). Built and unit-tested, not yet tried in the running title (`docs/lua-modding.md`) | `crates/leonida-lua`, `crates/leonida-menu/src/{overlay,mods}.rs`, `crates/leonida-gpu/src/host/trainer.rs` | Leonida's own; Lua 5.4 through the `mlua` crate (MIT) |
| No frame generation on RTX 20 and 30 cards (DLSS's runs on RTX 40 and 50 only) | `LEONIDA_FSR_FRAMEGEN=1` (off; `-FsrFrameGen`, or FSR 3 FRAME GEN in the F1 menu from the next start, off and on again while it runs) with AMD's signed `amd_fidelityfx_loader_dx12.dll` and `amd_fidelityfx_framegeneration_dx12.dll` in `amd-sdk\FidelityFX\bin` (`LEONIDA_FIDELITYFX_DIR`): the swap chain is FidelityFX's frame interpolation swap chain, given the depth and motion vectors DLSS frame generation is given, and one frame is generated between each two rendered; on Series S the base plane is the colour without the interface. Never with DLSS frame generation or ray tracing | `crates/leonida-gpu/src/fsr.rs`, `crates/leonida-gpu/src/fg.rs`, `scripts/play-nvidia.ps1` | trustno1-coder/VIctory-NVIDIA `29d8dbf` (`crates/victory-gpu/src/streamline/fsr.rs`, GPL-3.0) |
Expand Down
165 changes: 163 additions & 2 deletions d3d12/crates/leonida-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,19 @@ pub(crate) fn room(memory: Option<[u64; 4]>) -> bool {
/// 238), and the queue was refused from the first world frame on. With
/// their room kept, the queue stops that much earlier while the world
/// loads and gets it back as the targets are made.
///
/// With `LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS` set (0, the default: kept
/// for the run, as before; 20,000 is the value measured), nothing is kept
/// once the world's targets are made (half of the reserve or more) and have
/// not grown by 64 MiB for that many ms: at 1440p the targets level off at
/// 2.1-2.9 GB, and the rest of the 3,072 MB (990 MB at the first world
/// frame, 160-640 MB in play) stayed reserved for targets that never came
/// while thousands of textures waited for room (VIctory-NVIDIA's
/// 2026-11-26 diag run 16:28, 10:59:54: 8,722 MB in use + 836 MB kept for
/// targets against a line of 9,527 MB). The reserve comes back when the
/// targets grow by 64 MiB again (a new scene's), for as long again.
/// Ported from trustno1-coder/VIctory-NVIDIA 21b040da (readmet3xt-lab),
/// GPL-3.0; there the setting is on at 20,000, here it is off.
#[cfg(windows)]
pub(crate) fn targets_room_kept() -> u64 {
use std::sync::atomic::Ordering::Relaxed;
Expand All @@ -2091,9 +2104,109 @@ pub(crate) fn targets_room_kept() -> u64 {
.and_then(|v| v.trim().parse::<u64>().ok())
.map_or(3072 << 20, |mb| mb.min(8192) << 20)
});
static SETTLE: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
let settle = *SETTLE.get_or_init(|| {
std::env::var("LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS")
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
.unwrap_or(0)
});
// Every host texture's bytes less those of the copies the title fills.
let targets = HOST_TEXTURES[1].load(Relaxed).saturating_sub(KINDS[1].load(Relaxed));
most.saturating_sub(targets)
let targets_now = || HOST_TEXTURES[1].load(Relaxed).saturating_sub(KINDS[1].load(Relaxed));
if settle == 0 {
return targets_kept(most, targets_now(), 0, 0);
}
// The targets' bytes at their last change of 64 MiB or more, when they
// last grew by that much, and whether the reserve is given up: one lock
// over reading, computing and storing them, so a caller delayed between
// its read and its store cannot put back an older record (merzleckeier,
// #2589: a stale store kept 0 MiB where 709 were due). The bytes and the
// time are read under the lock for the same reason. Only this opt-in
// branch takes it; the default (0) above stays lock-free.
static STATE: std::sync::Mutex<(u64, u64, bool)> = std::sync::Mutex::new((0, 0, false));
let mut state = STATE.lock().unwrap_or_else(|e| e.into_inner());
let targets = targets_now();
let now = crate::host::now_ms();
let (last, since) = targets_seen(state.0, state.1, targets, now);
let kept = targets_kept(most, targets, now.saturating_sub(since), settle);
// One line each time the reserve is given up or taken back, for a run to
// show when and how much (none with the setting at 0).
let turn = targets_turn(state.2, most, targets, kept);
*state = (last, since, turn.unwrap_or(state.2));
drop(state);
if let Some(given_up) = turn {
tracing::info!("{}", targets_line(given_up, most, targets, now));
}
kept
}

/// Whether the reserve for render targets turns, and which way: `Some(true)`
/// when it is given up (nothing kept while the targets leave room under
/// `most`), `Some(false)` when it is taken back, which needs something kept
/// again (`kept > 0`): targets that grow from settled straight to `most`
/// keep nothing either, and a "taken back ... 0 MB kept" line would read as
/// textures losing room they keep. `None` when it does not turn.
#[cfg(any(windows, test))]
fn targets_turn(was_given_up: bool, most: u64, targets: u64, kept: u64) -> Option<bool> {
if !was_given_up && kept == 0 && targets < most {
Some(true)
} else if was_given_up && kept > 0 {
Some(false)
} else {
None
}
}

/// The line [`targets_room_kept`] writes when the reserve for render
/// targets is given up (`given_up`) or taken back: the targets' bytes and
/// what is freed or kept, in MB, and the time of the run in seconds.
#[cfg(any(windows, test))]
fn targets_line(given_up: bool, most: u64, targets: u64, now_ms: u64) -> String {
let mb = |b: u64| b >> 20;
let rest = mb(most.saturating_sub(targets));
if given_up {
format!(
"render-target reserve (LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS): given up at {:.1} s, targets {} MB, {} MB freed for textures",
now_ms as f64 / 1000.0,
mb(targets),
rest
)
} else {
format!(
"render-target reserve (LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS): taken back at {:.1} s, targets {} MB, {} MB kept",
now_ms as f64 / 1000.0,
mb(targets),
rest
)
}
}

/// [`targets_room_kept`]'s record of the targets' bytes (`last`) and of
/// when they last grew by 64 MiB (`since`, ms), with `targets` now: a fall
/// of 64 MiB lowers the record (so that growing back counts as growth), a
/// growth of 64 MiB raises it and restarts the clock.
#[cfg(any(windows, test))]
fn targets_seen(last: u64, since: u64, targets: u64, now: u64) -> (u64, u64) {
const STEP: u64 = 64 << 20;
if targets >= last.saturating_add(STEP) {
(targets, now)
} else if targets.saturating_add(STEP) <= last {
(targets, since)
} else {
(last, since)
}
}

/// What [`targets_room_kept`] keeps of `most`: what the targets do not
/// hold yet, or nothing once they hold half of it or more and have not
/// grown for `settle` ms (`still_ms`; `settle` 0: always kept).
#[cfg(any(windows, test))]
fn targets_kept(most: u64, targets: u64, still_ms: u64, settle: u64) -> u64 {
if settle > 0 && targets >= most / 2 && still_ms >= settle {
0
} else {
most.saturating_sub(targets)
}
}

/// [`room`] as it was at most a tenth of a second ago, for a question
Expand Down Expand Up @@ -5496,6 +5609,54 @@ float4 lit() : SV_Target { return 1.0; }
assert_eq!(texture_floor_bytes(Some(32 * GIB)), 2 * GIB);
}

/// The room kept for render targets goes once the world's targets are
/// made and have stopped growing, and comes back when they grow again.
/// Ported from trustno1-coder/VIctory-NVIDIA 21b040da (readmet3xt-lab).
#[test]
fn room_for_targets_is_given_up_once_they_settle() {
const MIB: u64 = 1 << 20;
let most = 3072 * MIB;
// Before the world: kept, however long.
assert_eq!(targets_kept(most, 652 * MIB, 60_000, 20_000), 2420 * MIB);
// The world's first frames: still growing.
assert_eq!(targets_kept(most, 2083 * MIB, 5_000, 20_000), 989 * MIB);
// Settled: nothing kept.
assert_eq!(targets_kept(most, 2432 * MIB, 20_000, 20_000), 0);
// Setting 0 (the default): kept as before, however long.
assert_eq!(targets_kept(most, 2432 * MIB, 600_000, 0), 640 * MIB);

// Growth of 64 MiB restarts the clock; less does not.
assert_eq!(targets_seen(2000 * MIB, 1_000, 2050 * MIB, 9_000), (2000 * MIB, 1_000));
assert_eq!(targets_seen(2000 * MIB, 1_000, 2064 * MIB, 9_000), (2064 * MIB, 9_000));
// A fall lowers the record without restarting it, so growing back
// counts as growth.
let (last, since) = targets_seen(2400 * MIB, 1_000, 1000 * MIB, 9_000);
assert_eq!((last, since), (1000 * MIB, 1_000));
assert_eq!(targets_seen(last, since, 2400 * MIB, 30_000), (2400 * MIB, 30_000));

// The run's line says when, and how much is freed or kept again.
assert_eq!(
targets_line(true, most, 2432 * MIB, 41_300),
"render-target reserve (LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS): given up at 41.3 s, targets 2432 MB, 640 MB freed for textures"
);
assert_eq!(
targets_line(false, most, 2496 * MIB, 95_000),
"render-target reserve (LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS): taken back at 95.0 s, targets 2496 MB, 576 MB kept"
);

// The reserve turns once each way. Settled at 2432 MiB under a cap
// of 3072: given up. Then grown straight to the cap: nothing is
// kept, so it is not taken back (no "0 MB kept" line). Grown again
// by 64 MiB from below the cap, 576 MiB kept: taken back.
assert_eq!(targets_turn(false, most, 2432 * MIB, 0), Some(true));
assert_eq!(targets_turn(true, most, 2432 * MIB, 0), None);
assert_eq!(targets_turn(true, most, 3072 * MIB, 0), None);
assert_eq!(targets_turn(true, most, 2496 * MIB, 576 * MIB), Some(false));
assert_eq!(targets_turn(false, most, 2496 * MIB, 576 * MIB), None);
// At the cap with nothing kept, the reserve was never given up.
assert_eq!(targets_turn(false, most, 3072 * MIB, 0), None);
}

/// A placed texture viewed from a later level has a host copy of that
/// and the smaller levels; a view from an earlier one gets another.
#[cfg(windows)]
Expand Down