From 76b6a3bf9c0dc50a7cc99cad1d82479228a5bf48 Mon Sep 17 00:00:00 2001 From: Chaeril M <1352081+ChaerilM@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:51:45 +0700 Subject: [PATCH] GPU: tune reloadable texture and staging memory budgets Adapt VItoriaBr texture budget and fxceless staging-pool tuning to the existing synchronous eviction and upload paths. Preserve default behavior, add regression tests and hardware preset values. Stacked on PR #3. --- README.md | 32 ++++++++++++------ crates/victory-gpu/src/resource.rs | 54 +++++++++++++++++++++++++++--- crates/victory-gpu/src/texture.rs | 46 +++++++++++++++++++++++-- presets/12gb.env | 2 ++ presets/16gb.env | 2 ++ presets/8gb.env | 2 ++ presets/default.env | 2 ++ presets/high.env | 2 ++ presets/low.env | 2 ++ scripts/launch.ps1 | 18 +++++++++- 10 files changed, 143 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7cfcb35435..4e33c96ae2 100644 --- a/README.md +++ b/README.md @@ -157,14 +157,14 @@ The PowerShell launcher applies a named preset before starting VIctory. These experimental switches require the corresponding performance backports (PR #1 and #2); older builds simply do not consume the new variables. -| Profile | Window grid | Idle scratch budget | Buffered title reads | Soft VRAM target | -|---------|-------------|---------------------|----------------------|------------------| -| default | 4096 KiB | 0 MiB | off | automatic | -| low | 1024 KiB | 0 MiB | off | automatic | -| high | 1024 KiB | 256 MiB | on | automatic | -| 8gb | 1024 KiB | 0 MiB | off | 5120 MiB | -| 12gb | 1024 KiB | 128 MiB | off | 7680 MiB | -| 16gb | 1024 KiB | 256 MiB | off | 10240 MiB | +| Profile | Window grid | Idle scratch | Buffered reads | Soft VRAM target | Reloadable textures | Idle staging | +|---------|-------------|--------------|----------------|------------------|---------------------|--------------| +| default | 4096 KiB | 0 MiB | off | automatic | automatic | 576 MiB | +| low | 1024 KiB | 0 MiB | off | automatic | automatic | 128 MiB | +| high | 1024 KiB | 256 MiB | on | automatic | automatic | 576 MiB | +| 8gb | 1024 KiB | 0 MiB | off | 5120 MiB | 4096 MiB | 128 MiB | +| 12gb | 1024 KiB | 128 MiB | off | 7680 MiB | 6144 MiB | 256 MiB | +| 16gb | 1024 KiB | 256 MiB | off | 10240 MiB | 8192 MiB | 576 MiB | The hardware profiles are for **dedicated VRAM**, not system RAM. Inspired by [VItoriaBr's 8 GB / 16 GB presets](https://github.com/uiliannn/VItoriaBr#english). @@ -173,7 +173,17 @@ The 12 GB target is an interpolation, not a measured setting. MiB (0 = automatic). It limits the existing DXGI budget threshold, never raises it. Only eligible title-fed textures can be evicted, so this is not a hard VRAM cap or a separate texture-only budget. More aggressive -eviction can cause texture churn. The fork's separate card-buffer budget, +eviction can cause texture churn. + +`VICTORY_TEXTURE_BUDGET_MB` is a separate soft limit on reloadable +title-fed texture copies (0 = automatic). It excludes render targets, +depth and GPU-written textures and uses the existing eviction pass. +It is not an allocation gate. The profile values leave nominal room +for other GPU allocations; they are provisional, not fork benchmarks. +`VICTORY_STAGING_POOL_MB` bounds the retained upload staging buffers +between submissions (0 = retain none, default 576). In-flight uploads +may exceed it. This primarily trades system memory for allocation churn, +not a hard VRAM limit. The fork's separate card-buffer budget, async submission and internal-resolution controls are not implemented here. From the repository root, after building the release executable: @@ -199,10 +209,10 @@ create a git-ignored `.env` in the repository root, for example: VICTORY_SCRATCH_SPARE_MB=128 ``` -Precedence for the four managed variables: local `.env` > selected preset +Precedence for the six managed variables: local `.env` > selected preset > inherited environment. All other environment variables are left alone. The launcher accepts plain KEY=VALUE lines, blank lines and whole-line -comments. It validates the four numeric settings, rejects unknown names +comments. It validates the six numeric settings, rejects unknown names and duplicate entries, and does not execute file contents. The parent PowerShell environment is restored when VIctory exits. diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index d22b1dd4ac..e600f7ba46 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1602,12 +1602,39 @@ fn vram_target() -> Option { /// Keep the existing hysteresis ratio and eviction eligibility. #[cfg(windows)] fn eviction_excess(used: f64, budget: f64, target_mib: Option) -> u64 { - let high = BUDGET_HIGH * budget; - let high = target_mib.map_or(high, |mib| high.min(mib as f64 * 1_048_576.0 / 1e6)); - let low = if target_mib.is_some() { high * (BUDGET_LOW / BUDGET_HIGH) } else { BUDGET_LOW * budget }; + let driver_high = BUDGET_HIGH * budget; + let high = target_mib.map_or(driver_high, |mib| driver_high.min(mib as f64 * 1_048_576.0 / 1e6)); + let low = if high < driver_high { high * (BUDGET_LOW / BUDGET_HIGH) } else { BUDGET_LOW * budget }; if used > high { ((used - low) * 1e6) as u64 } else { 0 } } +/// Soft budget of live, reloadable title-fed texture copies (MiB). +/// Zero or an invalid setting keeps the old DXGI-only policy. +#[cfg(windows)] +fn texture_budget() -> Option { + static LIMIT: std::sync::OnceLock> = std::sync::OnceLock::new(); + *LIMIT.get_or_init(|| { + let limit = std::env::var("VICTORY_TEXTURE_BUDGET_MB").ok() + .and_then(|v| v.trim().parse::().ok()) + .filter(|&v| (256..=65536).contains(&v)) + .map(|v| v << 20); + if let Some(bytes) = limit { + tracing::info!(mib = bytes >> 20, "soft reloadable texture budget enabled"); + } + limit + }) +} + +/// A second pressure signal, not additive with the DXGI excess: evicting +/// one texture satisfies both. Leave 5% headroom to reduce repeated sweeps. +#[cfg(windows)] +fn texture_excess(bytes: u64, limit: Option) -> u64 { + match limit { + Some(limit) if bytes > limit => bytes - (limit - limit / 20), + _ => 0, + } +} + /// Once per presented frame, before a submission (`Host::execute`, one at a /// time, the last one has run): gives up host textures fed from title memory /// that the title has not used for [`IDLE_MS`], and past [`BUDGET_HIGH`] of @@ -1643,7 +1670,16 @@ fn evict(frame: u64, now: u64, over: u64) -> (u64, u64) { // reported as new only once, so its first read alone stamped it; run // evict1 gave up and made again hundreds of textures a frame). let read = crate::descriptor::read_textures(&crate::shadow::reads_since()); - evict_among(&candidates(), &read, frame, now, over) + let candidates = candidates(); + let over = if let Some(limit) = texture_budget() { + // candidates() already deduplicates aliased host copies and excludes + // GPU-written textures, depth and render targets. + let bytes = candidates.iter().fold(0u64, |n, (t, _)| n.saturating_add(t.bytes)); + over.max(texture_excess(bytes, Some(limit))) + } else { + over + }; + evict_among(&candidates, &read, frame, now, over) } /// [`evict`] among `candidates`, with `read` (raw pointers) the host @@ -2518,6 +2554,16 @@ mod tests { assert!(choose(&[early], frame, now, 0).is_empty(), "too few frames ago"); } + #[cfg(windows)] + #[test] + fn texture_budget_uses_headroom_without_changing_default() { + assert_eq!(texture_excess(1000, None), 0); + assert_eq!(texture_excess(1000, Some(1000)), 0); + assert_eq!(texture_excess(1001, Some(1000)), 51); + assert_eq!(texture_excess(1200, Some(1000)), 250); + } + + #[cfg(windows)] #[test] fn vram_target_only_lowers_the_dynamic_eviction_threshold() { assert_eq!(eviction_excess(899.0, 1000.0, None), 0); diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 4a502c1abd..f8d7b8a18e 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -112,16 +112,44 @@ static BUFFERS: Mutex = Mutex::new(Buffers { used: Vec::new(), }); -/// Free staging buffers kept at most. +/// Free large staging buffers kept at most in the default configuration. const KEPT_BYTES: u64 = 64 << 20; +/// Split the idle budget between standalone buffers and 32 MiB chunks. +/// The original 64 MiB + 16 chunks policy is the default. +fn staging_limits(mib: Option) -> (u64, usize) { + let Some(mib) = mib.filter(|&v| v <= 2048) else { + return (KEPT_BYTES, CHUNKS_KEPT); + }; + let bytes = mib << 20; + let standalone = (bytes / 4).min(KEPT_BYTES); + let chunks = ((bytes - standalone) / CHUNK) as usize; + // Give the unused tail of the chunk allocation to standalone buffers. + (bytes - chunks as u64 * CHUNK, chunks) +} + +fn staging_budget() -> (u64, usize) { + static LIMITS: std::sync::OnceLock<(u64, usize)> = std::sync::OnceLock::new(); + *LIMITS.get_or_init(|| { + let mib = std::env::var("VICTORY_STAGING_POOL_MB").ok() + .and_then(|v| v.trim().parse::().ok()); + let limits = staging_limits(mib); + if mib.is_some_and(|v| v <= 2048) { + tracing::info!(standalone_mib = limits.0 >> 20, chunks = limits.1, + "texture staging idle budget enabled"); + } + limits + }) +} + /// Called before a submission's uploads: the buffers of the last one are /// free again (the oldest dropped past `KEPT_BYTES`). pub(crate) fn recycle() { + let (kept_bytes, chunks_kept) = staging_budget(); { let mut c = CHUNKS.lock().unwrap_or_else(|e| e.into_inner()); (c.current, c.used) = (0, 0); - c.all.truncate(CHUNKS_KEPT); + c.all.truncate(chunks_kept); } let mut b = BUFFERS.lock().unwrap_or_else(|e| e.into_inner()); let b = &mut *b; @@ -129,7 +157,7 @@ pub(crate) fn recycle() { b.free_bytes += e.0; b.free.push_back(e); } - while b.free_bytes > KEPT_BYTES + while b.free_bytes > kept_bytes && let Some((n, ..)) = b.free.pop_front() { b.free_bytes -= n; @@ -854,6 +882,18 @@ mod tests { assert_eq!(subresource_len(0, 256, 4, 1, 2048), 1024); } + #[test] + fn staging_idle_budget_is_bounded() { + assert_eq!(staging_limits(None), (64 << 20, 16)); + assert_eq!(staging_limits(Some(576)), staging_limits(None)); + assert_eq!(staging_limits(Some(0)), (0, 0)); + assert_eq!(staging_limits(Some(2049)), staging_limits(None)); + for mib in [1, 32, 64, 128, 256, 512, 1024, 2048] { + let (standalone, chunks) = staging_limits(Some(mib)); + assert_eq!(standalone + chunks as u64 * CHUNK, mib << 20); + } + } + #[test] fn committed_rejects_overflow_and_noaccess_pages() { use windows_sys::Win32::System::Memory::{ diff --git a/presets/12gb.env b/presets/12gb.env index d84f8f5531..20119a6743 100644 --- a/presets/12gb.env +++ b/presets/12gb.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=1024 VICTORY_SCRATCH_SPARE_MB=128 VICTORY_BUFFERED_TITLE_READS=0 VICTORY_VRAM_TARGET_MB=7680 +VICTORY_TEXTURE_BUDGET_MB=6144 +VICTORY_STAGING_POOL_MB=256 diff --git a/presets/16gb.env b/presets/16gb.env index eb424b4d4d..2b50ef3541 100644 --- a/presets/16gb.env +++ b/presets/16gb.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=1024 VICTORY_SCRATCH_SPARE_MB=256 VICTORY_BUFFERED_TITLE_READS=0 VICTORY_VRAM_TARGET_MB=10240 +VICTORY_TEXTURE_BUDGET_MB=8192 +VICTORY_STAGING_POOL_MB=576 diff --git a/presets/8gb.env b/presets/8gb.env index 2b43a74c85..b168a0a01b 100644 --- a/presets/8gb.env +++ b/presets/8gb.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=1024 VICTORY_SCRATCH_SPARE_MB=0 VICTORY_BUFFERED_TITLE_READS=0 VICTORY_VRAM_TARGET_MB=5120 +VICTORY_TEXTURE_BUDGET_MB=4096 +VICTORY_STAGING_POOL_MB=128 diff --git a/presets/default.env b/presets/default.env index 4dda06f4cc..53c6819606 100644 --- a/presets/default.env +++ b/presets/default.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=4096 VICTORY_SCRATCH_SPARE_MB=0 VICTORY_BUFFERED_TITLE_READS=0 VICTORY_VRAM_TARGET_MB=0 +VICTORY_TEXTURE_BUDGET_MB=0 +VICTORY_STAGING_POOL_MB=576 diff --git a/presets/high.env b/presets/high.env index 69bf5722f5..b64a621b02 100644 --- a/presets/high.env +++ b/presets/high.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=1024 VICTORY_SCRATCH_SPARE_MB=256 VICTORY_BUFFERED_TITLE_READS=1 VICTORY_VRAM_TARGET_MB=0 +VICTORY_TEXTURE_BUDGET_MB=0 +VICTORY_STAGING_POOL_MB=576 diff --git a/presets/low.env b/presets/low.env index edf754704e..95270dc55d 100644 --- a/presets/low.env +++ b/presets/low.env @@ -3,3 +3,5 @@ VICTORY_WINDOW_KB=1024 VICTORY_SCRATCH_SPARE_MB=0 VICTORY_BUFFERED_TITLE_READS=0 VICTORY_VRAM_TARGET_MB=0 +VICTORY_TEXTURE_BUDGET_MB=0 +VICTORY_STAGING_POOL_MB=128 diff --git a/scripts/launch.ps1 b/scripts/launch.ps1 index 113bd26f7a..649f0902af 100644 --- a/scripts/launch.ps1 +++ b/scripts/launch.ps1 @@ -18,7 +18,9 @@ $names = @( 'VICTORY_WINDOW_KB', 'VICTORY_SCRATCH_SPARE_MB', 'VICTORY_BUFFERED_TITLE_READS', - 'VICTORY_VRAM_TARGET_MB' + 'VICTORY_VRAM_TARGET_MB', + 'VICTORY_TEXTURE_BUDGET_MB', + 'VICTORY_STAGING_POOL_MB' ) function Read-PerformanceEnv([string]$Path) { @@ -59,6 +61,20 @@ function Read-PerformanceEnv([string]$Path) { } $value = $mb.ToString() } + 'VICTORY_TEXTURE_BUDGET_MB' { + $mb = 0 + if (-not [int]::TryParse($value, [ref]$mb) -or ($mb -ne 0 -and ($mb -lt 256 -or $mb -gt 65536))) { + throw "$name must be 0 (automatic) or between 256 and 65536" + } + $value = $mb.ToString() + } + 'VICTORY_STAGING_POOL_MB' { + $mb = 0 + if (-not [int]::TryParse($value, [ref]$mb) -or $mb -gt 2048) { + throw "$name must be between 0 and 2048" + } + $value = $mb.ToString() + } 'VICTORY_BUFFERED_TITLE_READS' { if (@('0','1') -cnotcontains $value) { throw "$name must be 0 or 1"