[Bf-blender-cvs] [c718c9e6c5e] functions: refactor usage of MeshSurfaceContext

Jacques Lucke noreply at git.blender.org
Wed Sep 4 19:43:25 CEST 2019


Commit: c718c9e6c5ef214866e79587a4c903b995d9dd13
Author: Jacques Lucke
Date:   Wed Sep 4 12:49:09 2019 +0200
Branches: functions
https://developer.blender.org/rBc718c9e6c5ef214866e79587a4c903b995d9dd13

refactor usage of MeshSurfaceContext

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

M	source/blender/simulations/bparticles/action_contexts.hpp
M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/emitters.cpp
M	source/blender/simulations/bparticles/events.cpp

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

diff --git a/source/blender/simulations/bparticles/action_contexts.hpp b/source/blender/simulations/bparticles/action_contexts.hpp
index c76c00b5bee..81e01d97400 100644
--- a/source/blender/simulations/bparticles/action_contexts.hpp
+++ b/source/blender/simulations/bparticles/action_contexts.hpp
@@ -11,6 +11,7 @@ using BLI::float4x4;
 
 class MeshSurfaceContext : public ActionContext {
  private:
+  Vector<void *> m_buffers_to_free;
   Object *m_object;
   ArrayRef<float4x4> m_world_transforms;
   ArrayRef<float3> m_local_positions;
@@ -34,6 +35,43 @@ class MeshSurfaceContext : public ActionContext {
   {
   }
 
+  MeshSurfaceContext(Object *object,
+                     float4x4 world_transform,
+                     ArrayRef<uint> pindices,
+                     ArrayRef<float3> local_positions,
+                     ArrayRef<float3> local_normals,
+                     ArrayRef<uint> looptri_indices)
+      : m_object(object),
+        m_local_positions(local_positions),
+        m_local_normals(local_normals),
+        m_looptri_indices(looptri_indices)
+  {
+    uint size = local_positions.size();
+    float4x4 *world_transforms_buffer = (float4x4 *)BLI_temporary_allocate(sizeof(float4x4) *
+                                                                           size);
+    float3 *world_normals_buffer = (float3 *)BLI_temporary_allocate(sizeof(float3) * size);
+
+    for (uint pindex : pindices) {
+      world_transforms_buffer[pindex] = world_transform;
+      world_normals_buffer[pindex] = local_normals[pindex];
+    }
+
+    m_world_transforms = ArrayRef<float4x4>(world_transforms_buffer, size);
+    m_world_normals = ArrayRef<float3>(world_normals_buffer, size);
+
+    m_buffers_to_free.extend({world_transforms_buffer, world_normals_buffer});
+  }
+
+  ~MeshSurfaceContext()
+  {
+    for (void *buffer : m_buffers_to_free) {
+      BLI_temporary_deallocate(buffer);
+    }
+  }
+
+  MeshSurfaceContext(const MeshSurfaceContext &other) = delete;
+  MeshSurfaceContext &operator=(const MeshSurfaceContext &other) = delete;
+
   Object *object() const
   {
     return m_object;
diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index 9b5b4f7f274..0e821810802 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -87,7 +87,7 @@ class Action {
 
   virtual void execute(ActionInterface &interface) = 0;
 
-  template<typename BuildContextF>
+  template<typename ContextT, typename BuildContextF>
   void execute_from_emitter(AttributesRefGroup &new_particles,
                             EmitterInterface &emitter_interface,
                             const BuildContextF &build_context);
@@ -124,7 +124,7 @@ inline ActionInterface::ActionInterface(ParticleAllocator &particle_allocator,
 class EmptyActionContext : public ActionContext {
 };
 
-template<typename BuildContextF>
+template<typename ContextT, typename BuildContextF>
 inline void Action::execute_from_emitter(AttributesRefGroup &new_particles,
                                          EmitterInterface &emitter_interface,
                                          const BuildContextF &build_context)
@@ -132,13 +132,15 @@ inline void Action::execute_from_emitter(AttributesRefGroup &new_particles,
   AttributesInfo info;
   std::array<void *, 0> buffers;
 
+  ContextT *action_context = (ContextT *)alloca(sizeof(ContextT));
+
   uint offset = 0;
   for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
     Range<uint> range(offset, offset + range_size);
     offset += range_size;
 
-    auto action_context = build_context(range);
+    build_context(range, (void *)action_context);
 
     AttributesRef offsets(info, buffers, range_size);
     TemporaryArray<float> durations(range_size);
@@ -150,8 +152,10 @@ inline void Action::execute_from_emitter(AttributesRefGroup &new_particles,
                                      offsets,
                                      attributes.get<float>("Birth Time"),
                                      durations,
-                                     action_context);
+                                     *action_context);
     this->execute(action_interface);
+
+    action_context->~ContextT();
   }
 }
 
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index 4f8dfa7925f..be5934f10e4 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -308,14 +308,14 @@ void SurfaceEmitter::emit(EmitterInterface &interface)
     new_particles.set<float3>("Position", positions_at_birth);
     new_particles.set<float>("Birth Time", birth_times);
 
-    m_on_birth_action->execute_from_emitter(
-        new_particles, interface, [&](Range<uint> range) -> MeshSurfaceContext {
-          return MeshSurfaceContext(m_object,
-                                    transforms_at_birth.as_ref().slice(range),
-                                    local_positions.as_ref().slice(range),
-                                    local_normals.as_ref().slice(range),
-                                    world_normals.as_ref().slice(range),
-                                    triangles_to_sample.as_ref().slice(range));
+    m_on_birth_action->execute_from_emitter<MeshSurfaceContext>(
+        new_particles, interface, [&](Range<uint> range, void *dst) {
+          new (dst) MeshSurfaceContext(m_object,
+                                       transforms_at_birth.as_ref().slice(range),
+                                       local_positions.as_ref().slice(range),
+                                       local_normals.as_ref().slice(range),
+                                       world_normals.as_ref().slice(range),
+                                       triangles_to_sample.as_ref().slice(range));
         });
   }
 }
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index d6da8943830..b20c09c56ce 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -136,8 +136,6 @@ void MeshCollisionEvent::execute(EventExecuteInterface &interface)
   TemporaryArray<float3> local_positions(array_size);
   TemporaryArray<float3> local_normals(array_size);
   TemporaryArray<uint> looptri_indices(array_size);
-  TemporaryArray<float4x4> world_transforms(array_size);
-  TemporaryArray<float3> world_normals(array_size);
 
   auto last_collision_times = interface.attributes().get<float>(m_identifier);
 
@@ -146,15 +144,15 @@ void MeshCollisionEvent::execute(EventExecuteInterface &interface)
     looptri_indices[pindex] = storage.looptri_index;
     local_positions[pindex] = storage.local_position;
     local_normals[pindex] = storage.local_normal;
-    world_transforms[pindex] = m_local_to_world;
-    world_normals[pindex] =
-        m_local_to_world.transform_direction(storage.local_normal).normalized();
-
     last_collision_times[pindex] = interface.current_times()[pindex];
   }
 
-  MeshSurfaceContext surface_context(
-      m_object, world_transforms, local_positions, local_normals, world_normals, looptri_indices);
+  MeshSurfaceContext surface_context(m_object,
+                                     m_local_to_world,
+                                     interface.pindices(),
+                                     local_positions,
+                                     local_normals,
+                                     looptri_indices);
 
   m_action->execute_from_event(interface, &surface_context);
 }



More information about the Bf-blender-cvs mailing list