Update time by sending frame instant through a channel (#4744)
# Objective - The time update is currently done in the wrong part of the schedule. For a single frame the current order of things is update input, update time (First stage), other stages, render stage (frame presentation). So when we update the time it includes the input processing of the current frame and the frame presentation of the previous frame. This is a problem when vsync is on. When input processing takes a longer amount of time for a frame, the vsync wait time gets shorter. So when these are not paired correctly we can potentially have a long input processing time added to the normal vsync wait time in the previous frame. This leads to inaccurate frame time reporting and more variance of the time than actually exists. For more details of why this is an issue see the linked issue below. - Helps with https://github.com/bevyengine/bevy/issues/4669 - Supercedes https://github.com/bevyengine/bevy/pull/4728 and https://github.com/bevyengine/bevy/pull/4735. This PR should be less controversial than those because it doesn't add to the API surface. ## Solution - The most accurate frame time would come from hardware. We currently don't have access to that for multiple reasons, so the next best thing we can do is measure the frame time as close to frame presentation as possible. This PR gets the Instant::now() for the time immediately after frame presentation in the render system and then sends that time to the app world through a channel. - implements suggestion from @aevyrie from here https://github.com/bevyengine/bevy/pull/4728#discussion_r872010606 ## Statistics  --- ## Changelog - Make frame time reporting more accurate. ## Migration Guide `time.delta()` now reports zero for 2 frames on startup instead of 1 frame.
This commit is contained in:
parent
4847f7e3ad
commit
a1d3f1b3b4
@ -39,6 +39,7 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" }
|
|||||||
bevy_mikktspace = { path = "../bevy_mikktspace", version = "0.8.0-dev" }
|
bevy_mikktspace = { path = "../bevy_mikktspace", version = "0.8.0-dev" }
|
||||||
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
|
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
|
||||||
bevy_render_macros = { path = "macros", version = "0.8.0-dev" }
|
bevy_render_macros = { path = "macros", version = "0.8.0-dev" }
|
||||||
|
bevy_time = { path = "../bevy_time", version = "0.8.0-dev" }
|
||||||
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
|
bevy_transform = { path = "../bevy_transform", version = "0.8.0-dev" }
|
||||||
bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }
|
bevy_window = { path = "../bevy_window", version = "0.8.0-dev" }
|
||||||
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
||||||
|
|||||||
@ -200,6 +200,10 @@ impl Plugin for RenderPlugin {
|
|||||||
.insert_resource(asset_server)
|
.insert_resource(asset_server)
|
||||||
.init_resource::<RenderGraph>();
|
.init_resource::<RenderGraph>();
|
||||||
|
|
||||||
|
let (sender, receiver) = bevy_time::create_time_channels();
|
||||||
|
app.insert_resource(receiver);
|
||||||
|
render_app.insert_resource(sender);
|
||||||
|
|
||||||
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
|
app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
|
||||||
#[cfg(feature = "trace")]
|
#[cfg(feature = "trace")]
|
||||||
let _render_span = bevy_utils::tracing::info_span!("renderer subapp").entered();
|
let _render_span = bevy_utils::tracing::info_span!("renderer subapp").entered();
|
||||||
|
|||||||
@ -11,6 +11,8 @@ use crate::{
|
|||||||
view::{ExtractedWindows, ViewTarget},
|
view::{ExtractedWindows, ViewTarget},
|
||||||
};
|
};
|
||||||
use bevy_ecs::prelude::*;
|
use bevy_ecs::prelude::*;
|
||||||
|
use bevy_time::TimeSender;
|
||||||
|
use bevy_utils::Instant;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wgpu::{AdapterInfo, CommandEncoder, Instance, Queue, RequestAdapterOptions};
|
use wgpu::{AdapterInfo, CommandEncoder, Instance, Queue, RequestAdapterOptions};
|
||||||
|
|
||||||
@ -73,6 +75,12 @@ pub fn render_system(world: &mut World) {
|
|||||||
tracy.frame_mark = true
|
tracy.frame_mark = true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update the time and send it to the app world
|
||||||
|
let time_sender = world.resource::<TimeSender>();
|
||||||
|
time_sender.0.try_send(Instant::now()).expect(
|
||||||
|
"The TimeSender channel should always be empty during render. You might need to add the bevy::core::time_system to your app.",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This queue is used to enqueue tasks for the GPU to execute asynchronously.
|
/// This queue is used to enqueue tasks for the GPU to execute asynchronously.
|
||||||
|
|||||||
@ -15,3 +15,6 @@ bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
|
|||||||
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev", features = ["bevy_reflect"] }
|
bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev", features = ["bevy_reflect"] }
|
||||||
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
|
bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] }
|
||||||
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
||||||
|
|
||||||
|
# other
|
||||||
|
crossbeam-channel = "0.5.0"
|
||||||
|
|||||||
@ -9,6 +9,10 @@ pub use stopwatch::*;
|
|||||||
pub use time::*;
|
pub use time::*;
|
||||||
pub use timer::*;
|
pub use timer::*;
|
||||||
|
|
||||||
|
use bevy_ecs::system::{Local, Res, ResMut};
|
||||||
|
use bevy_utils::{tracing::warn, Instant};
|
||||||
|
use crossbeam_channel::{Receiver, Sender};
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
//! The Bevy Time Prelude.
|
//! The Bevy Time Prelude.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@ -41,6 +45,35 @@ impl Plugin for TimePlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn time_system(mut time: ResMut<Time>) {
|
/// Channel resource used to receive time from render world
|
||||||
|
pub struct TimeReceiver(pub Receiver<Instant>);
|
||||||
|
/// Channel resource used to send time from render world
|
||||||
|
pub struct TimeSender(pub Sender<Instant>);
|
||||||
|
|
||||||
|
/// Creates channels used for sending time between render world and app world
|
||||||
|
pub fn create_time_channels() -> (TimeSender, TimeReceiver) {
|
||||||
|
// bound the channel to 2 since when pipelined the render phase can finish before
|
||||||
|
// the time system runs.
|
||||||
|
let (s, r) = crossbeam_channel::bounded::<Instant>(2);
|
||||||
|
(TimeSender(s), TimeReceiver(r))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The system used to update the [`Time`] used by app logic. If there is a render world the time is sent from
|
||||||
|
/// there to this system through channels. Otherwise the time is updated in this system.
|
||||||
|
fn time_system(
|
||||||
|
mut time: ResMut<Time>,
|
||||||
|
time_recv: Option<Res<TimeReceiver>>,
|
||||||
|
mut has_received_time: Local<bool>,
|
||||||
|
) {
|
||||||
|
if let Some(time_recv) = time_recv {
|
||||||
|
// TODO: Figure out how to handle this when using pipelined rendering.
|
||||||
|
if let Ok(new_time) = time_recv.0.try_recv() {
|
||||||
|
time.update_with_instant(new_time);
|
||||||
|
*has_received_time = true;
|
||||||
|
} else if *has_received_time {
|
||||||
|
warn!("time_system did not receive the time from the render world! Calculations depending on the time may be incorrect.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
time.update();
|
time.update();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user