[Bf-blender-cvs] [cd0f866aef3] functions: move time from ParticlesState to SimulationState

Jacques Lucke noreply at git.blender.org
Mon Aug 26 15:37:42 CEST 2019


Commit: cd0f866aef3fe5f87c044619141bd0e6551a4c19
Author: Jacques Lucke
Date:   Mon Aug 26 15:21:10 2019 +0200
Branches: functions
https://developer.blender.org/rBcd0f866aef3fe5f87c044619141bd0e6551a4c19

move time from ParticlesState to SimulationState

===================================================================

M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particles_state.hpp
M	source/blender/simulations/bparticles/simulate.cpp
M	source/blender/simulations/bparticles/simulate.hpp
M	source/blender/simulations/bparticles/simulation_state.hpp
M	source/blender/simulations/bparticles/step_description_interfaces.hpp
M	source/blender/simulations/bparticles/step_simulator.hpp

===================================================================

diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 3020aff5264..8fad20cd20d 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -61,11 +61,14 @@ void BParticles_simulate_modifier(BParticlesModifierData *bpmd,
   }
 
   SimulationState &simulation_state = *unwrap(state_c);
+  simulation_state.time().start_update(time_step);
 
   bNodeTree *btree = (bNodeTree *)DEG_get_original_id((ID *)bpmd->bparticles_tree);
   auto simulator = simulator_from_node_tree(btree);
 
