[Bf-blender-cvs] [799fb759a7f] functions: improve particle set

Jacques Lucke noreply at git.blender.org
Thu Jan 2 16:37:45 CET 2020


Commit: 799fb759a7fb5efc1993b493dc2a49293c4f7f82
Author: Jacques Lucke
Date:   Tue Dec 31 15:35:08 2019 +0100
Branches: functions
https://developer.blender.org/rB799fb759a7fb5efc1993b493dc2a49293c4f7f82

improve particle set

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

M	source/blender/blenlib/BLI_array_cxx.h
M	source/blender/simulations/CMakeLists.txt
M	source/blender/simulations/bparticles/particle_set.cpp
M	source/blender/simulations/bparticles/particle_set.hpp

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

diff --git a/source/blender/blenlib/BLI_array_cxx.h b/source/blender/blenlib/BLI_array_cxx.h
index 6cab335ac1d..7292c94616b 100644
--- a/source/blender/blenlib/BLI_array_cxx.h
+++ b/source/blender/blenlib/BLI_array_cxx.h
@@ -143,6 +143,11 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ar
     return *this;
   }
 
+  MutableArrayRef<T> as_mutable_ref() const
+  {
+    return *this;
+  }
+
   T &operator[](uint index)
   {
     BLI_assert(index < m_size);
diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index f4ade69f4fa..331b0c6f85b 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -51,6 +51,8 @@ set(SRC
   bparticles/offset_handlers.cpp
   bparticles/particle_function.hpp
   bparticles/particle_function.cpp
+  bparticles/particle_set.hpp
+  bparticles/particle_set.cpp
   bparticles/force_interface.hpp
   bparticles/force_interface.cpp
 )
diff --git a/source/blender/simulations/bparticles/particle_set.cpp b/source/blender/simulations/bparticles/particle_set.cpp
index f2f60994581..ef647cb9959 100644
--- a/source/blender/simulations/bparticles/particle_set.cpp
+++ b/source/blender/simulations/bparticles/particle_set.cpp
@@ -2,23 +2,31 @@
 
 namespace BParticles {
 
-ParticleSet::ParticleSet(const AttributesInfoBuilder &attributes_info_builder, uint size)
-    : m_attributes_info(BLI::make_unique<AttributesInfo>(attributes_info_builder)),
+ParticleSet::ParticleSet(const AttributesInfo *attributes_info, uint size)
+    : m_attributes_info(attributes_info),
+      m_attribute_buffers(attributes_info->size()),
       m_size(size),
       m_capacity(size)
 {
+  for (uint i : m_attributes_info->indices()) {
+    const CPPType &type = m_attributes_info->type_of(i);
+    const void *default_value = attributes_info->default_of(i);
+
+    void *buffer = MEM_mallocN_aligned(m_capacity * type.size(), type.alignment(), __func__);
+    type.fill_uninitialized(default_value, buffer, m_size);
+    m_attribute_buffers[i] = buffer;
+  }
 }
 
-void ParticleSet::update_attributes(const AttributesInfoBuilder &new_attributes_info_builder)
+void ParticleSet::update_attributes(const AttributesInfo *new_attributes_info)
 {
-  auto new_attributes_info = BLI::make_unique<AttributesInfo>(new_attributes_info_builder);
   FN::AttributesInfoDiff diff{*m_attributes_info, *new_attributes_info};
 
-  Vector<void *> new_buffers(diff.new_buffer_amount());
+  Array<void *> new_buffers(diff.new_buffer_amount());
   diff.update(m_capacity, m_size, m_attribute_buffers, new_buffers);
 
   m_attribute_buffers = std::move(new_buffers);
-  m_attributes_info = std::move(new_attributes_info);
+  m_attributes_info = new_attributes_info;
 }
 
 void ParticleSet::destruct_and_reorder(IndexMask indices_to_destruct)
@@ -29,7 +37,35 @@ void ParticleSet::destruct_and_reorder(IndexMask indices_to_destruct)
 
 void ParticleSet::add_particles(ParticleSet &particles)
 {
-  /* TODO */
+  BLI_assert(m_attributes_info == particles.m_attributes_info);
+
+  uint required_size = m_size + particles.size();
+  if (required_size > m_capacity) {
+    this->realloc_particle_attributes(required_size);
+  }
+
+  MutableAttributesRef dst{
+      *m_attributes_info, m_attribute_buffers, IndexRange(m_size, particles.size())};
+  MutableAttributesRef::RelocateUninitialized(particles.attributes(), dst);
+}
+
+void ParticleSet::realloc_particle_attributes(uint min_size)
+{
+  if (min_size <= m_capacity) {
+    return;
+  }
+
+  uint new_capacity = power_of_2_max_u(min_size);
+
+  for (uint index : m_attributes_info->indices()) {
+    const CPPType &type = m_attributes_info->type_of(index);
+    void *old_buffer = m_attribute_buffers[index];
+    void *new_buffer = MEM_mallocN_aligned(type.size() * new_capacity, type.alignment(), __func__);
+    type.relocate_to_uninitialized_n(old_buffer, new_buffer, m_size);
+    MEM_freeN(old_buffer);
+
+    m_attribute_buffers[index] = new_buffer;
+  }
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particle_set.hpp b/source/blender/simulations/bparticles/particle_set.hpp
index 4e1e8e675ac..0e3acba5f31 100644
--- a/source/blender/simulations/bparticles/particle_set.hpp
+++ b/source/blender/simulations/bparticles/particle_set.hpp
@@ -4,21 +4,25 @@
 
 namespace BParticles {
 
+using BLI::Array;
 using BLI::IndexMask;
+using BLI::IndexRange;
 using BLI::Vector;
 using FN::AttributesInfo;
 using FN::AttributesInfoBuilder;
 using FN::AttributesRef;
+using FN::CPPType;
+using FN::MutableAttributesRef;
 
-class ParticleSet {
+class ParticleSet : BLI::NonCopyable, BLI::NonMovable {
  private:
-  std::unique_ptr<AttributesInfo> m_attributes_info;
-  Vector<void *> m_attribute_buffers;
+  const AttributesInfo *m_attributes_info;
+  Array<void *> m_attribute_buffers;
   uint m_size;
   uint m_capacity;
 
  public:
-  ParticleSet(const AttributesInfoBuilder &attributes_info_builder, uint size);
+  ParticleSet(const AttributesInfo *attributes_info, uint size);
   ~ParticleSet();
 
   const AttributesInfo &attributes_info() const
@@ -26,9 +30,9 @@ class ParticleSet {
     return *m_attributes_info;
   }
 
-  AttributesRef attributes()
+  MutableAttributesRef attributes()
   {
-    return AttributesRef(*m_attributes_info, m_attribute_buffers, m_size);
+    return MutableAttributesRef(*m_attributes_info, m_attribute_buffers, m_size);
   }
 
   uint size() const
@@ -38,13 +42,16 @@ class ParticleSet {
 
   void add_particles(ParticleSet &particles);
 
-  void update_attributes(const AttributesInfoBuilder &new_attributes_info_builder);
+  void update_attributes(const AttributesInfo *new_attributes_info);
   void destruct_and_reorder(IndexMask indices_to_destruct);
 
   friend bool operator==(const ParticleSet &a, const ParticleSet &b)
   {
     return &a == &b;
   }
+
+ private:
+  void realloc_particle_attributes(uint min_size);
 };
 
 }  // namespace BParticles



More information about the Bf-blender-cvs mailing list