diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 45c4916..d3d0717 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -2859,6 +2859,14 @@ pub(crate) fn texture_memory(this: This) -> Option<(*const u8, ResourceDesc)> { (r.desc.dimension != BUFFER).then(|| (r.data().cast_const(), r.desc)) } +/// Whether resource `this` lies in memory the title mapped itself (made at +/// an address): the only resource memory that can be unmapped while the +/// resource lives. A committed resource's memory and a heap's are ours, +/// allocated and zeroed whole when they were made. +pub(crate) fn at_title_address(this: This) -> bool { + !this.is_null() && matches!(resource(this).backing, Backing::Address(_)) +} + /// A buffer's host memory and size; `None` for textures. pub(crate) fn buffer_bytes(this: This) -> Option<(*mut u8, u64)> { if this.is_null() { @@ -3605,6 +3613,30 @@ mod tests { heap } + /// Only a resource made at an address the title mapped can lose its + /// memory while it lives (`crate::texture::staged` asks only of those). + #[test] + fn only_resources_at_a_title_address_may_lose_their_memory() { + let null = std::ptr::null(); + let committed = test_buffer(256); + assert!(!at_title_address(committed)); + let heap = heap(KIB_64); + let mut placed = null; + assert_eq!(create_placed(null, heap, 0, &texture(64, 64, 28, 1), 0, null.cast(), &RESOURCE_IIDS[0], &mut placed), S_OK); + assert!(!at_title_address(placed)); + let title = vec![0u8; KIB_64 as usize]; + let mut at = null; + assert_eq!( + create_at_address(null, title.as_ptr() as u64, &texture(64, 64, 28, 1), 0, null.cast(), &RESOURCE_IIDS[0], &mut at), + S_OK + ); + assert!(at_title_address(at)); + assert!(!at_title_address(null)); + for r in [committed, placed, at, heap] { + slot::(r, 0x10)(r); + } + } + #[test] fn heaps_are_zeroed_aligned_memory() { let heap = heap(1 << 20); diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 08314bb..ef1831f 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -1742,11 +1742,25 @@ pub(crate) fn is_staged(resource: This) -> bool { /// The bytes of a title texture's memory, and whether all of it is mapped, /// if the texture has been staged for upload. /// (Whether it is mapped is asked only if `ask`; else it counts as mapped.) +/// Only of a texture at an address the title mapped +/// ([`crate::resource::at_title_address`]): the memory of any other is all +/// there while it lives. The pass over textures picked by index asked it of +/// every texture object it took, 20,000-57,000 every 5 seconds, most of them +/// read through tables (a question per 256 pages: 18-111 us for 1-16 MB, +/// milliseconds when the pages had left the working set, and each such page +/// read back into it), for 66 found not all there in a whole run +/// (coolfox-20261126-223059); the pass's unattributed time went with their +/// count, 13 us each. `VICTORY_STAGED_ASK_ALL=1`: asked of every texture, +/// as before. pub(crate) fn staged(resource: This, ask: bool) -> Option<(u64, bool)> { + static ALL: std::sync::OnceLock = std::sync::OnceLock::new(); let s = { let map = STAGING.lock().unwrap_or_else(|e| e.into_inner()); map.as_ref()?.get(&(resource as usize))?.clone() }; + let ask = ask + && (*ALL.get_or_init(|| std::env::var_os("VICTORY_STAGED_ASK_ALL").is_some_and(|v| v == "1")) + || crate::resource::at_title_address(resource)); Some((s.read as u64, !ask || committed(s.memory, s.read))) }