From c56ebcc663d15ded86233d8caa11d00179e5ec16 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 17:58:37 +1100 Subject: refactor testbed to use harness --- src_testbed/harness/mod.rs | 39 ++++++++++++++++++++++----------------- src_testbed/harness/plugin.rs | 4 ++-- 2 files changed, 24 insertions(+), 19 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index fa6c4c6..6467db5 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -7,24 +7,35 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; pub mod plugin; -pub struct HarnessState { +pub struct RunState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, pub timestep_id: usize, + pub time: f32, +} + +impl RunState { + pub fn new() -> Self { + Self { + #[cfg(feature = "parallel")] + thread_pool: rapier::rayon::ThreadPool, + timestep_id: 0, + time: 0.0 + } + } } pub struct Harness { - physics: PhysicsState, + pub physics: PhysicsState, max_steps: usize, callbacks: Callbacks, plugins: Vec>, - time: f32, events: PhysicsEvents, event_handler: ChannelEventCollector, - pub state: HarnessState, + pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { @@ -46,18 +57,13 @@ impl Harness { proximity_events: proximity_channel.1, }; let physics = PhysicsState::new(); - let state = HarnessState { - #[cfg(feature = "parallel")] - thread_pool, - timestep_id: 0, - }; + let state = RunState::new(); Self { physics, max_steps: 1000, callbacks: Vec::new(), plugins: Vec::new(), - time: 0.0, events, event_handler, state, @@ -101,7 +107,6 @@ impl Harness { self.physics.joints = joints; self.physics.broad_phase = BroadPhase::new(); self.physics.narrow_phase = NarrowPhase::new(); - self.time = 0.0; self.state.timestep_id = 0; self.physics.query_pipeline = QueryPipeline::new(); self.physics.pipeline = PhysicsPipeline::new(); @@ -113,7 +118,7 @@ impl Harness { } pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, + F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState, f32) + 'static, >( &mut self, callback: F, @@ -165,22 +170,22 @@ impl Harness { } for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state, self.time) + f(&mut self.physics, &self.events, &self.state, self.state.time) } for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.time) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.state.time) } self.events.poll_all(); - self.time += self.physics.integration_parameters.dt(); + self.state.time += self.physics.integration_parameters.dt(); + self.state.timestep_id += 1; } pub fn run(&mut self) { for _ in 0..self.max_steps { self.step(); - self.state.timestep_id += 1; } } } diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index 078219e..702efe2 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -1,4 +1,4 @@ -use crate::harness::HarnessState; +use crate::harness::RunState; use crate::physics::PhysicsEvents; use crate::PhysicsState; @@ -7,7 +7,7 @@ pub trait HarnessPlugin { &mut self, physics: &mut PhysicsState, events: &PhysicsEvents, - harness_state: &HarnessState, + harness_state: &RunState, t: f32, ); fn step(&mut self, physics: &mut PhysicsState); -- cgit From 2de41ee5e30df5e67c98427100304a5b4ded085f Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 18:31:59 +1100 Subject: change HarnessPlugin trait to add run_state to the step trait method --- src_testbed/harness/mod.rs | 2 +- src_testbed/harness/plugin.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 6467db5..dd49fe6 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -166,7 +166,7 @@ impl Harness { .update(&self.physics.bodies, &self.physics.colliders); for plugin in &mut self.plugins { - plugin.step(&mut self.physics) + plugin.step(&mut self.physics, &self.state) } for f in &mut self.callbacks { diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index 702efe2..ef0ebc4 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -10,6 +10,6 @@ pub trait HarnessPlugin { harness_state: &RunState, t: f32, ); - fn step(&mut self, physics: &mut PhysicsState); + fn step(&mut self, physics: &mut PhysicsState, run_state: &RunState); fn profiling_string(&self) -> String; } -- cgit From b1d0dc006d39faeb4a3ef85acc62f2981d26a85b Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 20:16:11 +1100 Subject: cargo fmt --- src_testbed/harness/mod.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index dd49fe6..c6f682c 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -20,7 +20,7 @@ impl RunState { #[cfg(feature = "parallel")] thread_pool: rapier::rayon::ThreadPool, timestep_id: 0, - time: 0.0 + time: 0.0, } } } @@ -117,9 +117,7 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, &RunState, f32) + 'static, - >( + pub fn add_callback( &mut self, callback: F, ) { @@ -170,11 +168,21 @@ impl Harness { } for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state, self.state.time) + f( + &mut self.physics, + &self.events, + &self.state, + self.state.time, + ) } for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.state.time) + plugin.run_callbacks( + &mut self.physics, + &self.events, + &self.state, + self.state.time, + ) } self.events.poll_all(); -- cgit From 70d05cc63f24945d5d59c6e14aa6f36c36304a84 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:24:04 +1100 Subject: fix typo with creating threadpool in RunState --- src_testbed/harness/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index c6f682c..379a42c 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -18,7 +18,10 @@ impl RunState { pub fn new() -> Self { Self { #[cfg(feature = "parallel")] - thread_pool: rapier::rayon::ThreadPool, + thread_pool: rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(), timestep_id: 0, time: 0.0, } -- cgit From 975f0d149f84068e46c7bb8bd6514378aa6aeaf4 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:25:49 +1100 Subject: add num_threads back in too --- src_testbed/harness/mod.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 379a42c..228ffe0 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -16,12 +16,18 @@ pub struct RunState { impl RunState { pub fn new() -> Self { + #[cfg(feature = "parallel")] + let num_threads = num_cpus::get_physical(); + + #[cfg(feature = "parallel")] + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + Self { #[cfg(feature = "parallel")] - thread_pool: rapier::rayon::ThreadPoolBuilder::new() - .num_threads(num_threads) - .build() - .unwrap(), + thread_pool: thread_pool, timestep_id: 0, time: 0.0, } -- cgit From 5fce09cb52f7c24662650a998d279433a324e517 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:42:46 +1100 Subject: rework some threading code with the ui --- src_testbed/harness/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 228ffe0..916823e 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -10,6 +10,8 @@ pub mod plugin; pub struct RunState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, + #[cfg(feature = "parallel")] + pub num_threads: usize, pub timestep_id: usize, pub time: f32, } @@ -28,6 +30,8 @@ impl RunState { Self { #[cfg(feature = "parallel")] thread_pool: thread_pool, + #[cfg(feature = "parallel")] + num_threads, timestep_id: 0, time: 0.0, } -- cgit From caf9d71bc7ae069d1d80194164f54b1132e3b2ad Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 24 Dec 2020 21:45:00 +1100 Subject: cargo fmt --- src_testbed/harness/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 916823e..45e52dc 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -19,10 +19,10 @@ pub struct RunState { impl RunState { pub fn new() -> Self { #[cfg(feature = "parallel")] - let num_threads = num_cpus::get_physical(); + let num_threads = num_cpus::get_physical(); #[cfg(feature = "parallel")] - let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() .num_threads(num_threads) .build() .unwrap(); -- cgit From 6f508e5d04e5652991f9feaad09231af84542ac1 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:23:25 +1100 Subject: remove redundant time :f32 from harness callbacks. it can be access via run_state.time --- src_testbed/harness/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 45e52dc..638c4ed 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -48,7 +48,7 @@ pub struct Harness { pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { @@ -130,7 +130,7 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback( + pub fn add_callback( &mut self, callback: F, ) { @@ -185,7 +185,6 @@ impl Harness { &mut self.physics, &self.events, &self.state, - self.state.time, ) } -- cgit From c300ce760c9151d7cfc9391600750ca9bc6ad600 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:26:11 +1100 Subject: cargo fmt --- src_testbed/harness/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 638c4ed..aa73afd 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -181,11 +181,7 @@ impl Harness { } for f in &mut self.callbacks { - f( - &mut self.physics, - &self.events, - &self.state, - ) + f(&mut self.physics, &self.events, &self.state) } for plugin in &mut self.plugins { -- cgit From 6d5b6d778da11decd8303ac23260fc7e5dd3080e Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:29:09 +1100 Subject: remove time field from HarnessPlugin trait --- src_testbed/harness/plugin.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/plugin.rs b/src_testbed/harness/plugin.rs index ef0ebc4..ac20945 100644 --- a/src_testbed/harness/plugin.rs +++ b/src_testbed/harness/plugin.rs @@ -8,7 +8,6 @@ pub trait HarnessPlugin { physics: &mut PhysicsState, events: &PhysicsEvents, harness_state: &RunState, - t: f32, ); fn step(&mut self, physics: &mut PhysicsState, run_state: &RunState); fn profiling_string(&self) -> String; -- cgit From 5fb9304f4cf7c7efdffae3f4c9b83a88ee2df048 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 31 Dec 2020 15:33:33 +1100 Subject: remove time from plugin.run_callbacks --- src_testbed/harness/mod.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index aa73afd..86cee09 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -185,12 +185,7 @@ impl Harness { } for plugin in &mut self.plugins { - plugin.run_callbacks( - &mut self.physics, - &self.events, - &self.state, - self.state.time, - ) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state) } self.events.poll_all(); -- cgit From 34e79e9afc21ff0202d8a0338d0e8e038402a159 Mon Sep 17 00:00:00 2001 From: rezural Date: Sat, 2 Jan 2021 16:45:55 +1100 Subject: unify callbacks with & without graphics & window --- src_testbed/harness/mod.rs | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 86cee09..51bf315 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,4 +1,8 @@ -use crate::physics::{PhysicsEvents, PhysicsState}; +use crate::{ + physics::{PhysicsEvents, PhysicsState}, + GraphicsManager, +}; +use kiss3d::window::Window; use plugin::HarnessPlugin; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; @@ -48,7 +52,17 @@ pub struct Harness { pub state: RunState, } -type Callbacks = Vec>; +type Callbacks = Vec< + Box< + dyn FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ), + >, +>; #[allow(dead_code)] impl Harness { @@ -130,7 +144,15 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback( + pub fn add_callback< + F: FnMut( + Option<&mut Window>, + Option<&mut GraphicsManager>, + &mut PhysicsState, + &PhysicsEvents, + &RunState, + ) + 'static, + >( &mut self, callback: F, ) { @@ -138,6 +160,14 @@ impl Harness { } pub fn step(&mut self) { + self.step_with_graphics(None, None); + } + + pub fn step_with_graphics( + &mut self, + window: Option<&mut Window>, + graphics: Option<&mut GraphicsManager>, + ) { #[cfg(feature = "parallel")] { let physics = &mut self.physics; @@ -180,8 +210,26 @@ impl Harness { plugin.step(&mut self.physics, &self.state) } - for f in &mut self.callbacks { - f(&mut self.physics, &self.events, &self.state) + // FIXME: This assumes either window & graphics are Some, or they are all None + // this is required as we cannot pass Option<&mut Window> & Option<&mut GraphicsManager directly in a loop + // there must be a better way of doing this? + match (window, graphics) { + (Some(window), Some(graphics)) => { + for f in &mut self.callbacks { + f( + Some(window), + Some(graphics), + &mut self.physics, + &self.events, + &self.state, + ); + } + } + _ => { + for f in &mut self.callbacks { + f(None, None, &mut self.physics, &self.events, &self.state); + } + } } for plugin in &mut self.plugins { -- cgit From 315493ebfb06af9924fdabb8f26da4d1a9b095bc Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 22 Jan 2021 13:38:59 +0100 Subject: IntegrationParameters: deprectate dt() and inv_dt() methods --- src_testbed/harness/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src_testbed/harness') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 51bf315..2251038 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -238,7 +238,7 @@ impl Harness { self.events.poll_all(); - self.state.time += self.physics.integration_parameters.dt(); + self.state.time += self.physics.integration_parameters.dt; self.state.timestep_id += 1; } -- cgit