From e3b7214e0e525ac451d6c0d0db172c99efa85a6d Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 13 Aug 2024 17:41:28 +0200 Subject: [PATCH 1/2] add visual feature for benchmarks + fix bad performance due to usage --- bevy_rapier_benches3d/Cargo.toml | 17 ++++++++++++-- bevy_rapier_benches3d/src/bin/bench.rs | 2 +- bevy_rapier_benches3d/src/common.rs | 31 ++++++++++++++++++++++++++ bevy_rapier_benches3d/src/lib.rs | 5 +++++ bevy_rapier_benches3d/src/pyramids.rs | 7 ++++-- 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/bevy_rapier_benches3d/Cargo.toml b/bevy_rapier_benches3d/Cargo.toml index a7529bfb..b5d32923 100644 --- a/bevy_rapier_benches3d/Cargo.toml +++ b/bevy_rapier_benches3d/Cargo.toml @@ -8,7 +8,20 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +visual = [ + "bevy/x11", + "bevy/tonemapping_luts", + "bevy/bevy_asset", + "bevy/bevy_scene", + "bevy/bevy_core_pipeline", + "bevy/bevy_pbr", + "bevy/bevy_gizmos", + "rapier3d/debug-render", + "bevy/bevy_asset", +] + [dependencies] -rapier3d = { features = ["profiler"], version = "0.22" } -bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d" } bevy = { version = "0.14", default-features = false } +rapier3d = { features = ["profiler"], version = "0.22" } +bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d", default-features = false } diff --git a/bevy_rapier_benches3d/src/bin/bench.rs b/bevy_rapier_benches3d/src/bin/bench.rs index e864541b..018358f1 100644 --- a/bevy_rapier_benches3d/src/bin/bench.rs +++ b/bevy_rapier_benches3d/src/bin/bench.rs @@ -5,7 +5,7 @@ fn pyramid_1_with_height_2() { } fn pyramid_2_with_height_20() { - custom_bencher(100, |app| setup_pyramids(app, 3, 20)); + custom_bencher(100, |app| setup_pyramids(app, 40, 20)); } fn main() { diff --git a/bevy_rapier_benches3d/src/common.rs b/bevy_rapier_benches3d/src/common.rs index 9a2f5f6f..679abbf2 100644 --- a/bevy_rapier_benches3d/src/common.rs +++ b/bevy_rapier_benches3d/src/common.rs @@ -1,5 +1,6 @@ use bevy::{ app::PluginsState, + log::LogPlugin, prelude::*, render::{ settings::{RenderCreation, WgpuSettings}, @@ -10,6 +11,7 @@ use bevy::{ }; use bevy_rapier3d::prelude::*; +#[cfg(not(feature = "visual"))] pub fn default_app() -> App { let mut app = App::new(); @@ -18,6 +20,7 @@ pub fn default_app() -> App { MinimalPlugins, AssetPlugin::default(), ScenePlugin, + LogPlugin::default(), RenderPlugin { render_creation: RenderCreation::Automatic(WgpuSettings { backends: None, @@ -38,6 +41,34 @@ pub fn default_app() -> App { app } +#[cfg(feature = "visual")] +pub fn default_app() -> App { + use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}; + + let mut app = App::new(); + println!("visual mode!"); + app.insert_resource(ClearColor(Color::srgb( + 0xF9 as f32 / 255.0, + 0xF9 as f32 / 255.0, + 0xFF as f32 / 255.0, + ))); + app.add_plugins(( + DefaultPlugins, + RapierPhysicsPlugin::<()>::default(), + RapierDebugRenderPlugin::default(), + FrameTimeDiagnosticsPlugin::default(), + LogDiagnosticsPlugin::default(), + )); + app.add_systems(Startup, |mut commands: Commands| { + commands.spawn(Camera3dBundle { + transform: Transform::from_xyz(-30.0, 30.0, 100.0) + .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), + ..Default::default() + }); + }); + app +} + pub fn wait_app_start(app: &mut App) { while app.plugins_state() != PluginsState::Ready { bevy::tasks::tick_global_task_pools_on_main_thread(); diff --git a/bevy_rapier_benches3d/src/lib.rs b/bevy_rapier_benches3d/src/lib.rs index 3e3220aa..baa3e605 100644 --- a/bevy_rapier_benches3d/src/lib.rs +++ b/bevy_rapier_benches3d/src/lib.rs @@ -14,6 +14,11 @@ use bevy_rapier3d::plugin::RapierContext; pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { let mut app = default_app(); setup(&mut app); + #[cfg(feature = "visual")] + { + app.run(); + return; + } wait_app_start(&mut app); let mut timer_total = rapier3d::counters::Timer::new(); diff --git a/bevy_rapier_benches3d/src/pyramids.rs b/bevy_rapier_benches3d/src/pyramids.rs index 363538a9..bbbca96f 100644 --- a/bevy_rapier_benches3d/src/pyramids.rs +++ b/bevy_rapier_benches3d/src/pyramids.rs @@ -16,8 +16,10 @@ pub fn create_pyramid(commands: &mut Commands, offset: Vect, stack_height: usize // Build the rigid body. commands.spawn(( RigidBody::Dynamic, - Transform::from_translation(Vec3::new(x, y, 0.0) + offset), - Collider::cuboid(1.0, 1.0, 1.0), + SpatialBundle::from_transform(Transform::from_translation( + Vec3::new(x, y, 0.0) + offset, + )), + Collider::cuboid(rad, rad, rad), )); } } @@ -47,6 +49,7 @@ pub fn setup_pyramids(app: &mut App, pyramid_count: usize, stack_height: usize) /* * Create the pyramids */ + for pyramid_index in 0..pyramid_count { let bottomy = rad; create_pyramid( From d66c3e98a67e772c9c1f0f86395e8d5b5471f044 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 6 Sep 2024 16:00:20 +0200 Subject: [PATCH 2/2] benches: add explaination for visual feature + cleanup the features --- bevy_rapier3d/Cargo.toml | 5 ++++- bevy_rapier_benches3d/Cargo.toml | 10 ++++++---- bevy_rapier_benches3d/README.md | 7 +++++++ bevy_rapier_benches3d/src/common.rs | 24 ++---------------------- bevy_rapier_benches3d/src/lib.rs | 4 ++-- bevy_rapier_benches3d/src/pyramids.rs | 2 +- 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/bevy_rapier3d/Cargo.toml b/bevy_rapier3d/Cargo.toml index ac470cec..e769fa58 100644 --- a/bevy_rapier3d/Cargo.toml +++ b/bevy_rapier3d/Cargo.toml @@ -48,7 +48,10 @@ headless = [] async-collider = ["bevy/bevy_asset", "bevy/bevy_scene"] [dependencies] -bevy = { version = "0.14", default-features = false } +bevy = { version = "0.14", default-features = false, features = [ + # This has to be enabled for non headless as of bevy 0.14 because we use AAbb + # "bevy_render", +] } nalgebra = { version = "0.33", features = ["convert-glam027"] } rapier3d = "0.22" bitflags = "2.4" diff --git a/bevy_rapier_benches3d/Cargo.toml b/bevy_rapier_benches3d/Cargo.toml index b5d32923..aca86132 100644 --- a/bevy_rapier_benches3d/Cargo.toml +++ b/bevy_rapier_benches3d/Cargo.toml @@ -12,16 +12,18 @@ edition = "2021" visual = [ "bevy/x11", "bevy/tonemapping_luts", - "bevy/bevy_asset", - "bevy/bevy_scene", "bevy/bevy_core_pipeline", "bevy/bevy_pbr", "bevy/bevy_gizmos", "rapier3d/debug-render", - "bevy/bevy_asset", + #"bevy/bevy_asset", + "bevy_rapier3d/debug-render-3d", ] [dependencies] bevy = { version = "0.14", default-features = false } rapier3d = { features = ["profiler"], version = "0.22" } -bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d", default-features = false } +bevy_rapier3d = { version = "0.27", path = "../bevy_rapier3d", default-features = false, features = [ + "dim3", + "headless", +] } diff --git a/bevy_rapier_benches3d/README.md b/bevy_rapier_benches3d/README.md index 7079cd61..5f3a49c6 100644 --- a/bevy_rapier_benches3d/README.md +++ b/bevy_rapier_benches3d/README.md @@ -10,6 +10,13 @@ and outputs them at the end. cargo run --release --bin bench ``` +To visually make sure your benchmarks setup is correct, you can use the feature `visual`, +and edit `bench.rs` to test only the scene you want. + +```sh +cargo run --release --bin bench --features visual +``` + ## cargo bench For short-lived benchmarks based on statistical analysis, diff --git a/bevy_rapier_benches3d/src/common.rs b/bevy_rapier_benches3d/src/common.rs index 679abbf2..a7ad1777 100644 --- a/bevy_rapier_benches3d/src/common.rs +++ b/bevy_rapier_benches3d/src/common.rs @@ -1,34 +1,14 @@ -use bevy::{ - app::PluginsState, - log::LogPlugin, - prelude::*, - render::{ - settings::{RenderCreation, WgpuSettings}, - RenderPlugin, - }, - scene::ScenePlugin, - time::TimeUpdateStrategy, -}; +use bevy::{app::PluginsState, prelude::*}; use bevy_rapier3d::prelude::*; #[cfg(not(feature = "visual"))] pub fn default_app() -> App { + use bevy::{log::LogPlugin, time::TimeUpdateStrategy}; let mut app = App::new(); app.add_plugins(( - WindowPlugin::default(), MinimalPlugins, - AssetPlugin::default(), - ScenePlugin, LogPlugin::default(), - RenderPlugin { - render_creation: RenderCreation::Automatic(WgpuSettings { - backends: None, - ..Default::default() - }), - ..Default::default() - }, - ImagePlugin::default(), HierarchyPlugin, TransformPlugin, RapierPhysicsPlugin::<()>::default(), diff --git a/bevy_rapier_benches3d/src/lib.rs b/bevy_rapier_benches3d/src/lib.rs index baa3e605..cabca236 100644 --- a/bevy_rapier_benches3d/src/lib.rs +++ b/bevy_rapier_benches3d/src/lib.rs @@ -11,7 +11,7 @@ use common::wait_app_start; use bevy::prelude::*; use bevy_rapier3d::plugin::RapierContext; -pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { +pub fn custom_bencher(_steps: usize, setup: impl Fn(&mut App)) { let mut app = default_app(); setup(&mut app); #[cfg(feature = "visual")] @@ -26,7 +26,7 @@ pub fn custom_bencher(steps: usize, setup: impl Fn(&mut App)) { let mut rapier_step_times = vec![]; let mut total_update_times = vec![]; timer_total.start(); - for _ in 0..steps { + for _ in 0.._steps { timer_full_update.start(); app.update(); timer_full_update.pause(); diff --git a/bevy_rapier_benches3d/src/pyramids.rs b/bevy_rapier_benches3d/src/pyramids.rs index bbbca96f..fbb7ee2c 100644 --- a/bevy_rapier_benches3d/src/pyramids.rs +++ b/bevy_rapier_benches3d/src/pyramids.rs @@ -16,7 +16,7 @@ pub fn create_pyramid(commands: &mut Commands, offset: Vect, stack_height: usize // Build the rigid body. commands.spawn(( RigidBody::Dynamic, - SpatialBundle::from_transform(Transform::from_translation( + TransformBundle::from_transform(Transform::from_translation( Vec3::new(x, y, 0.0) + offset, )), Collider::cuboid(rad, rad, rad),