Skip to content

Commit

Permalink
add frame count
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Sep 12, 2022
1 parent ea0fd1e commit 648075a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/bevy_pbr/src/render/mesh_view_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ struct Globals {
time: f32,
// The delta time of the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
frame_count: u32,
}
14 changes: 14 additions & 0 deletions crates/bevy_render/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ use crate::{
};
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use bevy_log::info;
use bevy_reflect::Reflect;
use bevy_time::Time;

#[derive(Default, Resource)]
struct FrameCount(u32);

pub struct GlobalsPlugin;

impl Plugin for GlobalsPlugin {
Expand All @@ -17,6 +21,7 @@ impl Plugin for GlobalsPlugin {
render_app
.init_resource::<GlobalsBuffer>()
.init_resource::<Time>()
.init_resource::<FrameCount>()
.add_system_to_stage(RenderStage::Extract, extract_time)
.add_system_to_stage(RenderStage::Prepare, prepare_globals_buffer);
}
Expand All @@ -36,6 +41,8 @@ pub struct GlobalsUniform {
time: f32,
/// The duration of the last frame in seconds
delta_time: f32,
/// Frame count since the start of the app.
frame_count: u32,
}

/// The buffer containing the [`GlobalsUniform`]
Expand All @@ -49,10 +56,17 @@ fn prepare_globals_buffer(
render_queue: Res<RenderQueue>,
mut globals_buffer: ResMut<GlobalsBuffer>,
time: Res<Time>,
mut frame_count: Local<u32>,
) {
// We can't rely on the FrameTimeDiagnosticsPlugin to be present since it is not part of DefaultPlugins
*frame_count = frame_count.wrapping_add(1);

let buffer = globals_buffer.buffer.get_mut();
buffer.time = time.seconds_since_startup() as f32;
buffer.delta_time = time.delta_seconds();
buffer.frame_count = *frame_count;

info!("{frame_count:?}");

globals_buffer
.buffer
Expand Down

0 comments on commit 648075a

Please sign in to comment.