[Bf-blender-cvs] [9351db7146e] functions: work-in-progress functions to compress a set of particle blocks

Jacques Lucke noreply at git.blender.org
Fri Jun 7 17:53:19 CEST 2019


Commit: 9351db7146eec6a886445508dc6fed5ad2d82490
Author: Jacques Lucke
Date:   Fri Jun 7 17:50:33 2019 +0200
Branches: functions
https://developer.blender.org/rB9351db7146eec6a886445508dc6fed5ad2d82490

work-in-progress functions to compress a set of particle blocks

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

M	source/blender/simulations/bparticles/particles_container.cpp
M	source/blender/simulations/bparticles/particles_container.hpp

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

diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index effc3198a14..6f4ce5c37fa 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -67,4 +67,61 @@ void ParticlesContainer::release_block(ParticlesBlock *block)
   delete block;
 }
 
+template<typename T>
+static void move_buffers(ArrayRef<T *> from_buffers,
+                         ArrayRef<T *> to_buffers,
+                         uint src_start,
+                         uint dst_start,
+                         uint move_amount)
+{
+  BLI_assert(from_buffers.size() == to_buffers.size());
+  for (uint i = 0; i < from_buffers.size(); i++) {
+    memcpy(to_buffers[i] + dst_start, from_buffers[i] + src_start, move_amount * sizeof(T));
+  }
+}
+
+/* TODO: test if this actually works */
+void ParticlesBlock::MoveUntilFull(ParticlesBlock *from, ParticlesBlock *to)
+{
+  BLI_assert(&from->container() == &to->container());
+  uint move_amount = MIN2(from->active_amount(), to->inactive_amount());
+  uint src_start = from->active_amount() - move_amount;
+  uint dst_start = to->next_inactive_index();
+
+  if (move_amount == 0) {
+    return;
+  }
+
+  move_buffers<float>(
+      from->float_buffers(), to->float_buffers(), src_start, dst_start, move_amount);
+  move_buffers<Vec3>(from->vec3_buffers(), to->vec3_buffers(), src_start, dst_start, move_amount);
+
+  from->active_amount() -= move_amount;
+  to->active_amount() += move_amount;
+}
+
+/* TODO: test if this actually works */
+void ParticlesBlock::Compress(ArrayRef<ParticlesBlock *> blocks)
+{
+  std::sort(blocks.begin(), blocks.end(), [](ParticlesBlock *a, ParticlesBlock *b) {
+    return a->active_amount() < b->active_amount();
+  });
+
+  uint last_non_full = blocks.size();
+
+  for (uint i = 0; i < blocks.size(); i++) {
+    while (i < last_non_full) {
+      ParticlesBlock *block = blocks[last_non_full];
+      if (block->is_full()) {
+        last_non_full--;
+        continue;
+      }
+      ParticlesBlock::MoveUntilFull(blocks[i], block);
+      if (blocks[i]->active_amount() == 0) {
+        break;
+      }
+    }
+  }
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 0c9baadf3b6..6e387149403 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -56,6 +56,7 @@ class ParticlesBlock {
                  uint active_amount = 0);
 
   uint &active_amount();
+  uint inactive_amount();
   bool is_full();
   uint next_inactive_index();
   uint size();
@@ -68,6 +69,9 @@ class ParticlesBlock {
   ArrayRef<Vec3 *> vec3_buffers();
   float *float_buffer(StringRef name);
   Vec3 *vec3_buffer(StringRef name);
+
+  static void MoveUntilFull(ParticlesBlock *from, ParticlesBlock *to);
+  static void Compress(ArrayRef<ParticlesBlock *> blocks);
 };
 
 /* Particles Container
@@ -111,6 +115,11 @@ inline uint &ParticlesBlock::active_amount()
   return m_active_amount;
 }
 
+inline uint ParticlesBlock::inactive_amount()
+{
+  return this->size() - m_active_amount;
+}
+
 inline bool ParticlesBlock::is_full()
 {
   return m_active_amount == this->size();



More information about the Bf-blender-cvs mailing list