Skip to content

Commit 191fa0f

Browse files
committed
feat(compositor): the Metal pass keeps what the scene shows, and maps the footage plane
Two things from the review of integration/shader-effects (#1213 and the review fix) that the Metal pass did not have yet. - The compiled-effect cache never evicts an effect the current scene shows (shader_fx::eviction_victim), as on Windows and Linux: set_scene installs the scene before it starts the compiles. - The Windows GPU test of fx_to_footage and fx_from_footage, ported to the Metal device: the footage plane maps back to what the compositor drew, flat, under a fixed angle (bilinear) and under the real camera (homography). It also checks their MSL translation.
1 parent e16a014 commit 191fa0f

1 file changed

Lines changed: 68 additions & 8 deletions

File tree

‎crates/compositor/src/compositor_macos.rs‎

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,14 @@ impl Compositor {
873873

874874
/// Installe (ou retire) la scène de l'app. Les effets shader nouveaux partent compiler tout de
875875
/// suite, hors du thread de rendu : à la première frame qui les montre, ils sont en général
876-
/// déjà prêts.
876+
/// déjà prêts. Après la pose de la scène : l'éviction épargne ce qu'elle montre.
877877
pub fn set_scene(&self, s: Option<Scene>) {
878-
if let Some(scene) = &s {
878+
*self.scene.borrow_mut() = s;
879+
if let Some(scene) = self.scene.borrow().as_ref() {
879880
for effect in &scene.shader_effects {
880881
self.shader_fx_entry(effect);
881882
}
882883
}
883-
*self.scene.borrow_mut() = s;
884884
}
885885

886886
/// Faux pour la preview : voir `shader_fx_blocking`.
@@ -925,9 +925,12 @@ impl Compositor {
925925
used: tick,
926926
})
927927
.used = tick;
928-
// Un agent qui itère crée une entrée par version : on garde les plus récentes.
928+
// Un agent qui itère crée une entrée par version : on garde les plus récentes, et toutes
929+
// celles que la scène montre.
929930
if fx.len() > SHADER_FX_CACHE {
930-
if let Some(oldest) = fx.iter().min_by_key(|(_, e)| e.used).map(|(k, _)| *k) {
931+
let scene = self.scene.borrow();
932+
let shown = scene.as_ref().map_or(&[][..], |s| &s.shader_effects[..]);
933+
if let Some(oldest) = crate::shader_fx::eviction_victim(fx.iter().map(|(k, e)| (*k, e.used)), shown) {
931934
fx.remove(&oldest);
932935
}
933936
}
@@ -4777,7 +4780,11 @@ mod tests {
47774780
/// Une scène à fond rouge plein, l'écran rétréci par le padding : les coins sont du fond.
47784781
/// `annotations` : celles de la scène, pour les tests de cible.
47794782
fn shader_fx_scene_with(effects: serde_json::Value, annotations: serde_json::Value) -> Scene {
4780-
let json = serde_json::json!({
4783+
Scene::from_json(&shader_fx_json(effects, annotations).to_string()).expect("scène")
4784+
}
4785+
4786+
fn shader_fx_json(effects: serde_json::Value, annotations: serde_json::Value) -> serde_json::Value {
4787+
serde_json::json!({
47814788
"clips": [],
47824789
"layout": {"preset":"no-webcam","webcamSize":1,"webcamShape":"rectangle","webcamMirror":false,"webcamPosition":null,"webcamReactiveZoom":false},
47834790
"effects": {"padding":0.5,"blur":false,"shadow":0,"roundnessFrac":0,"motionBlur":0},
@@ -4788,8 +4795,7 @@ mod tests {
47884795
"cropByClip": [],
47894796
"output": {"width":320,"height":180,"fps":null},
47904797
"shaderEffects": effects,
4791-
});
4792-
Scene::from_json(&json.to_string()).expect("scène")
4798+
})
47934799
}
47944800

47954801
fn shader_fx_scene(effects: serde_json::Value) -> Scene {
@@ -4898,6 +4904,60 @@ mod tests {
48984904
assert_eq!(corner, [0, 255, 0]);
48994905
}
49004906

4907+
/// `fx_to_footage` retrouve le point du métrage que le compositeur a dessiné sous un pixel,
4908+
/// à plat, sous un angle fixe (warp bilinéaire) et sous la caméra réelle (homographie), et
4909+
/// `fx_from_footage` le ramène sur ce pixel. En 1080p, l'arrondi 8 bits et le demi-pixel
4910+
/// tiennent sous 0,005 ; la pose orbit testée écarte l'homographie du bilinéaire de ~0,01.
4911+
/// Le pendant du test Windows : les deux fonctions passent ici par la traduction MSL.
4912+
#[test]
4913+
fn the_footage_plane_maps_back_to_what_the_compositor_drew() {
4914+
let Ok(gpu) = crate::d3d::Gpu::create(false) else {
4915+
eprintln!("pas de device Metal — test sauté");
4916+
return;
4917+
};
4918+
const PLANE: &str = "fn effect(uv: vec2<f32>) -> vec4<f32> {\n let p = fx_to_footage(uv);\n return vec4<f32>(p, distance(fx_from_footage(p), uv) * 100.0, 1.0);\n}\n";
4919+
for (rotation, projective) in [(None, false), (Some("left"), false), (Some("orbit"), true)] {
4920+
let mut json = shader_fx_json(serde_json::json!([fx(PLANE, "image", 0)]), serde_json::json!([]));
4921+
json["clips"] = serde_json::json!([{"screenPath": "/s.mp4", "webcamPath": "", "sourceStartSec": 0,
4922+
"sourceEndSec": 10, "webcamOffsetSec": 0, "hasAudio": false}]);
4923+
json["layout"]["screenRect"] = serde_json::json!({"x": 0.1, "y": 0.1, "width": 0.8, "height": 0.8});
4924+
json["cropByClip"] = serde_json::json!([null]);
4925+
if let Some(rotation) = rotation {
4926+
json["zoomRegions"] = serde_json::json!([{"clipIndex": 0, "startSec": 0, "endSec": 10,
4927+
"scale": 1.5, "focusX": 0.5, "focusY": 0.5, "rotation": rotation}]);
4928+
}
4929+
let comp = super::Compositor::new_sized(&gpu, 1920, 1080).expect("compositeur");
4930+
let scene = Scene::from_json(&json.to_string()).expect("scène");
4931+
comp.set_live_params(live_params_from_scene(&scene));
4932+
comp.set_scene(Some(scene));
4933+
let screen = FakeFrame::new(64, 64, |_, _| 126);
4934+
comp.set_timeline_time(Some(2.0));
4935+
comp.set_programme_time(Some(2.0));
4936+
unsafe { comp.compose_frame(screen.as_ptr(), screen.as_ptr(), 0.0, &fx_cfg()) }.expect("compose");
4937+
let quad = comp.footage_quad().expect("le métrage de la frame");
4938+
assert_eq!(quad.projective, projective, "{rotation:?}");
4939+
let (w, h, rgba) = unsafe { comp.readback_direct() }.expect("readback");
4940+
let [tl, tr, br, bl] = quad.corners.map(|[x, y]| (x, y));
4941+
for (s, t) in [(0.25, 0.3), (0.7, 0.4), (0.4, 0.75)] {
4942+
let (x, y) = if projective {
4943+
crate::regions::square_to_quad(&[tl, tr, br, bl], s, t)
4944+
} else {
4945+
let top = (tl.0 + (tr.0 - tl.0) * s, tl.1 + (tr.1 - tl.1) * s);
4946+
let bottom = (bl.0 + (br.0 - bl.0) * s, bl.1 + (br.1 - bl.1) * s);
4947+
(top.0 + (bottom.0 - top.0) * t, top.1 + (bottom.1 - top.1) * t)
4948+
};
4949+
let i = ((y * h as f32) as usize * w as usize + (x * w as f32) as usize) * 4;
4950+
let got = (rgba[i] as f32 / 255.0, rgba[i + 1] as f32 / 255.0);
4951+
assert!(
4952+
(got.0 - s).abs() < 0.005 && (got.1 - t).abs() < 0.005,
4953+
"{rotation:?} ({s}, {t}) : lu {got:?}, coins {:?}",
4954+
quad.corners
4955+
);
4956+
assert!(rgba[i + 2] < 3, "{rotation:?} ({s}, {t}) : aller-retour à {}", rgba[i + 2]);
4957+
}
4958+
}
4959+
}
4960+
49014961
/// Les lanes s'appliquent dans l'ordre : bleu puis inversion donne jaune, l'inverse donne bleu.
49024962
#[test]
49034963
fn lanes_apply_in_order() {

0 commit comments

Comments
 (0)