|
|
@@ -77,7 +77,12 @@ static STARTED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new
|
|
|
static FINISHED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
|
|
|
|
|
/// A retired object, dropped on whatever thread finishes the submission.
|
|
|
-struct Retired(#[allow(dead_code)] Box<dyn std::any::Any>);
|
|
|
+#[allow(dead_code)]
|
|
|
+enum Retired {
|
|
|
+ Any(Box<dyn std::any::Any>),
|
|
|
+ /// What a descriptor slot showed: 20,000 a frame, without a box each.
|
|
|
+ Resource(ID3D12Resource),
|
|
|
+}
|
|
|
// SAFETY: what is retired is D3D12 objects (free-threaded) and the
|
|
|
// reference-counted wrappers holding them, dropped once.
|
|
|
unsafe impl Send for Retired {}
|
|
|
@@ -88,12 +93,24 @@ pub(crate) fn now_ms() -> u64 {
|
|
|
EPOCH.get_or_init(std::time::Instant::now).elapsed().as_millis() as u64
|
|
|
}
|
|
|
|
|
|
+/// [`now_ms`] to the system's tick (16 ms): a few nanoseconds, where the
|
|
|
+/// performance counter's are 25. For what is noted tens of thousands of
|
|
|
+/// times a frame and read in tenths of a second.
|
|
|
+pub(crate) fn coarse_ms() -> u64 {
|
|
|
+ use windows_sys::Win32::System::SystemInformation::GetTickCount64;
|
|
|
+ // The same beginning as `now_ms`.
|
|
|
+ static BEGAN: OnceLock<u64> = OnceLock::new();
|
|
|
+ // SAFETY: a plain query.
|
|
|
+ let now = unsafe { GetTickCount64() };
|
|
|
+ now.saturating_sub(*BEGAN.get_or_init(|| now.saturating_sub(now_ms())))
|
|
|
+}
|
|
|
+
|
|
|
/// This moment, for telling later how long ago it was ([`long_ago`]): the
|
|
|
/// frames begun so far (high half, `crate::frame::current`) and tenths of a
|
|
|
/// second since the start. Host copies of title memory, staging buffers and
|
|
|
/// host textures note when they were last used with it.
|
|
|
pub(crate) fn moment() -> u64 {
|
|
|
- (crate::frame::current() << 32) | ((now_ms() / 100) & 0xffff_ffff)
|
|
|
+ (crate::frame::current() << 32) | ((coarse_ms() / 100) & 0xffff_ffff)
|
|
|
}
|
|
|
|
|
|
/// Whether `moment` lies at least `ms` milliseconds and `frames` frames
|
|
|
@@ -299,7 +316,20 @@ pub(crate) fn retire<T: 'static>(object: T) {
|
|
|
RETIRED
|
|
|
.lock()
|
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
|
- .push_back((started, Retired(Box::new(object))));
|
|
|
+ .push_back((started, Retired::Any(Box::new(object))));
|
|
|
+}
|
|
|
+
|
|
|
+/// [`retire`] for a resource.
|
|
|
+pub(crate) fn retire_resource(object: ID3D12Resource) {
|
|
|
+ let queued = crate::queue::queued();
|
|
|
+ let started = STARTED.load(Ordering::Acquire) + queued;
|
|
|
+ if bury_lag() == 0 && started <= FINISHED.load(Ordering::Acquire) {
|
|
|
+ RETIREMENTS[0].fetch_add(1, Ordering::Relaxed);
|
|
|
+ drop(object);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ RETIREMENTS[1].fetch_add(1, Ordering::Relaxed);
|
|
|
+ RETIRED.lock().unwrap_or_else(|e| e.into_inner()).push_back((started, Retired::Resource(object)));
|
|
|
}
|
|
|
|
|
|
/// How many submissions later than it could (the last one that may use it
|
|
|
@@ -992,6 +1022,10 @@ pub(crate) struct HostList {
|
|
|
/// Refreshes of host copies that a thread of ours is still finding, and
|
|
|
/// where among the list's steps they belong ([`aside`]).
|
|
|
coming: Mutex<Option<(usize, std::sync::Arc<aside::Coming>)>>,
|
|
|
+ /// Another number each time something changes that what a draw reads
|
|
|
+ /// depends on ([`HostList::changed_something`]); no two lists have the
|
|
|
+ /// same.
|
|
|
+ epoch: std::sync::atomic::AtomicU64,
|
|
|
/// Counts the list in [`ALIVE`] while it lives.
|
|
|
_alive: Counted,
|
|
|
/// The texts of the markers `note_op` set, in order (for device-removed
|
|
|
@@ -1993,7 +2027,7 @@ struct Deferred {
|
|
|
kept: Vec<windows::core::IUnknown>,
|
|
|
/// Buffer ranges refreshed since the last title copy (host copy,
|
|
|
/// address, bytes): refreshing them again would find the same bytes.
|
|
|
- refreshed: std::collections::HashSet<(usize, usize, usize)>,
|
|
|
+ refreshed: std::collections::HashSet<(usize, usize, usize), std::hash::BuildHasherDefault<AddressHash>>,
|
|
|
/// Textures whose contents the list brings up to date.
|
|
|
textures: std::collections::HashSet<usize>,
|
|
|
/// Bottom-of-pipe writes, done once the list has run: address, what,
|
|
|
@@ -3698,6 +3732,7 @@ impl Host {
|
|
|
indirect_args: Mutex::default(),
|
|
|
batch_args: Mutex::default(),
|
|
|
coming: Mutex::default(),
|
|
|
+ epoch: std::sync::atomic::AtomicU64::new(next_epoch()),
|
|
|
_alive: Counted::new(),
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
notes,
|
|
|
@@ -5753,6 +5788,7 @@ impl HostList {
|
|
|
/// Only after the list's last submission finished (every one is waited
|
|
|
/// for).
|
|
|
pub(crate) fn reset(&self) {
|
|
|
+ self.changed_something();
|
|
|
#[cfg(feature = "diagnostics")]
|
|
|
let started = std::time::Instant::now();
|
|
|
let _resetting = crate::timeline::span("resetting a host list");
|
|
|
@@ -5788,6 +5824,18 @@ impl HostList {
|
|
|
crate::copystat::add(crate::copystat::C::ResetNs, started.elapsed().as_nanos() as u64);
|
|
|
}
|
|
|
|
|
|
+ /// Something changed in this list that bringing a draw's textures and
|
|
|
+ /// buffers up to date depends on: a texture's state, a twin, what was
|
|
|
+ /// refreshed.
|
|
|
+ pub(crate) fn changed_something(&self) {
|
|
|
+ self.epoch.store(next_epoch(), Ordering::Relaxed);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// See [`HostList::changed_something`].
|
|
|
+ pub(crate) fn epoch(&self) -> u64 {
|
|
|
+ self.epoch.load(Ordering::Relaxed)
|
|
|
+ }
|
|
|
+
|
|
|
/// Moves a title texture's host copy to `wanted` (a host state) in this
|
|
|
/// list: the host texture and the state it leaves, if that is a change.
|
|
|
/// The list's first use of a texture records nothing: its prelude brings
|
|
|
@@ -5801,15 +5849,19 @@ impl HostList {
|
|
|
// SAFETY: as in `crate::resource::host_resource`; the list's states
|
|
|
// hold the copy from here on.
|
|
|
let texture: &'static ID3D12Resource = unsafe { &*std::ptr::from_ref(©.texture) };
|
|
|
- let wanted = legal_state(texture, wanted);
|
|
|
+ let wanted = legal_for(copy.flags(), wanted);
|
|
|
let mut states = self.states.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
match states.entry(std::sync::Arc::as_ptr(©) as usize) {
|
|
|
std::collections::hash_map::Entry::Vacant(v) => {
|
|
|
v.insert((wanted, wanted, copy));
|
|
|
+ self.changed_something();
|
|
|
None
|
|
|
}
|
|
|
std::collections::hash_map::Entry::Occupied(mut o) => {
|
|
|
let before = std::mem::replace(&mut o.get_mut().1, wanted);
|
|
|
+ if before != wanted {
|
|
|
+ self.changed_something();
|
|
|
+ }
|
|
|
(before != wanted).then_some((texture, before))
|
|
|
}
|
|
|
}
|
|
|
@@ -5820,7 +5872,8 @@ impl HostList {
|
|
|
/// records the barrier: for textures that are not a title resource's
|
|
|
/// host copy (colour twins) and for the copies between the two.
|
|
|
fn settle(&self, copy: &std::sync::Arc<crate::resource::HostTexture>, wanted: u32) {
|
|
|
- let wanted = legal_state(©.texture, wanted);
|
|
|
+ let wanted = legal_for(copy.flags(), wanted);
|
|
|
+ self.changed_something();
|
|
|
let mut states = self.states.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
let before = match states.entry(std::sync::Arc::as_ptr(copy) as usize) {
|
|
|
std::collections::hash_map::Entry::Vacant(v) => {
|
|
|
@@ -5884,6 +5937,7 @@ impl HostList {
|
|
|
writing: false,
|
|
|
late: false,
|
|
|
});
|
|
|
+ let was = (u.needs, u.behind, u.writing);
|
|
|
let copy = match u.behind[side] {
|
|
|
Some(behind) => behind,
|
|
|
None => {
|
|
|
@@ -5900,6 +5954,9 @@ impl HostList {
|
|
|
(true, false) => u.behind[1] = Some(true),
|
|
|
(false, _) => {}
|
|
|
}
|
|
|
+ if was != (u.needs, u.behind, u.writing) || copy || colour {
|
|
|
+ self.changed_something();
|
|
|
+ }
|
|
|
// shortcut: the depth plane's twin is not drawn into the plane
|
|
|
// here: the pass sets a pipeline and a target of ours, which a list
|
|
|
// still recording the title's commands cannot take. This use sees
|
|
|
@@ -6005,6 +6062,7 @@ impl HostList {
|
|
|
for u in twins.values_mut().filter(|u| u.writing) {
|
|
|
u.writing = false;
|
|
|
u.behind[0] = Some(true);
|
|
|
+ self.changed_something();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -6214,6 +6272,7 @@ impl HostList {
|
|
|
.copies
|
|
|
.push(Step::Copy(dst as usize, src as usize, len));
|
|
|
deferred.refreshed.clear();
|
|
|
+ self.changed_something();
|
|
|
}
|
|
|
|
|
|
/// Copies `len` bytes of title memory at `src` into the list's upload
|
|
|
@@ -6596,6 +6655,7 @@ impl HostList {
|
|
|
pub(crate) fn defer(&self, work: impl Fn() + Send + 'static) {
|
|
|
self.deferred().copies.push(Step::Run(Box::new(work)));
|
|
|
self.deferred().refreshed.clear();
|
|
|
+ self.changed_something();
|
|
|
}
|
|
|
|
|
|
/// As [`HostList::defer`], for work that writes only host memory.
|
|
|
@@ -6750,6 +6810,12 @@ impl HostList {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// The numbers of [`HostList::epoch`].
|
|
|
+fn next_epoch() -> u64 {
|
|
|
+ static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
|
|
+ NEXT.fetch_add(1, Ordering::Relaxed)
|
|
|
+}
|
|
|
+
|
|
|
/// A table by a copy's address.
|
|
|
pub(crate) type ByCopy<T> = std::collections::HashMap<usize, T, std::hash::BuildHasherDefault<AddressHash>>;
|
|
|
|
|
|
@@ -6772,6 +6838,12 @@ impl std::hash::Hasher for AddressHash {
|
|
|
let h = (self.0.rotate_left(5) ^ n as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
|
|
|
self.0 = h ^ (h >> 32);
|
|
|
}
|
|
|
+ fn write_u64(&mut self, n: u64) {
|
|
|
+ self.write_usize(n as usize);
|
|
|
+ }
|
|
|
+ fn write_u32(&mut self, n: u32) {
|
|
|
+ self.write_usize(n as usize);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// Since the last frame-rate line, nanoseconds of the submitting thread's
|