diff --git a/README.md b/README.md index 04fa578..1336372 100644 --- a/README.md +++ b/README.md @@ -164,9 +164,9 @@ What did it, on top of the list below: flight); the copy-back thread's wait fell from about 125 to 7 ms a frame. - `VICTORY_INDIRECT_WINDOW=1`: see above. - - `VICTORY_TEXTURE_NO_WAIT=1`: a texture refresh no longer waits while - the title remaps memory; the loading phase had 5 readings over a - second where it had 18. + - Texture refreshes do not wait while the title remaps memory by default + (`VICTORY_TEXTURE_NO_WAIT=0` restores the wait); the loading phase had + 5 readings over a second where it had 18. - `VICTORY_MERGE_GAP=64 VICTORY_MERGE_RUNS=4096`: title bytes put in under flights' shader writes are joined over small gaps (NOT exact: read the comment at `merge_gap` before using it). diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 2ae7188..3195509 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -9178,7 +9178,7 @@ impl Meter { .map(|i| crate::texture::UNLOCKED[i].swap(0, Ordering::Relaxed) as f64 / frames); if through + left > 0.0 { tracing::info!( - "texture refreshes that did not wait for title memory to hold still, per frame (VICTORY_TEXTURE_NO_WAIT=1): \ + "texture refreshes that did not wait for title memory to hold still, per frame (VICTORY_TEXTURE_NO_WAIT=0 restores the wait): \ {through:.1} read through the kernel, {left:.1} left for the next refresh (a page was not there)" ); } diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index fe1e372..8fcdf90 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -939,6 +939,9 @@ extern "system" fn copy_resource(this: This, dst: This, src: This) { // A source the title fills itself is uploaded first (upstream // PR #671): a copy read whatever the host texture held. if crate::texture::title_written(from) { + if !crate::texture::host_wrote(src as usize) { + crate::texture::refresh_copy_source(src, from); + } crate::texture::upload(list, src, from); } // Depth textures shaders also write: what they wrote, first. diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index c1d93d4..58ebff1 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -484,9 +484,14 @@ pub(crate) fn alias(list: &HostList, placed: This, source: This) -> bool { true } +thread_local! { + /// CopyResource sources waiting for remap exclusion on this thread. + static WAIT_FOR_COPY: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// Whether a texture refresh that finds the runtime remapping title memory -/// goes on without waiting for it (`VICTORY_TEXTURE_NO_WAIT=1`; off by -/// default). +/// goes on without waiting for it (on by default; +/// `VICTORY_TEXTURE_NO_WAIT=0` restores the wait). /// /// A texture whose memory is all mapped is read in place, under a lock /// the runtime takes alone while it changes a mapping @@ -508,7 +513,8 @@ pub(crate) fn alias(list: &HostList, placed: This, source: This) -> bool { /// is remapping may show a frame later. It never shows partly read. fn no_wait() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); - *ON.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_NO_WAIT").is_some_and(|v| v == "1")) + *ON.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_NO_WAIT").is_none_or(|v| v != "0")) + && WAIT_FOR_COPY.with(|wait| wait.get() == 0) } /// Since the last frame-rate line, with [`no_wait`]: refreshes that read @@ -844,6 +850,31 @@ pub(crate) fn title_written(texture: &ID3D12Resource) -> bool { unsafe { texture.GetDesc() }.Flags.0 & TARGET_DEPTH_UNORDERED == 0 } +/// Refreshes a texture used as a CopyResource source, waiting for remap +/// exclusion even when ordinary texture refreshes do not. A failed refresh +/// at submission must not leave the copy destination with pixels older than +/// the copy the title asked for: the destination is host-written afterwards +/// and cannot be repaired by the source's later upload. +pub(crate) fn refresh_copy_source(resource: This, texture: &ID3D12Resource) { + let Some(staging) = staging(resource, texture) else { + return; + }; + WAIT_FOR_COPY.with(|wait| { + wait.set(wait.get() + 1); + let read = staging.refresh_now(); + wait.set(wait.get() - 1); + // The copy's submission must look again even when this texture is + // partly mapped: more of it may arrive before the list runs. + staging + .last + .lock() + .unwrap_or_else(|e| e.into_inner()) + .partial = None; + let preparing = &crate::host::PREPARING; + preparing[3].fetch_add(read as u64, std::sync::atomic::Ordering::Relaxed); + }); +} + /// Records, once per list, the copy of `resource`'s staging buffer into /// its host copy, and the detiling that refreshes the staging buffer at /// submission if the memory changed. Leaves the texture in the copy