Streaming, copy queue, texture pools and a shader cache that survives rebuilds

Textures and staging cut from 64 MB blocks; a kept view per page section and
page lists read again until the title has filled them in; the GPU side told
when streamed bytes land; results fetched over the copy queue
(VICTORY_RELAY_COPY=1); the write-watch journal behind a reader-writer lock;
the shader cache keyed by the translator's sources and `victory shaders`;
helper lanes as in upstream 1733 (VICTORY_HELPER_OWN).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 18:55:40 -06:00
1 parent 7f166af4f3
commit 9fea7434cd
20 files changed
+1629 -232

No files matched your search

+158 -10
View File
@@ -33,7 +33,12 @@ macro_rules! once {
/// A texture's upload state: the staging buffer laid out as the host
/// texture's subresources, and what was last detiled.
pub(crate) struct Staging {
/// A buffer of its own, or the block [`Staging::piece`] is part of.
buffer: ID3D12Resource,
/// Where in `buffer` it begins (the footprints count from there).
base: u64,
/// Back in the pool with the last list that names it.
_piece: Option<crate::pool::StagingPiece>,
/// Its bytes.
#[cfg(feature = "diagnostics")]
size: u64,
@@ -552,6 +557,23 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::TexUploads, 1);
const COPY_DEST: u32 = 0x400;
// A texture cut from a block may lie where another did: what the card
// holds of that one is done with before the first upload into this one.
if crate::pool::on() && !staging.uploaded.load(Relaxed) {
let aliasing = D3D12_RESOURCE_BARRIER {
Type: D3D12_RESOURCE_BARRIER_TYPE_ALIASING,
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
Anonymous: D3D12_RESOURCE_BARRIER_0 {
Aliasing: std::mem::ManuallyDrop::new(D3D12_RESOURCE_ALIASING_BARRIER {
pResourceBefore: std::mem::ManuallyDrop::new(None),
// SAFETY: a borrowed pointer the barrier never releases.
pResourceAfter: unsafe { std::mem::transmute_copy(texture) },
}),
},
};
// SAFETY: an open list and a live texture.
unsafe { list.list.ResourceBarrier(&[aliasing]) };
}
crate::list::ensure_state(list, resource, COPY_DEST);
// The staging buffer lives as long as `staging`, which the deferred
// refresh below holds until the list is reset.
@@ -573,14 +595,14 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
d.MipLevels,
staging.footprints.len(),
staging.size,
footprint.Offset,
staging.base + footprint.Offset,
f.Width,
f.Height,
f.Depth,
f.RowPitch
)
},
(gpu + footprint.Offset, len),
(gpu + staging.base + footprint.Offset, len),
);
}
let to = D3D12_TEXTURE_COPY_LOCATION {
@@ -596,7 +618,10 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
pResource: unsafe { std::mem::transmute_copy(&staging.buffer) },
Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
PlacedFootprint: *footprint,
PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
Offset: staging.base + footprint.Offset,
..*footprint
},
},
};
// SAFETY: an open list; both locations name live resources.
@@ -1074,18 +1099,26 @@ fn staging(resource: This, texture: &ID3D12Resource) -> Option<Arc<Staging>> {
Some(&mut total),
);
}
let (buffer, data) = host.upload_buffer(total)?;
crate::host::name(&buffer, || {
format!(
"staging of {resource:?} {}x{}x{} f{}",
d.Width, d.Height, d.DepthOrArraySize, d.Format.0
)
});
let (piece, buffer, base, data) = match crate::pool::staging(host, total) {
Some((piece, buffer, base, data)) => (Some(piece), buffer, base, data),
None => {
let (buffer, data) = host.upload_buffer(total)?;
crate::host::name(&buffer, || {
format!(
"staging of {resource:?} {}x{}x{} f{}",
d.Width, d.Height, d.DepthOrArraySize, d.Format.0
)
});
(None, buffer, 0, data)
}
};
tracing::debug!(?desc, total, "texture contents uploaded from title memory");
STAGED[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
STAGED[1].fetch_add(total, std::sync::atomic::Ordering::Relaxed);
let s = Arc::new(Staging {
buffer,
base,
_piece: piece,
#[cfg(feature = "diagnostics")]
size: total,
data,
@@ -1214,3 +1247,118 @@ mod title_memory_tests {
}
}
}
/// How long this PC's card takes to make the allocations a staged texture needs, for
/// `cargo test -p victory-gpu allocation_costs -- --ignored --nocapture`.
#[cfg(test)]
mod allocation_costs {
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::*;
fn texture(size: u32) -> D3D12_RESOURCE_DESC {
D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Width: u64::from(size),
Height: size,
DepthOrArraySize: 1,
MipLevels: 0,
Format: DXGI_FORMAT_BC7_UNORM,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
..Default::default()
}
}
fn buffer(size: u64) -> D3D12_RESOURCE_DESC {
D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
Width: size,
Height: 1,
DepthOrArraySize: 1,
MipLevels: 1,
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
..Default::default()
}
}
fn committed(device: &ID3D12Device, kind: D3D12_HEAP_TYPE, flags: D3D12_HEAP_FLAGS, desc: &D3D12_RESOURCE_DESC) -> ID3D12Resource {
let heap = D3D12_HEAP_PROPERTIES { Type: kind, ..Default::default() };
let state = if kind == D3D12_HEAP_TYPE_UPLOAD { D3D12_RESOURCE_STATE_GENERIC_READ } else { D3D12_RESOURCE_STATE_COMMON };
let mut out = None;
// SAFETY: plain creation.
unsafe { device.CreateCommittedResource(&heap, flags, desc, state, None, &mut out) }.expect("committed");
out.unwrap()
}
fn timed(what: &str, n: usize, threads: usize, make: &(dyn Fn(usize) -> ID3D12Resource + Sync)) {
let began = std::time::Instant::now();
let all: Vec<_> = std::thread::scope(|s| {
(0..threads)
.map(|t| s.spawn(move || (t..n).step_by(threads).map(make).collect::<Vec<_>>()))
.collect::<Vec<_>>()
.into_iter()
.flat_map(|h| h.join().unwrap())
.collect()
});
let took = began.elapsed().as_secs_f64() * 1e3;
println!("{what}, {threads} thread(s): {n} in {took:.1} ms, {:.0} us each", took * 1e3 / n as f64);
drop(all);
}
#[test]
#[ignore = "a measurement, not a check"]
fn allocation_costs() {
let Some(host) = crate::host::host() else { return };
let device = &host.device;
let not_zeroed = D3D12_HEAP_FLAG_CREATE_NOT_ZEROED;
const N: usize = 1000;
for size in [128u32, 512, 1024] {
let (t, b) = (texture(size), buffer(u64::from(size * size) * 4 / 3 + 65536));
println!("-- BC7 {size}x{size}");
for threads in [1, 4, 8] {
timed("texture committed", N, threads, &|_| committed(device, D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_FLAG_NONE, &t));
timed("texture committed not zeroed", N, threads, &|_| committed(device, D3D12_HEAP_TYPE_DEFAULT, not_zeroed, &t));
let mapped = |flags| {
let b = &b;
move |_| {
let r = committed(device, D3D12_HEAP_TYPE_UPLOAD, flags, b);
let mut data = std::ptr::null_mut();
// SAFETY: a live upload buffer.
unsafe { r.Map(0, None, Some(&mut data)) }.unwrap();
// SAFETY: the first byte of the mapping (its first touch).
unsafe { data.cast::<u8>().write_volatile(1) };
r
}
};
timed("staging committed and mapped", N, threads, &mapped(D3D12_HEAP_FLAG_NONE));
timed("staging committed and mapped, not zeroed", N, threads, &mapped(not_zeroed));
}
// SAFETY: plain query.
let info = unsafe { device.GetResourceAllocationInfo(0, &[t]) };
let per_heap = ((64u64 << 20) / info.SizeInBytes).max(1) as usize;
let heaps: Vec<ID3D12Heap> = (0..N.div_ceil(per_heap))
.map(|_| {
let desc = D3D12_HEAP_DESC {
SizeInBytes: per_heap as u64 * info.SizeInBytes,
Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() },
Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES | not_zeroed,
..Default::default()
};
let mut heap = None;
// SAFETY: plain creation.
unsafe { device.CreateHeap(&desc, &mut heap) }.expect("heap");
heap.unwrap()
})
.collect();
let place = |i: usize| {
let mut out: Option<ID3D12Resource> = None;
let at = (i % per_heap) as u64 * info.SizeInBytes;
// SAFETY: a part of a live heap no other texture has.
unsafe { device.CreatePlacedResource(&heaps[i / per_heap], at, &t, D3D12_RESOURCE_STATE_COMMON, None, &mut out) }.expect("placed");
out.unwrap()
};
timed("texture placed in 64 MB heaps", N, 1, &place);
timed("texture placed in 64 MB heaps", N, 8, &place);
}
}
}