gpu: add 6GB VRAM mip dropping, 16GB RAM pool caps/OOM recovery, and clear_air +inf clamp

This commit is contained in:
theme613 committed 2026-11-28 13:19:56 +08:00
1 parent 63b902fe17
commit c9067a7d3f
8 files changed
+366 -51

No files matched your search

+138 -10
View File
@@ -1351,12 +1351,128 @@ pub(crate) fn read_from(layout: &victory_formats::texture::Layout, first: u32) -
if layout.bytes_from(first) < layout.size { first } else { 0 }
}
/// Configured `VICTORY_TEXTURE_DROP_MIPS` count (0..=4; 0 leaves out none).
#[cfg(windows)]
pub(crate) fn drop_mips_configured() -> u32 {
static N: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
*N.get_or_init(|| {
std::env::var("VICTORY_TEXTURE_DROP_MIPS")
.ok()
.and_then(|v| v.trim().parse::<u32>().ok())
.unwrap_or(0)
.min(4)
})
}
/// How many of its largest levels the host copy of a texture described by
/// `desc` leaves out (`VICTORY_TEXTURE_DROP_MIPS=N`, at most 4; 0, the
/// default, leaves out none). Each level left out divides the texture's
/// bytes by about four, keeping 6 GB / 4 GB GPUs within VRAM budget with
/// frame generation active. Only 2D single-slice textures the title fills
/// ([`fills`]), at least 512 texels on their shorter side, whose sides
/// halve cleanly into whole 4-texel blocks.
///
/// `VICTORY_TEXTURE_KEEP_SIDE=<texels>`: no texture is taken below that
/// many texels on its longer side ([`levels_above`]).
#[cfg(windows)]
pub(crate) fn drop_levels(desc: &ResourceDesc) -> u32 {
const TEXTURE_2D: u32 = 3;
let n = drop_mips_configured();
if n == 0
|| !fills(desc)
|| desc.dimension != TEXTURE_2D
|| desc.depth_or_array_size != 1
|| desc.sample_count > 1
|| desc.mip_levels < 5
{
return 0;
}
static KEEP: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
let keep = *KEEP.get_or_init(|| {
std::env::var("VICTORY_TEXTURE_KEEP_SIDE")
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
.unwrap_or(0)
});
let (w, h) = (desc.width, u64::from(desc.height));
let n = levels_above(n.min(u32::from(desc.mip_levels).saturating_sub(1)), w.max(h), keep);
let whole = |side: u64| side % (4 << n) == 0;
if n == 0 || w.min(h) < 512 || !whole(w) || !whole(h) {
return 0;
}
n
}
/// Of `n` levels, how many a texture `longer` texels long can give up and
/// stay at least `keep` texels long (`keep` 0: all `n`).
fn levels_above(n: u32, longer: u64, keep: u64) -> u32 {
match keep {
0 => n,
keep => n.min((longer / keep).checked_ilog2().unwrap_or(0)),
}
}
#[cfg(test)]
#[test]
fn a_kept_side_bounds_the_levels_dropped() {
assert_eq!(levels_above(2, 4096, 0), 2);
assert_eq!(levels_above(2, 4096, 1024), 2);
assert_eq!(levels_above(2, 2048, 1024), 1);
assert_eq!(levels_above(2, 1024, 1024), 0);
assert_eq!(levels_above(2, 512, 1024), 0);
assert_eq!(levels_above(4, 8192, 1024), 3);
assert_eq!(levels_above(1, 3000, 1024), 1);
}
/// Base mip level omitted by the host copy of `this` (0 for buffers and null).
#[cfg(windows)]
pub(crate) fn dropped(this: This) -> u32 {
if this.is_null() {
return 0;
}
made_copy(this).map_or_else(|| drop_levels(&resource(this).desc), |copy| copy.base)
}
/// The host copy's subresource for subresource `index` of title texture
/// `this`, whose host copy leaves out [`dropped`] levels: `None` for a
/// level it leaves out.
#[cfg(windows)]
pub(crate) fn host_subresource(this: This, index: u32) -> Option<u32> {
let drop = dropped(this);
let mips = u32::from(resource(this).desc.mip_levels).max(1);
let (mip, slice) = (index % mips, index / mips);
(mip >= drop).then(|| mip - drop + slice * (mips - drop))
}
/// Subresources in the first plane of title texture `this`, as the title
/// numbers them (levels times slices).
#[cfg(windows)]
pub(crate) fn title_subresources(this: This) -> u32 {
let desc = &resource(this).desc;
u32::from(desc.mip_levels).max(1) * u32::from(desc.depth_or_array_size).max(1)
}
/// Maps a readback before creating a host texture. Ordinary textures keep
/// their native subresource numbering. Shrunken textures support plane zero.
#[cfg(windows)]
pub(crate) fn readback_subresource(this: This, index: u32) -> Result<u32, &'static str> {
if dropped(this) == 0 {
return Ok(index);
}
let desc = &resource(this).desc;
if index >= u32::from(desc.mip_levels) * u32::from(desc.depth_or_array_size) {
return Err("a read back outside the first plane of the host texture");
}
host_subresource(this, index).ok_or("a mip level omitted from the host texture")
}
/// The level a host copy begins at for views that start at `first`: one the
/// host can have as a texture's level 0 ([`read_from`]; block-compressed
/// formats in whole blocks).
/// formats in whole blocks), never below [`drop_levels`].
#[cfg(windows)]
fn base_level(desc: &ResourceDesc, first: u32) -> u32 {
const TEXTURE_2D: u32 = 3;
let first = first.max(drop_levels(desc));
if first == 0
|| !fills(desc)
|| desc.dimension != TEXTURE_2D
@@ -1387,7 +1503,8 @@ fn base_level(desc: &ResourceDesc, first: u32) -> u32 {
/// [`placed_bytes`]. Remembered: asked for every slot that is looked at.
#[cfg(windows)]
pub(crate) fn placed_bytes_from(desc: &[u32; 14], first: u32) -> Option<u64> {
if first == 0 || !held_only() {
let drop_on = drop_mips_configured() > 0;
if !drop_on && (first == 0 || !held_only()) {
return placed_bytes(desc);
}
type Known = std::collections::HashMap<([u32; 14], u32), Option<u64>>;
@@ -1399,11 +1516,16 @@ pub(crate) fn placed_bytes_from(desc: &[u32; 14], first: u32) -> Option<u64> {
let whole = placed_bytes(desc)?;
// SAFETY: 56 bytes in the description's layout.
let d = unsafe { desc.as_ptr().cast::<ResourceDesc>().read_unaligned() };
let floor = drop_levels(&d);
let effective = if held_only() { first.max(floor) } else { floor };
if effective == 0 {
return Some(whole);
}
let Some(t) = texture_layout(&d).filter(|t| t.planes.len() == 1) else {
return Some(whole);
};
let layout = &t.planes[0].layout;
Some(layout.bytes_from(read_from(layout, first)).min(whole))
Some(layout.bytes_from(read_from(layout, effective)).min(whole))
})
})
}
@@ -1752,14 +1874,20 @@ fn hosted(this: This, make: bool) -> Option<Made> {
texture = reserved;
}
}
// The levels views show ([`held_only`]), those of the slots shaders
// pick by index among them.
// The levels views show ([`held_only`]) and any configured mip floor
// (`VICTORY_TEXTURE_DROP_MIPS`), for non-aliased, non-tiled textures.
let mut base = 0;
if shape_fits && r.host.placed.load(Relaxed) && held_only() && !is_aliased(this) {
let wanted = r.host.wanted.load(Relaxed);
let viewed = crate::descriptor::host::first_viewed(r.data() as u64).unwrap_or(u32::MAX);
let first = wanted.min(viewed);
if first != u32::MAX {
if shape_fits && !is_aliased(this) && tiled.is_none() {
let mut first = drop_levels(&r.desc);
if r.host.placed.load(Relaxed) && held_only() {
let wanted = r.host.wanted.load(Relaxed);
let viewed = crate::descriptor::host::first_viewed(r.data() as u64).unwrap_or(u32::MAX);
let held = wanted.min(viewed);
if held != u32::MAX {
first = first.max(held);
}
}
if first > 0 {
base = base_level(&r.desc, first);
}
}