mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 15:00:42 +01:00
Test retained-map allocations in streamed partial bakes
This commit is contained in:
1 parent
6cdc7789cb
commit
3fc540e570
2 files changed
+115
No files matched your search
@@ -107,3 +107,104 @@ impl BakeTask {
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Session, execute};
|
||||
use serde_json::json;
|
||||
use texturecraft_geom::primitives;
|
||||
|
||||
const PREVIOUS_MAPS: [BakedMap; 3] = [BakedMap::Normal, BakedMap::AmbientOcclusion, BakedMap::Height];
|
||||
const SELECTED_MAPS: [BakedMap; 2] = [BakedMap::AmbientOcclusion, BakedMap::Height];
|
||||
|
||||
fn partial_task(selected: &[BakedMap], resized: bool, excluded: bool) -> BakeTask {
|
||||
let mut session = Session::new_project("Partial bake", primitives::udim_plane(), "builtin:udim_plane", None, 128).unwrap();
|
||||
execute(&mut session, "bake.settings", &json!({"resolution": 64, "rays": 2})).unwrap();
|
||||
if excluded {
|
||||
execute(&mut session, "bake.tile.set", &json!({"tile": 1002, "enabled": false})).unwrap();
|
||||
}
|
||||
let mut task = session.bake_task(0).unwrap();
|
||||
task.tracer = None;
|
||||
task.settings.maps.select(selected.iter().copied());
|
||||
let (width, height) = if resized { (64, 32) } else { (128, 64) };
|
||||
task.previous_maps = BakedMaps {
|
||||
resolution: width,
|
||||
maps: PREVIOUS_MAPS.into_iter().map(|map| (map, Arc::new(vec![0.25; width as usize * height * map.components()]))).collect(),
|
||||
};
|
||||
task
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streaming_allocates_only_truly_unchecked_resized_maps_once() {
|
||||
for (selected, resized, excluded) in [
|
||||
(SELECTED_MAPS.as_slice(), true, true),
|
||||
(PREVIOUS_MAPS.as_slice(), true, true),
|
||||
(SELECTED_MAPS.as_slice(), false, true),
|
||||
(SELECTED_MAPS.as_slice(), true, false),
|
||||
] {
|
||||
let task = partial_task(selected, resized, excluded);
|
||||
let whole = task.run(&Progress::new()).unwrap();
|
||||
super::super::preserve_tiles::take_retained_allocations();
|
||||
let mut allocations = Vec::new();
|
||||
let mut handed_over = Vec::new();
|
||||
let output = task
|
||||
.run_streaming(&Progress::new(), &mut |map| {
|
||||
allocations.extend(super::super::preserve_tiles::take_retained_allocations());
|
||||
assert!(allocations.iter().all(|map| !selected.contains(map)), "maps scheduled to bake must never allocate retained buffers");
|
||||
handed_over.push(map);
|
||||
})
|
||||
.unwrap();
|
||||
allocations.extend(super::super::preserve_tiles::take_retained_allocations());
|
||||
let expected: Vec<_> = PREVIOUS_MAPS.into_iter().filter(|map| resized && excluded && !selected.contains(map)).collect();
|
||||
assert_eq!(allocations, expected, "each truly unchecked resized map must allocate exactly once");
|
||||
assert_eq!(output.maps.keys().collect::<Vec<_>>(), whole.maps.keys().collect::<Vec<_>>());
|
||||
for (map, pixels) in &whole.maps {
|
||||
assert_eq!(*output.maps[map], *pixels, "{map:?} must preserve the ordinary bake's pixels");
|
||||
}
|
||||
for map in handed_over {
|
||||
assert!(Arc::ptr_eq(&map.data, &output.maps[&map.map]), "streaming must keep sharing the baked buffer");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelling_a_partial_stream_does_not_resample_future_maps() {
|
||||
let task = partial_task(&SELECTED_MAPS, true, true);
|
||||
let whole = task.run(&Progress::new()).unwrap();
|
||||
super::super::preserve_tiles::take_retained_allocations();
|
||||
let progress = Progress::new();
|
||||
let mut handed_over = Vec::new();
|
||||
let result = task.run_streaming(&progress, &mut |map| {
|
||||
handed_over.push(map);
|
||||
progress.cancel();
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(handed_over.len(), 1);
|
||||
for map in handed_over {
|
||||
assert_eq!(*map.data, whole.maps[&map.map], "cancellation leaves the handed-over map whole");
|
||||
}
|
||||
assert_eq!(super::super::preserve_tiles::take_retained_allocations(), [BakedMap::Normal]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_streaming_selection_keeps_the_ordinary_bake_behavior() {
|
||||
let task = partial_task(&[], true, true);
|
||||
super::super::preserve_tiles::take_retained_allocations();
|
||||
let whole = task.run(&Progress::new());
|
||||
let expected_allocations = super::super::preserve_tiles::take_retained_allocations();
|
||||
let output = task.run_streaming(&Progress::new(), &mut |_| panic!("an empty selection must not stream a map"));
|
||||
assert_eq!(super::super::preserve_tiles::take_retained_allocations(), expected_allocations);
|
||||
match (whole, output) {
|
||||
(Ok(whole), Ok(output)) => {
|
||||
assert_eq!((output.resolution, output.height), (whole.resolution, whole.height));
|
||||
assert_eq!(output.maps.keys().collect::<Vec<_>>(), whole.maps.keys().collect::<Vec<_>>());
|
||||
for (map, pixels) in whole.maps {
|
||||
assert_eq!(*output.maps[&map], pixels);
|
||||
}
|
||||
}
|
||||
(Err(whole), Err(output)) => assert_eq!(whole.to_string(), output.to_string()),
|
||||
_ => panic!("empty streaming and ordinary bakes must agree"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,8 @@ pub(super) fn restore(output: &mut BakeOutput, previous: &BakedMaps, excluded_ti
|
||||
.ok_or_else(|| EngineError::Other("resized bake map exceeds the addressable size".into()))?;
|
||||
let mut pixels = Vec::new();
|
||||
pixels.try_reserve_exact(count).map_err(|error| EngineError::Other(format!("cannot retain mesh map {}: {error}", map.id())))?;
|
||||
#[cfg(test)]
|
||||
RETAINED_ALLOCATIONS.with(|allocations| allocations.borrow_mut().push(*map));
|
||||
for y in 0..size.1 {
|
||||
for x in 0..size.0 {
|
||||
let value = previous.sample(*map, x, y, size).unwrap_or_else(|| map.neutral());
|
||||
@@ -52,3 +54,15 @@ pub(super) fn restore(output: &mut BakeOutput, previous: &BakedMaps, excluded_ti
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Restoration is sequential on the calling thread. Record successful retained-buffer allocations
|
||||
// only in unit tests, without a global allocator or interference from parallel test threads.
|
||||
#[cfg(test)]
|
||||
std::thread_local! {
|
||||
static RETAINED_ALLOCATIONS: std::cell::RefCell<Vec<texturecraft_core::BakedMap>> = const { std::cell::RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(super) fn take_retained_allocations() -> Vec<texturecraft_core::BakedMap> {
|
||||
RETAINED_ALLOCATIONS.with(|allocations| std::mem::take(&mut *allocations.borrow_mut()))
|
||||
}
|
||||
Reference in new issue
Block a user