Skip to content

Commit

Permalink
Merge branch 'main' into contact-manifolds
Browse files Browse the repository at this point in the history
  • Loading branch information
Jondolf committed Jul 20, 2023
2 parents a208e67 + 66d112f commit b3d0b48
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
9 changes: 9 additions & 0 deletions crates/benches_common_3d/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "benches_common_3d"
version = "0.1.0"
edition = "2021"

[dependencies]
bevy = { version = "0.11", default-features = false }
bevy_xpbd_3d = { path = "../bevy_xpbd_3d", default-features = false }
criterion = "0.4"
40 changes: 40 additions & 0 deletions crates/benches_common_3d/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use bevy::prelude::*;
use bevy_xpbd_3d::prelude::*;
use criterion::{measurement::Measurement, BatchSize, Bencher};

pub fn bench_app<M: Measurement>(
bencher: &mut Bencher<'_, M>,
steps: u32,
setup: impl Fn(&mut App),
) {
bencher.iter_batched_ref(
move || {
let mut app = App::new();

app.add_plugins((
MinimalPlugins,
HierarchyPlugin,
TransformPlugin,
PhysicsPlugins::default(),
));

app.insert_resource(PhysicsTimestep::FixedOnce(1.0 / 60.0));

setup(&mut app);

while !app.ready() {
bevy::tasks::tick_global_task_pools_on_main_thread();
}

app.finish();
app.cleanup();
app
},
move |app| {
for _ in 0..steps {
app.update();
}
},
BatchSize::PerIteration,
);
}
6 changes: 6 additions & 0 deletions crates/bevy_xpbd_3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ derive_more = "0.99"

[dev-dependencies]
examples_common_3d = { path = "../examples_common_3d" }
benches_common_3d = { path = "../benches_common_3d" }
approx = "0.5"
criterion = { version = "0.4", features = ["html_reports"] }
glam = { version = "0.24", features = [ "approx" ] }
insta = "1.0"
itertools = "0.10"
Expand Down Expand Up @@ -76,3 +78,7 @@ required-features = ["3d"]
[[example]]
name = "custom_constraint"
required-features = ["3d"]

[[bench]]
name = "cubes"
harness = false
48 changes: 48 additions & 0 deletions crates/bevy_xpbd_3d/benches/cubes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::time::Duration;

use benches_common_3d::bench_app;
use bevy::prelude::*;
use bevy_xpbd_3d::math::*;
use bevy_xpbd_3d::prelude::*;
use criterion::{criterion_group, criterion_main, Criterion};

fn setup_cubes(app: &mut App, size: u32) {
app.add_systems(Startup, move |mut commands: Commands| {
commands.spawn((
RigidBody::Static,
Position(-2.0 * Vector::Z),
Collider::cuboid(100.0, 1.0, 100.0),
));

for x in 0..size {
for z in 0..size {
commands.spawn((
RigidBody::Dynamic,
Position(Vector::new(x as Scalar, 2.0, z as Scalar)),
Collider::cuboid(1.0, 1.0, 1.0),
));
}
}
});
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("cubes 3x3, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 3))
});

c.bench_function("cubes 5x5, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 5))
});

c.bench_function("cubes 10x10, 30 steps", |b| {
bench_app(b, 30, |app| setup_cubes(app, 10))
});
}

criterion_group!(
name = benches;
config = Criterion::default().measurement_time(Duration::from_secs(20));
targets = criterion_benchmark
);
criterion_main!(benches);
14 changes: 13 additions & 1 deletion src/plugins/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ impl Plugin for PreparePlugin {
app.add_systems(
self.schedule.dyn_clone(),
(
(init_rigid_bodies, init_mass_properties, init_colliders),
(
bevy::transform::systems::sync_simple_transforms,
bevy::transform::systems::propagate_transforms,
init_rigid_bodies,
)
.chain()
.run_if(any_new_rigid_bodies),
init_mass_properties,
init_colliders,
update_mass_properties,
)
.chain()
Expand All @@ -65,6 +73,10 @@ type RigidBodyComponents = (
Option<&'static TimeSleeping>,
);

fn any_new_rigid_bodies(query: Query<(), Added<RigidBody>>) -> bool {
!query.is_empty()
}

fn init_rigid_bodies(
mut commands: Commands,
mut bodies: Query<RigidBodyComponents, Added<RigidBody>>,
Expand Down
25 changes: 16 additions & 9 deletions src/plugins/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//!
//! See [`PhysicsSetupPlugin`].
use bevy::transform::TransformSystem;
use bevy::{
ecs::schedule::{ExecutorKind, ScheduleBuildSettings},
transform::TransformSystem,
};

use crate::prelude::*;

Expand Down Expand Up @@ -115,10 +118,12 @@ impl Plugin for PhysicsSetupPlugin {
// Create physics schedule, the schedule that advances the physics simulation
let mut physics_schedule = Schedule::default();

physics_schedule.set_build_settings(bevy::ecs::schedule::ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});
physics_schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

physics_schedule.configure_sets(
(
Expand All @@ -140,10 +145,12 @@ impl Plugin for PhysicsSetupPlugin {
// Create substep schedule, the schedule that runs the inner substepping loop
let mut substep_schedule = Schedule::default();

substep_schedule.set_build_settings(bevy::ecs::schedule::ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});
substep_schedule
.set_executor_kind(ExecutorKind::SingleThreaded)
.set_build_settings(ScheduleBuildSettings {
ambiguity_detection: LogLevel::Error,
..default()
});

substep_schedule.configure_sets(
(
Expand Down

0 comments on commit b3d0b48

Please sign in to comment.