diff --git a/d3d12/crates/leonida-gpu/src/descriptor/host.rs b/d3d12/crates/leonida-gpu/src/descriptor/host.rs index b06a80206d..02e18d8316 100644 --- a/d3d12/crates/leonida-gpu/src/descriptor/host.rs +++ b/d3d12/crates/leonida-gpu/src/descriptor/host.rs @@ -312,6 +312,15 @@ pub(crate) struct HostHeap { /// Last remap the heap's texture pass took ([`RemapQueue`]); the lock /// also serializes its passes. Ported from fxceless/victory-fork 42be495. remap_cursor: Mutex, + /// Whether this heap has its list in [`PAGE_WRITTEN`] (from its first + /// placed texture on; taken out when the heap goes). + page_writes_kept: std::sync::atomic::AtomicBool, + /// Physical-page writes retained for one-shot decoration/ped texture + /// bakes when `LEONIDA_PAGE_WRITES` bit 2 is off (the default): normal + /// frames stay free of queue flooding while every bake still sees + /// freshly DirectStorage-streamed texture bytes immediately instead of + /// waiting on `RECHECK_FRAMES`. This heap's own, as [`PAGE_WRITTEN`]. + bake_page_written: Mutex>, /// The shortest table that found no run of free slots since slots were /// last given back (changed under `next`'s lock; `u32::MAX`: none). /// Until something is given back the free runs only shrink, so a table @@ -1010,15 +1019,17 @@ impl Spans { /// Title memory written behind the guards' back whose bytes are there /// (DirectStorage's reads into physical pages, at the addresses the title /// has them mapped; `crate::watch::title_memory_written`), since a texture -/// pass last took the ranges ([`page_written`]). Kept apart from -/// [`REMAPPED`] so that what such writes do is counted by itself. -static PAGE_WRITTEN: Mutex> = Mutex::new(Vec::new()); -/// Physical-page writes retained specifically for one-shot decoration/ped -/// texture bakes when `LEONIDA_PAGE_WRITES` bit 2 is off (the default): normal -/// frames stay free of queue flooding while every bake still sees freshly -/// DirectStorage-streamed texture bytes immediately instead of waiting on -/// `RECHECK_FRAMES`. -static BAKE_PAGE_WRITTEN: Mutex> = Mutex::new(Vec::new()); +/// pass last took the ranges ([`page_written`]), for each view heap that +/// holds placed textures (by its `lease_id`). Kept apart from [`REMAPPED`] +/// so that what such writes do is counted by itself. +/// +/// Each heap has its own list: the title binds more than one view heap, +/// and with one list the first heap's pass took the ranges, so the placed +/// textures of the others kept the bytes from before (as [`RemapQueue`] +/// fixed for the remapped ranges). After TaynanGT's fix in +/// TaynanGT/Vice-Tay (bc44a9f). +static PAGE_WRITTEN: Mutex>> = + Mutex::new(std::collections::BTreeMap::new()); /// Since the start: such ranges kept for a pass; ranges that were in the /// list when it was full and was made smaller ([`compact`]: none is dropped); @@ -1048,19 +1059,21 @@ pub(crate) fn page_written(at: usize, len: usize) { if end <= SPANNED[0].load(Relaxed) || start >= SPANNED[1].load(Relaxed) { return; } - let mut ranges = PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner()); + let mut heaps = PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner()); PAGE_WRITES[0].fetch_add(1, Relaxed); - if let Some(last) = ranges.last_mut() - && last.1 == start - { - last.1 = end; - return; - } - if ranges.len() >= MOST { - PAGE_WRITES[1].fetch_add(ranges.len() as u64, Relaxed); - compact(&mut ranges, MOST); + for ranges in heaps.values_mut() { + if let Some(last) = ranges.last_mut() + && last.1 == start + { + last.1 = end; + continue; + } + if ranges.len() >= MOST { + PAGE_WRITES[1].fetch_add(ranges.len() as u64, Relaxed); + compact(ranges, MOST); + } + ranges.push((start, end)); } - ranges.push((start, end)); } /// Title memory the runtime mapped, unmapped or is about to write, kept for @@ -2280,6 +2293,14 @@ thread_local! { static IN_BUFFER_PASS: std::cell::Cell = const { std::cell::Cell::new(false) }; } +impl Drop for HostHeap { + fn drop(&mut self) { + if *self.page_writes_kept.get_mut() { + PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner()).remove(&self.lease_id); + } + } +} + impl HostHeap { pub(crate) fn new(sampler: bool, count: u32) -> Option { let host = host()?; @@ -2413,6 +2434,8 @@ impl HostHeap { NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) }, remap_cursor: Mutex::new(REMAPPED.lock().unwrap_or_else(|e| e.into_inner()).latest), + page_writes_kept: std::sync::atomic::AtomicBool::new(false), + bake_page_written: Mutex::default(), too_long: std::sync::atomic::AtomicU32::new(u32::MAX), strides, buffers: Slots::new(count as usize), @@ -2678,14 +2701,19 @@ impl HostHeap { /// whether memory was noted since a pass last took the notes. pub(crate) fn texture_news(&self) -> (u64, bool) { let writes = self.textures.lock().unwrap_or_else(|e| e.into_inner()).writes; - let noted = |ranges: &Mutex>| !ranges.lock().unwrap_or_else(|e| e.into_inner()).is_empty(); + let written = PAGE_WRITTEN + .lock() + .unwrap_or_else(|e| e.into_inner()) + .get(&self.lease_id) + .is_some_and(|ranges| !ranges.is_empty()); + let baked = !self.bake_page_written.lock().unwrap_or_else(|e| e.into_inner()).is_empty(); let cursor = if crate::toggled!("LEONIDA_REMAP_HISTORY") { *self.remap_cursor.lock().unwrap_or_else(|e| e.into_inner()) } else { *REMAP_SHARED_CURSOR.lock().unwrap_or_else(|e| e.into_inner()) }; let remapped = REMAPPED.lock().unwrap_or_else(|e| e.into_inner()).latest != cursor; - (writes, remapped || noted(&PAGE_WRITTEN) || noted(&BAKE_PAGE_WRITTEN)) + (writes, remapped || written || baked) } /// Every title slot of the queue that is due at `frame`, and whether @@ -2891,14 +2919,19 @@ impl HostHeap { /// are left to their draws. Counted either way; returns how many slots /// it made due. fn texture_slots_page_written(&self, frame: u64) -> usize { - let ranges = std::mem::take(&mut *PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner())); + let ranges = PAGE_WRITTEN + .lock() + .unwrap_or_else(|e| e.into_inner()) + .get_mut(&self.lease_id) + .map(std::mem::take) + .unwrap_or_default(); if ranges.is_empty() { return 0; } let due = crate::watch::page_writes() & 2 != 0; let made_due = self.slots_written_under(&ranges, frame, due)[2]; if !due { - let mut kept = BAKE_PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner()); + let mut kept = self.bake_page_written.lock().unwrap_or_else(|e| e.into_inner()); kept.extend(ranges); if kept.len() > 256 { join(&mut kept); @@ -2916,7 +2949,7 @@ impl HostHeap { /// writes (DirectStorage completions) immediately (`due = frame`) right /// before a one-shot ped/decoration bake draw. pub(crate) fn texture_slots_bake_written(&self, frame: u64) -> usize { - let mut ranges = std::mem::take(&mut *BAKE_PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner())); + let mut ranges = std::mem::take(&mut *self.bake_page_written.lock().unwrap_or_else(|e| e.into_inner())); if ranges.is_empty() { return 0; } @@ -3739,6 +3772,9 @@ impl HostHeap { }); match span { Some((start, end, first, held)) => { + if !self.page_writes_kept.swap(true, Relaxed) { + PAGE_WRITTEN.lock().unwrap_or_else(|e| e.into_inner()).entry(self.lease_id).or_default(); + } SPANNED[0].fetch_min(start, Relaxed); SPANNED[1].fetch_max(end, Relaxed); placed.rule = stale_rule(); @@ -7744,6 +7780,42 @@ void BY_FLOATS_OR_NOT(uint3 id : SV_DispatchThreadID) { assert_eq!(slots(62), [1]); } + /// A write behind the guards' back reaches every view heap's texture + /// pass, and every heap's bake: the title binds more than one, and the + /// first heap's pass took the ranges for all of them. After TaynanGT's + /// fix in TaynanGT/Vice-Tay (bc44a9f). + #[test] + fn a_page_write_reaches_each_view_heap() { + // 64x64 BC7, one mip, uploaded, at an address no test and no title uses. + let uploaded = |slot: u32, at: u64| -> Option { + let heap = HostHeap::new(false, 8)?; + let mut desc = [0u32; 16]; + (desc[10], desc[11]) = (at as u32, (at >> 32) as u32); + let view = View::Placed(false, [3, 0, 0, 0, 64, 0, 64, 0x1_0001, 98, 1, 0, 0x105, 0, 0], desc); + assert!(heap.note_buffer(slot, Some(&view), false).is_empty()); + let (queued, write) = heap.texture_slots_due(u64::MAX, 8).0[0]; + assert_eq!(queued, slot); + heap.texture_slot_done(slot, write, None); + Some(heap) + }; + let at = 0x7fee_0000_0000u64; + let (Some(first), Some(second)) = (uploaded(1, at), uploaded(3, at)) else { + return; // no host GPU + }; + page_written(at as usize, 0x1000); + for heap in [&first, &second] { + heap.texture_slots_page_written(50); + } + for (heap, slot) in [(&first, 1), (&second, 3)] { + if crate::watch::page_writes() & 2 == 0 { + // Counted only: the bake before a one-shot draw makes it due. + assert_eq!(heap.texture_slots_bake_written(51), 1, "slot {slot}'s bake"); + } + let due: Vec = heap.texture_slots_due(52, 8).0.into_iter().map(|d| d.0).collect(); + assert_eq!(due, [slot]); + } + } + /// `LEONIDA_STALE_TEXTURES=0`: nothing is ever stale, and a view that /// overlaps others is left out of the spans, as before. #[test]