-  simulator->simulate(simulation_state, time_step);
+  simulator->simulate(simulation_state);
+
+  simulation_state.time().end_update();
 
   auto &containers = simulation_state.particles().particle_containers();
   containers.foreach_key_value_pair([](StringRefNull type_name, ParticlesContainer *container) {
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index 5487c9c0ff8..46b0ebbfa74 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -495,7 +495,7 @@ class NodeTreeStepSimulator : public StepSimulator {
     m_vtree.freeze_and_index();
   }
 
-  void simulate(SimulationState &simulation_state, float time_step) override
+  void simulate(SimulationState &simulation_state) override
   {
     WorldState &old_world_state = simulation_state.world();
     WorldState new_world_state;
@@ -534,8 +534,7 @@ class NodeTreeStepSimulator : public StepSimulator {
       types_to_simulate.add_new(name, type_info);
     }
 
-    simulate_particles(
-        simulation_state.particles(), world_transition, time_step, emitters, types_to_simulate);
+    simulate_particles(simulation_state, world_transition, emitters, types_to_simulate);
 
     for (Emitter *emitter : emitters) {
       delete emitter;
diff --git a/source/blender/simulations/bparticles/particles_state.hpp b/source/blender/simulations/bparticles/particles_state.hpp
index df4703188f2..24dd55c9f48 100644
--- a/source/blender/simulations/bparticles/particles_state.hpp
+++ b/source/blender/simulations/bparticles/particles_state.hpp
@@ -7,39 +7,15 @@ namespace BParticles {
 
 using BLI::StringMap;
 
-/**
- * This holds the current state of an entire particle particle system. It only knows about the
- * particles and the current time, not how the system got there.
- *
- * The state can also be created independent of any particle system. It gets "fixed up" when it is
- * used in a simulation.
- */
 class ParticlesState {
  private:
   StringMap<ParticlesContainer *> m_container_by_id;
-  float m_current_time = 0.0f;
-  uint m_current_step = 0;
 
  public:
   ParticlesState() = default;
   ParticlesState(ParticlesState &other) = delete;
   ~ParticlesState();
 
-  /**
-   * Access the time since the simulation started.
-   */
-  float current_time() const;
-
-  /**
-   * Move current time forward.
-   */
-  void increase_time(float time_step);
-
-  /**
-   * Get the current simulation step.
-   */
-  uint current_step() const;
-
   /**
    * Access the mapping from particle type names to their corresponding containers.
    */
@@ -76,21 +52,4 @@ inline StringRefNull ParticlesState::particle_container_name(ParticlesContainer
   return result;
 }
 
-inline float ParticlesState::current_time() const
-{
-  return m_current_time;
-}
-
-inline void ParticlesState::increase_time(float time_step)
-{
-  BLI_assert(time_step >= 0.0f);
-  m_current_time += time_step;
-  m_current_step++;
-}
-
-inline uint ParticlesState::current_step() const
-{
-  return m_current_step;
-}
-
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 6b1bb4f819c..2ad036ba1f9 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -615,24 +615,25 @@ BLI_NOINLINE static void emit_and_simulate_particles(
   }
 }
 
-void simulate_particles(ParticlesState &state,
+void simulate_particles(SimulationState &state,
                         WorldTransition &world_transition,
-                        float time_step,
                         ArrayRef<Emitter *> emitters,
                         StringMap<ParticleTypeInfo> &types_to_simulate)
 {
   SCOPED_TIMER(__func__);
 
-  float start_time = state.current_time();
-  state.increase_time(time_step);
-  TimeSpan time_span(start_time, time_step);
+  ParticlesState &particles_state = state.particles();
 
-  ensure_required_containers_exist(state, types_to_simulate);
-  ensure_required_attributes_exist(state, types_to_simulate);
+  ensure_required_containers_exist(particles_state, types_to_simulate);
+  ensure_required_attributes_exist(particles_state, types_to_simulate);
 
-  emit_and_simulate_particles(state, time_span, emitters, types_to_simulate, world_transition);
+  emit_and_simulate_particles(particles_state,
+                              state.time().current_update_time(),
+                              emitters,
+                              types_to_simulate,
+                              world_transition);
 
-  compress_all_containers(state);
+  compress_all_containers(particles_state);
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/simulate.hpp b/source/blender/simulations/bparticles/simulate.hpp
index f126582b04f..ff3adeb9ae3 100644
--- a/source/blender/simulations/bparticles/simulate.hpp
+++ b/source/blender/simulations/bparticles/simulate.hpp
@@ -13,9 +13,8 @@ struct ParticleTypeInfo {
   ArrayRef<OffsetHandler *> offset_handlers;
 };
 
-void simulate_particles(ParticlesState &state,
+void simulate_particles(SimulationState &state,
                         WorldTransition &world_transition,
-                        float time_step,
                         ArrayRef<Emitter *> emitters,
                         StringMap<ParticleTypeInfo> &types_to_simulate);
 
diff --git a/source/blender/simulations/bparticles/simulation_state.hpp b/source/blender/simulations/bparticles/simulation_state.hpp
index 6fb317f5afd..a3f4302eace 100644
--- a/source/blender/simulations/bparticles/simulation_state.hpp
+++ b/source/blender/simulations/bparticles/simulation_state.hpp
@@ -2,13 +2,59 @@
 
 #include "particles_state.hpp"
 #include "world_state.hpp"
+#include "time_span.hpp"
 
 namespace BParticles {
 
+class SimulationTimeState {
+ private:
+  bool m_is_updating = false;
+  float m_simulation_time = 0.0f;
+  float m_update_start_time = 0.0f;
+  float m_update_duration = 0.0f;
+  uint m_current_update_index = 0;
+
+ public:
+  bool is_updating() const
+  {
+    return m_is_updating;
+  }
+
+  TimeSpan current_update_time() const
+  {
+    BLI_assert(m_is_updating);
+    return TimeSpan(m_update_start_time, m_update_duration);
+  }
+
+  uint current_update_index() const
+  {
+    BLI_assert(m_is_updating);
+    return m_current_update_index;
+  }
+
+  void start_update(float time_step)
+  {
+    BLI_assert(time_step >= 0);
+    BLI_assert(!m_is_updating);
+    m_is_updating = true;
+    m_update_start_time = m_simulation_time;
+    m_update_duration = time_step;
+    m_current_update_index++;
+  }
+
+  void end_update()
+  {
+    BLI_assert(m_is_updating);
+    m_is_updating = false;
+    m_simulation_time = m_update_start_time + m_update_duration;
+  }
+};
+
 class SimulationState {
  private:
   ParticlesState m_particles;
   WorldState m_world;
+  SimulationTimeState m_time_state;
 
  public:
   ParticlesState &particles()
@@ -20,6 +66,11 @@ class SimulationState {
   {
     return m_world;
   }
+
+  SimulationTimeState &time()
+  {
+    return m_time_state;
+  }
 };
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/step_description_interfaces.hpp b/source/blender/simulations/bparticles/step_description_interfaces.hpp
index 9a0c813b442..9d121b46003 100644
--- a/source/blender/simulations/bparticles/step_description_interfaces.hpp
+++ b/source/blender/simulations/bparticles/step_description_interfaces.hpp
@@ -240,7 +240,7 @@ inline TimeSpan EmitterInterface::time_span()
 
 inline bool EmitterInterface::is_first_step()
 {
-  return m_particle_allocator.particles_state().current_step() == 1;
+  return m_time_span.start() == 0.0f;
 }
 
 inline WorldTransition &EmitterInterface::world_transition()
diff --git a/source/blender/simulations/bparticles/step_simulator.hpp b/source/blender/simulations/bparticles/step_simulator.hpp
index a302b568b52..1c611a70fd0 100644
--- a/source/blender/simulations/bparticles/step_simulator.hpp
+++ b/source/blender/simulations/bparticles/step_simulator.hpp
@@ -10,7 +10,7 @@ class StepSimulator {
   {
   }
 
-  virtual void simulate(SimulationState &simulation_state, float time_step) = 0;
+  virtual void simulate(SimulationState &simulation_state) = 0;
 };
 
 }  // namespace BParticles



More information about the Bf-blender-cvs mailing list