-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add vello diagnostics plugin (#120)
Closes #117
- Loading branch information
Showing
13 changed files
with
356 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "diagnostics" | ||
version.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[dependencies] | ||
bevy_vello = { path = "../../" } | ||
bevy = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use bevy::{diagnostic::DiagnosticsStore, prelude::*}; | ||
use bevy_vello::{ | ||
diagnostics::VelloEntityCountDiagnosticsPlugin, | ||
diagnostics::VelloFrameProfileDiagnosticsPlugin, prelude::*, VelloPlugin, | ||
}; | ||
|
||
const SCENE_COUNT: usize = 5; | ||
|
||
fn main() { | ||
let mut app = App::new(); | ||
app.add_plugins(DefaultPlugins) | ||
.add_plugins(VelloPlugin::default()) | ||
.add_plugins(VelloEntityCountDiagnosticsPlugin) | ||
.add_plugins(VelloFrameProfileDiagnosticsPlugin) | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, simple_animation) | ||
.add_systems(Update, update_scene_count_ui); | ||
|
||
app.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands) { | ||
commands.spawn((Camera2d, VelloView)); | ||
for i in 0..SCENE_COUNT { | ||
commands.spawn(( | ||
VelloScene::new(), | ||
Transform::from_translation(Vec3::new(i as f32 * 100.0 - 200.0, 0.0, 0.0)), | ||
)); | ||
} | ||
|
||
// UI Text displaying the scene count | ||
commands.spawn(( | ||
Text::new("Total Scenes: 0"), | ||
TextFont { | ||
font_size: 30.0, | ||
..default() | ||
}, | ||
TextColor(Color::WHITE), | ||
Node { | ||
position_type: PositionType::Absolute, | ||
left: Val::Px(10.0), | ||
top: Val::Px(10.0), | ||
..default() | ||
}, | ||
DiagnosticsText, | ||
)); | ||
} | ||
|
||
#[derive(Component)] | ||
struct DiagnosticsText; | ||
|
||
fn simple_animation(mut query: Query<(&mut Transform, &mut VelloScene)>, time: Res<Time>) { | ||
let sin_time = time.elapsed_secs().sin().mul_add(0.5, 0.5); | ||
|
||
for (mut transform, mut scene) in query.iter_mut() { | ||
scene.reset(); | ||
|
||
let c = Vec3::lerp( | ||
Vec3::new(-1.0, 1.0, -1.0), | ||
Vec3::new(-1.0, 1.0, 1.0), | ||
sin_time + 0.5, | ||
); | ||
scene.fill( | ||
peniko::Fill::NonZero, | ||
kurbo::Affine::default(), | ||
peniko::Color::new([c.x, c.y, c.z, 1.]), | ||
None, | ||
&kurbo::RoundedRect::new(-50.0, -50.0, 50.0, 50.0, (sin_time as f64) * 50.0), | ||
); | ||
|
||
transform.scale = Vec3::lerp(Vec3::splat(0.5), Vec3::ONE, sin_time); | ||
transform.translation.y = sin_time * 50.0; | ||
transform.rotation = Quat::from_rotation_z(-std::f32::consts::TAU * sin_time); | ||
} | ||
} | ||
|
||
fn update_scene_count_ui( | ||
diagnostics: Res<DiagnosticsStore>, | ||
mut text: Single<&mut Text, With<DiagnosticsText>>, | ||
) { | ||
let Some(scenes) = diagnostics.get(&VelloEntityCountDiagnosticsPlugin::SCENE_COUNT) else { | ||
return; | ||
}; | ||
let Some(scene_count) = scenes.measurement() else { | ||
return; | ||
}; | ||
let Some(path_segs) = diagnostics.get(&VelloFrameProfileDiagnosticsPlugin::PATH_SEGMENTS_COUNT) | ||
else { | ||
return; | ||
}; | ||
let Some(path_segs_count) = path_segs.measurement() else { | ||
return; | ||
}; | ||
|
||
text.0 = format!( | ||
"Total scenes: {}\nTotal path segments: {}", | ||
scene_count.value, path_segs_count.value | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::render::{VelloEntityCountData, VelloFrameProfileData}; | ||
use bevy::{ | ||
diagnostic::{Diagnostic, DiagnosticPath, Diagnostics, RegisterDiagnostic}, | ||
prelude::*, | ||
}; | ||
|
||
/// Adds Vello entity counting diagnostics to an App. | ||
#[derive(Default)] | ||
pub struct VelloEntityCountDiagnosticsPlugin; | ||
|
||
impl Plugin for VelloEntityCountDiagnosticsPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.register_diagnostic(Diagnostic::new(Self::SCENE_COUNT).with_suffix(" scenes")) | ||
.register_diagnostic(Diagnostic::new(Self::TEXT_COUNT).with_suffix(" texts")) | ||
.add_systems(Update, Self::diagnostic_system); | ||
|
||
#[cfg(feature = "svg")] | ||
app.register_diagnostic(Diagnostic::new(Self::SVG_COUNT).with_suffix(" svgs")); | ||
#[cfg(feature = "lottie")] | ||
app.register_diagnostic(Diagnostic::new(Self::LOTTIE_COUNT).with_suffix(" lotties")); | ||
} | ||
} | ||
|
||
impl VelloEntityCountDiagnosticsPlugin { | ||
pub const SCENE_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_scenes"); | ||
pub const TEXT_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_texts"); | ||
#[cfg(feature = "svg")] | ||
pub const SVG_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_svgs"); | ||
#[cfg(feature = "lottie")] | ||
pub const LOTTIE_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_lotties"); | ||
|
||
fn diagnostic_system(mut diagnostics: Diagnostics, data: Res<VelloEntityCountData>) { | ||
diagnostics.add_measurement(&Self::SCENE_COUNT, || data.n_scenes as f64); | ||
diagnostics.add_measurement(&Self::TEXT_COUNT, || data.n_texts as f64); | ||
#[cfg(feature = "svg")] | ||
diagnostics.add_measurement(&Self::SVG_COUNT, || data.n_svgs as f64); | ||
#[cfg(feature = "lottie")] | ||
diagnostics.add_measurement(&Self::LOTTIE_COUNT, || data.n_lotties as f64); | ||
} | ||
} | ||
|
||
/// Adds Vello frame profile diagnostics to an App. | ||
#[derive(Default)] | ||
pub struct VelloFrameProfileDiagnosticsPlugin; | ||
|
||
impl Plugin for VelloFrameProfileDiagnosticsPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.register_diagnostic(Diagnostic::new(Self::PATH_COUNT).with_suffix(" paths")) | ||
.register_diagnostic( | ||
Diagnostic::new(Self::PATH_SEGMENTS_COUNT).with_suffix(" path segments"), | ||
) | ||
.register_diagnostic(Diagnostic::new(Self::CLIPS_COUNT).with_suffix(" clips")) | ||
.register_diagnostic(Diagnostic::new(Self::OPEN_CLIPS_COUNT).with_suffix(" open clips")) | ||
.add_systems(Update, Self::diagnostic_system); | ||
} | ||
} | ||
|
||
impl VelloFrameProfileDiagnosticsPlugin { | ||
pub const PATH_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_paths"); | ||
pub const PATH_SEGMENTS_COUNT: DiagnosticPath = | ||
DiagnosticPath::const_new("vello_path_segments"); | ||
pub const CLIPS_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_clips"); | ||
pub const OPEN_CLIPS_COUNT: DiagnosticPath = DiagnosticPath::const_new("vello_open_clips"); | ||
|
||
fn diagnostic_system(mut diagnostics: Diagnostics, data: Res<VelloFrameProfileData>) { | ||
diagnostics.add_measurement(&Self::PATH_COUNT, || data.n_paths as f64); | ||
diagnostics.add_measurement(&Self::PATH_SEGMENTS_COUNT, || data.n_path_segs as f64); | ||
diagnostics.add_measurement(&Self::CLIPS_COUNT, || data.n_clips as f64); | ||
diagnostics.add_measurement(&Self::OPEN_CLIPS_COUNT, || data.n_open_clips as f64); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.