[Bf-blender-cvs] [dee695f2da0] functions: cleanup usage of particle indices

Jacques Lucke noreply at git.blender.org
Wed Jul 10 17:18:24 CEST 2019


Commit: dee695f2da07a403b4e80b1a8a5940c2881b3e08
Author: Jacques Lucke
Date:   Wed Jul 10 16:45:05 2019 +0200
Branches: functions
https://developer.blender.org/rBdee695f2da07a403b4e80b1a8a5940c2881b3e08

cleanup usage of particle indices

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

M	source/blender/blenlib/BLI_array_allocator.hpp
M	source/blender/simulations/bparticles/action_interface.cpp
M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/actions.cpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/blenlib/BLI_array_allocator.hpp b/source/blender/blenlib/BLI_array_allocator.hpp
index 34a9d773be8..736a217b1bc 100644
--- a/source/blender/blenlib/BLI_array_allocator.hpp
+++ b/source/blender/blenlib/BLI_array_allocator.hpp
@@ -193,6 +193,11 @@ class ArrayAllocator {
     {
       return ArrayRef<T>(m_ptr, m_size);
     }
+
+    T &operator[](uint index)
+    {
+      return ((T *)m_ptr)[index];
+    }
   };
 
  private:
diff --git a/source/blender/simulations/bparticles/action_interface.cpp b/source/blender/simulations/bparticles/action_interface.cpp
index ac2b4bbe54d..66071153399 100644
--- a/source/blender/simulations/bparticles/action_interface.cpp
+++ b/source/blender/simulations/bparticles/action_interface.cpp
@@ -6,22 +6,15 @@ Action::~Action()
 {
 }
 
