[Bf-blender-cvs] [1f79a069d32] functions: move particle function to separate file

Jacques Lucke noreply at git.blender.org
Wed Jul 24 19:11:50 CEST 2019


Commit: 1f79a069d32d268b8bfef49d1b6cf80a0112f327
Author: Jacques Lucke
Date:   Wed Jul 24 14:44:36 2019 +0200
Branches: functions
https://developer.blender.org/rB1f79a069d32d268b8bfef49d1b6cf80a0112f327

move particle function to separate file

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

M	source/blender/blenlib/BLI_array_allocator.hpp
M	source/blender/simulations/CMakeLists.txt
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/actions.hpp
A	source/blender/simulations/bparticles/particle_function.cpp
A	source/blender/simulations/bparticles/particle_function.hpp

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

diff --git a/source/blender/blenlib/BLI_array_allocator.hpp b/source/blender/blenlib/BLI_array_allocator.hpp
index bc36d0f3ad7..a7878dd8ce0 100644
--- a/source/blender/blenlib/BLI_array_allocator.hpp
+++ b/source/blender/blenlib/BLI_array_allocator.hpp
@@ -198,6 +198,11 @@ class ArrayAllocator {
     {
       return ((T *)m_ptr)[index];
     }
+
+    ArrayRef<T> as_array_ref()
+    {
+      return ArrayRef<T>(m_ptr, m_size);
+    }
   };
 
  private:
diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index 44a431cce2b..6d4c5721526 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -52,6 +52,8 @@ set(SRC
   bparticles/particle_allocator.cpp
   bparticles/offset_handlers.hpp
   bparticles/offset_handlers.cpp
+  bparticles/particle_function.hpp
+  bparticles/particle_function.cpp
 )
 
 set(LIB
diff --git a/source/blender/simulations/bparticles/action_interface.cpp b/source/blender/simulations/bparticles/action_interface.cpp
index 5f9c5d1ce44..1e172d87aa5 100644
--- a/source/blender/simulations/bparticles/action_interface.cpp
+++ b/source/blender/simulations/bparticles/action_interface.cpp
@@ -6,37 +6,4 @@ Action::~Action()
 {
 }
 
-ParticleFunctionCaller ParticleFunction::get_caller(AttributeArrays attributes,
-                                                    ActionContext &action_context)
-{
-  ParticleFunctionCaller caller;
-  caller.m_body = m_tuple_call;
-
-  for (uint i = 0; i < m_function->input_amount(); i++) {
-    StringRef input_name = m_function->input_name(i);
-    void *ptr = nullptr;
-    uint stride = 0;
-    if (input_name.startswith("Event")) {
-      StringRef event_attribute_name = input_name.drop_prefix("Event: ");
-      ptr = action_context.get_context_array(event_attribute_name);
-      stride = sizeof(float3); /* TODO make not hardcoded */
-    }
-    else if (input_name.startswith("Attribute")) {
-      StringRef attribute_name = input_name.drop_prefix("Attribute: ");
-      uint index = attributes.attribute_index(attribute_name);
-      stride = attributes.attribute_stride(index);
-      ptr = attributes.get_ptr(index);
-    }
-    else {
-      BLI_assert(false);
-    }
-
-    BLI_assert(ptr);
-    caller.m_attribute_buffers.append(ptr);
-    caller.m_strides.append(stride);
-  }
-
-  return caller;
-}
-
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index 6e341c75691..d064b4eb3e0 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -16,51 +16,6 @@ class ActionContext {
   virtual void *get_context_array(StringRef name) = 0;
 };
 
-class ParticleFunction;
-class Action;
-
-class ParticleFunctionCaller {
- private:
-  TupleCallBody *m_body;
-  Vector<void *> m_attribute_buffers;
-  Vector<uint> m_strides;
-
-  friend ParticleFunction;
-
- public:
-  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &ctx, uint pindex)
-  {
-    BLI_assert(fn_in.size() == m_attribute_buffers.size());
-
-    for (uint i = 0; i < fn_in.size(); i++) {
-      void *ptr = POINTER_OFFSET(m_attribute_buffers[i], pindex * m_strides[i]);
-      fn_in.copy_in__dynamic(i, ptr);
-    }
-
-    m_body->call(fn_in, fn_out, ctx);
-  }
-
-  TupleCallBody *body() const
-  {
-    return m_body;
-  }
-};
-
-class ParticleFunction {
- private:
-  SharedFunction m_function;
-  TupleCallBody *m_tuple_call;
-
- public:
-  ParticleFunction(SharedFunction &fn) : m_function(fn)
-  {
-    m_tuple_call = fn->body<TupleCallBody>();
-    BLI_assert(m_tuple_call);
-  }
-
-  ParticleFunctionCaller get_caller(AttributeArrays attributes, ActionContext &action_context);
-};
-
 class ActionInterface {
  private:
   ParticleAllocator &m_particle_allocator;
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index d946a8671f2..0d0a3d63d3e 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -15,16 +15,12 @@ void ChangeDirectionAction::execute(ActionInterface &interface)
   auto position_offsets = interface.attribute_offsets().try_get_float3("Position");
   auto velocity_offsets = interface.attribute_offsets().try_get_float3("Velocity");
 
-  auto caller = m_compute_inputs.get_caller(particles.attributes(), interface.context());
-
-  FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
-
-  FN::ExecutionStack stack;
-  FN::ExecutionContext execution_context(stack);
+  auto caller = m_compute_inputs.get_caller(particles.attributes());
+  auto new_directions = caller.add_output_buffer<float3>(interface.array_allocator());
+  caller.call(particles.pindices());
 
   for (uint pindex : particles.pindices()) {
-    caller.call(fn_in, fn_out, execution_context, pindex);
-    float3 direction = fn_out.get<float3>(0);
+    float3 direction = new_directions[pindex];
 
     velocities[pindex] = direction;
 
@@ -66,16 +62,14 @@ void ExplodeAction::execute(ActionInterface &interface)
   Vector<float3> new_velocities;
   Vector<float> new_birth_times;
 
-  auto caller = m_compute_inputs.get_caller(particles.attributes(), interface.context());
-  FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
-
-  FN::ExecutionStack stack;
-  FN::ExecutionContext execution_context(stack);
+  auto caller = m_compute_inputs.get_caller(particles.attributes());
+  auto parts_amounts = caller.add_output_buffer<int>(interface.array_allocator());
+  auto speeds = caller.add_output_buffer<float>(interface.array_allocator());
+  caller.call(particles.pindices());
 
   for (uint pindex : particles.pindices()) {
-    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);
+    uint parts_amount = std::max(0, parts_amounts[pindex]);
+    float speed = speeds[pindex];
 
     new_positions.append_n_times(positions[pindex], parts_amount);
     new_birth_times.append_n_times(interface.current_times()[pindex], parts_amount);
@@ -98,8 +92,10 @@ void ExplodeAction::execute(ActionInterface &interface)
 void ConditionAction::execute(ActionInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  ArrayAllocator::Array<bool> conditions(interface.array_allocator());
-  this->compute_conditions(interface, conditions);
+
+  auto caller = m_compute_inputs.get_caller(particles.attributes());
+  auto conditions = caller.add_output_buffer<bool>(interface.array_allocator());
+  caller.call(particles.pindices());
 
   Vector<uint> true_pindices, false_pindices;
   for (uint pindex : particles.pindices()) {
@@ -115,20 +111,4 @@ void ConditionAction::execute(ActionInterface &interface)
   m_false_action->execute_for_subset(false_pindices, interface);
 }
 
-void ConditionAction::compute_conditions(ActionInterface &interface, ArrayRef<bool> r_conditions)
-{
-  ParticleSet particles = interface.particles();
-
-  auto caller = m_compute_inputs.get_caller(particles.attributes(), interface.context());
-  FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
-
-  FN::ExecutionStack stack;
-  FN::ExecutionContext execution_context(stack);
-  for (uint pindex : particles.pindices()) {
-    caller.call(fn_in, fn_out, execution_context, pindex);
-    bool condition = fn_out.get<bool>(0);
-    r_conditions[pindex] = condition;
-  }
-}
-
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/actions.hpp b/source/blender/simulations/bparticles/actions.hpp
index b19c004c097..a06fad36193 100644
--- a/source/blender/simulations/bparticles/actions.hpp
+++ b/source/blender/simulations/bparticles/actions.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "action_interface.hpp"
+#include "particle_function.hpp"
 
 namespace BParticles {
 
@@ -61,9 +62,6 @@ class ConditionAction : public Action {
   }
 
   void execute(ActionInterface &interface) override;
-
- private:
-  void compute_conditions(ActionInterface &interface, ArrayRef<bool> r_conditions);
 };
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particle_function.cpp b/source/blender/simulations/bparticles/particle_function.cpp
new file mode 100644
index 00000000000..f67fa8004de
--- /dev/null
+++ b/source/blender/simulations/bparticles/particle_function.cpp
@@ -0,0 +1,33 @@
+#include "particle_function.hpp"
+
+namespace BParticles {
+
+ParticleFunctionCaller ParticleFunction::get_caller(AttributeArrays attributes)
+{
+  ParticleFunctionCaller caller;
+  caller.m_body = m_body;
+  caller.m_min_buffer_length = attributes.size();
+
+  for (uint i = 0; i < m_fn->input_amount(); i++) {
+    StringRef input_name = m_fn->input_name(i);
+    void *input_buffer = nullptr;
+    uint input_stride = 0;
+    if (input_name.startswith("Attribute")) {
+      StringRef attribute_name = input_name.drop_prefix("Attribute: ");
+      uint attribute_index = attributes.attribute_index(attribute_name);
+      input_buffer = attributes.get_ptr(attribute_index);
+      input_stride = attributes.attribute_stride(attribute_index);
+    }
+    else {
+      BLI_assert(false);
+    }
+    BLI_assert(input_buffer != nullptr);
+
+    caller.m_input_buffers.append(input_buffer);
+    caller.m_input_strides.append(input_stride);
+  }
+
+  return caller;
+}
+
+}  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particle_function.hpp b/source/blender/simulations/bparticles/particle_function.hpp
new file mode 100644
index 00000000000..75b8adfcef9
--- /dev/null
+++ b/source/blender/simulations/bparticles/particle_function.hpp
@@ -0,0 +1,93 @@
+#pragma once
+
+#include "FN_tuple_call.hpp"
+#include "attributes.hpp"
+
+namespace BParticles {
+
+using BLI::ArrayRef;
+using BLI::Vector;
+using FN::CPPTypeInfo;
+using FN::ExecutionContext;
+using FN::ExecutionStack;
+using FN::SharedFunction;
+using FN::SharedType;
+using FN::TupleCallBody;
+
+class ParticleFunction;
+
+class ParticleFunctionCaller {
+ private:
+  TupleCallBody *m_body;
+  uint m_min_buffer_length;
+  Vector<void *> m_input_buffers;


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list