[Bf-blender-cvs] [b7b2849c825] functions: initial event storage

Jacques Lucke noreply at git.blender.org
Sun Jun 30 16:01:24 CEST 2019


Commit: b7b2849c825ffcafac83c61de3e34b6ac172daa6
Author: Jacques Lucke
Date:   Sun Jun 30 14:34:31 2019 +0200
Branches: functions
https://developer.blender.org/rBb7b2849c825ffcafac83c61de3e34b6ac172daa6

initial event storage

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

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

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

diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 083b92d0b88..cf308892d1e 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -295,6 +295,29 @@ class Force {
   virtual void add_force(ParticlesBlock &block, ArrayRef<float3> r_force) = 0;
 };
 
+class EventStorage {
+ private:
+  void *m_array;
+  uint m_stride;
+
+ public:
+  EventStorage(void *array, uint stride) : m_array(array), m_stride(stride)
+  {
+  }
+
+  EventStorage(EventStorage &other) = delete;
+
+  void *operator[](uint index)
+  {
+    return POINTER_OFFSET(m_array, m_stride * index);
+  }
+
+  template<typename T> T &get(uint index)
+  {
+    return *(T *)(*this)[index];
+  }
+};
+
 class EventFilterInterface {
  private:
   ParticleSet m_particles;
@@ -302,6 +325,7 @@ class EventFilterInterface {
   ArrayRef<float> m_durations;
   float m_end_time;
 
+  EventStorage &m_event_storage;
   SmallVector<uint> &m_filtered_indices;
   SmallVector<float> &m_filtered_time_factors;
 
@@ -310,12 +334,14 @@ class EventFilterInterface {
                        AttributeArrays &attribute_offsets,
                        ArrayRef<float> durations,
                        float end_time,
+                       EventStorage &r_event_storage,
                        SmallVector<uint> &r_filtered_indices,
                        SmallVector<float> &r_filtered_time_factors)
       : m_particles(particles),
         m_attribute_offsets(attribute_offsets),
         m_durations(durations),
         m_end_time(end_time),
+        m_event_storage(r_event_storage),
         m_filtered_indices(r_filtered_indices),
         m_filtered_time_factors(r_filtered_time_factors)
   {
@@ -352,6 +378,12 @@ class EventFilterInterface {
     m_filtered_indices.append(index);
     m_filtered_time_factors.append(time_factor);
   }
+
+  template<typename T> T &trigger_particle(uint index, float time_factor)
+  {
+    this->trigger_particle(index, time_factor);
+    return m_event_storage.get<T>(m_particles.get_particle_index(index));
+  }
 };
 
 class EventFilter {
@@ -368,15 +400,18 @@ class EventExecuteInterface {
   SmallVector<InstantEmitTarget *> m_emit_targets;
   ArrayRef<float> m_current_times;
   ArrayRef<uint8_t> m_kill_states;
+  EventStorage &m_event_storage;
 
  public:
   EventExecuteInterface(ParticleSet particles,
                         BlockAllocator &block_allocator,
-                        ArrayRef<float> current_times)
+                        ArrayRef<float> current_times,
+                        EventStorage &event_storage)
       : m_particles(particles),
         m_block_allocator(block_allocator),
         m_current_times(current_times),
-        m_kill_states(m_particles.attributes().get_byte("Kill State"))
+        m_kill_states(m_particles.attributes().get_byte("Kill State")),
+        m_event_storage(event_storage)
   {
   }
 
@@ -410,6 +445,11 @@ class EventExecuteInterface {
   {
     return m_current_times;
   }
+
+  template<typename T> T &get_storage(uint pindex)
+  {
+    return m_event_storage.get<T>(pindex);
+  }
 };
 
 class Action {
@@ -423,6 +463,11 @@ class Event {
  public:
   virtual ~Event();
 
+  virtual uint storage_size()
+  {
+    return 0;
+  }
+
   virtual void filter(EventFilterInterface &interface) = 0;
   virtual void execute(EventExecuteInterface &interface) = 0;
 };
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 6188d17a51a..3f7664d5387 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -41,6 +41,7 @@ BLI_NOINLINE static void find_next_event_per_particle(ParticleSet particles,
                                                       ArrayRef<float> durations,
                                                       float end_time,
                                                       ArrayRef<Event *> events,
+                                                      EventStorage &r_event_storage,
                                                       ArrayRef<int> r_next_event_indices,
                                                       ArrayRef<float> r_time_factors_to_next_event,
                                                       VectorAdaptor<uint> &r_indices_with_event)
@@ -52,11 +53,14 @@ BLI_NOINLINE static void find_next_event_per_particle(ParticleSet particles,
     SmallVector<uint> triggered_indices;
     SmallVector<float> triggered_time_factors;
 
+    /* TODO: make sure that one event does not override the storage of another,
+     * if it comes later. */
     Event *event = events[event_index];
     EventFilterInterface interface(particles,
                                    attribute_offsets,
                                    durations,
                                    end_time,
+                                   r_event_storage,
                                    triggered_indices,
                                    triggered_time_factors);
     event->filter(interface);
@@ -177,7 +181,8 @@ BLI_NOINLINE static void execute_events(BlockAllocator &block_allocator,
                                         ParticlesBlock &block,
                                         ArrayRef<SmallVector<uint>> particle_indices_per_event,
                                         ArrayRef<SmallVector<float>> current_time_per_particle,
-                                        ArrayRef<Event *> events)
+                                        ArrayRef<Event *> events,
+                                        EventStorage &event_storage)
 {
   BLI_assert(events.size() == particle_indices_per_event.size());
   BLI_assert(events.size() == current_time_per_particle.size());
@@ -190,7 +195,7 @@ BLI_NOINLINE static void execute_events(BlockAllocator &block_allocator,
     }
 
     EventExecuteInterface interface(
-        particles, block_allocator, current_time_per_particle[event_index]);
+        particles, block_allocator, current_time_per_particle[event_index], event_storage);
     event->execute(interface);
   }
 }
@@ -220,11 +225,19 @@ BLI_NOINLINE static void simulate_to_next_event(FixedArrayAllocator &array_alloc
       time_factors_to_next_event_array, amount, amount);
   VectorAdaptor<uint> indices_with_event(indices_with_event_array, amount);
 
+  uint max_event_storage_size = 1;
+  for (Event *event : events) {
+    max_event_storage_size = std::max(max_event_storage_size, event->storage_size());
+  }
+  void *event_storage_array = array_allocator.allocate_array(max_event_storage_size);
+  EventStorage event_storage(event_storage_array, max_event_storage_size);
+
   find_next_event_per_particle(particles,
                                attribute_offsets,
                                durations,
                                end_time,
                                events,
+                               event_storage,
                                next_event_indices,
                                time_factors_to_next_event,
                                indices_with_event);
@@ -256,8 +269,12 @@ BLI_NOINLINE static void simulate_to_next_event(FixedArrayAllocator &array_alloc
                                     time_factors_to_next_event,
                                     current_time_per_particle);
 
-  execute_events(
-      block_allocator, particles.block(), particles_per_event, current_time_per_particle, events);
+  execute_events(block_allocator,
+                 particles.block(),
+                 particles_per_event,
+                 current_time_per_particle,
+                 events,
+                 event_storage);
 
   find_unfinished_particles(indices_with_event,
                             particles.indices(),
@@ -271,6 +288,7 @@ BLI_NOINLINE static void simulate_to_next_event(FixedArrayAllocator &array_alloc
   array_allocator.deallocate_array(time_factors_to_next_event_array);
   array_allocator.deallocate_array(indices_with_event_array);
   array_allocator.deallocate_array(particle_indices_with_event_array);
+  array_allocator.deallocate_array(event_storage_array, max_event_storage_size);
 }
 
 BLI_NOINLINE static void simulate_with_max_n_events(



More information about the Bf-blender-cvs mailing list