Texture uploads wait for no mapping, ask no guards and hash cheaply; the sweep looks at a part; bakes pass once for what is new

A bake's uploads waited for the lock the runtime holds while it remaps, and a staging buffer a draw kept waited at every later
refresh (29 to 239 ms a frame, all threads, run n04): gone, a read through the kernel gets every page. A staging buffer made for
one upload asks neither the write watch nor the guards (VICTORY_TEXTURE_ONCE=0: as before). blake3 makes way for a 128-bit digest
(0.035 against 0.2 ms a megabyte). While textures are kept for minutes the sweep looks at 8,192 a time (VICTORY_TEXTURE_SWEEP=whole).
The pass before a bake is made again only for something new.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 05:14:38 -06:00
1 parent 917e1a7103
commit 2dfba4dbd8
5 files changed
+215 -38

No files matched your search

+135 -28
View File
@@ -57,9 +57,9 @@ pub(crate) struct Staging {
/// Which host copy it uploads into, and what that is told to hold.
serial: u64,
holds: Arc<crate::resource::Holds>,
/// Title memory being remapped is waited for whatever [`no_wait`] says:
/// a list that bakes from the texture now recorded the upload.
waits: std::sync::atomic::AtomicBool,
/// Made by the pass over textures picked by index, for one upload
/// ([`once`]): read when the list is submitted, and never looked at again.
once: std::sync::atomic::AtomicBool,
/// The staging buffer's bytes (counted in `STAGED` while it lives).
bytes: u64,
memory: *const u8,
@@ -95,7 +95,7 @@ pub(crate) struct Staging {
/// (memory not watched), or the write-watch asking it was detiled after.
#[derive(Default)]
struct Last {
hash: Option<blake3::Hash>,
hash: Option<Digest>,
asked: u64,
/// The frame a partly mapped texture was last read in.
partial: Option<u64>,
@@ -648,11 +648,76 @@ pub(crate) fn upload(list: &HostList, resource: This, _texture: &ID3D12Resource)
}
thread_local! {
/// The thread records uploads for a draw that bakes from them
/// ([`Staging::waits`]).
/// The thread records uploads for a draw that bakes from them.
pub(crate) static BAKING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
/// The thread records the upload of a texture picked by index, of which
/// that many bytes were found mapped just now (`u64::MAX`: not asked).
pub(crate) static PICKING: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
}
/// Whether a staging buffer made for one upload of a texture picked by index
/// is filled without asking the write watch and the guards
/// (`VICTORY_TEXTURE_ONCE=0`: with, as before). What they say is for the next
/// refresh, and there is none: the buffer goes with the upload. Asking guards
/// the texture's blocks (a change of protection, under the lock every other
/// refresh wants), and the title's next write there faults to open them.
fn once() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_ONCE").is_none_or(|v| v != "0"))
}
/// A 128-bit digest of `bytes`, to tell a texture's memory changed: four
/// lanes of folded multiplications over 64 bytes at a time. (Not a
/// cryptographic one, which took 0.27 ms a megabyte, four times the detiling
/// it was to spare; nobody makes up textures to collide.)
pub(crate) fn digest(bytes: &[u8]) -> Digest {
const K: [u64; 8] = [
0xa076_1d64_78bd_642f,
0xe703_7ed1_a0b4_28db,
0x8ebc_6af0_9c88_c6e3,
0x5899_65cc_7537_4cc3,
0x1d8e_4e27_c47d_124f,
0xeb44_acca_b455_d165,
0x9e37_79b9_7f4a_7c15,
0xc2b2_ae3d_27d4_eb4f,
];
let fold = |a: u64, b: u64| {
let p = u128::from(a) * u128::from(b);
p as u64 ^ (p >> 64) as u64
};
let word = |b: &[u8]| {
let mut w = [0; 8];
w[..b.len()].copy_from_slice(b);
u64::from_le_bytes(w)
};
let mut lanes = [K[0], K[1], K[2], K[3]];
let mut step = |block: &[u8; 64]| {
for (i, lane) in lanes.iter_mut().enumerate() {
let a = word(&block[16 * i..16 * i + 8]);
let b = word(&block[16 * i + 8..16 * i + 16]);
// (What went in is added again: a product with a factor of zero
// forgets neither the lane nor the block.)
*lane = fold(a ^ K[4 + i], b ^ *lane).wrapping_add(a.rotate_left(23) ^ lane.rotate_left(41)) ^ b;
}
};
let mut blocks = bytes.chunks_exact(64);
for block in &mut blocks {
step(block.try_into().unwrap_or(&[0; 64]));
}
let rest = blocks.remainder();
let mut last = [0u8; 64];
last[..rest.len()].copy_from_slice(rest);
step(&last);
let len = bytes.len() as u64;
let low = fold(lanes[0] ^ K[1], lanes[1] ^ len) ^ fold(lanes[2] ^ K[3], lanes[3] ^ K[5]);
let high = fold(lanes[0].rotate_left(32) ^ K[6], lanes[2] ^ len) ^ fold(lanes[1].rotate_left(32) ^ K[7], lanes[3] ^ K[2]);
Digest(u128::from(low) << 64 | u128::from(high))
}
/// See [`digest`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Digest(u128);
/// [`upload`] of the levels from `first` on, the ones a view shows: of a
/// texture the title placed there may be no memory of the larger ones, and
/// what lies there is another texture's. (After victory-amd 4436f8f1.)
@@ -684,8 +749,10 @@ pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
return;
};
use std::sync::atomic::Ordering::Relaxed;
if BAKING.get() {
staging.waits.store(true, Relaxed);
let picking = PICKING.get();
if picking.is_none() {
// A draw samples it through a table: kept and checked from here on.
staging.once.store(false, Relaxed);
}
staging.used.store(crate::host::moment(), Relaxed);
// Levels on demand: a view of the texture now starts at another
@@ -727,7 +794,10 @@ pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::TexUploads, 1);
if no_wait() && fill_unlocked() {
let there = mapped_bytes(staging.memory, staging.read);
let there = match picking {
Some(there) if there != u64::MAX => there,
_ => mapped_bytes(staging.memory, staging.read),
};
staging.expected.store(there, Relaxed);
}
const COPY_DEST: u32 = 0x400;
@@ -1064,7 +1134,11 @@ impl Staging {
let start = self.memory as usize;
let mut last = waited(0, || self.last.lock().unwrap_or_else(|e| e.into_inner()));
let last = &mut *last;
if let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, false)) {
let alone = self.once.load(std::sync::atomic::Ordering::Relaxed);
if alone && last.hash.is_some() {
return 0;
}
if !alone && let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, false)) {
if last.asked != 0 && written.iter().all(|&(_, w)| w <= last.asked) {
return 0;
}
@@ -1092,7 +1166,8 @@ impl Staging {
static GUARD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
// The guard saw no write: a change the hash finds came another way.
let mut blind = false;
if *GUARD.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_GUARD").is_none_or(|v| v != "0"))
if !alone
&& *GUARD.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_GUARD").is_none_or(|v| v != "0"))
&& let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, true))
{
const HASH_EVERY: u64 = 8;
@@ -1127,7 +1202,7 @@ impl Staging {
// stays mapped (see `crate::watch::title_memory_reading`; without
// it a page went between the asking and the read in runs 71 and
// 74).
let _mapped = if no_wait() && !self.waits.load(std::sync::atomic::Ordering::Relaxed) {
let _mapped = if no_wait() {
// Not waited for: while the runtime remaps title memory the
// texture is read through the kernel instead, whole or not at
// all (`no_wait`).
@@ -1155,7 +1230,7 @@ impl Staging {
crate::copystat::add(crate::copystat::C::TexHashed, size as u64);
let changed = with_read(size, |read| {
crate::host::copy_mapped(read.as_mut_ptr(), self.memory, size, false);
let hash = blake3::hash(read);
let hash = digest(read);
if last.hash == Some(hash) {
return false;
}
@@ -1177,7 +1252,7 @@ impl Staging {
}
if through_copy() {
return with_title(self.memory, size, |tiled| {
let hash = blake3::hash(tiled);
let hash = digest(tiled);
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::TexHashed, size as u64);
if last.hash == Some(hash) {
@@ -1194,7 +1269,7 @@ impl Staging {
// SAFETY: committed memory of the texture's layout size, and it
// stays committed while `_mapped` is held.
let tiled = unsafe { std::slice::from_raw_parts(self.memory, size) };
let hash = blake3::hash(tiled);
let hash = digest(tiled);
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::TexHashed, size as u64);
if last.hash == Some(hash) {
@@ -1221,7 +1296,7 @@ impl Staging {
if !crate::host::read_into(self.memory, read) {
return false;
}
let hash = blake3::hash(read);
let hash = digest(read);
if last.hash != Some(hash) {
last.hash = Some(hash);
self.detile(read);
@@ -1256,14 +1331,14 @@ impl Staging {
let mut got = 0;
for _ in 0..4 {
got = crate::host::read_there(self.memory, read) as u64;
crate::watch::change_over();
there = mapped_bytes(self.memory, size);
if got >= there {
if got >= there.max(self.expected.load(Relaxed)) {
break;
}
crate::watch::change_over();
}
if got != 0 {
last.hash = Some(blake3::hash(read));
last.hash = Some(digest(read));
self.detile(read);
}
got
@@ -1361,14 +1436,14 @@ pub(crate) fn fingerprint(at: u64, len: u64) -> u64 {
static READ: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) };
}
let len = len as usize;
let mut hasher = blake3::Hasher::new();
let mut print = 0u128;
READ.with(|read| {
let mut read = read.borrow_mut();
read.resize(3 * WINDOW, 0);
let mut take = |from: usize, bytes: usize| {
read[..bytes].fill(0);
crate::host::copy_mapped(read.as_mut_ptr(), (at as usize + from) as *const u8, bytes, false);
hasher.update(&read[..bytes]);
print = print.rotate_left(37) ^ digest(&read[..bytes]).0;
};
if len <= 3 * WINDOW {
take(0, len);
@@ -1378,9 +1453,7 @@ pub(crate) fn fingerprint(at: u64, len: u64) -> u64 {
take(len - WINDOW, WINDOW);
}
});
let hash = hasher.finalize();
let mut first = [0u8; 8];
first.copy_from_slice(&hash.as_bytes()[..8]);
let first = (print as u64 ^ (print >> 64) as u64).to_le_bytes();
u64::from_le_bytes(first) | 1
}
@@ -1436,7 +1509,7 @@ pub(crate) fn unchanged(resource: This, first: u32) -> bool {
if crate::host::read_there(memory, read) == 0 {
return false;
}
blake3::hash(read) == held
digest(read) == held
})
}
@@ -1456,12 +1529,13 @@ pub(crate) fn is_staged(resource: This) -> bool {
/// The bytes of a title texture's memory, and whether all of it is mapped,
/// if the texture has been staged for upload.
pub(crate) fn staged(resource: This) -> Option<(u64, bool)> {
/// (Whether it is mapped is asked only if `ask`; else it counts as mapped.)
pub(crate) fn staged(resource: This, ask: bool) -> Option<(u64, bool)> {
let s = {
let map = STAGING.lock().unwrap_or_else(|e| e.into_inner());
map.as_ref()?.get(&(resource as usize))?.clone()
};
Some((s.read as u64, committed(s.memory, s.read)))
Some((s.read as u64, !ask || committed(s.memory, s.read)))
}
/// Whether `[at, at + len)` is committed memory (streamed textures may be
@@ -1713,7 +1787,7 @@ fn staging(resource: This, copy: &crate::resource::HostTexture, first: u32) -> O
read,
serial: copy.serial,
holds: copy.holds.clone(),
waits: std::sync::atomic::AtomicBool::new(false),
once: std::sync::atomic::AtomicBool::new(once() && !on_demand && PICKING.get().is_some()),
bytes: total,
memory,
last: Mutex::default(),
@@ -1932,6 +2006,36 @@ mod allocation_costs {
}
}
/// The digest tells every length and every changed byte apart, zeros
/// among them.
#[test]
fn a_digest_tells_bytes_apart() {
use super::digest;
let zeros = [0u8; 400];
let mut seen: Vec<super::Digest> = (0..400).map(|n| digest(&zeros[..n])).collect();
for at in 0..400 {
for value in [1u8, 0x80, 0xff] {
let mut bytes = zeros;
bytes[at] = value;
seen.push(digest(&bytes));
}
}
let text: Vec<u8> = (0..400u32).map(|i| (i * 37 >> 2) as u8).collect();
for at in 0..399 {
let mut bytes = text.clone();
bytes.swap(at, at + 1);
if bytes != text {
seen.push(digest(&bytes));
}
}
seen.push(digest(&text));
let n = seen.len();
seen.sort_unstable_by_key(|d| d.0);
seen.dedup();
assert_eq!(seen.len(), n);
assert_eq!(digest(&text), digest(&text.clone()));
}
/// What a texture met for the first time costs, by step, in
/// milliseconds a megabyte on one thread: for whether a store of
/// detiled copies on disk could be worth it (its key is a hash of the
@@ -1965,6 +2069,9 @@ mod allocation_costs {
timed("blake3", &mut || {
std::hint::black_box(blake3::hash(&title));
});
timed("digest", &mut || {
std::hint::black_box(super::digest(&title));
});
timed("detiled into upload memory", &mut || {
let mut at = 0;
for (mip, m) in layout.mips.iter().enumerate() {