From 2c165e09cd573c266640d7b92d9dd43aac9adaec Mon Sep 17 00:00:00 2001 From: local Date: Wed, 25 Nov 2026 05:43:37 -0600 Subject: [PATCH] The first copy into a texture the title fills goes over zeros VICTORY_COPY_OVER_TITLE=title: over what title memory holds; =0: over whatever the block of the pool held. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/list.rs | 28 ++++++++++++++++------ crates/victory-gpu/src/texture.rs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index 84aa3a3..b0a1c33 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -1024,19 +1024,33 @@ extern "system" fn copy_texture_region( // source above too). list.twin_use(dst.resource, false, true); // The first copy into a texture of the title's: what it does not - // write is what title memory holds, as on the console. (The host + // write is zeros, as in a texture that was an allocation of its own + // (`VICTORY_COPY_OVER_TITLE=title`: what title memory holds, which + // may be another texture's: specks on the waitress's forehead were + // left in run n04). (The host // copy is cut from a block and held whatever lay there, and no // upload is made once a copy wrote it: the atlas the title copies // its baked decorations into, a 2048x2048 BC3 texture of which a // bake writes 256x256 at most, was drawn over a character's face // with other textures' bytes wherever nothing was baked, runs n01 and // textures-1.) - static OVER: std::sync::OnceLock = std::sync::OnceLock::new(); - if *OVER.get_or_init(|| std::env::var_os("VICTORY_COPY_OVER_TITLE").is_none_or(|v| v != "0")) - && crate::texture::title_written(to) - && !crate::texture::host_wrote(dst.resource as usize) - { - crate::texture::upload(list, dst.resource, to); + static OVER: std::sync::OnceLock = std::sync::OnceLock::new(); + let over = *OVER.get_or_init(|| match std::env::var("VICTORY_COPY_OVER_TITLE").as_deref() { + Ok("0") => 0, + Ok("title") => 1, + _ => 2, + }); + if over != 0 && crate::texture::title_written(to) && !crate::texture::host_wrote(dst.resource as usize) { + // One a list uploaded, or a draw sampled, holds the title's. + if over == 2 && !list.used(dst.resource as usize) && !crate::texture::is_staged(dst.resource) { + if let Some(host) = crate::host::host() { + ensure_state(list, dst.resource, COPY_DEST); + let _ = list.first_use(dst.resource as usize); + crate::texture::zeros(list, host, to); + } + } else { + crate::texture::upload(list, dst.resource, to); + } // (Done before the title's copy writes over it.) if list.used(dst.resource as usize) { ensure_state(list, dst.resource, COPY_SOURCE); diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 7ad9915..dc7bad1 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -937,6 +937,46 @@ impl Drop for Claimed { } } +/// Records copies of zeros into every subresource of `texture`, which is in +/// the copy destination state. +pub(crate) fn zeros(list: &HostList, host: &crate::host::Host, texture: &ID3D12Resource) { + // SAFETY: a live resource. + let d = unsafe { texture.GetDesc() }; + let layers = if d.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D { 1 } else { u32::from(d.DepthOrArraySize) }; + let count = u32::from(d.MipLevels) * layers; + let mut footprints = vec![D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); count as usize]; + let mut bytes = 0u64; + // SAFETY: outputs for `count` subresources. + unsafe { + host.device + .GetCopyableFootprints(&d, 0, count, 0, Some(footprints.as_mut_ptr()), None, None, Some(&mut bytes)); + } + if bytes == 0 || bytes == u64::MAX { + return; + } + let Some((buffer, data)) = list.upload(host, bytes) else { + return; + }; + // SAFETY: a mapped buffer of `bytes` bytes, just made. + unsafe { std::ptr::write_bytes(data, 0, bytes as usize) }; + for (i, footprint) in footprints.iter().enumerate() { + let to = D3D12_TEXTURE_COPY_LOCATION { + // SAFETY: a borrowed pointer the location never releases. + pResource: unsafe { std::mem::transmute_copy(texture) }, + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: i as u32 }, + }; + let from = D3D12_TEXTURE_COPY_LOCATION { + // SAFETY: as above; the list holds the buffer until it is reset. + pResource: unsafe { std::mem::transmute_copy(&buffer) }, + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: *footprint }, + }; + // SAFETY: an open list; both locations name live resources. + unsafe { list.list.CopyTextureRegion(&to, 0, 0, 0, &from, None) }; + } +} + /// Levels on demand (`crate::tiles`): the card itself is about to write /// title texture `resource` in `list` (a copy or a resolve into level /// `level`, or into every level). If its host copy `texture` was made