From 221f11a8d0ba40afc55de3bd18dfade5aca96ccd Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Sat, 28 Nov 2026 20:42:08 +0100 Subject: [PATCH 1/4] Render-target reserve expires once the targets settle (LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS, 0 = off by default) PORT-NV-VRAM-TARGET-RESERVE-SETTLE. The texture queue makes no new host copies while in use + kept for buffer copies + kept for render targets reaches 90 % of the card's budget. The target reserve (LEONIDA_CARD_ROOM_FOR_TARGETS_MB, 3,072 MB less the targets made) never expired: at 1440p targets level off at 2.1-2.9 GB, so 160-990 MB stayed kept for targets that never came while thousands of textures waited. With LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS set, once targets hold half the reserve or more and have not grown by 64 MiB for that many ms, nothing is kept; growth of 64 MiB brings the reserve back. Upstream defaults to 20,000 ms; here it is 0 (kept for the run, as before) because the behaviour would change for everyone. Ported from trustno1-coder/VIctory-NVIDIA 21b040da (readmet3xt-lab), GPL-3.0. Only the reserve change is taken; the same commit's room-test log line and pass timing counters (draw.rs, host.rs) are a separate subject and are left. Co-authored-by: readmet3xt-lab <238408149+readmet3xt-lab@users.noreply.github.com> Co-Authored-By: Claude Sonnet 5.5 --- d3d12/README.md | 1 + d3d12/crates/leonida-gpu/src/resource.rs | 89 +++++++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index e4886a5bec..d2d26e3833 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -19,6 +19,7 @@ project name changed where it named the program. | Skin that turns black later and stays so | `LEONIDA_HEAP_RECHECK_EVERY` (300 frames; 0 off): uploaded textures picked by index are looked at again on a slow beat after the first five looks | `crates/leonida-gpu/src/draw.rs` | fxceless/victory-fork, after RyanCase94 `920423a9` | | Black pixels where a NaN or infinity was scrubbed | `v_mac_legacy_f32` / `v_mad_legacy_f32` fused with flushed sources, as RDNA 2 runs them: a subnormal factor is a zero, so subnormal times infinity is no longer infinity (test `mad_legacy_takes_a_subnormal_factor_as_a_zero`) | `crates/leonida-shader/src/emit/lower.rs` | fxceless/victory-fork `f6a09d3` | | White glitter on floors, glowing windows and eyes | `LEONIDA_REFLECT_MOST` (off unless set since the renderer merged at `00698e1e8`, where it is unproven; 3 was the default before. It is not what posterizes some runs: those come out so with it off too): after `CS_UpdateHistory`, reflections are capped at a multiple of the room's light and colours far from grey pulled towards it; without rays they hold the sky of every miss | `crates/leonida-gpu/src/ambient.rs` | Leonida's own pass; the program and its view from lepotane/VIctory-leonida | +| 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\\` (`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) | diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 31fbcfe1a0..04ff572e93 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -1853,9 +1853,22 @@ 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; + use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; static MOST: std::sync::OnceLock = std::sync::OnceLock::new(); let most = *MOST.get_or_init(|| { std::env::var("LEONIDA_CARD_ROOM_FOR_TARGETS_MB") @@ -1863,9 +1876,55 @@ pub(crate) fn targets_room_kept() -> u64 { .and_then(|v| v.trim().parse::().ok()) .map_or(3072 << 20, |mb| mb.min(8192) << 20) }); + static SETTLE: std::sync::OnceLock = 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::().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) + if settle == 0 { + return targets_kept(most, targets, 0, 0); + } + // The targets' bytes at their last change of 64 MiB or more, and when + // they last grew by that much. + static GREW: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2]; + let now = crate::host::now_ms(); + let (last, since) = (GREW[0].load(Relaxed), GREW[1].load(Relaxed)); + let (last, since) = targets_seen(last, since, targets, now); + GREW[0].store(last, Relaxed); + GREW[1].store(since, Relaxed); + targets_kept(most, targets, now.saturating_sub(since), settle) +} + +/// [`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 @@ -5183,6 +5242,32 @@ 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)); + } + /// 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)] From 2058ecb5b4d5774943f9fec64f7a0781308a0505 Mon Sep 17 00:00:00 2001 From: MoonEyes <14891700+MoonEyes@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:18:21 +0100 Subject: [PATCH 2/4] Render-target reserve: one log line when it is given up or taken back Only with LEONIDA_CARD_ROOM_FOR_TARGETS_SETTLE_MS set; the default path returns before it. Lets a run show when the reserve expires and how many MB go to textures, for the A/B test of this setting. Co-Authored-By: Claude Opus 5.5 --- d3d12/crates/leonida-gpu/src/resource.rs | 44 +++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 04ff572e93..f1b42ebbde 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -1896,7 +1896,39 @@ pub(crate) fn targets_room_kept() -> u64 { let (last, since) = targets_seen(last, since, targets, now); GREW[0].store(last, Relaxed); GREW[1].store(since, Relaxed); - targets_kept(most, targets, now.saturating_sub(since), settle) + 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). + static GIVEN_UP: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + let given_up = kept == 0 && targets < most; + if GIVEN_UP.swap(given_up, Relaxed) != given_up { + tracing::info!("{}", targets_line(given_up, most, targets, now)); + } + kept +} + +/// 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 @@ -5266,6 +5298,16 @@ float4 lit() : SV_Target { return 1.0; } 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" + ); } /// A placed texture viewed from a later level has a host copy of that From c007cfeee39ac3a46b6a153d5f856e3d9c747261 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 09:48:49 +0000 Subject: [PATCH 3/4] render-target reserve: "taken back" only when something is kept again lixonik (review, resource.rs:1903): targets that settle under the cap and then grow straight to it flip the flag and write "taken back ... 0 MB kept", which a reader takes for textures losing room. The turn is now a pure function, targets_turn: given up when nothing is kept below the cap, taken back only with kept > 0, no line otherwise. The reserve itself is unchanged. Tested: the new asserts in room_for_targets_is_given_up_once_they_settle (lixonik's fixture: 2432 MiB settled, then 3072 MiB) compiled and run on their own with rustc; the crate's lib tests build on Windows only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDMsHtPdJ5kt8t7A5i2fzu --- d3d12/crates/leonida-gpu/src/resource.rs | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 9e255b9333..204aeb4719 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -1973,13 +1973,30 @@ pub(crate) fn targets_room_kept() -> u64 { // 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). static GIVEN_UP: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - let given_up = kept == 0 && targets < most; - if GIVEN_UP.swap(given_up, Relaxed) != given_up { + if let Some(given_up) = targets_turn(GIVEN_UP.load(Relaxed), most, targets, kept) { + GIVEN_UP.store(given_up, Relaxed); 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 { + 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. @@ -5381,6 +5398,18 @@ float4 lit() : SV_Target { return 1.0; } 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 From ee27e871966c01d59c0cbc36468fa7f7cf51f33f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 29 Nov 2026 12:23:45 +0000 Subject: [PATCH 4/4] render-target reserve: one lock over the settle record (opt-in branch only) merzleckeier (#2589, 11:55): targets_room_kept read the growth record (GREW) and the given-up flag, computed, then stored them back as separate atomics. A caller delayed between its read and its store put back an older record: with synchronized threads the reserve kept 0 MiB where 709 were due. Not seen in game. The record, the flag, and the targets' bytes and the time they are compared with are now read, computed and stored under one Mutex, so no store can go back in time. The default (setting 0) returns before the lock and stays lock-free. Tested: cargo check -p leonida-gpu --target x86_64-pc-windows-gnu (MinGW) passes with the same 19 warnings as before; the function is Windows-only and was not run. Not done: a threaded test of the race. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDMsHtPdJ5kt8t7A5i2fzu --- d3d12/crates/leonida-gpu/src/resource.rs | 33 ++++++++++++++---------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 204aeb4719..ce2720ebae 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -1941,7 +1941,7 @@ pub(crate) fn room(memory: Option<[u64; 4]>) -> bool { /// 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::{AtomicU64, Ordering::Relaxed}; + use std::sync::atomic::Ordering::Relaxed; static MOST: std::sync::OnceLock = std::sync::OnceLock::new(); let most = *MOST.get_or_init(|| { std::env::var("LEONIDA_CARD_ROOM_FOR_TARGETS_MB") @@ -1957,24 +1957,29 @@ pub(crate) fn targets_room_kept() -> u64 { .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)); + let targets_now = || HOST_TEXTURES[1].load(Relaxed).saturating_sub(KINDS[1].load(Relaxed)); if settle == 0 { - return targets_kept(most, targets, 0, 0); - } - // The targets' bytes at their last change of 64 MiB or more, and when - // they last grew by that much. - static GREW: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2]; + 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) = (GREW[0].load(Relaxed), GREW[1].load(Relaxed)); - let (last, since) = targets_seen(last, since, targets, now); - GREW[0].store(last, Relaxed); - GREW[1].store(since, Relaxed); + 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). - static GIVEN_UP: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - if let Some(given_up) = targets_turn(GIVEN_UP.load(Relaxed), most, targets, kept) { - GIVEN_UP.store(given_up, Relaxed); + 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