Packed textures stay in the upload queue; a texture on the card is refreshed when the card is full

Two fixes for textures that shaders pick by index, both proven with a
slot listing before they were made:

- The title keeps only a texture's small levels and places the next
  texture in the address range of the absent large ones. Described at full
  size every neighbour overlapped, and each view written took the textures
  beside it out of the upload queue and made their host slots null (tens
  of thousands of slots a run). A newer view now makes an older one stale
  only when it starts within that one's first 4 KiB.
  VICTORY_STALE_TEXTURES=overlap: the old rule; =0: never. Unit tests on
  the span table alone. The first room is lit with its walls and the
  characters' textures in the first launch with it.
- With the card at its limit, a texture that already has its host copy is
  uploaded again when the title changed its memory (it took no new card
  memory, and was refused all the same).
  VICTORY_REUPLOAD_WHEN_FULL=0: as before.

Logging only, off unless asked for: both video memory groups and budget
moves; card accounting (VICTORY_CARD_ACCOUNTING=1); the slots a listed
draw's shaders read, with their state (VICTORY_LIST_SLOTS=); a trace for
the missing first-room walls (VICTORY_WALLS_TRACE=1 or 2). A card test of
what a bare command costs (ignored by default):
cargo test -p victory-gpu --release -- --ignored bare_command_cost --nocapture

Known: about 3 to 5 frames a second; more than half of the texture memory
on the card is held for levels the title has not loaded yet (next).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
fxcelessandClaude Fable 5.2 committed 2026-11-24 22:19:10 -05:00
1 parent c96b4f2b83
commit 310c6eb6c1
9 files changed
+1807 -32

No files matched your search

+94
View File
@@ -2403,6 +2403,7 @@ impl Shadow {
if let Some((now, written)) = crate::watch::writes(from, to, true) {
self.watched.store(1, Ordering::Relaxed);
let copied = self.copy_stale(from, to, now, &written);
self.refresh_check(from, to);
#[cfg(feature = "diagnostics")]
{
use crate::copystat::{C, add};
@@ -2423,6 +2424,63 @@ impl Shadow {
to - from
}
/// Diagnostic (`VICTORY_REFRESH_CHECK=1`; off by default): after a
/// refresh of `[from, to)` that trusted the write watch or the guards,
/// the first 256 bytes of every 64 KiB block of it are read from title
/// memory and compared with the copy. A block that differs on three
/// refreshes in a row was changed with nothing to tell of it (a write
/// through another view of its pages, as DirectStorage's page
/// destinations make) and the card reads old bytes there: said with
/// the copy and the address. One that differs once is a write of the
/// title's that arrived between the refresh and the look.
fn refresh_check(&self, from: usize, to: usize) {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_REFRESH_CHECK").is_some_and(|v| v == "1") || crate::host::walls_trace()) {
return;
}
const SAMPLE: usize = 256;
// Blocks found differing: how many refreshes in a row.
static DIFFERING: Mutex<Option<std::collections::HashMap<usize, u32>>> = Mutex::new(None);
static SAID: AtomicU64 = AtomicU64::new(0);
let start = self.start as usize;
let mut title = [0u8; SAMPLE];
let mut at = from;
while at < to {
let n = SAMPLE.min(to - at);
if crate::host::read_into(at as *const u8, &mut title[..n]) {
// SAFETY: `[from, to)` lies inside the buffer (`update`),
// so these bytes are inside the copy's mapping.
let copy = unsafe { std::slice::from_raw_parts(self.data.add(at - start).cast_const(), n) };
let mut map = DIFFERING.lock().unwrap_or_else(|e| e.into_inner());
let map = map.get_or_insert_with(Default::default);
if copy == &title[..n] {
map.remove(&at);
} else {
let times = map.entry(at).or_insert(0);
*times += 1;
if *times == 3 {
let said = SAID.fetch_add(1, Ordering::Relaxed) + 1;
if said <= 64 || said.is_power_of_two() {
tracing::warn!(
said,
copy = self.id,
copy_start = format_args!("{start:#x}"),
copy_bytes = self.len,
address = format_args!("{at:#x}"),
refreshed = format_args!("{from:#x}..{to:#x}"),
on_the_card = self.card.is_some(),
window = self.window,
"a host copy of title memory is stale after its refresh, three times in a row: the memory changed with nothing to tell of it (VICTORY_REFRESH_CHECK=1)"
);
}
}
}
}
// The first bytes of the next 64 KiB block.
at = (at / PAGE + 1) * PAGE;
}
}
/// Copies the pages of `[from, to)` written after their last copy
/// (`written`: last writes by page, in page order; `now`: this
/// asking), whole pages within the buffer.
@@ -2542,6 +2600,42 @@ impl Shadow {
card_piece_of(self.start as usize, self.len as usize, span, i)
}
/// The walls trace (`crate::host::walls_trace`): a piece of `fetched`
/// bytes of this copy came back from the card and `written` of them
/// went into title memory. Summed per buffer and logged once a frame:
/// which shader-written buffers the title's CPU could see fresh in
/// that frame, and how much of each changed.
pub(crate) fn walls_merged(&self, fetched: usize, written: usize) {
if !crate::host::walls_trace() {
return;
}
static SEEN: Mutex<(u64, BTreeMap<u64, (u64, u32, u64, u64)>)> = Mutex::new((0, BTreeMap::new()));
let frame = crate::frame::current();
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
if seen.0 != frame {
let buffers: Vec<String> = seen
.1
.iter()
.map(|(at, v)| format!("{at:#x}+{:#x}: {} pieces, {} fetched, {} written", v.0, v.1, v.2, v.3))
.take(32)
.collect();
if !buffers.is_empty() {
tracing::info!(
frame = seen.0,
count = seen.1.len(),
buffers = ?buffers,
"walls trace: shader-written card buffers merged into title memory in this frame"
);
}
seen.1.clear();
seen.0 = frame;
}
let entry = seen.1.entry(self.start).or_insert((self.len, 0, 0, 0));
entry.1 += 1;
entry.2 += fetched as u64;
entry.3 += written as u64;
}
/// After a submission, for a copy in the card's memory: `gpu` is what
/// the card's side holds at offset `at` on, in a piece the compare
/// found differing. The bytes shaders wrote (those that differ from the