use super::*;
const DRAW_GEN_PIPELINES: [&str; 9] = [
"clear counters",
"cull",
"draw args bin",
"draw args starts",
"draw args scatter",
"occlusion early cull",
"occlusion late cull",
"draw args late scatter",
"draw args late finish",
];
fn created(recorder: &Recorder, kind: ObjectKind, label: &str) -> usize {
recorder
.events()
.into_iter()
.filter(|event| {
matches!(
event,
Event::Created { kind: created, label: Some(created_label) }
if *created == kind && created_label == label
)
})
.count()
}
#[test]
fn every_draw_generator_shares_one_set_of_pipelines() {
let (recorder, device, queue) = open();
let device = device.as_ref();
recorder.clear();
let mut renderer = ForwardRenderer::new(device, queue, Format::Rgba8UnormSrgb).expect("built");
let generators = 1 + SHADOW_CULLS;
assert_eq!(
created(&recorder, ObjectKind::BindGroup, "cull"),
generators * FRAMES_IN_FLIGHT,
"the build made a cull group per frame for the camera and each shadow cull"
);
for label in DRAW_GEN_PIPELINES {
assert_eq!(
created(&recorder, ObjectKind::ComputePipeline, label),
1,
"the build compiled `{label}` other than once across {generators} generators"
);
}
recorder.clear();
let view = renderer
.create_view(device, queue, &ViewDesc::default())
.expect("a second view fits");
assert_eq!(
created(&recorder, ObjectKind::BindGroup, "cull"),
FRAMES_IN_FLIGHT,
"the view built a generator"
);
for label in DRAW_GEN_PIPELINES {
assert_eq!(
created(&recorder, ObjectKind::ComputePipeline, label),
0,
"the view compiled `{label}` again rather than sharing the renderer's"
);
}
renderer.destroy_view(device, view);
frame(device, &mut renderer, queue).release(device);
recorder.assert_valid();
renderer.destroy(device);
assert_eq!(recorder.total_live_objects(), 0, "the renderer leaked");
recorder.assert_valid();
}
#[test]
fn a_failed_build_releases_the_shared_pipelines() {
let recorder = Recorder::new();
let mut limits = crcbl_hal::Limits::desktop();
limits.max_color_attachments = 2;
let features = Features::COMPUTE | Features::MESH_SHADER;
let instance = NullInstance::new(crcbl_hal::DeviceCaps { features, limits })
.with_recorder(recorder.clone());
let device = instance
.create_device(&DeviceDesc {
label: None,
adapter: crcbl_hal::AdapterId(0),
required_features: features,
optional_features: Features::empty(),
compatible_surface: None,
})
.expect("the null backend always opens");
let queue = device.queue(QueueKind::Graphics).expect("always present");
let before = recorder.total_live_objects();
ForwardRenderer::with_scene_on_path(
device.as_ref(),
queue,
Format::Rgba8UnormSrgb,
&scene::demo(),
GeometryPath::MeshShader,
)
.expect_err("three-target colour pipelines must be refused");
for label in DRAW_GEN_PIPELINES {
assert_eq!(
created(&recorder, ObjectKind::ComputePipeline, label),
1,
"the refusal came before `{label}` was compiled, so this releases nothing of it"
);
}
assert_eq!(
recorder.total_live_objects(),
before,
"the failed build leaked"
);
recorder.assert_valid();
}