feat(residency): VICTORY_RAM_PRESSURE_MB gates heavy trims on physical RAM

Add residency::ram_critical(): when VICTORY_RAM_PRESSURE_MB=N>0 and sampled
physical available RAM < N MiB (hysteresis, enter <N, leave >N+256MiB), the
once-a-second host sweep also runs the fence-safe heavy trims + let_go_idle
sweeps that were previously gated only on critical_commit() (OS commit
headroom), which never fires on a 16 GB box with a large pagefile. Inert and
behavior-identical when the var is unset/0: no new sample or branch taken.
Rate-limited to the existing ~1 Hz sweep; logs one line (avail MB, freed MB,
host copies/textures alive). trim_idle keeps a tighter idle pool (24 vs 64)
under RAM pressure. All existing fence/ownership guards preserved.

Verified: cargo test -p victory-gpu --lib -j 4 (495 passed, 0 failed);
cargo build --profile diag -p victory --features diagnostics -j 4 (ok).
Asset policy: no new violation (only pre-existing .agents .diff + imgui.ini).
This commit is contained in:
theme613 committed 2026-12-01 00:12:49 +08:00
1 parent 5d62b86f4a
commit f0d1eb7682
4 files changed
+964 -83

No files matched your search

+111 -4
View File
@@ -422,12 +422,18 @@ pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
#[cfg(windows)]
pub(crate) fn trim_idle() {
const PRESSURED_KEEP: usize = 64;
// Under physical-RAM pressure (VICTORY_RAM_PRESSURE_MB) keep a tighter
// but still generous idle pool so loose allocators are shed, without
// cutting below what a steady frame reuses. Unchanged (64) when the
// feature is off, since `ram_critical()` is then always false.
const RAM_CRITICAL_KEEP: usize = 24;
let keep = if crate::residency::ram_critical() { RAM_CRITICAL_KEEP } else { PRESSURED_KEEP };
let freed: Vec<Idle> = {
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
if idle.0.len() <= PRESSURED_KEEP {
if idle.0.len() <= keep {
return;
}
let dropped = idle.0.split_off(PRESSURED_KEEP);
let dropped = idle.0.split_off(keep);
let dropped_bytes: u64 = dropped.iter().map(|Idle(_, b, _)| *b).sum();
idle.1 = idle.1.saturating_sub(dropped_bytes);
dropped
@@ -2344,12 +2350,113 @@ extern "system" fn wait_value64(this: This, address: u64, comparison: u32, refer
wait_for(this, address, comparison, reference, true);
}
/// Whether enhanced barrier groups are inspected before emitting a full
/// global UAV barrier (`VICTORY_SMART_BARRIERS=1`, off by default: skipping
/// non-UAV barriers can omit state syncs for render targets/lighting buffers).
#[cfg(windows)]
fn smart_barriers() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_SMART_BARRIERS").is_some_and(|v| v == "1"))
}
/// Enhanced barrier groups `[kept, skipped]` since the last log interval.
#[cfg(windows)]
pub(crate) static BARRIER_GROUPS_STATS: [std::sync::atomic::AtomicU64; 2] =
[const { std::sync::atomic::AtomicU64::new(0) }; 2];
/// Returns `true` if the enhanced barrier groups in `groups[0..count]` require
/// a global UAV synchronization barrier (ordering a prior shader UAV write),
/// or `false` if they are purely non-UAV transitions (e.g. RT/DSV/copy texture
/// layout transitions that our texture state tracker already emits explicitly,
/// or `SYNC_NONE` / `NO_ACCESS` barriers).
#[cfg(windows)]
pub(crate) fn barrier_groups_needs_uav(count: u32, groups: *const u8) -> bool {
use windows::Win32::Graphics::Direct3D12::*;
if count == 0 {
return false;
}
if !smart_barriers() || groups.is_null() || count > 64 {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
let needs_access = |sync_before: D3D12_BARRIER_SYNC, access_before: D3D12_BARRIER_ACCESS| -> bool {
if sync_before == D3D12_BARRIER_SYNC_NONE {
return false;
}
// `COMMON` (0) or `UNORDERED_ACCESS` (0x80) may order a prior UAV write;
// pure RT/DSV/copy/SRV/no-access transitions do not need a global UAV drain.
access_before == D3D12_BARRIER_ACCESS_COMMON
|| (access_before.0 & D3D12_BARRIER_ACCESS_UNORDERED_ACCESS.0) != 0
};
// SAFETY: the title passes `count` valid `D3D12_BARRIER_GROUP` entries
// whose inner barrier arrays are valid for the duration of the call.
let slice = unsafe { std::slice::from_raw_parts(groups.cast::<D3D12_BARRIER_GROUP>(), count as usize) };
for g in slice {
let n = g.NumBarriers as usize;
if n == 0 {
continue;
}
if n > 4096 {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
match g.Type {
D3D12_BARRIER_TYPE_GLOBAL => {
let p = unsafe { g.Anonymous.pGlobalBarriers };
if p.is_null() {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
let bs = unsafe { std::slice::from_raw_parts(p, n) };
if bs.iter().any(|b| needs_access(b.SyncBefore, b.AccessBefore)) {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
}
D3D12_BARRIER_TYPE_BUFFER => {
let p = unsafe { g.Anonymous.pBufferBarriers };
if p.is_null() {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
let bs = unsafe { std::slice::from_raw_parts(p, n) };
if bs.iter().any(|b| needs_access(b.SyncBefore, b.AccessBefore)) {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
}
D3D12_BARRIER_TYPE_TEXTURE => {
let p = unsafe { g.Anonymous.pTextureBarriers };
if p.is_null() {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
let bs = unsafe { std::slice::from_raw_parts(p, n) };
if bs.iter().any(|b| needs_access(b.SyncBefore, b.AccessBefore)) {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
}
_ => {
BARRIER_GROUPS_STATS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return true;
}
}
}
BARRIER_GROUPS_STATS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
false
}
/// The enhanced barriers `(group count, groups)`: buffer, texture and
/// global barriers with layouts instead of states. Every host use of a
/// title texture (clears, copies) moves its host copy to the state it needs
/// first, so only their ordering matters: one barrier on every shader
/// write (the title's buffer barriers order compute passes).
extern "system" fn barrier_groups(this: This, count: u32, _groups: *const u8) {
extern "system" fn barrier_groups(this: This, count: u32, groups: *const u8) {
#[cfg(windows)]
if count == 0 || (!groups.is_null() && !barrier_groups_needs_uav(count, groups)) {
return;
}
// Diagnostics: counted, and left out under `VICTORY_BARRIER_TEST=groups`.
#[cfg(all(windows, feature = "diagnostics"))]
if count > 0 {
@@ -2370,7 +2477,7 @@ extern "system" fn barrier_groups(this: This, count: u32, _groups: *const u8) {
unsafe { list.list.ResourceBarrier(&[barrier]) };
}
#[cfg(not(windows))]
let _ = (this, count);
let _ = (this, count, groups);
}
/// The bottom-of-pipe value writes `(address, value, flush, flags)`, 32 and