use super::batching::{BatchKey, Batcher, InstanceDraw};
use super::{DrawInstances, DrawList, Geometry};
use crate::content::identity::{Identity, MaterialId, ModelId};
use crate::shading::uniforms::ViewUniform;
use crate::shading::vertex::DrawInstance;
use crate::view::View;
use crate::view::pipelines::{Alpha, Cull, Variant};
use crate::view::population::Population;
use crate::{AlphaMode, InstanceState, Mobility, Scene, test_support};
use bytemuck::Zeroable;
use glam::{Mat4, Vec3};
use std::collections::{HashMap, HashSet};
#[test]
fn merged_draws_share_material_pipeline_and_geometry() {
let Some((device, queue)) = test_support::device() else {
return;
};
let mut asset = test_support::cube();
asset.materials[0].double_sided = false;
let mut masked = asset.materials[0].clone();
masked.alpha = AlphaMode::Mask { cutoff: 0.5 };
masked.base = [0.2, 0.4, 0.6, 1.];
asset.materials.push(masked);
let mut second = asset.meshes[0].clone();
second.material = 1;
asset.meshes.push(second);
let mut scene = Scene::new(&device, &queue);
let a = scene.add_asset(&device, &queue, asset.clone()).unwrap();
let b = scene.add_asset(&device, &queue, asset).unwrap();
let placed = [
(&a, false),
(&b, false),
(&a, true),
(&a, false),
(&a, false),
(&b, true),
(&a, true),
(&a, false),
(&b, false),
];
let mut authored = HashMap::new();
for (index, &(ids, mirrored)) in placed.iter().enumerate() {
let x = index as f32 * 2.;
let scale = if mirrored {
Vec3::new(-1., 1., 1.)
} else {
Vec3::ONE
};
let state = InstanceState {
model: ids.model,
pose: Mat4::from_scale_rotation_translation(
scale,
glam::Quat::IDENTITY,
Vec3::new(x, 0., -10.),
),
visible: true,
capture_visible: true,
};
let instance = scene
.add_instance(&device, &queue, state, Mobility::Static)
.unwrap();
for mesh in 0..2 {
authored.insert(
(instance.index(), mesh),
(ids.model, mesh, ids.materials[mesh], mirrored),
);
}
}
let camera = View::camera(ViewUniform {
view: Mat4::IDENTITY.to_cols_array_2d(),
projection: crate::perspective(1., 1., 0.1).to_cols_array_2d(),
..ViewUniform::zeroed()
});
let mut list = DrawList::default();
list.build(
&mut DrawInstances::default(),
&scene,
&camera,
None,
Population::CaptureShadow,
);
let mut drawn = HashSet::new();
let mut order: HashMap<usize, Vec<usize>> = HashMap::new();
for batch in &list.batches {
let Geometry::Mesh { mesh, .. } = batch.key.geometry else {
panic!("rigid meshes draw their own geometry");
};
let instances = list.instances_of(batch);
let shared: HashSet<_> = instances
.iter()
.map(|drawn| authored[&(drawn.object as usize, mesh)])
.collect();
assert_eq!(
shared.len(),
1,
"one draw holds instances authored differently: {:?}",
shared.iter().map(|tuple| tuple.3).collect::<Vec<_>>()
);
for instance in instances {
let instance = instance.object as usize;
assert!(
drawn.insert((instance, mesh)),
"mesh {mesh} of {instance} drawn twice"
);
order.entry(instance).or_default().push(mesh);
}
}
assert_eq!(
drawn.len(),
authored.len(),
"every mesh of every instance draws"
);
let distinct: HashSet<_> = authored.values().collect();
assert_eq!(
list.batches.len(),
distinct.len(),
"each set of equally authored draws is one draw"
);
for (instance, meshes) in order {
assert_eq!(
meshes,
[0, 1],
"instance {instance}'s meshes in authored order"
);
}
}
#[test]
fn draws_merge_only_with_the_same_ranges() {
let key = BatchKey {
variant: Variant {
cull: Cull::Back,
alpha: Alpha::Opaque,
deformed: false,
},
material: MaterialId::issue(0, 1),
geometry: Geometry::Clusters {
model: ModelId::issue(0, 1),
mesh: 0,
},
mobility: Mobility::Static,
};
let authored = [
vec![0..384, 768..1152],
vec![0..384, 1152..1536],
vec![0..384, 768..1152],
];
let mut ranges = Vec::new();
let mut batcher = Batcher::default();
for (object, drawn) in authored.iter().enumerate() {
let start = ranges.len();
ranges.extend(drawn.iter().cloned());
batcher.push(InstanceDraw {
key,
ranges: start..ranges.len(),
instance: DrawInstance {
object: object as u32,
..Zeroable::zeroed()
},
order: (0, 0),
});
}
let (mut batches, mut instances) = (Vec::new(), Vec::new());
batcher.bin((&ranges, &mut batches, &mut instances));
let mut together: Vec<Vec<u32>> = batches
.iter()
.map(|batch| {
let held: Vec<u32> = instances
[batch.instances.start as usize..batch.instances.end as usize]
.iter()
.map(|drawn| drawn.object)
.collect();
for &object in &held {
assert_eq!(ranges[batch.ranges.clone()], authored[object as usize][..]);
}
held
})
.collect();
together.sort();
assert_eq!(together, [vec![0, 2], vec![1]]);
}
#[test]
fn draws_merge_only_with_the_same_mobility() {
let key = |mobility| BatchKey {
variant: Variant {
cull: Cull::Back,
alpha: Alpha::Opaque,
deformed: false,
},
material: MaterialId::issue(0, 1),
geometry: Geometry::Mesh {
model: ModelId::issue(0, 1),
mesh: 0,
},
mobility,
};
let authored = [
Mobility::Static,
Mobility::Static,
Mobility::Moving,
Mobility::Moving,
Mobility::Static,
];
let ranges = std::iter::once(0..384).collect::<Vec<_>>();
let batcher = || {
let mut batcher = Batcher::default();
for (object, &mobility) in authored.iter().enumerate() {
batcher.push(InstanceDraw {
key: key(mobility),
ranges: 0..1,
instance: DrawInstance {
object: object as u32,
..Zeroable::zeroed()
},
order: (0, 0),
});
}
batcher
};
let held = |batches: &[super::DrawBatch], instances: &[DrawInstance]| -> Vec<Vec<u32>> {
batches
.iter()
.map(|batch| {
let held: Vec<u32> = instances
[batch.instances.start as usize..batch.instances.end as usize]
.iter()
.map(|drawn| drawn.object)
.collect();
for &object in &held {
assert_eq!(batch.key.mobility, authored[object as usize]);
}
held
})
.collect()
};
let (mut batches, mut instances) = (Vec::new(), Vec::new());
batcher().bin((&ranges, &mut batches, &mut instances));
let mut binned = held(&batches, &instances);
binned.sort();
assert_eq!(binned, [vec![0, 1, 4], vec![2, 3]], "binned");
let (mut batches, mut instances) = (Vec::new(), Vec::new());
batcher().merge_adjacent((&ranges, &mut batches, &mut instances));
assert_eq!(
held(&batches, &instances),
[vec![0, 1], vec![2, 3], vec![4]],
"merged where adjacent"
);
}