diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 0272af1ce52..27f09158552 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -251,13 +251,13 @@ impl<'app> GlowWinitApp<'app> { .set_request_repaint_callback(move |info| { log::trace!("request_repaint_callback: {info:?}"); let when = Instant::now() + info.delay; - let frame_nr = info.current_frame_nr; + let cumulative_pass_nr = info.current_cumulative_pass_nr; event_loop_proxy .lock() .send_event(UserEvent::RequestRepaint { viewport_id: info.viewport_id, when, - frame_nr, + cumulative_pass_nr, }) .ok(); }); @@ -346,10 +346,10 @@ impl<'app> GlowWinitApp<'app> { } impl<'app> WinitApp for GlowWinitApp<'app> { - fn frame_nr(&self, viewport_id: ViewportId) -> u64 { - self.running - .as_ref() - .map_or(0, |r| r.integration.egui_ctx.frame_nr_for(viewport_id)) + fn cumulative_pass_nr(&self, viewport_id: ViewportId) -> u64 { + self.running.as_ref().map_or(0, |r| { + r.integration.egui_ctx.cumulative_pass_nr_for(viewport_id) + }) } fn window(&self, window_id: WindowId) -> Option> { @@ -712,7 +712,7 @@ impl<'app> GlowWinitRunning<'app> { // give it time to settle: #[cfg(feature = "__screenshot")] - if integration.egui_ctx.frame_nr() == 2 { + if integration.egui_ctx.cumulative_pass_nr() == 2 { if let Ok(path) = std::env::var("EFRAME_SCREENSHOT_TO") { save_screenshot_and_exit(&path, &painter, screen_size_in_pixels); } diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index f38c1b8f27f..eb5014e7540 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -228,11 +228,13 @@ impl ApplicationHandler for WinitAppWrapper { let event_result = match event { UserEvent::RequestRepaint { when, - frame_nr, + cumulative_pass_nr, viewport_id, } => { - let current_frame_nr = self.winit_app.frame_nr(viewport_id); - if current_frame_nr == frame_nr || current_frame_nr == frame_nr + 1 { + let current_pass_nr = self.winit_app.cumulative_pass_nr(viewport_id); + if current_pass_nr == cumulative_pass_nr + || current_pass_nr == cumulative_pass_nr + 1 + { log::trace!("UserEvent::RequestRepaint scheduling repaint at {when:?}"); if let Some(window_id) = self.winit_app.window_id_from_viewport_id(viewport_id) diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index c064a6037e2..11bfb00130f 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -223,13 +223,13 @@ impl<'app> WgpuWinitApp<'app> { egui_ctx.set_request_repaint_callback(move |info| { log::trace!("request_repaint_callback: {info:?}"); let when = Instant::now() + info.delay; - let frame_nr = info.current_frame_nr; + let cumulative_pass_nr = info.current_cumulative_pass_nr; event_loop_proxy .lock() .send_event(UserEvent::RequestRepaint { when, - frame_nr, + cumulative_pass_nr, viewport_id: info.viewport_id, }) .ok(); @@ -324,10 +324,10 @@ impl<'app> WgpuWinitApp<'app> { } impl<'app> WinitApp for WgpuWinitApp<'app> { - fn frame_nr(&self, viewport_id: ViewportId) -> u64 { - self.running - .as_ref() - .map_or(0, |r| r.integration.egui_ctx.frame_nr_for(viewport_id)) + fn cumulative_pass_nr(&self, viewport_id: ViewportId) -> u64 { + self.running.as_ref().map_or(0, |r| { + r.integration.egui_ctx.cumulative_pass_nr_for(viewport_id) + }) } fn window(&self, window_id: WindowId) -> Option> { diff --git a/crates/eframe/src/native/winit_integration.rs b/crates/eframe/src/native/winit_integration.rs index 2bc3cd1daf9..288bdcf4713 100644 --- a/crates/eframe/src/native/winit_integration.rs +++ b/crates/eframe/src/native/winit_integration.rs @@ -47,8 +47,8 @@ pub enum UserEvent { /// When to repaint. when: Instant, - /// What the frame number was when the repaint was _requested_. - frame_nr: u64, + /// What the cumulative pass number was when the repaint was _requested_. + cumulative_pass_nr: u64, }, /// A request related to [`accesskit`](https://accesskit.dev/). @@ -65,7 +65,7 @@ impl From for UserEvent { pub trait WinitApp { /// The current frame number, as reported by egui. - fn frame_nr(&self, viewport_id: ViewportId) -> u64; + fn cumulative_pass_nr(&self, viewport_id: ViewportId) -> u64; fn window(&self, window_id: WindowId) -> Option>; diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 4a190c77a5c..53ce7661531 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -50,11 +50,11 @@ pub struct RequestRepaintInfo { /// Repaint after this duration. If zero, repaint as soon as possible. pub delay: Duration, - /// The current frame number. + /// The number of fully completed passes, of the entire lifetime of the [`Context`]. /// - /// This can be compared to [`Context::frame_nr`] to see if we've already - /// triggered the painting of the next frame. - pub current_frame_nr: u64, + /// This can be compared to [`Context::cumulative_pass_nr`] to see if we we still + /// need another repaint (ui pass / frame), or if one has already happened. + pub current_cumulative_pass_nr: u64, } // ---------------------------------------------------------------------------- @@ -149,7 +149,7 @@ impl ContextImpl { (callback)(RequestRepaintInfo { viewport_id, delay: Duration::ZERO, - current_frame_nr: viewport.repaint.pass_nr, + current_cumulative_pass_nr: viewport.repaint.cumulative_pass_nr, }); } } @@ -195,17 +195,17 @@ impl ContextImpl { (callback)(RequestRepaintInfo { viewport_id, delay, - current_frame_nr: viewport.repaint.pass_nr, + current_cumulative_pass_nr: viewport.repaint.cumulative_pass_nr, }); } } } #[must_use] - fn requested_immediate_repaint_prev_frame(&self, viewport_id: &ViewportId) -> bool { - self.viewports.get(viewport_id).map_or(false, |v| { - v.repaint.requested_immediate_repaint_prev_frame() - }) + fn requested_immediate_repaint_prev_pass(&self, viewport_id: &ViewportId) -> bool { + self.viewports + .get(viewport_id) + .map_or(false, |v| v.repaint.requested_immediate_repaint_prev_pass()) } #[must_use] @@ -316,7 +316,7 @@ impl std::fmt::Display for RepaintCause { /// Per-viewport state related to repaint scheduling. struct ViewportRepaintInfo { /// Monotonically increasing counter. - pass_nr: u64, + cumulative_pass_nr: u64, /// The duration which the backend will poll for new events /// before forcing another egui update, even if there's no new events. @@ -346,7 +346,7 @@ struct ViewportRepaintInfo { impl Default for ViewportRepaintInfo { fn default() -> Self { Self { - pass_nr: 0, + cumulative_pass_nr: 0, // We haven't scheduled a repaint yet. repaint_delay: Duration::MAX, @@ -363,7 +363,7 @@ impl Default for ViewportRepaintInfo { } impl ViewportRepaintInfo { - pub fn requested_immediate_repaint_prev_frame(&self) -> bool { + pub fn requested_immediate_repaint_prev_pass(&self) -> bool { self.prev_pass_paint_delay == Duration::ZERO } } @@ -465,7 +465,7 @@ impl ContextImpl { viewport.input = std::mem::take(&mut viewport.input).begin_pass( new_raw_input, - viewport.repaint.requested_immediate_repaint_prev_frame(), + viewport.repaint.requested_immediate_repaint_prev_pass(), pixels_per_point, &self.memory.options, ); @@ -843,7 +843,7 @@ impl Context { self.write(|ctx| ctx.begin_pass(new_input)); - // Plugins run just after the frame has started: + // Plugins run just after the pass starts: self.read(|ctx| ctx.plugins.clone()).on_begin_pass(self); } @@ -1434,22 +1434,22 @@ impl Context { } } - /// The current frame number for the current viewport. - /// - /// Starts at zero, and is incremented at the end of [`Self::run`] or by [`Self::end_pass`]. + /// The total number of completed passes (usually there is one pass per rendered frame). /// - /// Between calls to [`Self::run`], this is the frame number of the coming frame. - pub fn frame_nr(&self) -> u64 { - self.frame_nr_for(self.viewport_id()) + /// Starts at zero, and is incremented for each completed pass inside of [`Self::run`] (usually once). + pub fn cumulative_pass_nr(&self) -> u64 { + self.cumulative_pass_nr_for(self.viewport_id()) } - /// The current frame number. + /// The total number of completed passes (usually there is one pass per rendered frame). /// - /// Starts at zero, and is incremented at the end of [`Self::run`] or by [`Self::end_pass`]. - /// - /// Between calls to [`Self::run`], this is the frame number of the coming frame. - pub fn frame_nr_for(&self, id: ViewportId) -> u64 { - self.read(|ctx| ctx.viewports.get(&id).map_or(0, |v| v.repaint.pass_nr)) + /// Starts at zero, and is incremented for each completed pass inside of [`Self::run`] (usually once). + pub fn cumulative_pass_nr_for(&self, id: ViewportId) -> u64 { + self.read(|ctx| { + ctx.viewports + .get(&id) + .map_or(0, |v| v.repaint.cumulative_pass_nr) + }) } /// Call this if there is need to repaint the UI, i.e. if you are showing an animation. @@ -1564,16 +1564,16 @@ impl Context { self.write(|ctx| ctx.request_repaint_after(duration, id, cause)); } - /// Was a repaint requested last frame for the current viewport? + /// Was a repaint requested last pass for the current viewport? #[must_use] - pub fn requested_repaint_last_frame(&self) -> bool { - self.requested_repaint_last_frame_for(&self.viewport_id()) + pub fn requested_repaint_last_pass(&self) -> bool { + self.requested_repaint_last_pass_for(&self.viewport_id()) } - /// Was a repaint requested last frame for the given viewport? + /// Was a repaint requested last pass for the given viewport? #[must_use] - pub fn requested_repaint_last_frame_for(&self, viewport_id: &ViewportId) -> bool { - self.read(|ctx| ctx.requested_immediate_repaint_prev_frame(viewport_id)) + pub fn requested_repaint_last_pass_for(&self, viewport_id: &ViewportId) -> bool { + self.read(|ctx| ctx.requested_immediate_repaint_prev_pass(viewport_id)) } /// Has a repaint been requested for the current viewport? @@ -1627,7 +1627,7 @@ impl Context { /// There is a limit to how many passes egui will perform, set by [`Options::max_passes`]. /// Therefore, the request might be declined. /// - /// You can check if the current frame will be discarded with [`Self::will_discard`]. + /// You can check if the current pass will be discarded with [`Self::will_discard`]. /// /// You should be very conservative with when you call [`Self::request_discard`], /// as it will cause an extra ui pass, potentially leading to extra CPU use and frame judder. @@ -2203,7 +2203,7 @@ impl ContextImpl { let viewport = self.viewports.entry(ended_viewport_id).or_default(); let pixels_per_point = viewport.input.pixels_per_point; - viewport.repaint.pass_nr += 1; + viewport.repaint.cumulative_pass_nr += 1; self.memory.end_pass(&viewport.this_pass.used_ids); diff --git a/crates/egui_demo_app/src/backend_panel.rs b/crates/egui_demo_app/src/backend_panel.rs index 7b1bc49016e..12738e6d61e 100644 --- a/crates/egui_demo_app/src/backend_panel.rs +++ b/crates/egui_demo_app/src/backend_panel.rs @@ -158,7 +158,7 @@ impl BackendPanel { ui.collapsing("Moreā€¦", |ui| { ui.horizontal(|ui| { ui.label("Frame number:"); - ui.monospace(ui.ctx().frame_nr().to_string()); + ui.monospace(ui.ctx().cumulative_pass_nr().to_string()); }); if ui .button("Wait 2s, then request repaint after another 3s") diff --git a/tests/test_viewports/src/main.rs b/tests/test_viewports/src/main.rs index 52cba800abd..24046e041ac 100644 --- a/tests/test_viewports/src/main.rs +++ b/tests/test_viewports/src/main.rs @@ -215,7 +215,7 @@ fn generic_ui(ui: &mut egui::Ui, children: &[Arc>], close_ let ctx = ui.ctx().clone(); ui.label(format!( "Frame nr: {} (this increases when this viewport is being rendered)", - ctx.frame_nr() + ctx.cumulative_pass_nr() )); ui.horizontal(|ui| { let mut show_spinner =