Textures from the first level viewed; equal uploads left out; bakes take textures as they are; kept by the card's real room

Placed textures are read, kept on the card and uploaded from the first level their views show (VICTORY_HELD_LEVELS=0: whole;
after victory-amd 4436f8f1's spans). An upload of the bytes a host copy holds is left out (VICTORY_TEXTURE_SAME=0). A slot
shows its new view once that view's upload has run. A decoration bake takes slots due a few frames later too and waits for
memory being remapped (VICTORY_BAKE_EXACT=0). Host copies go only past 88 % of the budget, the longest idle first
(VICTORY_TEXTURE_KEEP=1: the rule before). A slot rewritten with other filler words keeps its view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 02:56:28 -06:00
1 parent 320c93a30c
commit c00a0bbd79
6 files changed
+866 -45

No files matched your search

+209 -18
View File
@@ -46,8 +46,20 @@ pub(crate) struct Staging {
footprints: Vec<D3D12_PLACED_SUBRESOURCE_FOOTPRINT>,
rows: Vec<u32>,
layout: victory_formats::texture::Layout,
/// Mips per array slice.
/// Mips per array slice, of the title's texture.
mips: u32,
/// The first of them that is read and uploaded
/// (`crate::resource::held_only`), the one the host copy begins at, and
/// the bytes of title memory read.
first: u32,
level0: u32,
read: usize,
/// 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,
/// The staging buffer's bytes (counted in `STAGED` while it lives).
bytes: u64,
memory: *const u8,
@@ -160,6 +172,8 @@ pub(crate) fn alias(list: &HostList, placed: This, source: This) -> bool {
const TARGET_OR_UNORDERED: u32 = 0x4 | 0x8;
const COPY_DEST: u32 = 0x400;
const COPY_SOURCE: u32 = 0x800;
// (All of its levels: the copy is level by level.)
crate::resource::want_level(placed, 0);
let (Some(to), Some(from)) = (
crate::resource::host_resource(placed),
crate::resource::host_resource(source),
@@ -629,7 +643,21 @@ pub(crate) fn title_written(texture: &ID3D12Resource) -> bool {
/// its host copy, and the detiling that refreshes the staging buffer at
/// submission if the memory changed. Leaves the texture in the copy
/// destination state.
pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource) {
pub(crate) fn upload(list: &HostList, resource: This, _texture: &ID3D12Resource) {
upload_from(list, resource, 0);
}
thread_local! {
/// The thread records uploads for a draw that bakes from them
/// ([`Staging::waits`]).
pub(crate) static BAKING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// [`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.)
pub(crate) fn upload_from(list: &HostList, resource: This, first: u32) {
let first = if crate::resource::held_only() { first } else { 0 };
let host_written = HOST_WRITTEN
.lock()
.unwrap_or_else(|e| e.into_inner())
@@ -637,13 +665,28 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
.is_some_and(|set| set.contains(&(resource as usize)));
// shortcut: a texture the host copied into keeps the host's contents
// even if the title later writes its memory too.
if host_written || !list.first_use(resource as usize) {
if host_written {
return;
}
let Some(staging) = staging(resource, texture) else {
// Once a list for each first level; not again for one that the list's
// upload of larger ones held too.
if !list.first_use(1 << 63 | (resource as usize) << 4 | first as usize & 15) {
return;
}
if !list.first_use(resource as usize) && staged_from(resource) <= first {
return;
}
let Some(copy) = crate::resource::host_copy(resource) else {
return;
};
let texture = &copy.texture;
let Some(staging) = staging(resource, &copy, first) else {
return;
};
use std::sync::atomic::Ordering::Relaxed;
if BAKING.get() {
staging.waits.store(true, Relaxed);
}
staging.used.store(crate::host::moment(), Relaxed);
// Levels on demand: a view of the texture now starts at another
// level, or another texture now starts inside its span, with no byte
@@ -684,7 +727,7 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::TexUploads, 1);
if no_wait() && fill_unlocked() {
let there = mapped_bytes(staging.memory, staging.layout.size as usize);
let there = mapped_bytes(staging.memory, staging.read);
staging.expected.store(there, Relaxed);
}
const COPY_DEST: u32 = 0x400;
@@ -741,7 +784,7 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
pResource: unsafe { std::mem::transmute_copy(texture) },
Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
SubresourceIndex: i as u32,
SubresourceIndex: staging.first - staging.level0 + i as u32,
},
};
let from = D3D12_TEXTURE_COPY_LOCATION {
@@ -768,6 +811,13 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
staging.dirty.store(false, Relaxed);
});
}
// A host copy that took the place of one with fewer levels: the slots
// that show that one are made again once this upload has run.
if staging.first == staging.level0 && copy.grown.swap(false, Relaxed) {
let start = staging.memory as u64;
list.defer_host(move || crate::descriptor::host::levels_changed_after(start));
}
let noted = staging.clone();
// Levels on demand (`crate::tiles`): after the check below, in the
// order of this list's steps, and before the list is executed, the
// levels the staging buffer holds as the title's are given the card's
@@ -778,6 +828,8 @@ pub(crate) fn upload(list: &HostList, resource: This, texture: &ID3D12Resource)
let Some(on_demand) = on_demand else {
// Once per submission however many of its lists sample the texture.
list.defer_texture(staging);
// Then what the list's copy puts in the host copy is known.
list.defer_host(move || noted.note_held());
return;
};
list.defer_texture(staging.clone());
@@ -971,7 +1023,21 @@ impl Staging {
/// The title memory the texture is read from: address and bytes.
pub(crate) fn memory(&self) -> (usize, usize) {
(self.memory as usize, self.layout.size as usize)
(self.memory as usize, self.read)
}
/// The title's level and the array slice of the staging buffer's part
/// `i`.
fn part(&self, i: usize) -> (u32, u32) {
let mips = self.mips - self.first;
(self.first + i as u32 % mips, i as u32 / mips)
}
/// The host copy holds what the staging buffer was filled from
/// ([`unchanged`]).
fn note_held(&self) {
let hash = self.last.lock().unwrap_or_else(|e| e.into_inner()).hash;
*self.holds.lock().unwrap_or_else(|e| e.into_inner()) = hash.map(|hash| (self.first, hash));
}
/// Detiles the texture memory into the staging buffer if it changed
@@ -994,7 +1060,7 @@ impl Staging {
}
fn refresh_timed(&self) -> usize {
let size = self.layout.size as usize;
let size = self.read;
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;
@@ -1061,7 +1127,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() {
let _mapped = if no_wait() && !self.waits.load(std::sync::atomic::Ordering::Relaxed) {
// Not waited for: while the runtime remaps title memory the
// texture is read through the kernel instead, whole or not at
// all (`no_wait`).
@@ -1240,7 +1306,7 @@ impl Staging {
);
}
for (i, (footprint, &rows)) in self.footprints.iter().zip(&self.rows).enumerate() {
let (mip, slice) = (i as u32 % self.mips, i as u32 / self.mips);
let (mip, slice) = self.part(i);
let f = footprint.Footprint;
let pitch = f.RowPitch as usize;
// The last row of the last subresource is not padded to its pitch
@@ -1330,6 +1396,59 @@ pub(crate) fn staged_state(resource: This) -> Option<(bool, bool)> {
Some((staging.uploaded.load(Relaxed), staging.dirty.load(Relaxed)))
}
/// The first level a title texture's staging buffer holds; `u32::MAX`
/// without one.
fn staged_from(resource: This) -> u32 {
let map = STAGING.lock().unwrap_or_else(|e| e.into_inner());
map.as_ref()
.and_then(|map| map.get(&(resource as usize)))
.map_or(u32::MAX, |s| s.first)
}
/// Whether the levels from `first` on of `resource`'s host copy were filled
/// from the bytes title memory holds now: an upload would bring nothing new.
/// (Of 787 repeated uploads of a frame that streams 398 were equal in every
/// byte, and a texture uploaded again costs a staging buffer, a detiling and
/// a copy more than this look.) `VICTORY_TEXTURE_SAME=0`: not asked.
pub(crate) fn unchanged(resource: This, first: u32) -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_SAME").is_none_or(|v| v != "0")) {
return false;
}
let Some(copy) = crate::resource::made_copy(resource) else {
return false;
};
let Some((from, held)) = *copy.holds.lock().unwrap_or_else(|e| e.into_inner()) else {
return false;
};
let Some((memory, desc)) = crate::resource::texture_memory(resource) else {
return false;
};
let Some(layout) = crate::resource::texture_layout(&desc) else {
return false;
};
let layout = &layout.planes[0].layout;
if from > crate::resource::read_from(layout, first).max(copy.base) {
return false;
}
let size = layout.bytes_from(from) as usize;
with_read(size, |read| {
if crate::host::read_there(memory, read) == 0 {
return false;
}
blake3::hash(read) == held
})
}
/// Where the levels from `first` on of a title texture are: address and
/// bytes.
pub(crate) fn title_span(resource: This, first: u32) -> Option<(u64, u64)> {
let (memory, desc) = crate::resource::texture_memory(resource)?;
let layout = crate::resource::texture_layout(&desc)?;
let layout = &layout.planes[0].layout;
Some((memory as u64, layout.bytes_from(crate::resource::read_from(layout, first))))
}
pub(crate) fn is_staged(resource: This) -> bool {
let map = STAGING.lock().unwrap_or_else(|e| e.into_inner());
map.as_ref().is_some_and(|map| map.contains_key(&(resource as usize)))
@@ -1342,8 +1461,7 @@ pub(crate) fn staged(resource: This) -> Option<(u64, bool)> {
let map = STAGING.lock().unwrap_or_else(|e| e.into_inner());
map.as_ref()?.get(&(resource as usize))?.clone()
};
let size = s.layout.size as usize;
Some((size as u64, committed(s.memory, size)))
Some((s.read as u64, committed(s.memory, s.read)))
}
/// Whether `[at, at + len)` is committed memory (streamed textures may be
@@ -1509,13 +1627,17 @@ pub(crate) fn mapped_bytes(at: *const u8, len: usize) -> u64 {
}
/// The staging state of a title texture, made on first use.
fn staging(resource: This, texture: &ID3D12Resource) -> Option<Arc<Staging>> {
fn staging(resource: This, copy: &crate::resource::HostTexture, first: u32) -> Option<Arc<Staging>> {
let texture = &copy.texture;
// Asked before the table is locked: the sweep lets go of a staging
// buffer with its texture's host side locked, in that order.
let on_demand = crate::resource::host_tiled(resource).is_some();
let mut map = STAGING.lock().unwrap_or_else(|e| e.into_inner());
let map = map.get_or_insert_with(HashMap::new);
if let Some(s) = map.get(&(resource as usize)) {
if let Some(s) = map.get(&(resource as usize))
&& s.first <= first.max(copy.base)
&& s.serial == copy.serial
{
return Some(s.clone());
}
let (memory, desc) = crate::resource::texture_memory(resource)?;
@@ -1534,10 +1656,14 @@ fn staging(resource: This, texture: &ID3D12Resource) -> Option<Arc<Staging>> {
let host = host()?;
// SAFETY: a live resource.
let d = unsafe { texture.GetDesc() };
let level0 = copy.base;
let mips = level0 + u32::from(d.MipLevels);
let first = crate::resource::read_from(&layout, first).max(level0).min(mips - 1);
let read = layout.bytes_from(first) as usize;
let count = if d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D {
u32::from(d.MipLevels)
mips - first
} else {
u32::from(d.MipLevels) * u32::from(d.DepthOrArraySize)
(mips - first) * u32::from(d.DepthOrArraySize)
};
let mut footprints = vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); count as usize];
let mut rows = vec![0u32; count as usize];
@@ -1546,7 +1672,7 @@ fn staging(resource: This, texture: &ID3D12Resource) -> Option<Arc<Staging>> {
unsafe {
host.device.GetCopyableFootprints(
&d,
0,
first - level0,
count,
0,
Some(footprints.as_mut_ptr()),
@@ -1581,7 +1707,13 @@ fn staging(resource: This, texture: &ID3D12Resource) -> Option<Arc<Staging>> {
footprints,
rows,
layout,
mips: u32::from(d.MipLevels),
mips,
first,
level0,
read,
serial: copy.serial,
holds: copy.holds.clone(),
waits: std::sync::atomic::AtomicBool::new(false),
bytes: total,
memory,
last: Mutex::default(),
@@ -1800,6 +1932,65 @@ mod allocation_costs {
}
}
/// 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
/// title's bytes, so reading and hashing them stay; `VICTORY_BENCH_FILE`
/// is a big file to read past the system's cache).
#[test]
#[ignore = "a measurement, not a check"]
fn cold_texture_costs() {
let Some(host) = crate::host::host() else { return };
let desc = [3u32, 0, 0, 0, 1024, 0, 1024, 0xb_0001, 98, 1, 0, 0x105, 0, 0];
// SAFETY: 56 bytes in the description's layout.
let desc = unsafe { desc.as_ptr().cast::<crate::resource::ResourceDesc>().read_unaligned() };
let layout = crate::resource::texture_layout(&desc).unwrap().planes[0].layout.clone();
let size = layout.size as usize;
let title: Vec<u8> = (0..size).map(|i| (i.wrapping_mul(2654435761) >> 13) as u8).collect();
let (_buffer, up) = host.upload_buffer(2 * layout.size).unwrap();
// SAFETY: the mapping.
let up = unsafe { std::slice::from_raw_parts_mut(up, 2 * size) };
let mut read = vec![0u8; size];
const N: u32 = 200;
let per_mb = |t: std::time::Duration| t.as_secs_f64() * 1e3 / (f64::from(N) * size as f64 / 1e6);
let timed = |what: &str, f: &mut dyn FnMut()| {
f();
let started = std::time::Instant::now();
(0..N).for_each(|_| f());
println!("{what}: {:.3} ms/MB", per_mb(started.elapsed()));
};
timed("read through the kernel or a safe copy", &mut || {
std::hint::black_box(crate::host::read_into(title.as_ptr(), &mut read));
});
timed("blake3", &mut || {
std::hint::black_box(blake3::hash(&title));
});
timed("detiled into upload memory", &mut || {
let mut at = 0;
for (mip, m) in layout.mips.iter().enumerate() {
let pitch = (m.width as usize * 16).next_multiple_of(256);
let len = pitch * m.height as usize;
layout.detile(&title, mip as u32, 0, &mut up[at..at + len], pitch, len).unwrap();
at += len.next_multiple_of(512);
}
});
timed("copied into upload memory", &mut || up[..size].copy_from_slice(&title));
if let Some(file) = std::env::var_os("VICTORY_BENCH_FILE") {
use std::os::windows::fs::{FileExt, OpenOptionsExt};
const NO_BUFFERING: u32 = 0x2000_0000;
let file = std::fs::OpenOptions::new().read(true).custom_flags(NO_BUFFERING).open(file).unwrap();
let len = file.metadata().unwrap().len();
let mut block = vec![0u8; size + 4096];
let skew = block.as_ptr().align_offset(4096);
let n = size / 4096 * 4096;
let mut at = 0u64;
timed("read from the disk, one request at a time", &mut || {
at = (at + 7919 * 4096 * 64) % (len - n as u64) / 4096 * 4096;
file.seek_read(&mut block[skew..skew + n], at).unwrap();
});
}
}
#[test]
#[ignore = "a measurement, not a check"]
fn allocation_costs() {