From 9beff3cd4bb866bd7df5c17650afa445c06725a7 Mon Sep 17 00:00:00 2001 From: fever308 <89748148+fever308@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:53:05 -0600 Subject: [PATCH] gpu: scale texture VRAM floor and per-frame staging budget from VRAM --- crates/victory-gpu/src/draw.rs | 43 +++++++++++++++++++++++++++--- crates/victory-gpu/src/resource.rs | 41 ++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index f9937b1..f6eb649 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -1445,8 +1445,11 @@ impl State { if !*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_TEXTURES").is_none_or(|v| v != "0")) { return; } - /// Bytes of texture memory staged per frame. - const PER_FRAME: u64 = 192 << 20; + let per_frame = staging_budget_per_frame( + crate::host::host() + .and_then(|h| h.video_memory()) + .map(|[budget, ..]| budget), + ); static FRAME: AtomicU64 = AtomicU64::new(u64::MAX); static SPENT: AtomicU64 = AtomicU64::new(0); let frame = crate::frame::current(); @@ -1502,7 +1505,7 @@ impl State { const SPAN: std::time::Duration = std::time::Duration::from_millis(250); let began = std::time::Instant::now(); for (index, write) in due { - if SPENT.load(Ordering::Relaxed) >= PER_FRAME || began.elapsed() > SPAN { + if SPENT.load(Ordering::Relaxed) >= per_frame || began.elapsed() > SPAN { break; } // A placed texture whose memory is not mapped (not streamed in @@ -2690,3 +2693,37 @@ pub(crate) fn stencil_ref(list: &HostList, reference: u32) { // SAFETY: an open list. unsafe { list.list.OMSetStencilRef(reference) }; } + +/// Per-frame texture staging byte budget scaled from the adapter's local VRAM +/// budget (`None` keeps the 192 MiB 12 GiB baseline). +fn staging_budget_per_frame(vram_budget: Option) -> u64 { + const GIB: u64 = 1 << 30; + const MIB: u64 = 1 << 20; + match vram_budget { + Some(budget) if budget <= 6 * GIB => 96 * MIB, + Some(budget) if budget >= 20 * GIB => 384 * MIB, + Some(budget) if budget > 14 * GIB => 256 * MIB, + _ => 192 * MIB, + } +} + +#[cfg(test)] +mod tests { + use super::staging_budget_per_frame; + + #[test] + fn staging_budget_scales_with_vram_budget() { + const GIB: u64 = 1 << 30; + const MIB: u64 = 1 << 20; + assert_eq!(staging_budget_per_frame(None), 192 * MIB); + assert_eq!(staging_budget_per_frame(Some(4 * GIB)), 96 * MIB); + assert_eq!(staging_budget_per_frame(Some(6 * GIB)), 96 * MIB); + assert_eq!(staging_budget_per_frame(Some(8 * GIB)), 192 * MIB); + assert_eq!(staging_budget_per_frame(Some(12 * GIB)), 192 * MIB); + assert_eq!(staging_budget_per_frame(Some(14 * GIB)), 192 * MIB); + assert_eq!(staging_budget_per_frame(Some(16 * GIB)), 256 * MIB); + assert_eq!(staging_budget_per_frame(Some(20 * GIB)), 384 * MIB); + assert_eq!(staging_budget_per_frame(Some(24 * GIB)), 384 * MIB); + assert_eq!(staging_budget_per_frame(Some(32 * GIB)), 384 * MIB); + } +} diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 3782a0d..a3297ae 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1354,17 +1354,28 @@ pub(crate) fn host_made<'a>( Some(unsafe { &*std::ptr::from_ref(&host.texture) }) } +/// Unconditional host-texture floor for a given local VRAM budget (`None` when +/// the adapter does not report one): up to 2 GiB, capped at a quarter of the +/// reported budget so 4–6 GiB cards do not bypass the budget check early. +#[cfg(any(windows, test))] +fn texture_floor_bytes(vram_budget: Option) -> u64 { + const MAX_FLOOR: u64 = 2 << 30; + match vram_budget { + Some(budget) => MAX_FLOOR.min(budget / 4), + None => MAX_FLOOR, + } +} + /// Whether more host copies of textures the title fills may be made for /// shaders that pick textures by index (`draw.rs` `heap_textures`): always -/// up to `FLOOR` bytes of them, beyond that while the card's memory in use -/// by this process is under nine tenths of the budget the system gives it -/// (`VICTORY_TEXTURE_BUDGET_MB` sets a limit on those copies' bytes -/// instead). Past the budget the system moves the card's memory into the -/// PC's, which ended runs 20 and 24. +/// up to [`texture_floor_bytes`] bytes of them, beyond that while the card's +/// memory in use by this process is under nine tenths of the budget the system +/// gives it (`VICTORY_TEXTURE_BUDGET_MB` sets a limit on those copies' bytes +/// instead). Past the budget the system moves the card's memory into the PC's, +/// which ended runs 20 and 24. #[cfg(windows)] pub(crate) fn room() -> bool { use std::sync::atomic::Ordering::Relaxed; - const FLOOR: u64 = 2 << 30; const WITHOUT_BUDGET: u64 = 4 << 30; static LIMIT: std::sync::OnceLock> = std::sync::OnceLock::new(); let limit = *LIMIT.get_or_init(|| { @@ -1375,10 +1386,12 @@ pub(crate) fn room() -> bool { if let Some(limit) = limit { return filled < limit; } - if filled < FLOOR { + let memory = crate::host::host().and_then(|h| h.video_memory()); + let floor = texture_floor_bytes(memory.map(|[budget, ..]| budget)); + if filled < floor { return true; } - match crate::host::host().and_then(|h| h.video_memory()) { + match memory { Some([budget, used, ..]) => used < budget / 10 * 9, None => filled < WITHOUT_BUDGET, } @@ -3407,4 +3420,16 @@ float4 lit() : SV_Target { return 1.0; } assert_eq!(slot::(heap, 0x10)(heap), 1); assert_eq!(slot::(placed, 0x10)(placed), 0); } + + #[test] + fn texture_floor_scales_with_vram_budget() { + const GIB: u64 = 1 << 30; + assert_eq!(texture_floor_bytes(None), 2 * GIB); + assert_eq!(texture_floor_bytes(Some(4 * GIB)), GIB); + assert_eq!(texture_floor_bytes(Some(6 * GIB)), 6 * GIB / 4); + assert_eq!(texture_floor_bytes(Some(8 * GIB)), 2 * GIB); + assert_eq!(texture_floor_bytes(Some(12 * GIB)), 2 * GIB); + assert_eq!(texture_floor_bytes(Some(24 * GIB)), 2 * GIB); + assert_eq!(texture_floor_bytes(Some(32 * GIB)), 2 * GIB); + } }