diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md index 92d1cd04..ae61989d 100644 --- a/docs/ENVIRONMENT.md +++ b/docs/ENVIRONMENT.md @@ -115,6 +115,7 @@ The launcher sets these for a Proton or Wine launch. A value you set yourself is | `TUXBOX_PARALLEL_PUBLICATION` | `0` publishes every texture of a readback batch in order on the queue's publication thread, instead of on several threads when the batch is large and its textures' memory does not overlap | | `TUXBOX_XMEM_SECTION_WATCH` | `1` watches section-backed XMem pages for CPU writes by protecting their views read-only until the first store, so placed textures over them are not digested before every batch (#472). Pages with a GPU alias or under an import hold stay unwatched | | `TUXBOX_LAZY_PUBLICATION` | A texture the GPU wrote stays unpublished until something touches its title memory, when nothing but the CPU could read it there (no imported buffer, and no other placed image over its bytes or in its pages): its pages are made inaccessible and the first access publishes it. `0` publishes every written texture after its batch; `count` only logs what would be deferred. Not yet checked on a device that an access reads the GPU's bytes | +| `TUXBOX_LAZY_PUBLICATION_RESERVED` | `1` defers a reserved (tiled) image's publication too. By default it is published with its batch: a publication on access copies it on a queue of its own, and for reserved images that copy did not run and the copy engine faulted | | `TUXBOX_READ_RESIDENCY` | Gives programs that index the resource heap the heap bitmap (`0` turns it off), whose second half their translations use to note each image view they take from the heap. A batch whose heap reach came only from such programs then backs and refreshes only the textures read in the last 120 frames, instead of every texture with a view in the heap. A texture no program has read yet has no memory: the batch that first reads it reads zeros, and the next batch backs it. Changes those programs' roots, so turning it off or on translates them again | | `TUXBOX_GATHER_ALWAYS` | Copy every queue-backed texture from its pages in every batch that uses it (`1`), as before skipping was added. Otherwise a batch with no title fence wait pending ahead of it uses the image as it is when nothing can have changed its pages since the last copy: no CPU write or mapping change XMem saw, no GPU write the provider recorded, and no new mip memory. For comparing pictures | | `TUXBOX_REQUIRE_BUFFER_IMPORT` | `1` fails instead of falling back when memory can't be imported | diff --git a/runtime/d3d12/src/device/placement/lazy.rs b/runtime/d3d12/src/device/placement/lazy.rs index 2c7351d5..6d8b6bc7 100644 --- a/runtime/d3d12/src/device/placement/lazy.rs +++ b/runtime/d3d12/src/device/placement/lazy.rs @@ -27,7 +27,7 @@ use core::ffi::c_void; use core::ops::Range; use std::collections::BTreeMap; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, LazyLock, Mutex, Once, Weak}; +use std::sync::{Arc, LazyLock, Mutex, Once, OnceLock, Weak}; use std::time::{Duration, Instant}; use windows::Win32::Graphics::Direct3D12::{D3D12_COMMAND_LIST_TYPE_COPY, ID3D12Device}; use windows::Win32::System::Diagnostics::Debug::{AddVectoredExceptionHandler, EXCEPTION_POINTERS}; @@ -392,9 +392,34 @@ fn set_protection(page: u64, protection: u32) -> Option { .map(|()| old.0) } +/// Whether a reserved (tiled) image's publication is deferred too: +/// `TUXBOX_LAZY_PUBLICATION_RESERVED=1`. By default it is not. A publication +/// on access copies the image on a copy queue of its own, not the queue that +/// wrote it, and for reserved images that copy never ran: the title's thread +/// waited in its access fault and the copy engine then faulted (Xid 32). The +/// batch's own readback, on the queue that wrote the image, publishes them. +pub(super) fn defers_reserved(setting: Option<&str>) -> bool { + setting.map(str::trim) == Some("1") +} + +/// Whether `texture` may wait for an access: a reserved image only when +/// reserved images are deferred. +pub(super) fn may_defer(reserved: bool, reserved_deferred: bool) -> bool { + !reserved || reserved_deferred +} + +fn reserved_deferred() -> bool { + static DEFER: OnceLock = OnceLock::new(); + *DEFER.get_or_init(|| defers_reserved(std::env::var("TUXBOX_LAZY_PUBLICATION_RESERVED").ok().as_deref())) +} + /// Leave `texture` unpublished until something touches its pages. False if -/// they could not be protected, in which case the caller publishes it now. +/// they could not be protected, or `texture` is a reserved image (see +/// `defers_reserved`), in which case the caller publishes it now. pub(super) fn defer(texture: &Arc, device: &ID3D12Device) -> bool { + if !may_defer(texture.reserved(), reserved_deferred()) { + return false; + } install_handler(); let mut state = lock(&STATE); if texture.deferred.load(Ordering::Acquire) { diff --git a/runtime/d3d12/src/device/placement/residency.rs b/runtime/d3d12/src/device/placement/residency.rs index ca135140..3ba2ef5e 100644 --- a/runtime/d3d12/src/device/placement/residency.rs +++ b/runtime/d3d12/src/device/placement/residency.rs @@ -458,6 +458,11 @@ impl Residency { } impl Texture { + /// Whether the image is reserved (its memory mapped in tiles). + pub(super) fn reserved(&self) -> bool { + matches!(self.residency, Residency::Reserved { .. }) + } + /// What queued work must keep until the GPU has finished with this image. pub(super) fn hold(&self) -> Held { let heaps = match &self.residency { diff --git a/runtime/d3d12/src/tests/device/placement/lazy.rs b/runtime/d3d12/src/tests/device/placement/lazy.rs index c3dda1ef..cc4dabf2 100644 --- a/runtime/d3d12/src/tests/device/placement/lazy.rs +++ b/runtime/d3d12/src/tests/device/placement/lazy.rs @@ -258,3 +258,14 @@ mod pages { assert!(pages.covers(0x2000)); } } + +#[test] +fn reserved_images_are_published_with_their_batch_unless_the_switch_defers_them() { + use super::{defers_reserved, may_defer}; + assert!(!defers_reserved(None), "reserved images are not deferred by default"); + assert!(!defers_reserved(Some("0"))); + assert!(defers_reserved(Some("1"))); + assert!(may_defer(false, false), "a committed image is deferred either way"); + assert!(!may_defer(true, false)); + assert!(may_defer(true, true)); +}