Skip to content
Draft
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
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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:
Expand All @@ -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.

Expand Down
54 changes: 50 additions & 4 deletions crates/victory-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,12 +1602,39 @@ fn vram_target() -> Option<u64> {
/// Keep the existing hysteresis ratio and eviction eligibility.
#[cfg(windows)]
fn eviction_excess(used: f64, budget: f64, target_mib: Option<u64>) -> 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<u64> {
static LIMIT: std::sync::OnceLock<Option<u64>> = std::sync::OnceLock::new();
*LIMIT.get_or_init(|| {
let limit = std::env::var("VICTORY_TEXTURE_BUDGET_MB").ok()
.and_then(|v| v.trim().parse::<u64>().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>) -> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 43 additions & 3 deletions crates/victory-gpu/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,52 @@ static BUFFERS: Mutex<Buffers> = 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>) -> (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::<u64>().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;
for e in b.used.drain(..) {
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;
Expand Down Expand Up @@ -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::{
Expand Down
2 changes: 2 additions & 0 deletions presets/12gb.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions presets/16gb.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions presets/8gb.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions presets/default.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions presets/high.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions presets/low.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion scripts/launch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down