-void ActionInterface::execute_action_for_subset(ArrayRef<uint> indices,
+void ActionInterface::execute_action_for_subset(ArrayRef<uint> pindices,
                                                 std::unique_ptr<Action> &action)
 {
-  SmallVector<float> sub_current_times;
-  SmallVector<uint> pindices;
-  for (uint i : indices) {
-    pindices.append(m_particles.get_particle_index(i));
-    sub_current_times.append(m_current_times[i]);
-  }
-
   ParticleSet sub_particles(m_particles.block(), pindices);
   ActionInterface sub_interface(m_particle_allocator,
                                 m_array_allocator,
                                 sub_particles,
                                 m_attribute_offsets,
-                                sub_current_times,
+                                m_current_times,
                                 m_remaining_times,
                                 m_event_info);
   action->execute(sub_interface);
diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index 992c765faa7..6e56f8cf346 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -87,7 +87,7 @@ class ActionInterface {
   float remaining_time_in_step(uint pindex);
   ArrayRef<float> current_times();
   void kill(ArrayRef<uint> pindices);
-  void execute_action_for_subset(ArrayRef<uint> indices, std::unique_ptr<Action> &action);
+  void execute_action_for_subset(ArrayRef<uint> pindices, std::unique_ptr<Action> &action);
   ParticleAllocator &particle_allocator();
   ArrayAllocator &array_allocator();
 };
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index 0ba0fed7168..f74d465a4bd 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -35,9 +35,7 @@ class ChangeDirectionAction : public Action {
     FN::ExecutionStack stack;
     FN::ExecutionContext execution_context(stack);
 
-    for (uint i : particles.range()) {
-      uint pindex = particles.get_particle_index(i);
-
+    for (uint pindex : particles.indices()) {
       caller.call(fn_in, fn_out, execution_context, pindex);
       float3 direction = fn_out.get<float3>(0);
 
@@ -101,15 +99,13 @@ class ExplodeAction : public Action {
     FN::ExecutionStack stack;
     FN::ExecutionContext execution_context(stack);
 
-    for (uint i : particles.range()) {
-      uint pindex = particles.get_particle_index(i);
-
+    for (uint pindex : particles.indices()) {
       caller.call(fn_in, fn_out, execution_context, pindex);
       uint parts_amount = std::max(0, fn_out.get<int>(0));
       float speed = fn_out.get<float>(1);
 
       new_positions.append_n_times(positions[pindex], parts_amount);
-      new_birth_times.append_n_times(interface.current_times()[i], parts_amount);
+      new_birth_times.append_n_times(interface.current_times()[pindex], parts_amount);
 
       for (uint j = 0; j < parts_amount; j++) {
         new_velocities.append(random_direction() * speed);
@@ -145,38 +141,36 @@ class ConditionAction : public Action {
   void execute(ActionInterface &interface) override
   {
     ParticleSet particles = interface.particles();
-    SmallVector<bool> conditions(particles.size());
+    ArrayAllocator::Array<bool> conditions(interface.array_allocator());
     this->compute_conditions(interface, conditions);
 
-    SmallVector<uint> true_indices, false_indices;
-    for (uint i : particles.range()) {
-      if (conditions[i]) {
-        true_indices.append(i);
+    SmallVector<uint> true_pindices, false_pindices;
+    for (uint pindex : particles.indices()) {
+      if (conditions[pindex]) {
+        true_pindices.append(pindex);
       }
       else {
-        false_indices.append(i);
+        false_pindices.append(pindex);
       }
     }
 
-    interface.execute_action_for_subset(true_indices, m_true_action);
-    interface.execute_action_for_subset(false_indices, m_false_action);
+    interface.execute_action_for_subset(true_pindices, m_true_action);
+    interface.execute_action_for_subset(false_pindices, m_false_action);
   }
 
   void compute_conditions(ActionInterface &interface, ArrayRef<bool> r_conditions)
   {
     ParticleSet particles = interface.particles();
-    BLI_assert(particles.size() == r_conditions.size());
 
     auto caller = m_compute_inputs.get_caller(particles.attributes(), interface.event_info());
     FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
 
     FN::ExecutionStack stack;
     FN::ExecutionContext execution_context(stack);
-    for (uint i : particles.range()) {
-      uint pindex = particles.get_particle_index(i);
+    for (uint pindex : particles.indices()) {
       caller.call(fn_in, fn_out, execution_context, pindex);
       bool condition = fn_out.get<bool>(0);
-      r_conditions[i] = condition;
+      r_conditions[pindex] = condition;
     }
   }
 };
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index c79137e83c5..84dec3e312f 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -133,17 +133,13 @@ BLI_NOINLINE static void find_pindices_per_event(ArrayRef<uint> pindices_with_ev
   }
 }
 
-BLI_NOINLINE static void compute_current_time_per_particle(
-    ArrayRef<uint> pindices_with_event,
-    ArrayRef<float> remaining_durations,
-    float end_time,
-    ArrayRef<int> next_event_indices,
-    ArrayRef<SmallVector<float>> r_current_time_per_particle)
+BLI_NOINLINE static void compute_current_time_per_particle(ArrayRef<uint> pindices_with_event,
+                                                           ArrayRef<float> remaining_durations,
+                                                           float end_time,
+                                                           ArrayRef<float> r_current_times)
 {
   for (uint pindex : pindices_with_event) {
-    int event_index = next_event_indices[pindex];
-    BLI_assert(event_index >= 0);
-    r_current_time_per_particle[event_index].append(end_time - remaining_durations[pindex]);
+    r_current_times[pindex] = end_time - remaining_durations[pindex];
   }
 }
 
@@ -167,14 +163,13 @@ BLI_NOINLINE static void execute_events(ParticleAllocator &particle_allocator,
                                         ArrayAllocator &array_allocator,
                                         ParticlesBlock &block,
                                         ArrayRef<SmallVector<uint>> pindices_per_event,
-                                        ArrayRef<SmallVector<float>> current_time_per_particle,
+                                        ArrayRef<float> current_times,
                                         ArrayRef<float> remaining_durations,
                                         ArrayRef<Event *> events,
                                         EventStorage &event_storage,
                                         AttributeArrays attribute_offsets)
 {
   BLI_assert(events.size() == pindices_per_event.size());
-  BLI_assert(events.size() == current_time_per_particle.size());
 
   for (uint event_index = 0; event_index < events.size(); event_index++) {
     Event *event = events[event_index];
@@ -186,7 +181,7 @@ BLI_NOINLINE static void execute_events(ParticleAllocator &particle_allocator,
     EventExecuteInterface interface(particles,
                                     particle_allocator,
                                     array_allocator,
-                                    current_time_per_particle[event_index],
+                                    current_times,
                                     remaining_durations,
                                     event_storage,
                                     attribute_offsets);
@@ -234,18 +229,15 @@ BLI_NOINLINE static void simulate_to_next_event(ArrayAllocator &array_allocator,
   SmallVector<SmallVector<uint>> particles_per_event(events.size());
   find_pindices_per_event(pindices_with_event, next_event_indices, particles_per_event);
 
-  SmallVector<SmallVector<float>> current_time_per_particle(events.size());
-  compute_current_time_per_particle(pindices_with_event,
-                                    remaining_durations,
-                                    end_time,
-                                    next_event_indices,
-                                    current_time_per_particle);
+  ArrayAllocator::Array<float> current_times(array_allocator);
+  compute_current_time_per_particle(
+      pindices_with_event, remaining_durations, end_time, current_times);
 
   execute_events(particle_allocator,
                  array_allocator,
                  particles.block(),
                  particles_per_event,
-                 current_time_per_particle,
+                 current_times,
                  remaining_durations,
                  events,
                  event_storage,



More information about the Bf-blender-cvs mailing list