D3D12.X: the title textures of a big batch are detiled on several threads

`title_textures::upload_all` read and detiled each texture of a batch
one after another, inside the title's own submit and present. Batches of
1 MB or more are now detiled on up to 8 threads (half the cores), each
taking the next texture; the results keep the batch's order. Smaller
batches stay on the calling thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1984742628
2026-11-24 01:26:17 +01:00
parent 7d60a48492
commit 2cefc88956
+77 -3
View File
@@ -778,17 +778,21 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) {
};
let mut todo = todo.into_iter().peekable();
while todo.peek().is_some() {
let mut batch = Vec::new();
let mut picked = Vec::new();
let mut bytes = 0usize;
let mut device = 0usize;
while let Some((_, (dev, _, tex, _))) = todo.peek() {
if !batch.is_empty() && (bytes + tex.size() as usize > BATCH_BYTES || *dev != device) {
if !picked.is_empty() && (bytes + tex.size() as usize > BATCH_BYTES || *dev != device) {
break;
}
let (r, (dev, va, tex, states)) = todo.next().unwrap();
device = dev;
bytes += tex.size() as usize;
let subs = subresources(va, &tex);
picked.push((r, va, tex, states));
}
let subs = subresources_of_all(&picked, bytes);
let mut batch = Vec::with_capacity(picked.len());
for ((r, va, _, states), subs) in picked.into_iter().zip(subs) {
if super::present::trace_frame() {
note_content(va, &r, &subs);
}
@@ -994,6 +998,50 @@ unsafe fn read_committed(at: usize, len: usize) -> Vec<u8> {
out
}
/// Batches of at least this many bytes are detiled on several threads (`subresources_of_all`)
const PARALLEL_BYTES: usize = 1 << 20;
/// `subresources` of every texture of a batch (`bytes` of them), in order: on several threads
/// when the batch is big. They were read and detiled one after another in the title's own
/// submit – most of a retail game's render thread while it streamed textures the CPU writes.
unsafe fn subresources_of_all(picked: &[(ID3D12Resource, usize, Arc<xg::Texture>, Vec<D3D12_RESOURCE_STATES>)], bytes: usize) -> Vec<Vec<Subresource>> {
let picked: Vec<(usize, &xg::Texture)> = picked.iter().map(|(_, va, tex, _)| (*va, &**tex)).collect();
let threads = if bytes < PARALLEL_BYTES { 1 } else { std::thread::available_parallelism().map_or(1, |n| n.get() / 2).clamp(1, 8) };
subresources_on(&picked, threads)
}
/// `subresources` of each (address, texture), in order, on up to `threads` threads.
unsafe fn subresources_on(picked: &[(usize, &xg::Texture)], threads: usize) -> Vec<Vec<Subresource>> {
let threads = threads.min(picked.len());
if threads <= 1 {
return picked.iter().map(|&(va, tex)| subresources(va, tex)).collect();
}
// the next texture to take; each thread hands back (index, subresources)
let next = std::sync::atomic::AtomicUsize::new(0);
let mut out: Vec<Vec<Subresource>> = (0..picked.len()).map(|_| Vec::new()).collect();
let done: Vec<Vec<(usize, Vec<Subresource>)>> = std::thread::scope(|s| {
let workers: Vec<_> = (0..threads)
.map(|_| {
s.spawn(|| {
let mut mine = Vec::new();
loop {
let k = next.fetch_add(1, Ordering::Relaxed);
let Some(&(va, tex)) = picked.get(k) else { break };
// SAFETY: as in the serial path – the title's memory is only read
mine.push((k, unsafe { subresources(va, tex) }));
}
mine
})
})
.collect();
workers.into_iter().map(|w| w.join().unwrap_or_default()).collect()
});
for (k, subs) in done.into_iter().flatten() {
out[k] = subs;
}
out
}
/// 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() };
@@ -1294,6 +1342,32 @@ mod tests {
assert_eq!(watch_ranges(&regions, wanted), vec![(0x1000_0000, 0x8_1000), (0x1100_0000, 0x1000)]);
}
/// The textures of a batch detiled on several threads come back as on one: complete and in
/// order (2D with mips and slices, 3D, BC7, a tiny one; more textures than threads).
#[test]
fn a_batch_detiled_on_several_threads_matches_one_thread() {
let descs = [
console_desc(3, 64, 64, 2, 3, 28, 0x105),
console_desc(3, 200, 72, 1, 5, 28, 0x105),
console_desc(4, 40, 24, 6, 3, 28, 0x105),
console_desc(3, 256, 256, 1, 9, 98, 0x10A),
console_desc(3, 4, 4, 1, 1, 28, 0x105),
console_desc(3, 128, 32, 3, 2, 28, 0x105),
console_desc(3, 64, 64, 1, 1, 28, 0x105),
];
let textures: Vec<xg::Texture> = descs.iter().map(|d| xg::Texture::new(d).unwrap()).collect();
// each texture's console memory: distinct bytes per texture
let mems: Vec<Vec<u8>> = textures.iter().enumerate().map(|(t, x)| (0..x.size() as usize).map(|i| (i * 7 + t * 61) as u8).collect()).collect();
let picked: Vec<(usize, &xg::Texture)> = mems.iter().zip(&textures).map(|(m, x)| (m.as_ptr() as usize, x)).collect();
unsafe {
let one = subresources_on(&picked, 1);
assert!(one.iter().all(|s| !s.is_empty()), "a texture gave no subresources");
for threads in [2, 3, 8] {
assert!(subresources_on(&picked, threads) == one, "{threads} threads differ from one");
}
}
}
fn console_desc(dim: u32, w: u64, h: u32, array_or_depth: u16, mips: u16, format: u32, layout: u32) -> [u8; 0x38] {
let mut d = [0u8; 0x38];
d[0..4].copy_from_slice(&dim.to_le_bytes());