[Bf-blender-cvs] [b2188f052b5] functions: initial particle info node

Jacques Lucke noreply at git.blender.org
Sat Jul 6 17:30:26 CEST 2019


Commit: b2188f052b53a8904261e3ee50d0d8f7aee01e9b
Author: Jacques Lucke
Date:   Sat Jul 6 15:36:02 2019 +0200
Branches: functions
https://developer.blender.org/rBb2188f052b53a8904261e3ee50d0d8f7aee01e9b

initial particle info node

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

A	release/scripts/startup/nodes/bparticle_nodes/particle_info.py
M	source/blender/functions/backends/tuple/tuple.hpp
M	source/blender/simulations/bparticles/actions.cpp
M	source/blender/simulations/bparticles/actions.hpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/c_wrapper.cpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/particle_info.py b/release/scripts/startup/nodes/bparticle_nodes/particle_info.py
new file mode 100644
index 00000000000..e2930553448
--- /dev/null
+++ b/release/scripts/startup/nodes/bparticle_nodes/particle_info.py
@@ -0,0 +1,13 @@
+import bpy
+from bpy.props import *
+from .. base import BParticlesNode
+from .. socket_builder import SocketBuilder
+
+class ParticleInfoNode(bpy.types.Node, BParticlesNode):
+    bl_idname = "bp_ParticleInfoNode"
+    bl_label = "Particle Info"
+
+    def declaration(self, builder : SocketBuilder):
+        builder.fixed_output("position", "Position", "Vector")
+        builder.fixed_output("velocity", "Velocity", "Vector")
+        builder.fixed_output("birth_time", "Birth Time", "Float")
diff --git a/source/blender/functions/backends/tuple/tuple.hpp b/source/blender/functions/backends/tuple/tuple.hpp
index da37a14f35a..9dcb70a00b8 100644
--- a/source/blender/functions/backends/tuple/tuple.hpp
+++ b/source/blender/functions/backends/tuple/tuple.hpp
@@ -517,6 +517,14 @@ class Tuple {
     }
   }
 
