Skip to content

Commit

Permalink
Rename pass_nr to cumulative_pass_nr
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 13, 2024
1 parent dd433ca commit f8ffa5c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 56 deletions.
14 changes: 7 additions & 7 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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<Arc<Window>> {
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 5 additions & 3 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,13 @@ impl<T: WinitApp> ApplicationHandler<UserEvent> for WinitAppWrapper<T> {
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)
Expand Down
12 changes: 6 additions & 6 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Arc<Window>> {
Expand Down
6 changes: 3 additions & 3 deletions crates/eframe/src/native/winit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand All @@ -65,7 +65,7 @@ impl From<accesskit_winit::Event> 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<Arc<Window>>;

Expand Down
70 changes: 35 additions & 35 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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,
});
}
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion crates/egui_demo_app/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>], 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 =
Expand Down

0 comments on commit f8ffa5c

Please sign in to comment.