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
50 changes: 50 additions & 0 deletions crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,39 @@ pub(crate) fn vram_idle_ms(video_memory: Option<[u64; 4]>) -> u64 {
}
}

/// With `LEONIDA_TEXTURE_KEEP=1` (off by default): how long host copies of
/// textures the title fills must have been idle to be let go, in
/// milliseconds, and how many bytes of them go at most (the longest idle
/// first). Under 85 % of the card's budget only those idle for ten minutes;
/// under 93 % those idle for 5 s, enough to come down to 80 %; above that
/// those idle for 3 s, enough to come down to 84 %. After
/// Belobug/VIctory-Belobug-AMD (`vram_keep`). [`vram_idle_ms`] lets every
/// copy idle for 1 s go from 85 % on: 55,370 let go and 14,986 made again
/// in a 330 s run on an RTX 3080 12 GB that sat at 86 % of its budget.
pub(crate) fn vram_keep(budget: u64, used: u64) -> (u64, u64) {
const KEPT_MS: u64 = 600_000;
const IDLE_MS: u64 = 5_000;
const SHORT_MS: u64 = 3_000;
if budget == 0 {
return (IDLE_MS, u64::MAX);
}
let share = |percent: u64| budget / 100 * percent;
if used < share(85) {
(KEPT_MS, u64::MAX)
} else if used < share(93) {
(IDLE_MS, used - share(80))
} else {
(SHORT_MS, used - share(84))
}
}

/// Whether [`vram_keep`] decides what the texture sweep lets go
/// (`LEONIDA_TEXTURE_KEEP=1`; off by default).
pub(crate) fn texture_keep() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("LEONIDA_TEXTURE_KEEP").is_some_and(|v| v == "1"))
}

/// Effective idle timeout (in milliseconds) for system-RAM caches (staging
/// buffers, loose shadows, and the mapped pipeline library), scaled down
/// when free physical RAM is low. Never returns less than 2,000 ms.
Expand Down Expand Up @@ -8904,6 +8937,23 @@ mod batch { // Batch::texture_due lives here; `mod tests` is already taken at
assert_eq!(vram_idle_ms(Some([0, 100, 0, 0])), 5_000);
}

#[test]
fn vram_keep_lets_go_only_what_the_card_needs() {
use super::vram_keep;
let budget = 10_000;
// Under 85 %: only copies idle for ten minutes, as many as there are.
assert_eq!(vram_keep(budget, 0), (600_000, u64::MAX));
assert_eq!(vram_keep(budget, 8_499), (600_000, u64::MAX));
// 85 % to 93 %: idle for 5 s, down to 80 %.
assert_eq!(vram_keep(budget, 8_500), (5_000, 500));
assert_eq!(vram_keep(budget, 9_299), (5_000, 1_299));
// From 93 % (and over the budget): idle for 3 s, down to 84 %.
assert_eq!(vram_keep(budget, 9_300), (3_000, 900));
assert_eq!(vram_keep(budget, 12_000), (3_000, 3_600));
// No budget: the old idle time, no limit.
assert_eq!(vram_keep(0, 100), (5_000, u64::MAX));
}

