[Bf-blender-cvs] [f693a5b66d1] functions: automatically initialize attributes the emitter does not care about

Jacques Lucke noreply at git.blender.org
Sat Jun 8 14:21:59 CEST 2019


Commit: f693a5b66d106ecbae39b8865be8fb645ac2406a
Author: Jacques Lucke
Date:   Sat Jun 8 13:30:33 2019 +0200
Branches: functions
https://developer.blender.org/rBf693a5b66d106ecbae39b8865be8fb645ac2406a

automatically initialize attributes the emitter does not care about

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

M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/core.hpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/playground_solver.cpp

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

diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index f0d28cda988..28bcb600b74 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -23,6 +23,7 @@ using BParticles::StateBase;
 using BParticles::WrappedState;
 
 using BLI::ArrayRef;
+using BLI::SmallVector;
 using BLI::StringRef;
 using BLI::Vec3;
 
@@ -49,13 +50,25 @@ class TestForce : public BParticles::Force {
 };
 
 class TestEmitter : public BParticles::Emitter {
+ private:
+  SmallVector<std::string> m_used_float_attributes;
+  SmallVector<std::string> m_used_vec3_attributes;
+
  public:
-  void attributes(std::function<void(AttributeType type, StringRef attribute_name)>
-                      register_attribute) override
+  TestEmitter()
+  {
+    m_used_float_attributes = {};
+    m_used_vec3_attributes = {"Position", "Velocity"};
+  }
+
+  ArrayRef<std::string> used_float_attributes() override
+  {
+    return m_used_float_attributes;
+  }
+
+  ArrayRef<std::string> used_vec3_attributes() override
   {
-    register_attribute(AttributeType::Vector3, "Position");
-    register_attribute(AttributeType::Vector3, "Velocity");
-    register_attribute(AttributeType::Float, "Age");
+    return m_used_vec3_attributes;
   }
 
   void emit(std::function<EmitterDestination &()> request_destination) override
@@ -64,8 +77,7 @@ class TestEmitter : public BParticles::Emitter {
     BLI_assert(dst.size() > 0);
 
     dst.vec3_buffer("Position")[0] = {(float)(rand() % 100) / 30.0f, 0, 1};
-    dst.vec3_buffer("Velocity")[0] = {0, 0.1, 0};
-    dst.float_buffer("Age")[0] = 0;
+    dst.vec3_buffer("Velocity")[0] = {0, 0.1, 0.1};
     dst.initialized_n(1);
   }
 };
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index ecd9c6f483c..388877bf7c3 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -79,8 +79,9 @@ class EmitterDestination {
 class Emitter {
  public:
   virtual ~Emitter();
-  virtual void attributes(
-      std::function<void(AttributeType type, StringRef name)> register_attribute) = 0;
+
+  virtual ArrayRef<std::string> used_float_attributes() = 0;
+  virtual ArrayRef<std::string> used_vec3_attributes() = 0;
   virtual void emit(std::function<EmitterDestination &()> request_destination) = 0;
 };
 
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 11d2a014412..4e0d4dbb4f0 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -36,6 +36,8 @@ class ParticlesContainer {
   ~ParticlesContainer();
 
   uint block_size() const;
+  SmallSetVector<std::string> &float_attribute_names();
+  SmallSetVector<std::string> &vec3_attribute_names();
   uint float_attribute_amount() const;
   uint vec3_attribute_amount() const;
   uint float_buffer_index(StringRef name) const;
@@ -107,6 +109,16 @@ inline uint ParticlesContainer::block_size() const
   return m_block_size;
 }
 
+inline SmallSetVector<std::string> &ParticlesContainer::float_attribute_names()
+{
+  return m_float_attribute_names;
+}
+
+inline SmallSetVector<std::string> &ParticlesContainer::vec3_attribute_names()
+{
+  return m_vec3_attribute_names;
+}
+
 inline uint ParticlesContainer::float_attribute_amount() const
 {
   return m_float_attribute_names.size();
diff --git a/source/blender/simulations/bparticles/playground_solver.cpp b/source/blender/simulations/bparticles/playground_solver.cpp
index 03a28aab011..ab84ce8a804 100644
--- a/source/blender/simulations/bparticles/playground_solver.cpp
+++ b/source/blender/simulations/bparticles/playground_solver.cpp
@@ -25,23 +25,16 @@ class SimpleSolver : public Solver {
 
   StateBase *init() override
   {
-    SmallSetVector<std::string> float_attributes;
+    SmallSetVector<std::string> float_attributes = {"Age"};
     SmallSetVector<std::string> vec3_attributes;
 
     for (Emitter *emitter : m_description.emitters()) {
-      emitter->attributes(
-          [&float_attributes, &vec3_attributes](AttributeType type, StringRef name) -> void {
-            switch (type) {
-              case AttributeType::Float:
-                float_attributes.add(name.to_std_string());
-                break;
-              case AttributeType::Vector3:
-                vec3_attributes.add(name.to_std_string());
-                break;
-              default:
-                BLI_assert(false);
-            }
-          });
+      for (auto &name : emitter->used_float_attributes()) {
+        float_attributes.add(name);
+      }
+      for (auto &name : emitter->used_vec3_attributes()) {
+        vec3_attributes.add(name);
+      }
     }
 
     MyState *state = new MyState();
@@ -105,6 +98,13 @@ class SimpleSolver : public Solver {
   }
 
   void emit_new_particles(ParticlesContainer &particles)
+  {
+    for (Emitter *emitter : m_description.emitters()) {
+      this->emit_from_emitter(particles, *emitter);
+    }
+  }
+
+  void emit_from_emitter(ParticlesContainer &particles, Emitter &emitter)
   {
     SmallVector<ParticlesBlockSlice> block_slices;
     SmallVector<EmitterDestination> destinations;
@@ -116,14 +116,24 @@ class SimpleSolver : public Solver {
       return destinations.last();
     };
 
-    for (Emitter *emitter : m_description.emitters()) {
-      emitter->emit(request_destination);
-    }
+    emitter.emit(request_destination);
 
     for (uint i = 0; i < destinations.size(); i++) {
       ParticlesBlockSlice &slice = block_slices[i];
       EmitterDestination &dst = destinations[i];
       ParticlesBlock *block = slice.block();
+
+      for (auto &name : particles.float_attribute_names()) {
+        if (!emitter.used_float_attributes().contains(name)) {
+          slice.float_buffer(name).fill(0);
+        }
+      }
+      for (auto &name : particles.vec3_attribute_names()) {
+        if (!emitter.used_vec3_attributes().contains(name)) {
+          slice.vec3_buffer(name).fill(Vec3{0, 0, 0});
+        }
+      }
+
       block->active_amount() += dst.emitted_amount();
     }
   }



More information about the Bf-blender-cvs mailing list