Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions d3d12/crates/leonida-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions d3d12/crates/leonida-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions d3d12/crates/leonida-gpu/src/texback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> = 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
Expand Down
Loading