Title textures: detile and hash the title's memory in place, not a copy

Before each upload of a title texture, and before hashing it to see whether
it changed, its memory was copied into a new zeroed buffer of its size
(read_committed). The copy, a fresh allocation and its page faults every
time, cost as much as the detile itself: GTA VI's intro video re-uploads
its 3840x2160 and 1920x1080 R8 planes several times a frame.

title_bytes now hands out the title's memory where it is when the whole
range is readable (gpu::shader::readable), and the page-by-page copy as
before when it is not. The 2D slices, 3D textures and the unchanged-content
hash take it.

Measured on an RX 5700 XT / Ryzen 5 2600 during the video (release, a
local counter around the upload batches, with a row-at-a-time detile like
the one #451 merged): reading and detiling 0.9 -> 0.4 s of every 2 s,
36 -> 54 presents per 2 s.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Deividas Nastajus
2026-11-24 03:29:45 +02:00
parent 9ce2070ecc
commit 3984bf1671
+37 -3
View File
@@ -829,7 +829,7 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) {
// retail title's video planes and UI targets went up again for every submit, ~25 MB
// a presented frame where ~3 MB had changed, and the submits waited here.
if unchanged_skip() {
let h = content_hash(&read_committed(va, tex.size() as usize));
let h = content_hash(&title_bytes(va, tex.size() as usize));
if same_as_last_upload(r.as_raw() as usize, h) {
let n = UNCHANGED.fetch_add(1, Ordering::Relaxed) + 1;
if n == 1 || (n.is_power_of_two() && n >= 1024) {
@@ -1094,18 +1094,28 @@ unsafe fn subresources_on(picked: &[(usize, &xg::Texture)], threads: usize) -> V
out
}
/// The title's bytes at `at`: where they are when all of them are readable (a copy first cost
/// as much as the detile of a video frame), else read page run by page run.
unsafe fn title_bytes(at: usize, len: usize) -> std::borrow::Cow<'static, [u8]> {
if crate::gpu::shader::readable(at, len) {
std::borrow::Cow::Borrowed(std::slice::from_raw_parts(at as *const u8, len))
} else {
std::borrow::Cow::Owned(read_committed(at, len))
}
}
/// Every subresource (D3D12 order) of a texture from the title's memory at `va`.
unsafe fn subresources(va: usize, tex: &xg::Texture) -> Vec<Subresource> {
let Some(tex) = tex.texels() else { return Vec::new() };
let c = &tex.chain;
let bpe = tex.bpe as usize;
if tex.depth > 0 {
let mem = read_committed(va, tex.size() as usize);
let mem = title_bytes(va, tex.size() as usize);
return c.levels.iter().enumerate().map(|(i, l)| Subresource { depth: l.depth, rows: l.height, row_bytes: l.width * bpe, data: c.detile_volume(&mem, i) }).collect();
}
let mut out = Vec::with_capacity(tex.array as usize * c.levels.len());
for s in 0..tex.array as usize {
let slice = read_committed(va + s * c.slice_bytes, c.slice_bytes);
let slice = title_bytes(va + s * c.slice_bytes, c.slice_bytes);
for (i, l) in c.levels.iter().enumerate() {
out.push(Subresource { depth: 1, rows: l.height, row_bytes: l.width * bpe, data: c.detile_level(&slice, i, s) });
}
@@ -1412,6 +1422,30 @@ mod tests {
assert!(LIST_NOTES.lock().unwrap().get(&l1).is_none());
}
/// The title's bytes are used where they are when all of them are readable, and read with the
/// pages that are not committed as zeros otherwise – the same bytes either way.
#[test]
fn title_bytes_are_used_in_place_when_all_are_readable() {
use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE};
unsafe {
let base = VirtualAlloc(std::ptr::null(), 3 << 12, MEM_RESERVE, PAGE_READWRITE) as usize;
assert_ne!(base, 0);
for page in [0usize, 2] {
VirtualAlloc((base + (page << 12)) as *const c_void, 1 << 12, MEM_COMMIT, PAGE_READWRITE);
std::ptr::write_bytes((base + (page << 12)) as *mut u8, 0xA0 + page as u8, 1 << 12);
}
let whole = title_bytes(base, 1 << 12);
assert!(matches!(whole, std::borrow::Cow::Borrowed(_)), "a committed page is not copied");
assert_eq!(whole.as_ptr() as usize, base);
assert!(whole.iter().all(|&b| b == 0xA0));
let holed = title_bytes(base, 3 << 12);
assert!(matches!(holed, std::borrow::Cow::Owned(_)), "a range with a reserved page is read");
assert_eq!(&holed[..], &read_committed(base, 3 << 12)[..]);
assert!(holed[..1 << 12].iter().all(|&b| b == 0xA0) && holed[1 << 12..2 << 12].iter().all(|&b| b == 0) && holed[2 << 12..].iter().all(|&b| b == 0xA2));
VirtualFree(base as *mut c_void, 0, MEM_RELEASE);
}
}
/// Only the wanted parts of the watched regions are scanned, merged, one region each.
#[test]
fn watch_ranges_clip_and_merge() {