diff --git a/d3d12/crates/leonida-gpu/src/draw.rs b/d3d12/crates/leonida-gpu/src/draw.rs index ff6ad22280..30646dbd89 100644 --- a/d3d12/crates/leonida-gpu/src/draw.rs +++ b/d3d12/crates/leonida-gpu/src/draw.rs @@ -3062,6 +3062,9 @@ impl State { const RENDER_TARGET: u32 = 0x4; for &t in &self.targets { crate::list::ensure_state(list, t as This, RENDER_TARGET); + // A target laid out linearly goes back into title memory when the + // list closes (`crate::texback::targets_back`): the title's CPU reads it. + list.texture_written(t as This); } if self.depth != 0 { // A depth texture shaders also write: what they wrote, first. diff --git a/d3d12/crates/leonida-gpu/src/resource.rs b/d3d12/crates/leonida-gpu/src/resource.rs index 2a781c09e5..e976ae90df 100644 --- a/d3d12/crates/leonida-gpu/src/resource.rs +++ b/d3d12/crates/leonida-gpu/src/resource.rs @@ -3938,6 +3938,31 @@ pub(crate) fn texture_kind(this: This) -> Option<(u64, u32, u32)> { .then_some((r.desc.width, r.desc.height, r.desc.format)) } +/// Whether title texture `this` is a render target laid out linearly in +/// title memory (layout 1, or `0x100 |` the linear swizzle mode): a layout a +/// title picks for a target its CPU reads, as a photo it encodes to an image +/// file from that memory. Its host copy is the +/// target's only picture: what the card draws there goes back into title +/// memory (`crate::texback::wanted`). +#[cfg(windows)] +pub(crate) fn linear_target(this: This) -> bool { + if this.is_null() { + return false; + } + let r = resource(this); + linear_target_desc(&r.desc) +} + +/// [`linear_target`] of a description. +pub(crate) fn linear_target_desc(desc: &ResourceDesc) -> bool { + const TEXTURE_2D: u32 = 3; + const RENDER_TARGET: u32 = 1; + desc.dimension == TEXTURE_2D + && desc.sample_count <= 1 + && desc.flags & RENDER_TARGET != 0 + && (desc.layout == 1 || desc.layout == 0x100 | texture::Swizzle::LINEAR.0) +} + /// Texture `this`'s console layout (one plane), where it lies in title /// memory and the bytes that layout takes, for `crate::texback`; `None` for /// a buffer or a texture with planes. @@ -4386,6 +4411,26 @@ mod tests { } } + /// A render target laid out linearly (a photo the CPU reads) goes back + /// into title memory; a target of the library's pick, a tiled one, a + /// texture without the target flag, or one of several samples do not. + #[test] + fn only_linear_render_targets_go_back() { + let target = |layout: u32, flags: u32, samples: u32| ResourceDesc { + layout, + flags, + sample_count: samples, + ..texture(960, 536, 28, 1) + }; + assert!(super::linear_target_desc(&target(0x100, 0x300001, 1))); + assert!(super::linear_target_desc(&target(1, 0x1, 1))); + assert!(!super::linear_target_desc(&target(0, 0x300001, 1))); + assert!(!super::linear_target_desc(&target(0x100 | 27, 0x1, 1))); + assert!(!super::linear_target_desc(&target(0x100, 0, 1))); + assert!(!super::linear_target_desc(&target(0x100, 0x1, 4))); + assert!(!super::linear_target_desc(&buffer(4096))); + } + fn texture(width: u64, height: u32, format: u32, mips: u16) -> ResourceDesc { ResourceDesc { dimension: 3, diff --git a/d3d12/crates/leonida-gpu/src/texback.rs b/d3d12/crates/leonida-gpu/src/texback.rs index c578e23c82..883edba516 100644 --- a/d3d12/crates/leonida-gpu/src/texback.rs +++ b/d3d12/crates/leonida-gpu/src/texback.rs @@ -64,11 +64,22 @@ fn kinds() -> &'static [(u64, u32, u32)] { }) } -/// Whether title texture `resource` is of a kind copied back. +/// Whether title texture `resource` is of a kind copied back, or a render +/// target laid out linearly ([`targets_back`]). pub(crate) fn wanted(resource: This) -> bool { let kinds = kinds(); - !kinds.is_empty() - && crate::resource::texture_kind(resource).is_some_and(|k| kinds.contains(&k)) + (!kinds.is_empty() && crate::resource::texture_kind(resource).is_some_and(|k| kinds.contains(&k))) + || (targets_back() && crate::resource::linear_target(resource)) +} + +/// Whether what draws render into a render target laid out linearly in title +/// memory goes back into that memory (`LEONIDA_TARGET_BACK_LINEAR=0`: not, as +/// before). A title lays a target out linearly when its CPU reads it, as a +/// photo it encodes to an image file from title memory. Without this the +/// memory under such a target stayed zero, and those photos were saved black. +pub(crate) fn targets_back() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("LEONIDA_TARGET_BACK_LINEAR").is_none_or(|v| v != "0")) } /// Whether copies and resolves into textures the title fills go back into