[Bf-blender-cvs] [06adff95b4e] functions: cleanup Spawn Particles node

Jacques Lucke noreply at git.blender.org
Wed Dec 18 13:53:28 CET 2019


Commit: 06adff95b4e7a4c4841dee6e4fd386da2ed80097
Author: Jacques Lucke
Date:   Wed Dec 18 11:18:55 2019 +0100
Branches: functions
https://developer.blender.org/rB06adff95b4e7a4c4841dee6e4fd386da2ed80097

cleanup Spawn Particles node

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

M	source/blender/simulations/bparticles/actions.cpp
M	source/blender/simulations/bparticles/actions.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particle_function.hpp

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

diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index af5297a8a06..9d2a10aa4e2 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -226,47 +226,22 @@ using FN::MFParamType;
 
 void SpawnParticlesAction::execute(ActionInterface &interface)
 {
-  FN::MFMask mask(interface.pindices());
-  uint min_array_size = mask.min_array_size();
-  FN::MFParamsBuilder params_builder{m_spawn_function, min_array_size};
-
-  for (uint param_index : m_spawn_function.param_indices()) {
-    MFParamType param_type = m_spawn_function.param_type(param_index);
-    MFDataType data_type = param_type.data_type();
-    BLI_assert(param_type.is_output());
-    switch (param_type.data_type().category()) {
-      case MFDataType::Single: {
-        const FN::CPPType &type = data_type.single__cpp_type();
-        void *buffer = MEM_malloc_arrayN(min_array_size, type.size(), __func__);
-        FN::GenericMutableArrayRef array{type, buffer, min_array_size};
-        params_builder.add_single_output(array);
-        break;
-      }
-      case MFDataType::Vector: {
-        const FN::CPPType &base_type = data_type.vector__cpp_base_type();
-        FN::GenericVectorArray *vector_array = new FN::GenericVectorArray(base_type,
-                                                                          min_array_size);
-        params_builder.add_vector_output(*vector_array);
-        break;
-      }
-    }
+  if (interface.pindices().size() == 0) {
+    return;
   }
 
-  FN::ParticleAttributesContext attributes_context = {interface.attributes()};
+  uint array_size = interface.pindices().last() + 1;
 
-  FN::MFContextBuilder context_builder;
-  context_builder.add_global_context(m_id_data_cache);
-  context_builder.add_global_context(m_id_handle_lookup);
-  context_builder.add_element_context(attributes_context, IndexRange(min_array_size));
-
-  m_spawn_function.call(mask, params_builder, context_builder);
+  auto inputs = ParticleFunctionResult::Compute(
+      m_spawn_function, interface.pindices(), interface.attributes());
 
-  LargeScopedArray<int> particle_counts(min_array_size, -1);
+  LargeScopedArray<int> particle_counts(array_size, -1);
 
-  for (uint param_index : m_spawn_function.param_indices()) {
-    MFParamType param_type = m_spawn_function.param_type(param_index);
+  const MultiFunction &fn = m_spawn_function.fn();
+  for (uint param_index : fn.param_indices()) {
+    MFParamType param_type = fn.param_type(param_index);
     if (param_type.is_vector_output()) {
-      FN::GenericVectorArray &vector_array = params_builder.computed_vector_array(param_index);
+      FN::GenericVectorArray &vector_array = inputs.computed_vector_array(param_index);
       for (uint i : interface.pindices()) {
         FN::GenericArrayRef array = vector_array[i];
         particle_counts[i] = std::max<int>(particle_counts[i], array.size());
@@ -293,8 +268,8 @@ void SpawnParticlesAction::execute(ActionInterface &interface)
   }
   attribute_arrays.add_new("Birth Time", new_birth_times.as_mutable_ref());
 
-  for (uint param_index : m_spawn_function.param_indices()) {
-    MFParamType param_type = m_spawn_function.param_type(param_index);
+  for (uint param_index : fn.param_indices()) {
+    MFParamType param_type = fn.param_type(param_index);
     MFDataType data_type = param_type.data_type();
     StringRef attribute_name = m_attribute_names[param_index];
 
@@ -303,7 +278,7 @@ void SpawnParticlesAction::execute(ActionInterface &interface)
         const FN::CPPType &type = data_type.single__cpp_type();
         void *buffer = MEM_malloc_arrayN(total_spawn_amount, type.size(), __func__);
         GenericMutableArrayRef array(type, buffer, total_spawn_amount);
-        GenericMutableArrayRef computed_array = params_builder.computed_array(param_index);
+        GenericArrayRef computed_array = inputs.computed_array(param_index);
 
         uint current = 0;
         for (uint i : interface.pindices()) {
@@ -313,17 +288,13 @@ void SpawnParticlesAction::execute(ActionInterface &interface)
         }
 
         attribute_arrays.add(attribute_name, array);
-
-        computed_array.destruct_indices(interface.pindices());
-        MEM_freeN(computed_array.buffer());
         break;
       }
       case MFDataType::Vector: {
         const FN::CPPType &base_type = data_type.vector__cpp_base_type();
         void *buffer = MEM_malloc_arrayN(total_spawn_amount, base_type.size(), __func__);
         GenericMutableArrayRef array(base_type, buffer, total_spawn_amount);
-        FN::GenericVectorArray &computed_vector_array = params_builder.computed_vector_array(
-            param_index);
+        FN::GenericVectorArray &computed_vector_array = inputs.computed_vector_array(param_index);
 
         uint current = 0;
         for (uint pindex : interface.pindices()) {
@@ -350,8 +321,6 @@ void SpawnParticlesAction::execute(ActionInterface &interface)
         }
 
         attribute_arrays.add(attribute_name, array);
-
-        delete &computed_vector_array;
         break;
       }
     }
diff --git a/source/blender/simulations/bparticles/actions.hpp b/source/blender/simulations/bparticles/actions.hpp
index aae7d73b6c5..e92f2a01a0b 100644
--- a/source/blender/simulations/bparticles/actions.hpp
+++ b/source/blender/simulations/bparticles/actions.hpp
@@ -167,25 +167,19 @@ class SetAttributeAction : public Action {
 class SpawnParticlesAction : public Action {
  private:
   ArrayRef<std::string> m_systems_to_emit;
-  const MultiFunction &m_spawn_function;
+  const ParticleFunction &m_spawn_function;
   Vector<std::string> m_attribute_names;
   Action &m_action;
-  const BKE::IDHandleLookup &m_id_handle_lookup;
-  const BKE::IDDataCache &m_id_data_cache;
 
  public:
   SpawnParticlesAction(ArrayRef<std::string> systems_to_emit,
-                       const MultiFunction &spawn_function,
+                       const ParticleFunction &spawn_function,
                        Vector<std::string> attribute_names,
-                       Action &action,
-                       const BKE::IDHandleLookup &id_handle_lookup,
-                       const BKE::IDDataCache &id_data_cache)
+                       Action &action)
       : m_systems_to_emit(systems_to_emit),
         m_spawn_function(spawn_function),
         m_attribute_names(std::move(attribute_names)),
-        m_action(action),
-        m_id_handle_lookup(id_handle_lookup),
-        m_id_data_cache(id_data_cache)
+        m_action(action)
   {
   }
 
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index cbb15e3b103..c1f5f27f76b 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -562,8 +562,8 @@ static void ACTION_spawn(XSocketActionBuilder &builder)
   ArrayRef<const XInputSocket *> data_inputs = xnode.inputs().take_front(
       first_execute_socket.index());
   ArrayRef<uint> input_indices = IndexRange(data_inputs.size()).as_array_ref();
-  const MultiFunction *spawn_function = builder.function_for_inputs(input_indices);
-  if (spawn_function == nullptr) {
+  const ParticleFunction *inputs_fn = builder.particle_function_for_inputs(input_indices);
+  if (inputs_fn == nullptr) {
     return;
   }
 
@@ -595,12 +595,8 @@ static void ACTION_spawn(XSocketActionBuilder &builder)
 
   Action &action = builder.build_input_action_list("Execute on Birth", system_names);
 
-  builder.set_constructed<SpawnParticlesAction>(system_names,
-                                                *spawn_function,
-                                                std::move(attribute_names),
-                                                action,
-                                                builder.id_handle_lookup(),
-                                                builder.id_data_cache());
+  builder.set_constructed<SpawnParticlesAction>(
+      system_names, *inputs_fn, std::move(attribute_names), action);
 }
 
 static void ACTION_condition(XSocketActionBuilder &builder)
diff --git a/source/blender/simulations/bparticles/particle_function.hpp b/source/blender/simulations/bparticles/particle_function.hpp
index ad422f0fe4e..fed618ca3c5 100644
--- a/source/blender/simulations/bparticles/particle_function.hpp
+++ b/source/blender/simulations/bparticles/particle_function.hpp
@@ -37,6 +37,11 @@ class ParticleFunction {
                    Vector<std::string> computed_names,
                    const BKE::IDDataCache &id_data_cache,
                    const BKE::IDHandleLookup &id_handle_lookup);
+
+  const MultiFunction &fn() const
+  {
+    return m_fn;
+  }
 };
 
 class ParticleFunctionResult : BLI::NonCopyable {
@@ -93,6 +98,18 @@ class ParticleFunctionResult : BLI::NonCopyable {
     GenericVectorArray &vector_array = *m_vector_arrays[corrected_index];
     return vector_array[pindex].as_typed_ref<T>();
   }
+
+  GenericVectorArray &computed_vector_array(uint param_index)
+  {
+    uint corrected_index = m_index_mapping[param_index];
+    return *m_vector_arrays[corrected_index];
+  }
+
+  GenericArrayRef computed_array(uint param_index)
+  {
+    uint corrected_index = m_index_mapping[param_index];
+    return m_arrays[corrected_index];
+  }
 };
 
 }  // namespace BParticles



More information about the Bf-blender-cvs mailing list