#[test]
fn ram_idle_ms_scales_with_pressure() {
use super::ram_idle_ms;
Expand Down
28 changes: 26 additions & 2 deletions crates/leonida-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,12 +1759,36 @@ pub(crate) fn let_go_idle() {
if !*ON.get_or_init(|| std::env::var_os("LEONIDA_TEXTURE_SWEEP").is_none_or(|v| v != "0")) {
return;
}
let idle_ms = crate::host::vram_idle_ms(crate::host::host().and_then(|h| h.video_memory()));
// `most`: the bytes that may go in this look (`LEONIDA_TEXTURE_KEEP=1`,
// `crate::host::vram_keep`), the longest idle first.
let memory = crate::host::host().and_then(|h| h.video_memory());
let (idle_ms, most) = match memory {
Some([budget, used, ..]) if crate::host::texture_keep() => {
crate::host::vram_keep(budget, used)
}
_ => (crate::host::vram_idle_ms(memory), u64::MAX),
};
// For the trail (`crate::host::trail`): how many went, their bytes,
// and the first one.
let (mut went, mut bytes, mut first) = (0u64, 0u64, None);
let all = textures();
for &at in all.iter() {
let mut by_age = Vec::new();
if most != u64::MAX {
for &at in all.iter() {
// SAFETY: as below.
let r = unsafe { &*(at as *const Resource) };
let used = r.host.used.load(Relaxed);
if used != 0 && fills(&r.desc) && crate::host::long_ago(used, idle_ms, IDLE_FRAMES) {
by_age.push((used & 0xffff_ffff, at));
}
}
by_age.sort_unstable();
}
let whole = if most == u64::MAX { usize::MAX } else { 0 };
for at in all.iter().copied().take(whole).chain(by_age.iter().map(|&(_, at)| at)) {
if bytes >= most {
break;
}
// SAFETY: a texture leaves the table before it is freed, under the
// lock held here (`Drop for Resource`).
let r = unsafe { &*(at as *const Resource) };
Expand Down
30 changes: 30 additions & 0 deletions docs/fork-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,36 @@ More runs the same way, with levels on demand on (same PC, same day):
stays at 15.4 ms (60 pipelines) with it: it is not the ray walk. What
it is, is still open.

## Texture keep rule (2026-11-26)

`LEONIDA_TEXTURE_KEEP=1` (off; opt-in profile `texture-keep`): the
sweep of host copies of textures the title fills lets go only what the
card needs. Under 85 % of its budget only copies idle for ten minutes go;
from 85 % those idle for 5 s, the longest idle first, until 80 %; from
93 % those idle for 3 s until 84 % (`host::vram_keep`, after
Belobug/VIctory-Belobug-AMD 8d79927). Without it every copy idle for 1 s
goes from 85 % on, and a 12 GB card sits there all the time in the first
rooms.

RTX 3080 12 GB, Ryzen 7 7800X3D, driver 621.24, two pairs of unattended
runs (`scripts/measure-run.ps1`), on `e8aa6152`:

| Run | Usual frame (ms) | Median (ms) | Made again | No room |
|---|---|---|---|---|
| `main` | 156.5 | 160 | 14,986 | 96 |
| keep | 142 | 152 | 8,545 | 319 |
| `main` | 145 | 158 | 17,149 | 12 |
| keep | 130.5 | 132.5 | 5,674 | 390 |

"Made again": host copies of filled textures let go and made again since
the start; "no room": passes that made none for lack of room. Frames
were 14 to 15 ms shorter in both pairs, and copies made again fell by 43
and 67 %. But passes without room grew, and the first keep run's shot of
world frame 600 had 4.6 % near-black pixels against 2.0 %; in the second
pair the shots were alike (0.6 % against 0.0 %). No run lost the device
or wrote a crash report. Opt-in until it is measured on more cards and
the passes without room are understood.

## Debugging

A release build has no debugging aids. Build them in with
Expand Down
7 changes: 7 additions & 0 deletions gpu-profiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
"LEONIDA_WINDOW_UPLOAD": "1",
"LEONIDA_TEXTURE_ROOM_PERCENT": "70"
}
},
{
"name": "texture-keep",
"opt_in": true,
"match": {},
"measured": "RTX 3080 12 GB, Ryzen 7 7800X3D, driver 621.24, first rooms, two pairs of unattended runs, 2026-11-26: usual frame 156.5 -> 142 and 145 -> 130.5 ms; host copies of textures made again 14,986 -> 8,545 and 17,149 -> 5,674; but passes that made none for lack of room 96 -> 319 and 12 -> 390. Opt-in until measured on more cards.",
"env": { "LEONIDA_TEXTURE_KEEP": "1" }
}
]
}