665bfac0b339
viewer_frames.rs
use super::*;
impl EffectcraftApp {
/// The size of the viewer frame's texture (CPU frames; fitted to the GPU's texture limit).
pub fn viewer_texture_size(&self) -> Option<[usize; 2]> {
self.viewer_tex.as_ref().map(|(t, _, _)| t.size())
}
/// The viewer's current pixels as 8-bit premultiplied RGBA, reading a GPU frame back the
/// first time something asks for it. A half-float frame (32 bpc) is converted the first time:
/// as rendered with Project Settings ▸ HDR, without the viewer's exposure or display colour.
///
/// The browser can't wait for the GPU: there the first call starts an asynchronous readback
/// and returns `None`; a later call (the next frame) has the pixels.
pub fn viewer_pixels(&mut self) -> Option<Arc<egui::ColorImage>> {
if self.viewer_image.is_none()
&& let Some(f) = &self.viewer_f16
{
let pass = effectcraft_engine::viewer::DisplayPass::new(effectcraft_engine::viewer::Channel::Rgb, false, 0.0, f.encoding, None);
self.viewer_image = Some(Arc::new(f.to_color_image(&pass)));
}
let to_image = |bytes: Vec<u8>, w: u32, h: u32| {
let px = bytes.as_chunks::<4>().0.iter().map(|c| egui::Color32::from_rgba_premultiplied(c[0], c[1], c[2], c[3])).collect();
Arc::new(egui::ColorImage::new([w as usize, h as usize], px))
};
if self.viewer_image.is_none()
&& let (Some((_, key, f)), Some(g)) = (&self.viewer_native, &self.gpu)
&& self.viewer_shown.as_ref().map(|s| s.1) == Some(*key)
{
if cfg!(target_arch = "wasm32") {
let ready = self.viewer_readback.1.lock().ok().and_then(|mut r| r.take_if(|r| r.0 == *key));
if let Some((_, img)) = ready {
self.viewer_image = Some(img);
} else if self.viewer_readback.0 != Some(*key) {
self.viewer_readback.0 = Some(*key);
let (slot, k, (w, h)) = (self.viewer_readback.1.clone(), *key, (f.width, f.height));
let ctx = self.frames.context();
g.read_display_async(f, move |bytes| {
if let (Some(b), Ok(mut s)) = (bytes, slot.lock()) {
*s = Some((k, to_image(b, w, h)));
}
if let Some(c) = ctx {
c.request_repaint();
}
});
}
} else if let Some(bytes) = g.read_display(f) {
self.viewer_image = Some(to_image(bytes, f.width, f.height));
}
}
self.viewer_image.clone()
}
/// The half-float frame on screen (32 bpc projects), for float readouts.
pub fn viewer_f16(&self) -> Option<&Arc<frames::F16Frame>> {
self.viewer_f16.as_ref()
}
/// The texture the Composition viewer's display pass drew last (half-float frames, or 8-bit
/// frames with exposure, a channel view or display colour), when it ran on the GPU.
pub fn viewer_display_texture(&self) -> Option<egui::TextureId> {
self.viewer_bake.id()
}
/// The display pass's pipelines on egui-wgpu's device, made the first time (desktop; the
/// browser's viewer converts on the CPU for now). `None` without a device.
pub(crate) fn display_gpu(&mut self) -> Option<Arc<crate::display_gpu::DisplayGpu>> {
if cfg!(target_arch = "wasm32") || self.display_gpu_failed {
return None;
}
if self.display_gpu.is_none() {
let rs = self.window_wgpu.as_ref()?;
match crate::display_gpu::DisplayGpu::try_new(&rs.device) {
Ok(g) => self.display_gpu = Some(Arc::new(g)),
Err(e) => {
log::warn!("the viewer's display pass runs on the CPU: {e}");
self.display_gpu_failed = true;
}
}
}
self.display_gpu.clone()
}
/// The viewer's Resolution: the active composition's (saved with the project).
pub fn viewer_res(&self) -> state::Resolution {
self.session.active_comp().map(|c| c.resolution).unwrap_or_default()
}
/// The render scale used by the viewer right now.
pub fn viewer_scale(&self, zoom: f32, ppp: f32) -> f64 {
// The preview's Resolution (when not Auto) while it plays.
if self.playback.playing
&& let Some(f) = self.playback.res
{
return state::Resolution::from_factor(f.into()).scale(zoom, ppp);
}
// Auto follows the zoom alike while playing and paused, so frames cached while scrubbing
// play back, and frames cached by a preview stay on screen when it stops.
let res = self.viewer_res();
let full = res.scale(zoom, ppp);
// A preview that couldn't keep up stepped down (`step_down`).
if self.playback.playing
&& res.is_auto()
&& let Some((s, _)) = self.playback.adaptive
{
return full.min(s);
}
// Fast Previews ▸ Adaptive Resolution / Fast Draft: lower resolution while dragging.
let (_, k) = self.session.state.viewer.fast_previews.render(self.ui.viewer.interacting || self.ui.viewer.property_interacting);
if k < 1.0 && res.is_auto() {
return full.min((full * 0.5).max(self.session.prefs.adaptive_limit()));
}
full
}
/// The cache key of viewer frame `frame` of `comp` at `scale` (see [`Self::frame_series`]).
pub fn frame_key(&self, comp: ItemId, frame: i64, scale: f64) -> FrameKey {
FrameKey { frame, ..self.frame_series(comp, scale) }
}
/// The key of frame 0 of `comp`'s viewer frames at `scale` (the content, view and render
/// options are the same for every frame: loops compute this once).
pub fn frame_series(&self, comp: ItemId, scale: f64) -> FrameKey {
FrameKey {
revision: self.session.revision,
content: self.frames.content_of(&self.session.project, self.session.revision, comp),
comp: comp.0,
frame: 0,
scale: (scale * 1000.0).round() as u32,
view: self.view_hash(comp),
opts: frames::opts_hash(&self.frame_opts(comp, scale)) ^ self.roto_model_stamp() ^ self.lut_stamp() ^ self.purge_stamp(),
}
}
/// Changes when the Roto Brush model in use does: a frame drawn with the classic fallback while the model loaded
/// is not the frame the model gives, so the two must not share a cache entry.
fn roto_model_stamp(&self) -> u64 {
use std::hash::{Hash, Hasher};
let Some(id) = self.session.models_roto_active() else { return 0 };
let mut h = std::collections::hash_map::DefaultHasher::new();
id.hash(&mut h);
h.finish() | 1
}
/// Changes when a LUT file in use changed on disk (#3493): the frame on screen and the ones
/// still rendering were made with the old LUT, so the new ones must not share their keys
/// (the viewers show a frame again only when its key changes). 0 until a LUT file changes.
fn lut_stamp(&self) -> u64 {
self.lut_generation.wrapping_mul(0x9e37_79b9_7f4a_7c15)
}
/// Changes when the caches are purged (Event::PurgeCaches: footage reloaded, Purge): the
/// frames on screen and the ones still rendering may show the files as they were, and a reload
/// that changes no metadata keeps the project's revision, so the new frames must not share
/// their keys (#3539; the viewers show a frame again only when its key changes). 0 until then.
fn purge_stamp(&self) -> u64 {
self.purges.wrapping_mul(0xc2b2_ae3d_27d4_eb4f)
}
/// The frame series the viewer of `comp` shows now (its scale, view and options, for the
/// comp's current content): what the cache bars count.
pub fn shown_series(&self, comp: ItemId) -> FrameKey {
let scale = self.viewer_shown.as_ref().filter(|(_, k)| k.comp == comp.0).map_or(1000, |(_, k)| k.scale);
self.frame_series(comp, scale as f64 / 1000.0)
}
/// The frame series a preview with options `o` plays: at its Resolution, or the one the
/// viewer shows with Auto (it follows the zoom). The Preview panel's cache bar counts it.
pub fn preview_series(&self, comp: ItemId, o: &effectcraft_engine::preview::PreviewPreset) -> FrameKey {
match o.resolution.factor(o.custom_resolution) {
Some(n) => self.frame_series(comp, 1.0 / f64::from(n)),
None => self.shown_series(comp),
}
}
/// Hash of the comp viewer's 3D view camera (0 for the active camera view).
pub fn view_hash(&self, comp: ItemId) -> u64 {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
// The region of interest changes what is rendered too.
let roi = self.viewer_region(comp);
if let Some(r) = roi {
r.map(f64::to_bits).hash(&mut h);
}
let Some(cam) = self.session.view_camera(comp) else { return if roi.is_some() { h.finish() | 1 } else { 0 } };
for row in cam.view.0 {
for v in row {
v.to_bits().hash(&mut h);
}
}
cam.zoom.to_bits().hash(&mut h);
cam.ortho.hash(&mut h);
h.finish() | 1
}
/// The comp-space region viewer frames of `comp` cover: the region of interest, else the
/// Extended Viewer's area (frame plus pasteboard), else `None` (the whole frame).
pub fn viewer_region(&self, comp: ItemId) -> Option<[f64; 4]> {
if self.session.active_comp_id() != Some(comp) {
return None;
}
self.session.state.region_of_interest.or(self.ui.viewer.extended)
}
/// Request a prefetch frame render (no-op if cached/in flight).
pub fn request_frame(&self, comp: ItemId, frame: i64, scale: f64) {
self.request_frame_with(comp, frame, scale, frames::Frames::request);
}
/// Request the frame on screen: rendered before any queued prefetch.
pub fn request_frame_urgent(&self, comp: ItemId, frame: i64, scale: f64) {
self.request_frame_with(comp, frame, scale, frames::Frames::request_urgent);
}
/// Request a frame nobody waits for (Cache Frames When Idle): rendered on one thread
/// (`Frames::request_background`).
pub fn request_frame_background(&self, comp: ItemId, frame: i64, scale: f64) {
self.request_frame_with(comp, frame, scale, frames::Frames::request_background);
}
/// Render options of viewer frames of `comp` at `scale`.
pub fn frame_opts(&self, comp: ItemId, scale: f64) -> RenderOpts {
let (draft, _) = self.session.state.viewer.fast_previews.render(self.ui.viewer.interacting || self.ui.viewer.property_interacting);
let roi = self.viewer_region(comp);
RenderOpts {
scale,
motion_blur: true,
guides: true,
draft: self.ui.viewer.fast_preview || draft,
view: self.session.view_camera(comp),
roi,
backend: effectcraft_engine::render::Backend::Auto,
nested_switches: self.session.prefs.general.switches_affect_nested_comps,
draft_shadows: self.session.prefs.three_d.realtime_shadows,
// 32 bpc: half-float frames without the HDR step; the display pass applies it after
// the viewer's exposure (#2797).
skip_hdr_step: frames::f16_frames(&self.session.project.settings),
proxy: Default::default(),
}
}
fn request_frame_with(
&self,
comp: ItemId,
frame: i64,
scale: f64,
request: fn(&frames::Frames, &frames::RenderSource, FrameKey, ItemId, Tick, RenderOpts),
) {
let Some(c) = self.session.project.comp(comp) else { return };
let key = self.frame_key(comp, frame, scale);
let t = c.frame_rate.tick_of(frame);
let opts = self.frame_opts(comp, scale);
request(&self.frames, &self.render_source(), key, comp, t, opts);
}
}
#[cfg(test)]
mod tests {
/// #3493: the viewer shows the frame again once the LUT file it was graded with changes,
/// though its key was otherwise the same (same project revision, frame and options).
#[test]
fn the_viewer_shows_a_lut_file_changed_on_disk() {
let dir = tempfile::tempdir().unwrap();
let lut = dir.path().join("viewer.cube");
let write = |text: &str, secs: u64| {
std::fs::write(&lut, text).unwrap();
let f = std::fs::File::options().write(true).open(&lut).unwrap();
f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs)).unwrap();
};
write("LUT_1D_SIZE 2\n0 0 0\n0 0 0\n", 1_000);
let mut session = effectcraft_engine::Session::default();
session.execute("comp.new", serde_json::json!({"width": 64, "height": 36, "duration": 2})).unwrap();
session.execute("layer.newSolid", serde_json::json!({"color": "#808080"})).unwrap();
session.execute("effect.apply", serde_json::json!({"layer": 1, "effect": "Apply Color LUT"})).unwrap();
session.execute("prop.set", serde_json::json!({"layer": 1, "path": "effects/#1/lut", "value": lut.to_string_lossy()})).unwrap();
let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(|_| crate::EffectcraftApp::new(session));
let centre = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| {
h.state().viewer_image.as_ref().map(|img| img.pixels[img.pixels.len() / 2 + img.size[0] / 2].r())
};
let settle = |h: &mut egui_kittest::Harness<'_, crate::EffectcraftApp>, want: u8| {
for _ in 0..400 {
h.step();
if centre(h) == Some(want) {
return;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
panic!("the viewer shows {:?}, not {want}", centre(h));
};
settle(&mut harness, 0);
write("LUT_1D_SIZE 2\n1 1 1\n1 1 1\n", 2_000);
// (what the app's watcher does every two seconds)
effectcraft_engine::lut_reload::check();
settle(&mut harness, 255);
}
/// #3539: a still re-saved at the same size and reloaded as changed on disk keeps the project
/// as it was (same revision and content key); the viewers showed the old picture, the new
/// frame having the key of the one on screen. A reload's purge gives the frames new keys, once.
#[test]
fn the_viewers_show_a_still_reloaded_at_the_same_size() {
let dir = tempfile::tempdir().unwrap();
let png = dir.path().join("still.png");
let save = |rgb: [u8; 3]| image::RgbaImage::from_pixel(64, 36, image::Rgba([rgb[0], rgb[1], rgb[2], 255])).save(&png).unwrap();
save([255, 0, 0]);
let mut session = effectcraft_host::session();
session.execute("comp.new", serde_json::json!({"width": 64, "height": 36, "duration": 2})).unwrap();
let cid = session.active_comp_id().unwrap();
let item = session.execute("file.import", serde_json::json!({"paths": [png.to_string_lossy()]})).unwrap()["items"][0].clone();
session.execute("layer.addItem", serde_json::json!({"item": item})).unwrap();
let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(|_| crate::EffectcraftApp::new(session));
let centre = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| {
h.state().viewer_image.as_ref().map(|img| img.pixels[img.pixels.len() / 2 + img.size[0] / 2].r())
};
let settle = |h: &mut egui_kittest::Harness<'_, crate::EffectcraftApp>, want: u8| {
for _ in 0..400 {
h.step();
if centre(h) == Some(want) {
return;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
panic!("the viewer shows {:?}, not {want}", centre(h));
};
settle(&mut harness, 255);
// (every viewer, passive ones too, asks for its frame by this key)
let key = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| h.state().frame_key(cid, 0, 1.0);
let before = key(&harness);
harness.step();
assert_eq!(key(&harness), before, "no purge, the same key: nothing is uploaded again");
// Reload Footage Changed on Disk: the app's watcher saw the file's stamp move.
let path = png.to_string_lossy().into_owned();
let stamps = |n| vec![(path.clone(), Some((n, u128::from(n))))];
effectcraft_engine::footage_reload::apply_stamps(&mut harness.state_mut().session, stamps(1)).unwrap();
save([0, 0, 255]);
let revision = harness.state().session.revision;
let reloaded = effectcraft_engine::footage_reload::apply_stamps(&mut harness.state_mut().session, stamps(2)).unwrap();
assert_eq!(reloaded["files"], 1, "{reloaded}");
assert_eq!(harness.state().session.revision, revision, "the project is as it was");
settle(&mut harness, 0);
let after = key(&harness);
assert_ne!(after, before, "the reloaded frame has a key of its own");
harness.step();
assert_eq!(key(&harness), after, "one new key per purge");
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.