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

+205 -15
View File
@@ -140,6 +140,42 @@ pub(crate) fn levels_changed(start: u64) {
RESHOWN.lock().unwrap_or_else(|e| e.into_inner()).push(start);
}
/// [`levels_changed`] once the submissions begun so far (that of the list
/// whose step calls this among them) have run.
pub(crate) fn levels_changed_after(start: u64) {
let after = crate::host::submissions().0;
RESHOWN_AFTER.lock().unwrap_or_else(|e| e.into_inner()).push((after, start));
}
static RESHOWN_AFTER: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());
/// Slots that show a placed texture from another level than the title's view
/// in them says now, and are made from that view when the upload it needs
/// has run ([`HostHeap::texture_slot_flip`]).
struct Flip {
/// The submission to wait for.
after: u64,
/// The heap (its address), the slot, where its record is and which
/// record it was.
heap: usize,
index: u32,
handle: u64,
key: u64,
}
static FLIPS: Mutex<Vec<Flip>> = Mutex::new(Vec::new());
/// Since the start: slots made from their new view after its upload had run,
/// and those the title had written again by then.
pub(crate) static FLIPPED: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
/// The key of the record at `handle`, a view's descriptor slot; 0: none.
fn record_key(handle: u64) -> u64 {
// SAFETY: the handle is a descriptor slot in title memory.
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
if tag == VIEW_TAG { u64::from(generation) << 32 | u64::from(i) } else { 0 }
}
/// Levels on demand (`crate::tiles`): where the placed textures start
/// whose last upload left a held level without tiles (the card had no
/// room, or no heap could be had), and when (`crate::host::moment`). A
@@ -210,8 +246,9 @@ pub(crate) struct Spans {
rule: Rule,
by_start: std::collections::BTreeMap<u64, (u64, std::collections::BTreeSet<u32>)>,
by_slot: std::collections::HashMap<u32, u64>,
/// The level each slot's view starts at ([`Spans::shows`]).
first: std::collections::HashMap<u32, u32>,
/// The level each slot's view starts at, and where what it is read from
/// ends ([`Spans::shows`]).
first: std::collections::HashMap<u32, (u32, u64)>,
/// The bytes a placed texture takes, by resource description: worked
/// out once for each (a title writes the views of a few thousand
/// descriptions, thousands of times a frame).
@@ -329,14 +366,20 @@ impl Spans {
stale
}
/// The view in slot `index` starts at level `first` of its texture.
fn shows(&mut self, index: u32, first: u32) {
self.first.insert(index, first);
/// The view in slot `index` starts at level `first` of its texture; the
/// memory of that and the smaller levels ends at `end`.
fn shows(&mut self, index: u32, first: u32, end: u64) {
self.first.insert(index, (first, end));
}
/// Whether slot `index`'s view is read from memory at or past `start`.
fn reaches(&self, index: u32, start: u64) -> bool {
self.first.get(&index).is_none_or(|&(_, end)| end > start)
}
/// The lowest level a view of the texture at `start` starts at.
fn first_viewed(&self, start: u64) -> Option<u32> {
self.by_start.get(&start)?.1.iter().filter_map(|slot| self.first.get(slot)).min().copied()
self.by_start.get(&start)?.1.iter().filter_map(|slot| self.first.get(slot)).map(|&(first, _)| first).min()
}
/// The first span that starts inside `[start, end)` past the first
@@ -370,6 +413,7 @@ impl Spans {
.iter()
.filter_map(|at| self.by_start.get(at))
.flat_map(|span| span.1.iter().copied())
.filter(|&slot| self.reaches(slot, start))
.collect()
}
@@ -391,6 +435,7 @@ impl Spans {
.into_iter()
.chain(self.by_start.range(start..end).map(|(_, span)| span))
.flat_map(|span| span.1.iter().copied())
.filter(|&slot| self.reaches(slot, start))
.collect()
}
}
@@ -1525,6 +1570,19 @@ impl HostHeap {
if self.sampler {
return 0;
}
{
let mut after = RESHOWN_AFTER.lock().unwrap_or_else(|e| e.into_inner());
if !after.is_empty() {
let finished = crate::host::submissions().1;
let mut now = RESHOWN.lock().unwrap_or_else(|e| e.into_inner());
after.retain(|&(at, start)| {
if at <= finished {
now.push(start);
}
at > finished
});
}
}
let starts = std::mem::take(&mut *RESHOWN.lock().unwrap_or_else(|e| e.into_inner()));
if starts.is_empty() {
return 0;
@@ -1594,6 +1652,75 @@ impl HostHeap {
true
}
/// Whether host slot `index` shows the placed texture the title's record
/// at `handle` views, by an earlier view of it.
pub(crate) fn texture_slot_shows_other(&self, index: u32, handle: u64) -> bool {
let key = record_key(handle);
let made = self.made.lock().unwrap_or_else(|e| e.into_inner());
let Some(&shown) = made.get(index as usize) else {
return false;
};
if key == 0 || shown == 0 || shown == key {
return false;
}
let view = |key: u64| view_at(key as u32, (key >> 32) as u32);
matches!((view(shown), view(key)), (Some(a), Some(b)) if same_placed_texture(&a, &b))
}
/// Slot `index` is made from the record at `handle` (what it is now)
/// when `list`, which holds the upload of what that view shows, has run:
/// until then it shows what it showed. (Made at once, the frames on the
/// card read the new view before its levels were filled.)
pub(crate) fn texture_slot_flip(&self, list: &crate::host::HostList, index: u32, handle: u64) {
let (heap, key) = (std::ptr::from_ref(self) as usize, record_key(handle));
list.defer_host(move || {
let after = crate::host::submissions().0;
let flip = Flip { after, heap, index, handle, key };
FLIPS.lock().unwrap_or_else(|e| e.into_inner()).push(flip);
});
}
/// Makes the slots whose new view's upload has run
/// ([`HostHeap::texture_slot_flip`]); returns how many.
pub(crate) fn texture_slots_flip(&self) -> usize {
use std::sync::atomic::Ordering::Relaxed;
let finished = crate::host::submissions().1;
let ready: Vec<Flip> = {
let mut flips = FLIPS.lock().unwrap_or_else(|e| e.into_inner());
if flips.is_empty() {
return 0;
}
let own = std::ptr::from_ref(self) as usize;
let (ready, wait): (Vec<Flip>, Vec<Flip>) =
flips.drain(..).partition(|f| f.after <= finished && f.heap == own);
*flips = wait;
ready
};
let mut flipped = 0;
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
for f in ready {
let Some(shown) = made.get_mut(f.index as usize) else {
continue;
};
if *shown == f.key {
continue;
}
// Written again since, or made null: that write is in the queue.
let view = view_at(f.key as u32, (f.key >> 32) as u32);
let (true, true, Some(view)) = (f.key != 0 && record_key(f.handle) == f.key, *shown != 0, view) else {
FLIPPED[1].fetch_add(1, Relaxed);
continue;
};
if let Some(kept) = make(&view, self.cpu(f.index)) {
*shown = f.key;
self.keep(f.index, kept);
flipped += 1;
}
}
FLIPPED[0].fetch_add(flipped as u64, Relaxed);
flipped
}
/// The memory of the texture title slot `index` views is there (again):
/// see [`HostHeap::texture_slot_gone`].
pub(crate) fn texture_slot_here(&self, index: u32) {
@@ -1761,23 +1888,25 @@ impl HostHeap {
// (And the level the view starts at: word 4 of a texture
// view's description, after format, dimension, mapping
// and a pad.)
Some((resource, u64::from(desc[10]) | u64::from(desc[11]) << 32, desc[4]))
let at = u64::from(desc[10]) | u64::from(desc[11]) << 32;
let held = crate::resource::placed_bytes_from(resource, crate::resource::first_mip(desc));
Some((resource, at, desc[4], held.map_or(u64::MAX, |held| at.saturating_add(held))))
}
_ => None,
};
let stale = {
use std::sync::atomic::Ordering::Relaxed;
let mut placed = self.placed.lock().unwrap_or_else(|e| e.into_inner());
let span = placed_view.and_then(|(resource, start, first)| {
Some((start, start.saturating_add(placed.bytes(resource)?), first))
let span = placed_view.and_then(|(resource, start, first, held)| {
Some((start, start.saturating_add(placed.bytes(resource)?), first, held))
});
match span {
Some((start, end, first)) => {
Some((start, end, first, held)) => {
SPANNED[0].fetch_min(start, Relaxed);
SPANNED[1].fetch_max(end, Relaxed);
placed.rule = stale_rule();
let stale = placed.note(index, start, end, !seen);
placed.shows(index, first);
placed.shows(index, first, held);
stale
}
None => {
@@ -2786,7 +2915,8 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<Kept> {
}
View::Shader(resource, desc) => {
let resource = *resource as crate::object::This;
if let Some(texture) = crate::resource::host_resource(resource) {
if let Some(copy) = crate::resource::host_copy(resource) {
let texture = &copy.texture;
// The title's views of textures with a stencil plane, as it
// describes them (format, dimension, component mapping, a
// pad, then first level, levels, plane): each distinct one
@@ -2841,6 +2971,11 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<Kept> {
};
crate::tiles::show_from(desc.get_or_insert(whole), first, u32::from(own.MipLevels));
}
if copy.base > 0
&& let Some(desc) = &mut desc
{
from_base(desc, copy.base);
}
// SAFETY: a live texture and a slot of our heap.
unsafe {
device.CreateShaderResourceView(
@@ -2975,6 +3110,23 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option<Kept> {
}
}
/// The view `desc` of a texture, of a host copy of its levels from `base`
/// on (`crate::resource::held_only`). What it shows of larger ones, if any,
/// is left out.
fn from_base(desc: &mut D3D12_SHADER_RESOURCE_VIEW_DESC, base: u32) {
if desc.ViewDimension != D3D12_SRV_DIMENSION_TEXTURE2D {
return;
}
// SAFETY: the member its dimension says.
let t = unsafe { &mut desc.Anonymous.Texture2D };
let lost = base.saturating_sub(t.MostDetailedMip);
t.MostDetailedMip = t.MostDetailedMip.saturating_sub(base);
if t.MipLevels != u32::MAX {
t.MipLevels = t.MipLevels.saturating_sub(lost).max(1);
}
t.ResourceMinLODClamp = (t.ResourceMinLODClamp - base as f32).max(0.0);
}
/// Writes of a slot with another view of the placed texture it showed,
/// since the start: the slot kept showing the old view.
pub(crate) static KEPT_VIEWS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
@@ -2993,7 +3145,10 @@ fn keep_views() -> bool {
fn same_placed_texture(a: &View, b: &View) -> bool {
match (a, b) {
(View::Placed(false, resource, desc), View::Placed(false, other, other_desc)) => {
resource[0] != 1 && resource == other && desc[10..12] == other_desc[10..12]
// (Words 1 and 13 are the filler of the description's layout, not
// part of what a texture is: `crate::resource::texture_at`.)
let same = |from: usize, to: usize| resource[from..to] == other[from..to];
resource[0] != 1 && resource[0] == other[0] && same(2, 13) && desc[10..12] == other_desc[10..12]
}
_ => false,
}
@@ -3011,6 +3166,7 @@ pub(crate) fn placed_texture(view: &View) -> Option<View> {
}
let address = u64::from(desc[10]) | u64::from(desc[11]) << 32;
let texture = crate::resource::texture_at(address, resource)?;
crate::resource::want_level(texture, crate::resource::first_mip(desc));
Some(View::Shader(texture as usize, desc[..10].try_into().ok()))
}
@@ -3186,6 +3342,40 @@ fn null_view(at: D3D12_CPU_DESCRIPTOR_HANDLE) {
mod tests {
use super::*;
/// A view of a host copy that begins at a later level shows the same
/// levels of it.
#[test]
fn views_count_from_the_first_level_of_the_host_copy() {
let view = |first, levels, clamp| {
let mut desc = srv_desc(&[98, 4, 5768, 0, first, levels, 0, 0, 0, 0]);
desc.Anonymous.Texture2D.ResourceMinLODClamp = clamp;
from_base(&mut desc, 3);
// SAFETY: the member its dimension says.
let t = unsafe { desc.Anonymous.Texture2D };
(t.MostDetailedMip, t.MipLevels, t.ResourceMinLODClamp)
};
assert_eq!(view(3, 4, 0.0), (0, 4, 0.0));
assert_eq!(view(5, u32::MAX, 5.5), (2, u32::MAX, 2.5));
// (Two levels the copy has not.)
assert_eq!(view(1, 4, 0.0), (0, 2, 0.0));
}
/// Memory past the levels a slot's view shows is not its texture's.
#[test]
fn a_view_of_smaller_levels_is_not_told_of_memory_past_them() {
let mut spans = Spans::default();
spans.note(1, 0x10000, 0x50000, true);
spans.shows(1, 3, 0x12000);
spans.note(2, 0x10000, 0x50000, true);
spans.shows(2, 0, 0x50000);
assert_eq!(spans.slots_holding(0x11000, 0x11800), vec![1, 2]);
assert_eq!(spans.slots_holding(0x20000, 0x21000), vec![2]);
assert_eq!(spans.slots_in(0x20000, 0x21000), vec![2]);
spans.forget(2);
assert!(spans.slots_in(0x20000, 0x21000).is_empty());
assert_eq!(spans.slots_in(0x0, 0x10001), vec![1]);
}
/// A texture filled with less than was mapped is looked at again, but
/// not for ever: memory that is shown and cannot be read ends it, and a
/// filling that got everything starts the count anew.
@@ -3235,9 +3425,9 @@ mod tests {
// A 512x512 texture of 0x56000 bytes viewed from levels 3 and 1,
// and the next one 0x6000 after it (run 188).
spans.note(1, 0x1_0000, 0x1_0000 + 0x5_6000, true);
spans.shows(1, 3);
spans.shows(1, 3, u64::MAX);
spans.note(2, 0x1_0000, 0x1_0000 + 0x5_6000, true);
spans.shows(2, 1);
spans.shows(2, 1, u64::MAX);
spans.note(3, 0x1_6000, 0x1_6000 + 0x5_6000, true);
assert_eq!(spans.next_start(0x1_0000, 0x1_0000 + 0x5_6000), Some(0x1_6000));
assert_eq!(spans.next_start(0x1_6000, 0x1_6000 + 0x5_6000), None);