From f80b12899845674df6652a8004aa7150d8bbeb1e Mon Sep 17 00:00:00 2001 From: gdemontalivet Date: Thu, 26 Nov 2026 21:11:39 +0000 Subject: [PATCH] perf(gpu): lock-free heap sync scan and batched template descriptor mirroring Cut submission-time descriptor heap sync overhead (`sync_heaps` / `sync_slots_bytes`) across large dirty page ranges and repeated records: - Add a lock-free 2-level generation mirror (`VIEW_GENS`) and batched `views_at_batch()` in `descriptor.rs`, plus a thread-local reusable page read buffer in `sync_heaps()`. - Add a lock-free `synced` mirror of `made` on `HostHeap` (covering placed textures left null and evicted slots whose `ready` entry is 0), and scan `sync_slots_bytes` per 128-slot block and 64-slot `made_nonzero` word with zero lock acquisitions or heap allocations on clean slots. - Add `mirror_synced_batch()` with a lazy CPU-only staging heap (`D3D12_DESCRIPTOR_HEAP_FLAG_NONE`) pre-populated with all 17 null SRV/UAV/sampler kinds plus per-batch template slots for repeated keys, flushing descriptor copies in a single `CopyDescriptors` call per page and batching `made`, `kept` (`keep_batch` with same-resource fast path), `viewed` (`Viewed::set_batch` with pointer-keyed `Seen` dedup), `shadow::forget_reads`, and `host::retire_all` under one lock hold each. - Gate the optimized path behind `VICTORY_FAST_HEAP_SYNC` (default on; set `=0` for legacy A/B comparison). - Add unit tests covering `view_gen_current`, `views_at_batch`, placed-null lock-free skips, duplicate template copies, immediate `forget()` invalidation, concurrent rewrites during `prepare()`, and a synthetic 1.5M-slot fast-vs-legacy benchmark. --- crates/victory-gpu/src/bindless.rs | 41 + crates/victory-gpu/src/descriptor.rs | 177 +++- crates/victory-gpu/src/descriptor/host.rs | 1105 ++++++++++++++++++++- crates/victory-gpu/src/host.rs | 11 + crates/victory-gpu/src/shadow.rs | 12 + 5 files changed, 1300 insertions(+), 46 deletions(-) diff --git a/crates/victory-gpu/src/bindless.rs b/crates/victory-gpu/src/bindless.rs index 44224027..e0f2281c 100644 --- a/crates/victory-gpu/src/bindless.rs +++ b/crates/victory-gpu/src/bindless.rs @@ -141,6 +141,47 @@ impl Viewed { } } + /// Batched [`Viewed::set`]: computes each unique view's entry outside the + /// lock (deduplicating repeated view pointers) and applies all slot + /// updates under one hold of `self.inner`. + #[cfg(windows)] + pub(crate) fn set_batch(&self, updates: &[(u32, Option<&View>, bool)]) { + if updates.is_empty() { + return; + } + let mut cache: HashMap<(usize, bool), Option> = HashMap::new(); + let mut entries: Vec<(u32, Option)> = Vec::with_capacity(updates.len()); + for &(index, view, made) in updates { + let entry = match view { + None => None, + Some(v) => { + let key = (std::ptr::from_ref(v) as usize, made); + cache + .entry(key) + .or_insert_with(|| { + if made { + seen(v) + } else { + Some(Seen::Left(left_kind(v))) + } + }) + .clone() + } + }; + entries.push((index, entry)); + } + let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner()); + for (index, entry) in entries { + if let Some(old) = inner.slots.remove(&index) { + inner.count(&old, false); + } + if let Some(entry) = entry { + inner.count(&entry, true); + inner.slots.insert(index, entry); + } + } + } + /// The buffer ranges views reach, merged per host copy and kind /// (shader resource, unordered access): copy, start, bytes. #[cfg(any(test, feature = "diagnostics"))] diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 8e63b4e9..5bad8bf1 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -239,8 +239,15 @@ pub(crate) fn sync_heaps() { // 650,000 slots checked a frame, 29 submissions). A heap is not dropped // while its page is made: `Heap::drop` takes the write lock to leave the // list, so a heap still listed under the lock is alive. + #[cfg(windows)] + thread_local! { + static SYNC_PAGE_BUF: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } let page_size = crate::watch::PAGE; - let mut page_buf = vec![0u8; page_size]; + let mut page_buf = SYNC_PAGE_BUF.with(|b| std::mem::take(&mut *b.borrow_mut())); + if page_buf.len() < page_size { + page_buf.resize(page_size, 0); + } for (start, at, ranges) in work { for (from, to) in ranges { for page in (from & !(page_size - 1)..to).step_by(page_size) { @@ -282,6 +289,9 @@ pub(crate) fn sync_heaps() { } } } + SYNC_PAGE_BUF.with(|b| { + *b.borrow_mut() = page_buf; + }); } /// Makes the views of slots `slots` that were left null (textures the title @@ -629,6 +639,8 @@ fn record(handle: u64, view: View) { views.list.len() as u32 - 1 } }; + let generation = views.list[i as usize].1; + set_view_gen(i, generation); for r in view.resources().into_iter().filter(|&r| r != 0) { views.by_resource.entry(r).or_default().push(i); } @@ -644,6 +656,50 @@ fn record(handle: u64, view: View) { mirror(handle); } +/// Lock-free mirror of each recorded view entry's active generation (0 when +/// free), chunked in 65,536-entry blocks up to 16M entries. Updated under +/// `VIEWS.write()` before publishing records or bumping `VIEWS_FORGOT`, so +/// heap syncs can check record liveness without locking `VIEWS`. +const VIEW_GEN_CHUNK_SHIFT: u32 = 16; +const VIEW_GEN_CHUNK_LEN: usize = 1 << VIEW_GEN_CHUNK_SHIFT; +const VIEW_GEN_CHUNKS: usize = 256; +static VIEW_GENS: [std::sync::OnceLock>; VIEW_GEN_CHUNKS] = + [const { std::sync::OnceLock::new() }; VIEW_GEN_CHUNKS]; + +fn set_view_gen(i: u32, generation: u32) { + let chunk = (i >> VIEW_GEN_CHUNK_SHIFT) as usize; + let offset = (i as usize) & (VIEW_GEN_CHUNK_LEN - 1); + if let Some(slot) = VIEW_GENS.get(chunk) { + let table = slot.get_or_init(|| { + (0..VIEW_GEN_CHUNK_LEN) + .map(|_| std::sync::atomic::AtomicU32::new(0)) + .collect() + }); + table[offset].store(generation, std::sync::atomic::Ordering::Release); + } +} + +/// Lock-free check of whether view entry `i` is still live at `generation`. +#[cfg(any(windows, test))] +pub(crate) fn view_gen_current(i: u32, generation: u32) -> bool { + if generation == 0 { + return false; + } + let chunk = (i >> VIEW_GEN_CHUNK_SHIFT) as usize; + let offset = (i as usize) & (VIEW_GEN_CHUNK_LEN - 1); + if let Some(slot) = VIEW_GENS.get(chunk) { + slot.get().is_some_and(|table| { + table[offset].load(std::sync::atomic::Ordering::Acquire) == generation + }) + } else { + let views = VIEWS.read().unwrap_or_else(|e| e.into_inner()); + views + .list + .get(i as usize) + .is_some_and(|(_, current)| *current == generation) + } +} + /// Bumped whenever [`forget`] invalidates a recorded view while holding `VIEWS`. static VIEWS_FORGOT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -668,6 +724,7 @@ pub(crate) fn forget(resource: usize) { } views.index.remove(&view); views.list[i as usize].1 = generation.wrapping_add(1).max(1); + set_view_gen(i, 0); // The view's other resource (a counter) keeps a stale entry, which // `forget` of that resource skips. views.free.push(i); @@ -751,8 +808,55 @@ fn view_at(i: u32, generation: u32) -> Option { all.then_some(pin) } +/// Batched [`view_at`] for a slice of packed `(generation << 32 | index)` keys +/// under one shared hold of `VIEWS`. Any partial pin whose other resource is +/// already dying is dropped after releasing `VIEWS`. +#[cfg(any(windows, test))] +pub(crate) fn views_at_batch(keys: &[u64]) -> Vec> { + let mut results = Vec::with_capacity(keys.len()); + let mut failed = Vec::new(); + { + let views = VIEWS.read().unwrap_or_else(|e| e.into_inner()); + for &key in keys { + let i = key as u32; + let generation = (key >> 32) as u32; + let Some((view, current)) = views.list.get(i as usize) else { + results.push(None); + continue; + }; + if *current != generation { + results.push(None); + continue; + } + let view = view.clone(); + let mut held = [0usize; 2]; + let mut all = true; + for (slot, r) in view.resources().into_iter().enumerate() { + if r == 0 { + continue; + } + if crate::object::try_add_ref(r as This) { + held[slot] = r; + } else { + all = false; + } + } + let pin = ViewPin { view, held }; + if all { + results.push(Some(pin)); + } else { + failed.push(pin); + results.push(None); + } + } + } + drop(failed); + results +} + /// For each record (tag, index, generation): whether it names a view that /// is current, as [`view_at`] would find it (one lock for them all). +#[cfg(windows)] fn views_current_into(records: &[[u32; 3]], out: &mut Vec) { out.clear(); let views = VIEWS.read().unwrap_or_else(|e| e.into_inner()); @@ -1442,4 +1546,75 @@ pub(crate) mod tests { assert!(queued, "the descriptor write reached the heap at the reused address"); } } + + #[test] + fn view_gen_current_tracks_record_forget_and_reuse() { + #[repr(C, align(8))] + struct AlignedDesc([u32; 14]); + let mut slot_a = [0u32; 4]; + let mut slot_b = [0u32; 4]; + let mut res_a = std::ptr::null(); + let mut res_b = std::ptr::null(); + let desc = AlignedDesc([1, 0, 0, 0, 64, 0, 1, 1 | 1 << 16, 0, 1, 0, 0, 0, 0]); + let properties = [1u32, 0, 0, 0, 0]; + let iid = crate::object::guid( + 0x696442be, + 0xa72e, + 0x4059, + [0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad], + ); + assert_eq!( + crate::resource::create_committed( + std::ptr::null(), + properties.as_ptr().cast(), + 0, + desc.0.as_ptr().cast(), + 0, + std::ptr::null(), + &iid, + &mut res_a, + ), + S_OK + ); + assert_eq!( + crate::resource::create_committed( + std::ptr::null(), + properties.as_ptr().cast(), + 0, + desc.0.as_ptr().cast(), + 0, + std::ptr::null(), + &iid, + &mut res_b, + ), + S_OK + ); + + create_srv(std::ptr::null(), res_a, std::ptr::null(), slot_a.as_mut_ptr() as u64); + let [tag, idx, gen_a] = [slot_a[0], slot_a[1], slot_a[2]]; + assert_eq!(tag, VIEW_TAG); + assert!(gen_a >= 1); + assert!(view_gen_current(idx, gen_a)); + assert!(!view_gen_current(idx, 0)); + assert!(!view_gen_current(idx, gen_a.wrapping_add(1).max(1))); + + let key_a = u64::from(gen_a) << 32 | u64::from(idx); + let batch = views_at_batch(&[key_a, 0]); + assert!(batch[0].is_some()); + assert!(batch[1].is_none()); + drop(batch); + + forget(res_a as usize); + assert!(!view_gen_current(idx, gen_a)); + assert!(views_at_batch(&[key_a])[0].is_none()); + + create_srv(std::ptr::null(), res_b, std::ptr::null(), slot_b.as_mut_ptr() as u64); + let [tag_b, idx_b, gen_b] = [slot_b[0], slot_b[1], slot_b[2]]; + assert_eq!(tag_b, VIEW_TAG); + assert!(view_gen_current(idx_b, gen_b)); + assert!(!view_gen_current(idx, gen_a)); + + crate::object::release(res_a); + crate::object::release(res_b); + } } diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index fa2c471e..b7077df7 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -13,11 +13,178 @@ use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; use super::paged::Paged; -use super::{VIEW_TAG, View, ViewPin, view_at, views_current_into, views_forgot}; +use super::{ + VIEW_TAG, View, ViewPin, view_at, view_gen_current, views_at_batch, views_current_into, + views_forgot, +}; use crate::host::host; const SYNC_BLOCK_SLOTS: u32 = 128; +/// Pre-populated null descriptor templates in [`StagingHeap`] (slot 0 for +/// sampler heaps; slots `0..17` covering every null SRV/UAV kind for view +/// heaps). Remaining slots hold per-batch templates for repeated views. +const STAGING_NULL_SLOTS: u32 = 17; +const STAGING_TOTAL_SLOTS: u32 = 256; + +fn fast_heap_sync_value(value: Option<&std::ffi::OsStr>) -> bool { + value != Some(std::ffi::OsStr::new("0")) +} + +fn fast_heap_sync() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| fast_heap_sync_value(std::env::var_os("VICTORY_FAST_HEAP_SYNC").as_deref())) +} + +/// CPU-only (`D3D12_DESCRIPTOR_HEAP_FLAG_NONE`) staging heap holding null +/// templates (`0..STAGING_NULL_SLOTS`) and scratch template slots for +/// `CopyDescriptorsSimple` during batched heap syncs. +struct StagingHeap { + _heap: ID3D12DescriptorHeap, + start: usize, + step: u32, + kind: D3D12_DESCRIPTOR_HEAP_TYPE, + count: u32, +} + +impl StagingHeap { + fn new(sampler: bool, step: u32) -> Option { + let host = host()?; + let kind = if sampler { + D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER + } else { + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV + }; + // SAFETY: CPU-only staging descriptor heap; valid source for + // `CopyDescriptorsSimple` into shader-visible heaps. + let heap: ID3D12DescriptorHeap = unsafe { + host.device + .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: kind, + NumDescriptors: STAGING_TOTAL_SLOTS, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_NONE, + NodeMask: 0, + }) + } + .ok()?; + // SAFETY: live heap. + let start = unsafe { heap.GetCPUDescriptorHandleForHeapStart() }.ptr; + let staging = Self { + _heap: heap, + start, + step, + kind, + count: STAGING_TOTAL_SLOTS, + }; + if sampler { + null_sampler(staging.cpu(0)); + } else { + null_view(staging.cpu(0)); + null_buffer_view(staging.cpu(1)); + for dim in 2..=10u32 { + let mut words = [0u32; 10]; + words[1] = dim; + null_like(&words, staging.cpu(dim)); + } + let buf_uav = D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: DXGI_FORMAT_R32_UINT, + ViewDimension: D3D12_UAV_DIMENSION_BUFFER, + ..Default::default() + }; + let uav_1d = null_uav_desc(Some(D3D12_UAV_DIMENSION_TEXTURE1D.0 as u32)); + let uav_1d_arr = null_uav_desc(Some(D3D12_UAV_DIMENSION_TEXTURE1DARRAY.0 as u32)); + let uav_2d_arr = null_uav_desc(Some(D3D12_UAV_DIMENSION_TEXTURE2DARRAY.0 as u32)); + let uav_3d = null_uav_desc(Some(D3D12_UAV_DIMENSION_TEXTURE3D.0 as u32)); + let uav_2d = null_uav_desc(None); + // SAFETY: null resources with explicit descriptions in our staging heap. + unsafe { + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&buf_uav), + staging.cpu(11), + ); + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&uav_1d), + staging.cpu(12), + ); + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&uav_1d_arr), + staging.cpu(13), + ); + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&uav_2d_arr), + staging.cpu(14), + ); + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&uav_3d), + staging.cpu(15), + ); + host.device.CreateUnorderedAccessView( + None::<&ID3D12Resource>, + None::<&ID3D12Resource>, + Some(&uav_2d), + staging.cpu(16), + ); + } + } + Some(staging) + } + + fn cpu(&self, index: u32) -> D3D12_CPU_DESCRIPTOR_HANDLE { + D3D12_CPU_DESCRIPTOR_HANDLE { + ptr: self.start + index as usize * self.step as usize, + } + } +} + +/// Slot index in [`StagingHeap`] matching [`null_for`] / [`null_view`] / +/// [`null_sampler`]. +fn null_slot_index(sampler: bool, view: Option<&View>) -> u32 { + if sampler { + return 0; + } + let Some(view) = view else { + return 0; + }; + let is_buffer = |r: &usize| crate::resource::buffer_bytes(*r as crate::object::This).is_some(); + let (buffer, dimension) = match view { + View::Unordered(r, _, desc) => ( + is_buffer(r) || desc.is_some_and(|d| d[1] == 1), + desc.map(|d| d[1]), + ), + View::Placed(true, resource, desc) => (resource[0] == 1, Some(desc[1])), + View::Shader(r, desc) if is_buffer(r) || desc.is_some_and(|d| d[1] == 1) => return 1, + View::Placed(false, resource, _) if resource[0] == 1 => return 1, + View::Shader(_, Some(desc)) => { + return if (2..=10).contains(&desc[1]) { desc[1] } else { 0 }; + } + View::Placed(false, _, desc) => { + return if (2..=10).contains(&desc[1]) { desc[1] } else { 0 }; + } + View::Constant(..) => return 1, + _ => return 0, + }; + if buffer { + return 11; + } + match dimension.map(|d| D3D12_UAV_DIMENSION(d as i32)) { + Some(D3D12_UAV_DIMENSION_TEXTURE1D) => 12, + Some(D3D12_UAV_DIMENSION_TEXTURE1DARRAY) => 13, + Some(D3D12_UAV_DIMENSION_TEXTURE2DARRAY) => 14, + Some(D3D12_UAV_DIMENSION_TEXTURE3D) => 15, + _ => 16, + } +} + pub(crate) struct HostHeap { pub(crate) heap: ID3D12DescriptorHeap, cpu: usize, @@ -30,6 +197,10 @@ pub(crate) struct HostHeap { sampler: bool, /// The record (view index and generation) each slot was last made from. made: Mutex>, + /// Lock-free mirror of `made`, including texture views left null until + /// drawn or evicted: published last under `made` and cleared first, so + /// heap syncs skip already-synced slots without locking `made` or `VIEWS`. + synced: Box<[AtomicU64]>, /// One bit per slot: whether `made[index] != 0`, so empty slots skip /// `made` and `VIEWS` locks during `sync_slots`. made_nonzero: Box<[AtomicU64]>, @@ -41,6 +212,9 @@ pub(crate) struct HostHeap { /// title thread binding or writing views would otherwise wait on. Set /// last, under that lock, once the slot is made; cleared first. ready: Box<[AtomicU64]>, + /// CPU-only staging heap for null and repeated-view template copies during + /// batched heap syncs, created on first use. + staging: std::sync::OnceLock>, /// The host resource each slot views, held while the slot does: shaders /// index heaps directly, so a slot the title has not rewritten since it /// released the resource may still be read, and a freed host resource @@ -233,6 +407,7 @@ impl HostHeap { dynamic: extra, sampler, made: Mutex::new(vec![0; count as usize]), + synced: (0..count).map(|_| AtomicU64::new(0)).collect(), made_nonzero: (0..(count as usize).div_ceil(64)) .map(|_| AtomicU64::new(0)) .collect(), @@ -240,6 +415,7 @@ impl HostHeap { .map(|_| AtomicU64::new(0)) .collect(), ready: (0..count).map(|_| AtomicU64::new(0)).collect(), + staging: std::sync::OnceLock::new(), kept: Mutex::new(Paged::new((count + extra) as usize)), nulled_unordered: Mutex::new(Vec::new()), next: Mutex::new((0, vec![false; extra as usize])), @@ -639,6 +815,81 @@ impl HostHeap { } } + /// Batched [`HostHeap::keep`]: applies all slot updates under one hold of + /// `self.kept` and pushes displaced host resources into `retired` for the + /// caller to retire outside locks. + fn keep_batch(&self, updates: Vec<(u32, Kept)>, retired: &mut Vec) { + if updates.is_empty() { + return; + } + let mut widened_changes: Vec<(std::sync::Arc, bool)> = Vec::new(); + { + let mut kept = self.kept.lock().unwrap_or_else(|e| e.into_inner()); + for (index, resource) in updates { + let Some(slot) = kept.slot(index as usize, resource.is_some()) else { + continue; + }; + if let Some((_, Some(HeldSource::Widened(view, _)))) = slot.as_ref() { + widened_changes.push((view.clone(), false)); + } + if let Some((_, Some(HeldSource::Widened(view, _)))) = resource.as_ref() { + widened_changes.push((view.clone(), true)); + } + let same_resource = match (slot.as_ref(), resource.as_ref()) { + (Some((cur, _)), Some((new, _))) => { + windows::core::Interface::as_raw(cur) + == windows::core::Interface::as_raw(new) + } + _ => false, + }; + if same_resource { + if let (Some(cur), Some((_, new_src))) = (slot.as_mut(), resource) { + cur.1 = new_src; + } + continue; + } + let old = std::mem::replace(slot, resource); + #[cfg(feature = "diagnostics")] + if let Some((old, _)) = &old + && !self.sampler + && slot.as_ref().map(|s| windows::core::Interface::as_raw(&s.0)) + != Some(windows::core::Interface::as_raw(old)) + // SAFETY: a live resource. + && unsafe { old.GetDesc() }.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER + { + use windows::core::Interface; + let left = crate::resource::life::holders(old); + crate::resource::life::note( + old.as_raw() as usize, + crate::resource::life::SLOT, + u64::from(index), + left, + ); + } + if let Some((old, _)) = old { + retired.push(old); + } + } + } + if !widened_changes.is_empty() { + let mut widened = self.widened.lock().unwrap_or_else(|e| e.into_inner()); + for (view, add) in widened_changes { + let key = std::sync::Arc::as_ptr(&view) as usize; + if add { + let entry = widened + .entry(key) + .or_insert_with(|| (0, std::sync::Arc::downgrade(&view))); + entry.0 += 1; + } else if let Some((count, _)) = widened.get_mut(&key) { + *count = count.saturating_sub(1); + if *count == 0 { + widened.remove(&key); + } + } + } + } + } + fn note_widened(&self, view: Option<&std::sync::Arc>, add: bool) { let Some(view) = view else { return }; let key = std::sync::Arc::as_ptr(view) as usize; @@ -824,7 +1075,26 @@ impl HostHeap { buf_start: u64, buf: &[u8], ) { - let last = last.min(self.count); + self.sync_slots_bytes_with(first..last, start, step, buf_start, buf, fast_heap_sync()); + } + + fn staging(&self) -> Option<&StagingHeap> { + self.staging + .get_or_init(|| StagingHeap::new(self.sampler, self.step)) + .as_ref() + } + + fn sync_slots_bytes_with( + &self, + slots: std::ops::Range, + start: u64, + step: u64, + buf_start: u64, + buf: &[u8], + fast: bool, + ) { + let first = slots.start; + let last = slots.end.min(self.count); if first >= last { return; } @@ -832,56 +1102,157 @@ impl HostHeap { let forgot = views_forgot(); let b_first = first / SYNC_BLOCK_SLOTS; let b_last = (last - 1) / SYNC_BLOCK_SLOTS; - let blocks_verified = (b_first..=b_last) - .all(|b| self.block_forgot[b as usize].load(Ordering::Acquire) == forgot); - let buf_end = buf_start.saturating_add(buf.len() as u64); - let mut stale: Vec = Vec::new(); - let mut candidates: Vec<(u32, [u32; 3])> = Vec::new(); - - for i in first..last { - let addr = at(i); - let record = if addr >= buf_start && addr.saturating_add(12) <= buf_end { - let off = (addr - buf_start) as usize; - // SAFETY: `off + 12 <= buf.len()`, unaligned copy of 12 bytes. - unsafe { buf.as_ptr().add(off).cast::<[u32; 3]>().read_unaligned() } - } else { - // SAFETY: record in title heap memory the caller found mapped. - unsafe { (addr as *const [u32; 3]).read_unaligned() } - }; - let [tag, view, generation] = record; - if tag != VIEW_TAG { - if self.made_is_nonzero(i) { - stale.push(i); + + if fast { + let mut stale: Vec<(u32, [u32; 3])> = Vec::new(); + let all_in_buf = + at(first) >= buf_start && at(last - 1).saturating_add(12) <= buf_end; + let step_usize = step as usize; + for b in b_first..=b_last { + let block_verified = + self.block_forgot[b as usize].load(Ordering::Acquire) == forgot; + let s_first = (b * SYNC_BLOCK_SLOTS).max(first); + let s_last = ((b + 1) * SYNC_BLOCK_SLOTS).min(last); + // Load `made_nonzero` once per 64-slot word so empty slots avoid + // per-slot Acquire atomic loads. + let mut i = s_first; + while i < s_last { + let word_idx = (i / 64) as usize; + let word_end = ((word_idx as u32 + 1) * 64).min(s_last); + let nonzero_word = self.made_nonzero[word_idx].load(Ordering::Acquire); + if all_in_buf { + let mut ptr = buf + .as_ptr() + .wrapping_add((at(i) - buf_start) as usize); + while i < word_end { + // SAFETY: `all_in_buf` checks `[at(first), at(last - 1) + 12)` + // lies inside `[buf_start, buf_start + buf.len())`. + let record = unsafe { ptr.cast::<[u32; 3]>().read_unaligned() }; + ptr = ptr.wrapping_add(step_usize); + let [tag, view, generation] = record; + if tag != VIEW_TAG || generation == 0 { + if (nonzero_word >> (i & 63)) & 1 != 0 { + stale.push((i, record)); + } + i += 1; + continue; + } + let key = u64::from(generation) << 32 | u64::from(view); + let synced = self.synced[i as usize].load(Ordering::Acquire); + if synced == key { + if !block_verified && !view_gen_current(view, generation) { + stale.push((i, record)); + } + i += 1; + continue; + } + if view_gen_current(view, generation) + || synced != 0 + || (nonzero_word >> (i & 63)) & 1 != 0 + { + stale.push((i, record)); + } + i += 1; + } + } else { + while i < word_end { + let addr = at(i); + let record = + if addr >= buf_start && addr.saturating_add(12) <= buf_end { + let off = (addr - buf_start) as usize; + // SAFETY: `off + 12 <= buf.len()`, unaligned copy of 12 bytes. + unsafe { + buf.as_ptr().add(off).cast::<[u32; 3]>().read_unaligned() + } + } else { + // SAFETY: record in title heap memory the caller found mapped. + unsafe { (addr as *const [u32; 3]).read_unaligned() } + }; + let [tag, view, generation] = record; + if tag != VIEW_TAG || generation == 0 { + if (nonzero_word >> (i & 63)) & 1 != 0 { + stale.push((i, record)); + } + i += 1; + continue; + } + let key = u64::from(generation) << 32 | u64::from(view); + let synced = self.synced[i as usize].load(Ordering::Acquire); + if synced == key { + if !block_verified && !view_gen_current(view, generation) { + stale.push((i, record)); + } + i += 1; + continue; + } + if view_gen_current(view, generation) + || synced != 0 + || (nonzero_word >> (i & 63)) & 1 != 0 + { + stale.push((i, record)); + } + i += 1; + } + } } - continue; } - let key = u64::from(generation) << 32 | u64::from(view); - if blocks_verified && self.ready[i as usize].load(Ordering::Acquire) == key { - continue; + if !stale.is_empty() { + self.mirror_synced_batch(&stale, at); } - candidates.push((i, record)); - } - - if !candidates.is_empty() { - let records: Vec<[u32; 3]> = candidates.iter().map(|&(_, r)| r).collect(); - let mut current = Vec::with_capacity(records.len()); - views_current_into(&records, &mut current); - let made = self.made.lock().unwrap_or_else(|e| e.into_inner()); - for (&(i, [_, view, generation]), is_current) in candidates.iter().zip(current) { - let changed = match made.get(i as usize) { - Some(&m) if is_current => m != (u64::from(generation) << 32 | u64::from(view)), - Some(&m) => m != 0, - None => true, + } else { + let blocks_verified = (b_first..=b_last) + .all(|b| self.block_forgot[b as usize].load(Ordering::Acquire) == forgot); + + let mut stale: Vec = Vec::new(); + let mut candidates: Vec<(u32, [u32; 3])> = Vec::new(); + + for i in first..last { + let addr = at(i); + let record = if addr >= buf_start && addr.saturating_add(12) <= buf_end { + let off = (addr - buf_start) as usize; + // SAFETY: `off + 12 <= buf.len()`, unaligned copy of 12 bytes. + unsafe { buf.as_ptr().add(off).cast::<[u32; 3]>().read_unaligned() } + } else { + // SAFETY: record in title heap memory the caller found mapped. + unsafe { (addr as *const [u32; 3]).read_unaligned() } }; - if changed { - stale.push(i); + let [tag, view, generation] = record; + if tag != VIEW_TAG { + if self.made_is_nonzero(i) { + stale.push(i); + } + continue; + } + let key = u64::from(generation) << 32 | u64::from(view); + if blocks_verified && self.ready[i as usize].load(Ordering::Acquire) == key { + continue; + } + candidates.push((i, record)); + } + + if !candidates.is_empty() { + let records: Vec<[u32; 3]> = candidates.iter().map(|&(_, r)| r).collect(); + let mut current = Vec::with_capacity(records.len()); + views_current_into(&records, &mut current); + let made = self.made.lock().unwrap_or_else(|e| e.into_inner()); + for (&(i, [_, view, generation]), is_current) in candidates.iter().zip(current) { + let changed = match made.get(i as usize) { + Some(&m) if is_current => { + m != (u64::from(generation) << 32 | u64::from(view)) + } + Some(&m) => m != 0, + None => true, + }; + if changed { + stale.push(i); + } } } - } - for i in stale { - self.mirror(i, at(i), false); + for i in stale { + self.mirror(i, at(i), false); + } } for b in b_first..=b_last { @@ -893,6 +1264,310 @@ impl HostHeap { } } + /// Mirrors a page's stale slots (`for_draw == false`) under one hold each + /// of `VIEWS`, `self.made`, `self.kept`, `self.viewed`, and `shadow::READS`, + /// deduplicating `prepare` and descriptor creation via [`StagingHeap`] + + /// `CopyDescriptorsSimple`. + fn mirror_synced_batch(&self, stale: &[(u32, [u32; 3])], at: impl Fn(u32) -> u64) { + struct CachedTemplate { + src: D3D12_CPU_DESCRIPTOR_HANDLE, + kept: Option, + mark: Option<(u32, u32)>, + } + + let mut unique_keys: Vec = Vec::new(); + let mut key_counts: Vec = Vec::new(); + let mut key_to_idx: HashMap = HashMap::new(); + let mut slot_key_idx: Vec> = Vec::with_capacity(stale.len()); + + for &(_, [tag, view, generation]) in stale { + if tag == VIEW_TAG && generation != 0 { + let key = u64::from(generation) << 32 | u64::from(view); + let idx = match key_to_idx.get(&key) { + Some(&idx) => { + key_counts[idx] += 1; + idx + } + None => { + let idx = unique_keys.len(); + unique_keys.push(key); + key_counts.push(1); + key_to_idx.insert(key, idx); + idx + } + }; + slot_key_idx.push(Some(idx)); + } else { + slot_key_idx.push(None); + } + } + + let pins = views_at_batch(&unique_keys); + let mut is_placed: Vec = Vec::with_capacity(unique_keys.len()); + let mut is_tex: Vec = Vec::with_capacity(unique_keys.len()); + let mut strides: Vec = Vec::with_capacity(unique_keys.len()); + for pin in &pins { + if let Some(view) = pin.as_deref() { + let placed = is_placed_texture(view); + if !placed { + prepare(view); + } + is_placed.push(placed); + is_tex.push(is_texture(view)); + strides.push(view_stride(view) | view_kind(view)); + } else { + is_placed.push(false); + is_tex.push(false); + strides.push(0); + } + } + + // Initialize the staging heap before acquiring `self.made`. + let staging = self.staging(); + let host = host(); + + let mut again: Vec = Vec::new(); + let mut retired: Vec = Vec::new(); + + { + let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner()); + let mut templates: Vec> = + (0..unique_keys.len()).map(|_| None).collect(); + let mut next_template = if self.sampler { 1 } else { STAGING_NULL_SLOTS }; + let mut kept_updates: Vec<(u32, Kept)> = Vec::with_capacity(stale.len()); + let mut copy_dsts: Vec = Vec::with_capacity(stale.len()); + let mut copy_srcs: Vec = Vec::with_capacity(stale.len()); + let mut viewed_updates: Vec<(u32, Option<&View>, bool)> = if self.sampler { + Vec::new() + } else { + Vec::with_capacity(stale.len()) + }; + let mut forgot_reads: Vec = Vec::new(); + let mut finalize: Vec<(u32, u64, u64)> = Vec::with_capacity(stale.len()); + let addr_data = self + .addresses + .get() + .and_then(Option::as_ref) + .map(|&(_, data, _)| data); + let addr_slots = self.address_slots(); + + for (entry_idx, &(index, snapshot)) in stale.iter().enumerate() { + let addr = at(index); + // Re-read live title memory under `self.made`: if a title + // thread overwrote the slot since the page snapshot (including + // during `prepare`), defer to `mirror` after dropping locks. + // SAFETY: the caller holds the heap alive until the sync completes. + let live = unsafe { (addr as *const [u32; 3]).read_unaligned() }; + if live != snapshot { + again.push(index); + continue; + } + let resolved = slot_key_idx[entry_idx].and_then(|kidx| { + let [_, view_i, generation] = snapshot; + if !view_gen_current(view_i, generation) { + return None; + } + pins[kidx] + .as_deref() + .map(|view| (kidx, unique_keys[kidx], view)) + }); + let Some((kidx, key, view)) = resolved else { + if made[index as usize] != 0 { + self.ready[index as usize].store(0, Ordering::Relaxed); + self.synced[index as usize].store(0, Ordering::Relaxed); + made[index as usize] = 0; + if let (Some(_), Some(st)) = (host, staging) { + copy_dsts.push(self.cpu(index)); + copy_srcs.push(st.cpu(0)); + } else if self.sampler { + self.null_sampler(index); + } else { + null_view(self.cpu(index)); + } + self.write_stride(index, 0); + self.set_mark(index, None); + if let Some(data) = addr_data + && index < addr_slots + { + write_address(data, index as usize, None); + } + kept_updates.push((index, None)); + if !self.sampler { + viewed_updates.push((index, None, false)); + } + finalize.push((index, 0, 0)); + } + continue; + }; + if made[index as usize] == key { + continue; + } + self.ready[index as usize].store(0, Ordering::Relaxed); + self.synced[index as usize].store(0, Ordering::Relaxed); + self.write_stride(index, strides[kidx]); + if let Some(data) = addr_data + && index < addr_slots + { + write_address(data, index as usize, Some(view)); + } + let later = is_placed[kidx]; + if later { + forgot_reads.push(index); + } + made[index as usize] = key; + + let (kept, mark) = if later { + if !self.sampler { + if let (Some(_), Some(st)) = (host, staging) { + let null_slot = null_slot_index(false, Some(view)); + copy_dsts.push(self.cpu(index)); + copy_srcs.push(st.cpu(null_slot)); + } else { + null_for(view, self.cpu(index)); + } + } + (None, None) + } else if key_counts[kidx] >= 2 + && let (Some(_), Some(st)) = (host, staging) + { + if let Some(tpl) = &templates[kidx] { + copy_dsts.push(self.cpu(index)); + copy_srcs.push(tpl.src); + (tpl.kept.clone(), tpl.mark) + } else if next_template < st.count { + let t = next_template; + next_template += 1; + let (k, m) = make_marked(view, st.cpu(t)); + let src = if k.is_none() && !self.sampler { + st.cpu(null_slot_index(false, Some(view))) + } else { + st.cpu(t) + }; + copy_dsts.push(self.cpu(index)); + copy_srcs.push(src); + templates[kidx] = Some(CachedTemplate { + src, + kept: k.clone(), + mark: m, + }); + (k, m) + } else { + let (k, m) = make_marked(view, self.cpu(index)); + if k.is_none() && !self.sampler { + let null_slot = null_slot_index(false, Some(view)); + copy_dsts.push(self.cpu(index)); + copy_srcs.push(st.cpu(null_slot)); + } + (k, m) + } + } else { + let (k, m) = make_marked(view, self.cpu(index)); + if k.is_none() && !self.sampler { + if let (Some(_), Some(st)) = (host, staging) { + let null_slot = null_slot_index(false, Some(view)); + copy_dsts.push(self.cpu(index)); + copy_srcs.push(st.cpu(null_slot)); + } else { + null_for(view, self.cpu(index)); + } + } + (k, m) + }; + + self.set_mark(index, mark); + if !self.sampler { + viewed_updates.push((index, Some(view), kept.is_some())); + } + let kept = kept.flatten(); + let ready = if is_tex[kidx] && kept.is_none() { + 0 + } else { + key + }; + kept_updates.push((index, kept)); + finalize.push((index, key, ready)); + } + + if !copy_dsts.is_empty() + && let (Some(h), Some(st)) = (host, staging) + { + let count = copy_dsts.len() as u32; + // SAFETY: `copy_dsts` and `copy_srcs` have length `count`; each + // source handle lives in our CPU-only staging heap (already + // populated above) and each dest handle in our shader-visible + // heap; `None` range sizes specify 1 descriptor per range. + unsafe { + h.device.CopyDescriptors( + count, + copy_dsts.as_ptr(), + None, + count, + copy_srcs.as_ptr(), + None, + st.kind, + ); + } + } + + self.keep_batch(kept_updates, &mut retired); + if !self.sampler { + self.viewed.set_batch(&viewed_updates); + } + crate::shadow::forget_reads(&forgot_reads); + // Coalesce `made_nonzero` bit updates per 64-slot word (stale indices + // arrive in ascending order): set nonzero bits before publishing + // `synced`/`ready`, and clear nonzero bits after clearing them. + let mut set_word = usize::MAX; + let mut set_mask = 0u64; + for &(index, new_synced, _) in &finalize { + if new_synced == 0 { + continue; + } + let word = (index / 64) as usize; + if word != set_word { + if set_word != usize::MAX && set_mask != 0 { + self.made_nonzero[set_word].fetch_or(set_mask, Ordering::Release); + } + set_word = word; + set_mask = 0; + } + set_mask |= 1u64 << (index & 63); + } + if set_word != usize::MAX && set_mask != 0 { + self.made_nonzero[set_word].fetch_or(set_mask, Ordering::Release); + } + for &(index, new_synced, new_ready) in &finalize { + self.synced[index as usize].store(new_synced, Ordering::Release); + self.ready[index as usize].store(new_ready, Ordering::Release); + } + let mut clr_word = usize::MAX; + let mut clr_mask = 0u64; + for &(index, new_synced, _) in &finalize { + if new_synced != 0 { + continue; + } + let word = (index / 64) as usize; + if word != clr_word { + if clr_word != usize::MAX && clr_mask != 0 { + self.made_nonzero[clr_word].fetch_and(!clr_mask, Ordering::Release); + } + clr_word = word; + clr_mask = 0; + } + clr_mask |= 1u64 << (index & 63); + } + if clr_word != usize::MAX && clr_mask != 0 { + self.made_nonzero[clr_word].fetch_and(!clr_mask, Ordering::Release); + } + } + + crate::host::retire_all(retired); + drop(pins); + for index in again { + self.mirror(index, at(index), false); + } + } + /// Whether the view made in slot `index` must be made again for a draw /// that reads it: a placed texture's view left null is made when a draw /// reads the slot through a table (heap syncs revisit it at every @@ -974,10 +1649,11 @@ impl HostHeap { // title released): nothing to read. if made[index as usize] != 0 { self.ready[index as usize].store(0, Ordering::Relaxed); + self.synced[index as usize].store(0, Ordering::Relaxed); made[index as usize] = 0; - self.set_made_nonzero(index, false); self.make_null(index); self.note_address(index, None); + self.set_made_nonzero(index, false); } return None; }; @@ -1017,6 +1693,7 @@ impl HostHeap { } { self.ready[index as usize].store(0, Ordering::Relaxed); + self.synced[index as usize].store(0, Ordering::Relaxed); self.set_stride(index, Some(&view)); self.note_address(index, Some(&view)); if later { @@ -1041,6 +1718,7 @@ impl HostHeap { // A texture's view left null is made again when a draw reads it. let ready = if is_texture(&view) && kept.is_none() { 0 } else { key }; self.keep(index, kept); + self.synced[index as usize].store(key, Ordering::Release); self.ready[index as usize].store(ready, Ordering::Release); } Some(view) @@ -1124,6 +1802,8 @@ impl HostHeap { for &index in &slots { { let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner()); + self.ready[index as usize].store(0, Ordering::Relaxed); + self.synced[index as usize].store(0, Ordering::Release); made[index as usize] = 0; self.set_made_nonzero(index, false); } @@ -1501,6 +2181,7 @@ type Held = (ID3D12Resource, Option); /// The fields keep the normal and widened copies alive for as long as the /// slot shows them. #[allow(dead_code)] +#[derive(Clone)] enum HeldSource { Shadow(std::sync::Arc), /// A widened three-channel view and the normal shadow it reads. @@ -3264,4 +3945,338 @@ mod tests { crate::object::release(resource); let _ = title_heap.as_mut_ptr(); } + + #[test] + fn fast_heap_sync_env_toggle() { + use std::ffi::OsStr; + assert!(fast_heap_sync_value(None)); + assert!(fast_heap_sync_value(Some(OsStr::new("1")))); + assert!(fast_heap_sync_value(Some(OsStr::new("")))); + assert!(!fast_heap_sync_value(Some(OsStr::new("0")))); + } + + #[test] + fn fast_heap_sync_handles_placed_null_duplicates_forget_and_concurrent_rewrite() { + let Some(host) = HostHeap::new(false, 512) else { + return; + }; + let make_shadowed_buffer = || { + let b = crate::resource::test_buffer(256); + let (ptr, len) = crate::resource::buffer_bytes(b).expect("buffer bytes"); + crate::shadow::register(b as usize, ptr as u64, len); + b + }; + let buf_a = make_shadowed_buffer(); + let buf_b = make_shadowed_buffer(); + + let mut title_heap = vec![0u8; 512 * 32]; + let start = title_heap.as_ptr() as u64; + let slot_addr = |i: u32| start + u64::from(i) * 32; + // R8_UINT buffer SRV, all 256 elements (valid host buffer SRV; word 6 = NumElements). + let srv_desc = [62u32, 1, 0x1688, 0, 0, 0, 256, 0, 0, 0]; + + // 10 duplicate copies of buffer SRV A (exercises template copy via StagingHeap). + for i in 0..10u32 { + crate::descriptor::create_srv(std::ptr::null(), buf_a, &srv_desc, slot_addr(i)); + } + + // 10 duplicate copies of a placed 2D texture (left null on sync, ready == 0, synced == key). + let tex_mem = vec![0u8; 1 << 16]; + let tex_addr = tex_mem.as_ptr() as u64; + let tex_words: [u32; 14] = [3, 0, 0, 0, 4, 0, 1, 1 | 1 << 16, 28, 1, 0, 0x100, 0, 0]; + let mut tex_view = [0u32; 16]; + tex_view[..6].copy_from_slice(&[28, 4, 0x1688, 0, 0, 1]); + (tex_view[10], tex_view[11]) = (tex_addr as u32, (tex_addr >> 32) as u32); + for i in 20..30u32 { + crate::descriptor::create_placed_srv( + std::ptr::null(), + &tex_words, + &tex_view, + slot_addr(i), + ); + } + + // First fast sync populates buffer slots via staging template and placed textures via null template. + host.sync_slots_bytes_with(0..512, start, 32, start, &title_heap, true); + for i in 0..10u32 { + assert!(host.made(i), "buffer slot {i} made"); + assert!(host.kept(i), "buffer slot {i} kept"); + assert_ne!(host.synced[i as usize].load(Ordering::Acquire), 0); + assert_eq!( + host.ready[i as usize].load(Ordering::Acquire), + host.synced[i as usize].load(Ordering::Acquire) + ); + } + for i in 20..30u32 { + assert!(host.made(i), "placed texture slot {i} noted"); + assert!(!host.kept(i), "placed texture slot {i} left null until drawn"); + assert_ne!(host.synced[i as usize].load(Ordering::Acquire), 0); + assert_eq!(host.ready[i as usize].load(Ordering::Acquire), 0); + } + + // Second fast sync skips all slots locklessly and preserves placed texture ready == 0. + host.sync_slots_bytes_with(0..512, start, 32, start, &title_heap, true); + for i in 20..30u32 { + assert!(!host.kept(i)); + assert_eq!(host.ready[i as usize].load(Ordering::Acquire), 0); + } + // Drawing slot 20 still remakes the placed texture on demand. + assert!(host.mirror(20, slot_addr(20), true).is_some()); + assert!(host.kept(20), "placed texture made on draw"); + + // Forgetting buffer A immediately invalidates all 10 buffer slots on next sync + // even though title heap bytes did not change. + crate::descriptor::forget(buf_a as usize); + host.sync_slots_bytes_with(0..512, start, 32, start, &title_heap, true); + for i in 0..10u32 { + assert!(!host.made(i), "forgotten slot {i} cleared"); + assert!(!host.made_is_nonzero(i), "forgotten slot {i} nonzero bit cleared"); + assert!(!host.kept(i), "forgotten slot {i} no longer kept"); + assert_eq!(host.synced[i as usize].load(Ordering::Acquire), 0); + assert_eq!(host.ready[i as usize].load(Ordering::Acquire), 0); + } + + // Concurrent title rewrite during `prepare` leaves the slot holding the latest view. + let buf_c = make_shadowed_buffer(); + let race_slot = 64u32; + let race_addr = slot_addr(race_slot); + crate::descriptor::create_srv(std::ptr::null(), buf_b, &srv_desc, race_addr); + let buf_c_addr = buf_c as usize; + let mut expected_c = [0u32; 4]; + crate::descriptor::create_srv( + std::ptr::null(), + buf_c, + &srv_desc, + expected_c.as_mut_ptr() as u64, + ); + let expected_c_key = u64::from(expected_c[2]) << 32 | u64::from(expected_c[1]); + *WHILE_PREPARING.lock().unwrap_or_else(|e| e.into_inner()) = Some(Box::new(move || { + crate::descriptor::create_srv( + std::ptr::null(), + buf_c_addr as crate::object::This, + &srv_desc, + race_addr, + ); + })); + host.sync_slots_bytes_with(0..128, start, 32, start, &title_heap[..128 * 32], true); + assert!(WHILE_PREPARING.lock().unwrap_or_else(|e| e.into_inner()).is_none()); + assert_eq!( + host.synced[race_slot as usize].load(Ordering::Acquire), + expected_c_key, + "concurrent rewrite during prepare won" + ); + assert!(host.kept(race_slot)); + // SAFETY: plain device health check after batched CopyDescriptors. + assert!(unsafe { crate::host::host().unwrap().device.GetDeviceRemovedReason() }.is_ok()); + + crate::object::release(buf_a); + crate::object::release(buf_b); + crate::object::release(buf_c); + let _ = title_heap.as_mut_ptr(); + let _ = tex_mem.as_ptr(); + } + + #[test] + fn bench_sync_slots_1_5m_fast_vs_legacy() { + const SLOTS: u32 = 65_536; // 32 x 64 KiB pages (2,048 32-byte slots/page) + const PASSES: usize = 23; // 23 * 65,536 = 1,507,328 slots scanned + const PAGE_SLOTS: u32 = 2_048; + + let Some(host_legacy) = HostHeap::new(false, SLOTS) else { + return; + }; + let Some(host_fast) = HostHeap::new(false, SLOTS) else { + return; + }; + + // Valid R8_UINT buffer SRV covering all 256 bytes (word 6 = NumElements). + let srv_desc = [62u32, 1, 0x1688, 0, 0, 0, 256, 0, 0, 0]; + + // 150 distinct buffer resources (10x duplication across active/stale slots), + // registered with shadow so every buffer SRV creates a real D3D12 view + bindless hull. + let make_shadowed_buffer = || { + let b = crate::resource::test_buffer(256); + let (ptr, len) = crate::resource::buffer_bytes(b).expect("buffer bytes"); + crate::shadow::register(b as usize, ptr as u64, len); + b + }; + let buffers: Vec<_> = (0..150).map(|_| make_shadowed_buffer()).collect(); + // Extra resource forgotten after benchmark to verify Views/HostHeap invalidation. + let ephemeral = make_shadowed_buffer(); + + let tex_mem = vec![0u8; 1 << 16]; + let tex_addr = tex_mem.as_ptr() as u64; + let tex_words: [u32; 14] = [3, 0, 0, 0, 4, 0, 1, 1 | 1 << 16, 28, 1, 0, 0x100, 0, 0]; + let mut placed_records: Vec<[u32; 3]> = Vec::with_capacity(40); + for k in 0..40u64 { + let mut view = [0u32; 16]; + view[..6].copy_from_slice(&[28, 4, 0x1688, 0, 0, 1]); + let addr = tex_addr + k * 256; + (view[10], view[11]) = (addr as u32, (addr >> 32) as u32); + let mut rec = [0u32; 3]; + crate::descriptor::create_placed_srv( + std::ptr::null(), + &tex_words, + &view, + rec.as_mut_ptr() as u64, + ); + placed_records.push(rec); + } + + let mut buf_records: Vec<[u32; 3]> = Vec::with_capacity(buffers.len()); + for &b in &buffers { + let mut rec = [0u32; 3]; + crate::descriptor::create_srv( + std::ptr::null(), + b, + &srv_desc, + rec.as_mut_ptr() as u64, + ); + buf_records.push(rec); + } + let mut eph_record = [0u32; 3]; + crate::descriptor::create_srv( + std::ptr::null(), + ephemeral, + &srv_desc, + eph_record.as_mut_ptr() as u64, + ); + + let write_rec = |mem: &mut [u8], slot: usize, rec: [u32; 3]| { + let off = slot * 32; + mem[off..off + 4].copy_from_slice(&rec[0].to_le_bytes()); + mem[off + 4..off + 8].copy_from_slice(&rec[1].to_le_bytes()); + mem[off + 8..off + 12].copy_from_slice(&rec[2].to_le_bytes()); + }; + + let build_heap = || { + let mut mem = vec![0u8; SLOTS as usize * 32]; + // 10,000 steady-state buffer views (real host SRVs), 20,000 steady-state + // placed-texture-left-null slots (ready == 0, synced == key), 16 ephemeral + // slots, remainder empty. + for i in 0..10_000usize { + write_rec(&mut mem, i, buf_records[i % 75]); + } + for i in 0..20_000usize { + write_rec(&mut mem, 12_288 + i, placed_records[i % 20]); + } + for i in 0..16usize { + write_rec(&mut mem, 34_816 + i, eph_record); + } + mem + }; + + let mut mem_legacy = build_heap(); + let mut mem_fast = build_heap(); + + let sync_all_pages = |host: &HostHeap, mem: &[u8], fast: bool| { + let start = mem.as_ptr() as u64; + for p in 0..(SLOTS / PAGE_SLOTS) { + let first = p * PAGE_SLOTS; + let last = first + PAGE_SLOTS; + let byte_start = first as usize * 32; + let byte_end = last as usize * 32; + host.sync_slots_bytes_with( + first..last, + start, + 32, + start + byte_start as u64, + &mem[byte_start..byte_end], + fast, + ); + } + }; + + // Initial warm sync so steady-state slots (including 20,000 placed-left-null) are populated. + sync_all_pages(&host_legacy, &mem_legacy, false); + sync_all_pages(&host_fast, &mem_fast, true); + assert!(host_fast.kept(0), "benchmark buffer SRVs are real host views"); + assert!(!host_fast.kept(12_288), "placed textures remain null on sync"); + + // Mutate ~1,700 slots/pass (740 real buffer views of 75 keys + 960 placed-null/cleared) + // across 23 passes = ~39,100 stale slots + 1,507,328 total scanned slots. + let mutate_pass = |mem: &mut [u8], pass: usize| { + let toggle = pass & 1; + for j in 0..740usize { + write_rec(mem, 40_960 + j, buf_records[toggle * 75 + (j % 75)]); + } + for j in 0..960usize { + let rec = if toggle == 0 { + placed_records[20 + (j % 20)] + } else { + [0, 0, 0] + }; + write_rec(mem, 45_056 + j, rec); + } + }; + + // Two extra resources whose views are forgotten mid-run so each pass + // exercises a real `VIEWS_FORGOT` epoch bump without affecting live slots. + let bump_legacy = crate::resource::test_buffer(256); + let bump_fast = crate::resource::test_buffer(256); + for &r in &[bump_legacy, bump_fast] { + let mut scratch = [0u32; 4]; + crate::descriptor::create_srv( + std::ptr::null(), + r, + &srv_desc, + scratch.as_mut_ptr() as u64, + ); + } + + // Run legacy across 23 passes (1.5M scanned + ~39k stale) with a mid-run forget() epoch bump. + let t_legacy = std::time::Instant::now(); + for pass in 0..PASSES { + mutate_pass(&mut mem_legacy, pass); + if pass == PASSES / 2 { + crate::descriptor::forget(bump_legacy as usize); + } + sync_all_pages(&host_legacy, &mem_legacy, false); + } + let legacy_ms = t_legacy.elapsed().as_secs_f64() * 1000.0; + + // Run fast across identical 23 passes (1.5M scanned + ~39k stale) with identical forget() epoch bump. + let t_fast = std::time::Instant::now(); + for pass in 0..PASSES { + mutate_pass(&mut mem_fast, pass); + if pass == PASSES / 2 { + crate::descriptor::forget(bump_fast as usize); + } + sync_all_pages(&host_fast, &mem_fast, true); + } + let fast_ms = t_fast.elapsed().as_secs_f64() * 1000.0; + + // Verify identical final made/kept state across all 65,536 slots. + for i in 0..SLOTS { + assert_eq!(host_fast.made(i), host_legacy.made(i), "made mismatch at slot {i}"); + assert_eq!(host_fast.kept(i), host_legacy.kept(i), "kept mismatch at slot {i}"); + } + + // Now actually forget ephemeral and verify both clear slots 34,816..34,832. + crate::descriptor::forget(ephemeral as usize); + sync_all_pages(&host_legacy, &mem_legacy, false); + sync_all_pages(&host_fast, &mem_fast, true); + for i in 34_816..34_832u32 { + assert!(!host_legacy.made(i)); + assert!(!host_fast.made(i)); + assert!(!host_fast.kept(i)); + } + + let speedup = legacy_ms / fast_ms.max(0.001); + eprintln!( + "bench_sync_slots_1_5m_fast_vs_legacy: slots={} stale=~39100 legacy={:.2}ms fast={:.2}ms speedup={:.2}x", + SLOTS as usize * PASSES, + legacy_ms, + fast_ms, + speedup + ); + + crate::object::release(bump_legacy); + crate::object::release(bump_fast); + crate::object::release(ephemeral); + for b in buffers { + crate::object::release(b); + } + let _ = tex_mem.as_ptr(); + } } diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 5d7ad469..ab91dd29 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -4829,6 +4829,17 @@ pub(crate) fn retire(resource: ID3D12Resource) { RETIRED.lock().unwrap_or_else(|e| e.into_inner()).push(resource); } +/// Batched [`retire`] under one hold of the retired-resources lock. +pub(crate) fn retire_all(mut resources: Vec) { + if resources.is_empty() { + return; + } + RETIRED + .lock() + .unwrap_or_else(|e| e.into_inner()) + .append(&mut resources); +} + /// After a submission has run, with none running: the retired resources go. pub(crate) fn release_retired() { let retired = std::mem::take(&mut *RETIRED.lock().unwrap_or_else(|e| e.into_inner())); diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index ca502923..05e3ca78 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1343,6 +1343,18 @@ pub(crate) fn forget_read(slot: u32) { READS.lock().unwrap_or_else(|e| e.into_inner()).1.push(slot); } +/// Batched [`forget_read`] under one hold of the read-marks lock. +pub(crate) fn forget_reads(slots: &[u32]) { + if slots.is_empty() { + return; + } + READS + .lock() + .unwrap_or_else(|e| e.into_inner()) + .1 + .extend_from_slice(slots); +} + /// After a submission: every page its shaders marked as written through /// views goes back ([`Shadow::copy_back_marked`]), but for pages in /// `withheld` (ranges of copies that lists whose writes stay out of title