diff --git a/crates/victory-gpu/src/bindless.rs b/crates/victory-gpu/src/bindless.rs index f17e9ba6..555b93a3 100644 --- a/crates/victory-gpu/src/bindless.rs +++ b/crates/victory-gpu/src/bindless.rs @@ -475,6 +475,82 @@ void main(uint3 id : SV_DispatchThreadID) { out } + fn dex_resource(dimension: u32, width: u64, height: u32, format: u32, layout: u32, flags: u32) -> This { + let desc: [u32; 14] = [ + dimension, 0, 0, 0, width as u32, (width >> 32) as u32, height, 1 | 1 << 16, format, 1, 0, + layout, flags, 0, + ]; + let properties = [1u32, 0, 0, 0, 0]; + let mut out = std::ptr::null(); + let iid = crate::object::guid( + 0x696442be, + 0xa72e, + 0x4059, + [0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad], + ); + let made: HRESULT = crate::resource::create_committed( + std::ptr::null(), + properties.as_ptr().cast(), + 0, + desc.as_ptr().cast(), + 0, + std::ptr::null(), + &iid, + &mut out, + ); + assert_eq!(made, S_OK); + out + } + + #[test] + fn dexmetal_waits_for_table_and_heap() { + assert!(crate::descriptor::host::dex_enabled()); + let _host = crate::host::host() + .expect("DexMetal needs a real D3D12 host"); + let null = std::ptr::null(); + let a = dex_resource(3, 4, 1, 28, LINEAR, 0); + let b = dex_resource(3, 4, 1, 28, LINEAR, 4); + let heap_desc: [u32; 8] = [0, 8, 1, 0, 0, 0, 0, 0]; + let mut heap = null; + let iid = crate::object::guid( + 0x8efb471d, 0x616c, 0x4f49, + [0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51], + ); + assert_eq!( + crate::descriptor::create_heap( + null, heap_desc.as_ptr().cast(), &iid, &mut heap + ), S_OK + ); + let title_heap = crate::descriptor::heap(heap); + let slot = |i: u64| title_heap.start + 32 * i; + crate::descriptor::create_srv(null, a, null.cast(), slot(0)); + crate::descriptor::create_srv(null, a, null.cast(), slot(1)); + let uav: [u32; 10] = [28, 4, 0, 0, 0, 0, 0, 0, 0, 0]; + crate::descriptor::create_uav(null, b, null, &uav, slot(2)); + let h = title_heap.host().expect("host heap"); + assert!(crate::resource::made_copy(a).is_none()); + assert!(crate::resource::made_copy(b).is_none()); + assert!(!h.kept(0) && !h.kept(1) && !h.kept(2)); + assert!(h.mirror(0, slot(0), true).is_some()); + assert!(h.kept(0)); + assert!(crate::resource::made_copy(a).is_some()); + assert!(!h.kept(1)); + crate::descriptor::dex_restore_pending(&[h.viewed.clone()]); + assert!(h.kept(1) && h.kept(2)); + assert!(crate::resource::made_copy(b).is_some()); + eprintln!("DEXMETAL_PUBLICATION_VERIFIED"); + } + + #[test] + fn dexmetal_direct_heap_numeric() { + assert!(crate::descriptor::host::dex_enabled()); + let _host = crate::host::host() + .expect("DexMetal needs a real D3D12 host"); + heap_views_are_current(); + eprintln!("DEXMETAL_NUMERICS_VERIFIED"); + } + + /// A title resource at `address` (title memory of its own), as the /// title places buffers and textures in its heaps. fn resource_at(address: usize, dimension: u32, width: u64, height: u32, format: u32, layout: u32) -> This { @@ -622,6 +698,11 @@ void main(uint3 id : SV_DispatchThreadID) { crate::descriptor::create_uav(null, dst, null, &uav, slot(1)); crate::descriptor::create_srv(null, tex, std::ptr::null(), slot(2)); let heap_host = title_heap.host().expect("a host view heap"); + if crate::descriptor::host::dex_enabled() { + assert!(crate::resource::made_copy(tex).is_none(), + "texture must still be deferred before submission"); + assert!(!heap_host.kept(2)); + } let (root, pso) = compile(); let list = host.new_list().unwrap(); let (dst_bytes, _) = crate::resource::buffer_bytes(dst).unwrap(); @@ -644,6 +725,10 @@ void main(uint3 id : SV_DispatchThreadID) { write(base, texel); host.execute(&[&list]); let words = out.read_host(dst_bytes as u64, 64); + if crate::descriptor::host::dex_enabled() { + assert!(heap_host.kept(2)); + assert_eq!(words.len(), 64); + } for (i, &v) in words.iter().enumerate() { assert_eq!(v, base + i as u32 + u32::from(texel), "round {round}, dword {i}"); } diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 97a067ce..aec4e193 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -205,6 +205,37 @@ pub(crate) fn sync_heaps() { } } +// DexMetal conservative fallback: direct indexing may select +// a texture before a previous read mark exists. Restore the active +// heap's waiting object views before GPU execution and uploads. +#[cfg(windows)] +pub(crate) fn dex_restore_pending( + active: &[std::sync::Arc] +) { + if active.is_empty() || !host::dex_enabled() { + return; + } + let heaps = SHADER_HEAPS.lock() + .unwrap_or_else(|e| e.into_inner()); + for &(start, _, at) in heaps.iter() { + // SAFETY: registered heaps remain live under this lock. + let heap = unsafe { &*(at as *const Heap) }; + if heap.desc.kind != VIEWS_KIND { + continue; + } + let Some(Some(h)) = heap.host.get() else { + continue; + }; + if active.iter().any(|v| + std::sync::Arc::ptr_eq(v, &h.viewed) + ) { + if let Some(step) = increment(heap.desc.kind) { + h.dex_restore(start, u64::from(step)); + } + } + } +} + /// Makes the views of slots `slots` that were left null (textures the title /// placed itself, textures given up) in every shader-visible view heap, now /// that shaders read them (`crate::shadow::take_reads`), and stamps what diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index f8ff7c8c..a02d2a5b 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -26,6 +26,8 @@ pub(crate) struct HostHeap { sampler: bool, /// The record (view index and generation) each slot was last made from. made: Mutex>, + // Resource-object views whose host texture has not been made. + dex_pending: Mutex>, /// 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 @@ -207,6 +209,7 @@ impl HostHeap { dynamic: extra, sampler, made: Mutex::new(vec![0; count as usize]), + dex_pending: Mutex::new(Default::default()), kept: Mutex::new(Paged::new((count + extra) as usize)), next: Mutex::new((0, vec![false; extra as usize])), pairs: Mutex::new((count + extra, Vec::new())), @@ -702,6 +705,9 @@ impl HostHeap { let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner()); let view = (tag == VIEW_TAG).then(|| view_at(i, generation)).flatten(); let Some(view) = view else { + self.dex_pending.lock() + .unwrap_or_else(|e| e.into_inner()) + .remove(&index); // Not a view of ours (never written, or of a resource the // title released): nothing to read. if made[index as usize] != 0 { @@ -716,7 +722,10 @@ impl HostHeap { // (heap syncs revisit it at every submission). let lazy = || { for_draw - && is_texture(&view) + && (is_texture(&view) || + self.dex_pending.lock() + .unwrap_or_else(|e| e.into_inner()) + .contains(&index)) && self.kept.lock().unwrap_or_else(|e| e.into_inner()).get(index as usize).is_none() }; if made[index as usize] == key && !lazy() { @@ -728,7 +737,10 @@ impl HostHeap { // when a draw reads them through a table: a host texture (and a // staging copy) for each of the ~20,000 the title has views of // ran the host out of memory (run b2). - let later = !for_draw && is_placed_texture(&view); + let dex_later = !for_draw && dex_waits(&view); + let later = !for_draw && ( + is_placed_texture(&view) || dex_later + ); if !later { // What the view needs (the texture it views, the copy of the // memory) is made without the slot lock, which title threads @@ -760,6 +772,22 @@ impl HostHeap { } else { make_marked(&view, self.cpu(index)) }; + { + use std::sync::atomic::Ordering::Relaxed; + let mut pending = self.dex_pending.lock() + .unwrap_or_else(|e| e.into_inner()); + if dex_later { + if pending.insert(index) { + DEX_DEFERRED.fetch_add(1, Relaxed); + } + } else if pending.remove(&index) { + if kept.is_some() { + DEX_RESOLVED.fetch_add(1, Relaxed); + } else if for_draw { + DEX_FAILED.fetch_add(1, Relaxed); + } + } + } self.set_mark(index, mark); if kept.is_none() && !self.sampler { null_for(&view, self.cpu(index)); @@ -772,6 +800,24 @@ impl HostHeap { Some(view) } + /// Before a direct-heap submission, materialize every + /// pending object view in that heap, not just prior read marks. + pub(crate) fn dex_restore(&self, start: u64, step: u64) { + let slots: Vec = self.dex_pending.lock() + .unwrap_or_else(|e| e.into_inner()) + .iter().copied().collect(); + for i in slots { + let handle = start + u64::from(i) * step; + // Follow the official heap-sync accessibility check. + if !crate::host::read_into( + handle as *const u8, &mut [0u8; 12] + ) { + continue; + } + self.mirror(i, handle, true); + } + } + /// The view host slot `index` was made from, in a few words, with the /// title and host addresses of a buffer view (diagnostics). "null" for /// a slot never made or left null. @@ -1526,6 +1572,41 @@ fn make(view: &View, at: D3D12_CPU_DESCRIPTOR_HANDLE) -> Option { } /// Whether `view` is a view of a texture the title placed in memory itself. +pub(crate) fn dex_enabled() -> bool { + static ENABLED: std::sync::OnceLock = + std::sync::OnceLock::new(); + *ENABLED.get_or_init(|| { + std::env::var_os("VICTORY_DEXMETAL_DEFER") + .is_some_and(|v| v == "1") + }) +} + +static DEX_DEFERRED: std::sync::atomic::AtomicU64 = + std::sync::atomic::AtomicU64::new(0); +static DEX_RESOLVED: std::sync::atomic::AtomicU64 = + std::sync::atomic::AtomicU64::new(0); +static DEX_FAILED: std::sync::atomic::AtomicU64 = + std::sync::atomic::AtomicU64::new(0); + +pub(crate) fn dex_stats() -> [u64; 3] { + use std::sync::atomic::Ordering::Relaxed; + [ + DEX_DEFERRED.load(Relaxed), + DEX_RESOLVED.load(Relaxed), + DEX_FAILED.load(Relaxed), + ] +} + +fn dex_waits(view: &View) -> bool { + dex_enabled() && match view { + View::Shader(r, _) | View::Unordered(r, _, _) => + crate::resource::dex_waiting( + *r as crate::object::This + ), + _ => false, + } +} + fn is_placed_texture(view: &View) -> bool { matches!(view, View::Placed(_, resource, _) if resource[0] != 1) } diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index a619b34b..6a0c582c 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1238,6 +1238,7 @@ impl Host { .collect::>() }) .collect(); + crate::descriptor::dex_restore_pending(&heaps); // And textures read through tables, uploaded by the same list. let textures: Vec<_> = runs .iter() @@ -3182,6 +3183,14 @@ impl Meter { crate::list::UPLOADS[1].load(Ordering::Relaxed) as f64 / 1e6, ); } + if crate::descriptor::host::dex_enabled() { + let [deferred, resolved, failed] = + crate::descriptor::host::dex_stats(); + tracing::info!( + deferred, resolved, failed, + "DexMetal descriptor census" + ); + } let [gpu, title] = crate::resource::texture_census().map(|r| r.map(|b| b as f64 / 1e6)); tracing::info!( "host texture MB (used by lists, viewed by heap slots only, neither): GPU-written {:.0} {:.0} {:.0}, \ diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 89cd3b29..a9ebcc98 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1105,6 +1105,21 @@ pub(crate) fn made_copy(this: This) -> Option> { resource(this).host.lock().unwrap_or_else(|e| e.into_inner()).clone().flatten() } +// DexMetal: adapted from the deferred descriptor principle in +// DexNom/VIctory, revision 2e01ab4760db91820ef5f9a9f85761f83cd6b5d0. +// Keep the upstream host ownership and refusal states. +#[cfg(windows)] +pub(crate) fn dex_waiting(this: This) -> bool { + if this.is_null() { + return false; + } + let r = resource(this); + if r.desc.dimension == BUFFER { + return false; + } + r.host.lock().unwrap_or_else(|e| e.into_inner()).is_none() +} + /// A title texture's host copy, shared, for a list to hold. #[cfg(windows)] pub(crate) fn host_copy(this: This) -> Option> {