[Bf-blender-cvs] [83a8f830c07] functions: simulate particles of all types at the same time

Jacques Lucke noreply at git.blender.org
Fri Jun 28 10:38:16 CEST 2019


Commit: 83a8f830c072e5b41b0d15c579d57a8b51e88f19
Author: Jacques Lucke
Date:   Fri Jun 28 09:16:09 2019 +0200
Branches: functions
https://developer.blender.org/rB83a8f830c072e5b41b0d15c579d57a8b51e88f19

simulate particles of all types at the same time

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

M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 0ff727bc4a6..f8f4da0b2f8 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -28,7 +28,7 @@ StepDescription::~StepDescription()
 
 ParticlesState::~ParticlesState()
 {
-  for (ParticlesContainer *container : m_particle_containers.values()) {
+  for (ParticlesContainer *container : m_container_by_id.values()) {
     delete container;
   }
 }
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 2e784ae9cc6..e8596e97930 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -27,7 +27,7 @@ using std::unique_ptr;
 
 class ParticlesState {
  private:
-  SmallMap<uint, ParticlesContainer *> m_particle_containers;
+  SmallMap<uint, ParticlesContainer *> m_container_by_id;
 
  public:
   float m_current_time = 0.0f;
@@ -38,12 +38,23 @@ class ParticlesState {
 
   SmallMap<uint, ParticlesContainer *> &particle_containers()
   {
-    return m_particle_containers;
+    return m_container_by_id;
   }
 
   ParticlesContainer &particle_container(uint type_id)
   {
-    return *m_particle_containers.lookup(type_id);
+    return *m_container_by_id.lookup(type_id);
+  }
+
+  uint particle_container_id(ParticlesContainer &container)
+  {
+    for (auto item : m_container_by_id.items()) {
+      if (item.value == &container) {
+        return item.key;
+      }
+    }
+    BLI_assert(false);
+    return 0;
   }
 };
 
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 3f4bbbde9af..baa774077bd 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -355,8 +355,8 @@ struct StepBlocksParallelData {
   ArrayRef<ParticlesBlock *> blocks;
   ArrayRef<float> all_durations;
   float end_time;
-  ParticleType &particle_type;
   BlockAllocators &block_allocators;
+  StepDescription &step_description;
 };
 
 BLI_NOINLINE static void step_individual_particles_cb(void *__restrict userdata,
@@ -369,19 +369,24 @@ BLI_NOINLINE static void step_individual_particles_cb(void *__restrict userdata,
   BlockAllocator block_allocator = data->block_allocators.get_threadlocal_allocator(
       tls->thread_id);
 
+  ParticlesState &state = block_allocator.particles_state();
+  uint particle_type_id = state.particle_container_id(block.container());
+
+  ParticleType &particle_type = data->step_description.particle_type(particle_type_id);
+
   uint active_amount = block.active_amount();
   ParticleSet active_particles(block, static_number_range_ref(0, active_amount));
   step_particle_set(block_allocator,
                     active_particles,
                     data->all_durations.take_front(active_amount),
                     data->end_time,
-                    data->particle_type);
+                    particle_type);
 }
 
-BLI_NOINLINE static void step_individual_particles(BlockAllocators &block_allocators,
-                                                   ArrayRef<ParticlesBlock *> blocks,
-                                                   TimeSpan time_span,
-                                                   ParticleType &particle_type)
+BLI_NOINLINE static void simulate_blocks_threaded(BlockAllocators &block_allocators,
+                                                  ArrayRef<ParticlesBlock *> blocks,
+                                                  TimeSpan time_span,
+                                                  StepDescription &step_description)
 {
   if (blocks.size() == 0) {
     return;
@@ -395,7 +400,7 @@ BLI_NOINLINE static void step_individual_particles(BlockAllocators &block_alloca
   all_durations.fill(time_span.duration());
 
   StepBlocksParallelData data = {
-      blocks, all_durations, time_span.end(), particle_type, block_allocators};
+      blocks, all_durations, time_span.end(), block_allocators, step_description};
 
   BLI_task_parallel_range(
       0, blocks.size(), (void *)&data, step_individual_particles_cb, &settings);
@@ -537,13 +542,12 @@ void simulate_step(ParticlesState &state, StepDescription &description)
 
   BlockAllocators block_allocators(state);
 
+  SmallVector<ParticlesBlock *> existing_blocks;
   for (uint type_id : description.particle_type_ids()) {
-    ParticleType &type = description.particle_type(type_id);
     ParticlesContainer &container = *containers.lookup(type_id);
-
-    step_individual_particles(
-        block_allocators, container.active_blocks().to_small_vector(), time_span, type);
+    existing_blocks.extend(container.active_blocks().to_small_vector());
   }
+  simulate_blocks_threaded(block_allocators, existing_blocks, time_span, description);
 
   BlockAllocator &emitter_allocator = block_allocators.get_standalone_allocator();
   for (Emitter *emitter : description.emitters()) {



More information about the Bf-blender-cvs mailing list