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

+101 -17
View File
@@ -300,14 +300,18 @@ unsafe impl Send for Idle {}
#[cfg(windows)]
static IDLE: std::sync::Mutex<(Vec<Idle>, u64)> = std::sync::Mutex::new((Vec::new(), 0));
/// The most host lists kept idle (128 were too few: flights land several at
/// a time, so lists were thrown away and others made in the same frame, with
/// their preludes and buffers, 12 % of the submitting thread in the opening,
/// profile cpu-1), and the most upload memory they may keep
/// between them (a list keeps scratch chunks over a reset only while lists
/// do not share them, `VICTORY_SCRATCH_POOL=0`).
/// The most host lists kept idle, and the most upload memory they may keep
/// between them. On hosts with <= 32 GiB RAM, cap at 96 idle lists / 64 MiB
/// instead of 512 / 256 MiB so command-allocator pools cannot push a 16 GB
/// host into `E_OUTOFMEMORY` during long open-world sessions.
#[cfg(windows)]
const IDLE_MOST: (usize, u64) = (512, 256 << 20);
fn idle_most() -> (usize, u64) {
if crate::residency::limited_host_ram() {
(96, 64 << 20)
} else {
(512, 256 << 20)
}
}
// Idle-list trim: memory stack by skornuta (skornuta/victory-memory-stack 57fb136, GPL-3.0),
// patch 13 (and the idle trim of its base).
@@ -382,7 +386,8 @@ pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
if !ran.contains(&i) {
continue;
}
if IDLE.lock().unwrap_or_else(|e| e.into_inner()).0.len() >= IDLE_MOST.0 {
let (max_lists, max_bytes) = idle_most();
if IDLE.lock().unwrap_or_else(|e| e.into_inner()).0.len() >= max_lists {
// Its submission has run: its prelude serves another list.
host.give_prelude();
continue;
@@ -392,7 +397,7 @@ pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
host.reset();
let bytes = host.scratch_kept();
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
if idle.0.len() < IDLE_MOST.0 && idle.1 + bytes <= IDLE_MOST.1 {
if idle.0.len() < max_lists && idle.1 + bytes <= max_bytes {
let now = crate::host::now_ms();
idle.0.push(Idle(host, bytes, now));
idle.1 += bytes;
@@ -412,6 +417,24 @@ pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
}
}
/// Drops excess idle command lists/allocators during critical host commit
/// pressure, keeping a small working set (64 lists).
#[cfg(windows)]
pub(crate) fn trim_idle() {
const PRESSURED_KEEP: usize = 64;
let freed: Vec<Idle> = {
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
if idle.0.len() <= PRESSURED_KEEP {
return;
}
let dropped = idle.0.split_off(PRESSURED_KEEP);
let dropped_bytes: u64 = dropped.iter().map(|Idle(_, b, _)| *b).sum();
idle.1 = idle.1.saturating_sub(dropped_bytes);
dropped
};
drop(freed);
}
/// A wait for a memory value recorded into list `this`, with waits cut
/// into lists (`crate::queue::cuts`). On the console the queue runs the
/// list up to the wait, stands there until the value is in memory, and
@@ -1007,7 +1030,14 @@ extern "system" fn copy_buffer_region(
/// other is not ([`crate::resource::target_format`]): their texels differ
/// in size, so the host cannot copy between them (logged once).
#[cfg(windows)]
fn unlike(dst: This, src: This) -> bool {
fn unlike(dst: This, src: This, mapped: bool) -> bool {
if !mapped && (crate::resource::dropped(src) > 0 || crate::resource::dropped(dst) > 0) {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
tracing::warn!(?dst, ?src, "copy with a texture whose host copy leaves out levels skipped")
});
return true;
}
let unlike = crate::resource::stands_in(dst) != crate::resource::stands_in(src);
if unlike {
static ONCE: std::sync::Once = std::sync::Once::new();
@@ -1033,7 +1063,8 @@ extern "system" fn copy_resource(this: This, dst: This, src: This) {
crate::resource::host_resource(dst),
crate::resource::host_resource(src),
) {
if unlike(dst, src) {
let levelwise = crate::resource::dropped(dst) != crate::resource::dropped(src);
if unlike(dst, src, true) {
return;
}
// Two title textures over the same memory have one host
@@ -1075,8 +1106,12 @@ extern "system" fn copy_resource(this: This, dst: This, src: This) {
ensure_state(list, src, COPY_SOURCE);
#[cfg(feature = "diagnostics")]
list.note_op(|| format!("copy resource {src:?} to {dst:?}"));
// SAFETY: an open list and live textures.
unsafe { list.list.CopyResource(to, from) };
if levelwise {
copy_levels(list, dst, to, src, from);
} else {
// SAFETY: an open list and live textures.
unsafe { list.list.CopyResource(to, from) };
}
} else if let (Some((to, a)), Some((from, b))) = (
crate::resource::buffer_bytes(dst),
crate::resource::buffer_bytes(src),
@@ -1088,6 +1123,41 @@ extern "system" fn copy_resource(this: This, dst: This, src: This) {
let _ = (this, dst, src);
}
/// A whole-texture copy between host copies that leave out different
/// numbers of levels (`crate::resource::drop_levels`): each title level both
/// host copies have, at its own index in each.
#[cfg(windows)]
fn copy_levels(
list: &crate::host::HostList,
dst: This,
to: &windows::Win32::Graphics::Direct3D12::ID3D12Resource,
src: This,
from: &windows::Win32::Graphics::Direct3D12::ID3D12Resource,
) {
use windows::Win32::Graphics::Direct3D12::{
D3D12_TEXTURE_COPY_LOCATION, D3D12_TEXTURE_COPY_LOCATION_0,
D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, ID3D12Resource,
};
let location = |texture: &ID3D12Resource, index: u32| D3D12_TEXTURE_COPY_LOCATION {
// SAFETY: a borrowed pointer the location never releases.
pResource: unsafe { std::mem::transmute_copy(texture) },
Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: index },
};
let count = crate::resource::title_subresources(dst).min(crate::resource::title_subresources(src));
for index in 0..count {
let (Some(d), Some(s)) = (
crate::resource::host_subresource(dst, index),
crate::resource::host_subresource(src, index),
) else {
continue;
};
// SAFETY: an open list and live textures; both subresources exist
// and have the title level's size.
unsafe { list.list.CopyTextureRegion(&location(to, d), 0, 0, 0, &location(from, s), None) };
}
}
extern "system" fn copy_resource_x(this: This, dst: This, src: This, _flags: u32) {
copy_resource(this, dst, src);
}
@@ -1161,9 +1231,16 @@ extern "system" fn copy_texture_region(
}
return;
};
if unlike(dst.resource, src.resource) {
if unlike(dst.resource, src.resource, true) {
return;
}
let mut dst = dst;
if dst.kind != FOOTPRINT && crate::resource::dropped(dst.resource) > 0 {
match crate::resource::host_subresource(dst.resource, dst.footprint.offset as u32) {
Some(index) => dst.footprint.offset = u64::from(index),
None => return,
}
}
let mut from = src;
// Both sides are the same subresource of one host texture (set below).
let mut same = false;
@@ -1207,10 +1284,15 @@ extern "system" fn copy_texture_region(
// title textures over one memory): not a copy the host allows.
// The states and the copy are left out below; the rest is
// noted as before.
if crate::resource::dropped(src.resource) > 0 {
match crate::resource::host_subresource(src.resource, src.footprint.offset as u32) {
Some(index) => from.footprint.offset = u64::from(index),
None => return,
}
}
same = texture.as_raw() == to.as_raw()
&& src.kind != FOOTPRINT
&& dst.kind != FOOTPRINT
&& src.footprint.offset as u32 == dst.footprint.offset as u32;
&& from.footprint.offset == dst.footprint.offset;
list.twin_use(src.resource, false, false);
if crate::texture::title_written(texture) {
crate::texture::upload(list, src.resource, texture);
@@ -1342,6 +1424,7 @@ fn read_back(
// The title's buffer holds every row at its own pitch.
let bytes = pitch * (rows - 1) + row;
let data = buffer_range(dst.resource, dst.footprint.offset, bytes).ok_or("a destination outside its buffer")?;
let index = crate::resource::readback_subresource(src.resource, src.footprint.offset as u32)?;
let texture = crate::resource::host_resource(src.resource).ok_or("no host texture")?;
let buffer = host.read_back_buffer(size).ok_or("no read-back buffer")?;
// As a texture source of any copy: what shaders wrote to a depth twin
@@ -1353,6 +1436,7 @@ fn read_back(
ensure_state(list, src.resource, COPY_SOURCE);
let mut from = src;
from.resource = texture.as_raw().cast_const().cast();
from.footprint.offset = u64::from(index);
let mut to = dst;
to.resource = buffer.as_raw().cast_const().cast();
to.footprint = footprint;
@@ -1412,7 +1496,7 @@ extern "system" fn resolve(
use windows::core::Interface;
const RESOLVE_DEST: u32 = 0x1000;
const RESOLVE_SOURCE: u32 = 0x2000;
if unlike(dst, src) {
if unlike(dst, src, false) {
return;
}
// Depth textures shaders also write: what they wrote, first.