+  /**
+   * Return the number of elements in the tuple.
+   */
+  uint size() const
+  {
+    return m_meta->element_amount();
+  }
+
   void print_initialized(std::string name = "");
 
  private:
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index 00328f0699e..3dfdee3f1d6 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -16,15 +16,13 @@ class NoneAction : public Action {
 
 class ChangeDirectionAction : public Action {
  private:
-  SharedFunction m_compute_direction_fn;
-  TupleCallBody *m_compute_direction_body;
+  ParticleFunction m_compute_inputs;
   Action *m_post_action;
 
  public:
-  ChangeDirectionAction(SharedFunction &compute_direction_fn, Action *post_action)
-      : m_compute_direction_fn(compute_direction_fn), m_post_action(post_action)
+  ChangeDirectionAction(ParticleFunction &compute_inputs, Action *post_action)
+      : m_compute_inputs(compute_inputs), m_post_action(post_action)
   {
-    m_compute_direction_body = m_compute_direction_fn->body<TupleCallBody>();
   }
 
   ~ChangeDirectionAction()
@@ -39,15 +37,19 @@ class ChangeDirectionAction : public Action {
     auto position_offsets = interface.attribute_offsets().get_float3("Position");
     auto velocity_offsets = interface.attribute_offsets().get_float3("Velocity");
 
-    FN_TUPLE_CALL_ALLOC_TUPLES(m_compute_direction_body, fn_in, fn_out);
+    auto caller = m_compute_inputs.get_caller(particles.attributes());
+
+    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);
-      m_compute_direction_body->call(fn_in, fn_out, execution_context);
+
+      caller.call(fn_in, fn_out, execution_context, pindex);
       float3 direction = fn_out.get<float3>(0);
+
       velocities[pindex] = direction;
       position_offsets[pindex] = direction * interface.remaining_time_in_step(i);
       velocity_offsets[pindex] = float3(0);
@@ -123,19 +125,17 @@ static float3 random_direction()
 class ExplodeAction : public Action {
  private:
   std::string m_new_particle_name;
-  SharedFunction m_compute_amount_fn;
-  TupleCallBody *m_compute_amount_body;
+  ParticleFunction m_compute_inputs;
   std::unique_ptr<Action> m_post_action;
 
  public:
   ExplodeAction(StringRef new_particle_name,
-                SharedFunction &compute_amount_fn,
+                ParticleFunction &compute_inputs,
                 std::unique_ptr<Action> post_action)
       : m_new_particle_name(new_particle_name.to_std_string()),
-        m_compute_amount_fn(compute_amount_fn),
+        m_compute_inputs(compute_inputs),
         m_post_action(std::move(post_action))
   {
-    m_compute_amount_body = m_compute_amount_fn->body<TupleCallBody>();
   }
 
   void execute(EventExecuteInterface &interface) override
@@ -148,17 +148,19 @@ class ExplodeAction : public Action {
     SmallVector<float3> new_velocities;
     SmallVector<uint> original_indices;
 
-    FN_TUPLE_CALL_ALLOC_TUPLES(m_compute_amount_body, fn_in, fn_out);
+    auto caller = m_compute_inputs.get_caller(particles.attributes());
+    FN_TUPLE_CALL_ALLOC_TUPLES(caller.body(), fn_in, fn_out);
 
     FN::ExecutionStack stack;
     FN::ExecutionContext execution_context(stack);
-    m_compute_amount_body->call(fn_in, fn_out, execution_context);
 
-    uint parts_amount = std::max(0, fn_out.get<int>(0));
-    float speed = fn_out.get<float>(1);
     for (uint i : particles.range()) {
       uint pindex = particles.get_particle_index(i);
 
+      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);
       original_indices.append_n_times(i, parts_amount);
 
@@ -180,9 +182,9 @@ Action *ACTION_none()
   return new NoneAction();
 }
 
-Action *ACTION_change_direction(SharedFunction &compute_direction_fn, Action *post_action)
+Action *ACTION_change_direction(ParticleFunction &compute_inputs, Action *post_action)
 {
-  return new ChangeDirectionAction(compute_direction_fn, post_action);
+  return new ChangeDirectionAction(compute_inputs, post_action);
 }
 
 Action *ACTION_kill()
@@ -201,11 +203,11 @@ Action *ACTION_spawn()
 }
 
 Action *ACTION_explode(StringRef new_particle_name,
-                       SharedFunction &compute_amount_fn,
+                       ParticleFunction &compute_inputs,
                        Action *post_action)
 {
   return new ExplodeAction(
-      new_particle_name, compute_amount_fn, std::unique_ptr<Action>(post_action));
+      new_particle_name, compute_inputs, std::unique_ptr<Action>(post_action));
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/actions.hpp b/source/blender/simulations/bparticles/actions.hpp
index 2a6725368ee..62b2ea5a9a0 100644
--- a/source/blender/simulations/bparticles/actions.hpp
+++ b/source/blender/simulations/bparticles/actions.hpp
@@ -6,7 +6,9 @@
 
 namespace BParticles {
 
+using FN::ExecutionContext;
 using FN::SharedFunction;
+using FN::Tuple;
 using FN::TupleCallBody;
 
 class Action {
@@ -16,13 +18,73 @@ class Action {
   virtual void execute(EventExecuteInterface &interface) = 0;
 };
 
+class ParticleFunction;
+
+class ParticleFunctionCaller {
+ private:
+  TupleCallBody *m_body;
+  SmallVector<void *> m_attribute_buffers;
+  SmallVector<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)
+  {
+    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);
+      uint index = attributes.attribute_index(input_name);
+      uint stride = attributes.attribute_stride(index);
+      void *ptr = attributes.get_ptr(index);
+
+      caller.m_attribute_buffers.append(ptr);
+      caller.m_strides.append(stride);
+    }
+
+    return caller;
+  }
+};
+
 Action *ACTION_none();
-Action *ACTION_change_direction(SharedFunction &compute_direction_fn, Action *post_action);
+Action *ACTION_change_direction(ParticleFunction &compute_inputs, Action *post_action);
 Action *ACTION_kill();
 Action *ACTION_move(float3 offset);
 Action *ACTION_spawn();
 Action *ACTION_explode(StringRef new_particle_name,
-                       SharedFunction &compute_amount_fn,
+                       ParticleFunction &compute_inputs,
                        Action *post_action);
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index 06c47f81fcd..803818430a7 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -297,6 +297,11 @@ class AttributeArrays {
    */
   uint attribute_index(StringRef name);
 
+  /**
+   * Get the size of an element in one attribute.
+   */
+  uint attribute_stride(uint index);
+
   /**
    * Get the raw pointer to the buffer that contains attribute values.
    */
@@ -386,6 +391,11 @@ inline uint AttributeArrays::attribute_index(StringRef name)
   return this->info().attribute_index(name);
 }
 
+inline uint AttributeArrays::attribute_stride(uint index)
+{
+  return size_of_attribute_type(this->info().type_of(index));
+}
+
 inline void *AttributeArrays::get_ptr(uint index) const
 {
   void *ptr = m_core.get_ptr(index);
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 782af8ed106..9dcbdb26f38 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -255,22 +255,65 @@ static bool is_particle_type_node(bNode *bnode)
   return STREQ(bnode->idname, "bp_ParticleTypeNode");
 }
 
+static bool is_particle_info_node(bNode *bnode)
+{
+  return STREQ(bnode->idname, "bp_ParticleInfoNode");
+}
+
 static ArrayRef<bNode *> get_particle_type_nodes(IndexedNodeTree &indexed_tree)
 {
   return indexed_tree.nodes_with_idname("bp_ParticleTypeNode");
 }
 
-static SharedFunction create_function(IndexedNodeTree &UNUSED(indexed_tree),

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list