[Bf-blender-cvs] [9fdef6ae5b6] functions-experimental-refactor: attributes block container compression and utilities

Jacques Lucke noreply at git.blender.org
Sun Nov 3 15:09:26 CET 2019


Commit: 9fdef6ae5b694ac5ab84351d69d2339b0e27953d
Author: Jacques Lucke
Date:   Sun Nov 3 15:09:21 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rB9fdef6ae5b694ac5ab84351d69d2339b0e27953d

attributes block container compression and utilities

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

M	source/blender/functions2/CMakeLists.txt
M	source/blender/functions2/FN_attributes_block_container.h
M	source/blender/functions2/FN_attributes_ref.h
M	source/blender/functions2/FN_cpp_type.h
M	source/blender/functions2/FN_generic_array_ref.h
M	source/blender/functions2/intern/attributes_block_container.cc
M	source/blender/functions2/intern/attributes_ref.cc
M	source/blender/functions2/intern/cpp_types.cc
A	source/blender/functions2/intern/generic_array_ref.cc

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

diff --git a/source/blender/functions2/CMakeLists.txt b/source/blender/functions2/CMakeLists.txt
index 5fcc35d1a39..770b7e758c3 100644
--- a/source/blender/functions2/CMakeLists.txt
+++ b/source/blender/functions2/CMakeLists.txt
@@ -34,6 +34,7 @@ set(SRC
   intern/attributes_ref.cc
   intern/cpp_type.cc
   intern/cpp_types.cc
+  intern/generic_array_ref.cc
   intern/generic_tuple.cc
   intern/initialize.cc
   intern/multi_function_network.cc
diff --git a/source/blender/functions2/FN_attributes_block_container.h b/source/blender/functions2/FN_attributes_block_container.h
index 99ce7e7b298..81b9ad1d96a 100644
--- a/source/blender/functions2/FN_attributes_block_container.h
+++ b/source/blender/functions2/FN_attributes_block_container.h
@@ -34,6 +34,11 @@ class AttributesBlockContainer : BLI::NonCopyable, BLI::NonMovable {
 
   AttributesBlock &new_block();
   void release_block(AttributesBlock &block);
+
+  friend bool operator==(const AttributesBlockContainer &a, const AttributesBlockContainer &b)
+  {
+    return &a == &b;
+  }
 };
 
 class AttributesBlock : BLI::NonCopyable, BLI::NonMovable {
@@ -63,6 +68,11 @@ class AttributesBlock : BLI::NonCopyable, BLI::NonMovable {
     return m_owner.block_size();
   }
 
+  uint unused_capacity() const
+  {
+    return this->capacity() - this->used_size();
+  }
+
   IndexRange used_range() const
   {
     return IndexRange(m_used_size);
@@ -74,6 +84,8 @@ class AttributesBlock : BLI::NonCopyable, BLI::NonMovable {
     m_used_size = new_used_size;
   }
 
+  void destruct_and_reorder(ArrayRef<uint> sorted_indices_to_destruct);
+
   AttributesBlockContainer &owner()
   {
     return m_owner;
@@ -88,6 +100,9 @@ class AttributesBlock : BLI::NonCopyable, BLI::NonMovable {
   {
     return AttributesRef(m_owner.info(), m_buffers, this->capacity());
   }
+
+  static void MoveUntilFull(AttributesBlock &from, AttributesBlock &to);
+  static void Compress(MutableArrayRef<AttributesBlock *> blocks);
 };
 
 }  // namespace FN
diff --git a/source/blender/functions2/FN_attributes_ref.h b/source/blender/functions2/FN_attributes_ref.h
index 9889da37f08..dab294d1cc6 100644
--- a/source/blender/functions2/FN_attributes_ref.h
+++ b/source/blender/functions2/FN_attributes_ref.h
@@ -209,6 +209,8 @@ class AttributesRef {
   }
 
   void destruct_and_reorder(ArrayRef<uint> sorted_indices_to_destruct);
+
+  static void RelocateUninitialized(AttributesRef from, AttributesRef to);
 };
 
 class AttributesRefGroup {
diff --git a/source/blender/functions2/FN_cpp_type.h b/source/blender/functions2/FN_cpp_type.h
index d9610d78cd9..766b7c118dd 100644
--- a/source/blender/functions2/FN_cpp_type.h
+++ b/source/blender/functions2/FN_cpp_type.h
@@ -19,6 +19,7 @@ class CPPType {
   using CopyToUninitializedF = void (*)(const void *src, void *dst);
   using RelocateToInitializedF = void (*)(void *src, void *dst);
   using RelocateToUninitializedF = void (*)(void *src, void *dst);
+  using RelocateToUninitializedNF = void (*)(void *src, void *dst, uint n);
 
   CPPType(std::string name,
           uint size,
@@ -31,6 +32,7 @@ class CPPType {
           CopyToUninitializedF copy_to_uninitialized,
           RelocateToInitializedF relocate_to_initialized,
           RelocateToUninitializedF relocate_to_uninitialized,
+          RelocateToUninitializedNF relocate_to_uninitialized_n,
           const CPPType *generalization)
       : m_size(size),
         m_alignment(alignment),
@@ -42,6 +44,7 @@ class CPPType {
         m_copy_to_uninitialized(copy_to_uninitialized),
         m_relocate_to_initialized(relocate_to_initialized),
         m_relocate_to_uninitialized(relocate_to_uninitialized),
+        m_relocate_to_uninitialized_n(relocate_to_uninitialized_n),
         m_generalization(generalization),
         m_name(name)
   {
@@ -137,6 +140,14 @@ class CPPType {
     m_relocate_to_uninitialized(src, dst);
   }
 
+  void relocate_to_uninitialized_n(void *src, void *dst, uint n) const
+  {
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
+
+    m_relocate_to_uninitialized_n(src, dst, n);
+  }
+
   bool is_same_or_generalization(const CPPType &other) const
   {
     if (&other == this) {
@@ -170,6 +181,7 @@ class CPPType {
   CopyToUninitializedF m_copy_to_uninitialized;
   RelocateToInitializedF m_relocate_to_initialized;
   RelocateToUninitializedF m_relocate_to_uninitialized;
+  RelocateToUninitializedNF m_relocate_to_uninitialized_n;
   const CPPType *m_generalization;
   std::string m_name;
 };
diff --git a/source/blender/functions2/FN_generic_array_ref.h b/source/blender/functions2/FN_generic_array_ref.h
index 418f80cff3d..d4225509e07 100644
--- a/source/blender/functions2/FN_generic_array_ref.h
+++ b/source/blender/functions2/FN_generic_array_ref.h
@@ -134,6 +134,8 @@ class GenericMutableArrayRef {
     m_type->copy_to_uninitialized(src, dst);
   }
 
+  static void RelocateUninitialized(GenericMutableArrayRef from, GenericMutableArrayRef to);
+
   void *operator[](uint index)
   {
     BLI_assert(index < m_size);
diff --git a/source/blender/functions2/intern/attributes_block_container.cc b/source/blender/functions2/intern/attributes_block_container.cc
index d083ed4c925..d7cca2a022f 100644
--- a/source/blender/functions2/intern/attributes_block_container.cc
+++ b/source/blender/functions2/intern/attributes_block_container.cc
@@ -62,4 +62,53 @@ AttributesBlock::~AttributesBlock()
   }
 }
 
+void AttributesBlock::destruct_and_reorder(ArrayRef<uint> sorted_indices_to_destruct)
+{
+  this->as_ref().destruct_and_reorder(sorted_indices_to_destruct);
+  this->set_used_size(m_used_size - sorted_indices_to_destruct.size());
+}
+
+void AttributesBlock::MoveUntilFull(AttributesBlock &from, AttributesBlock &to)
+{
+  BLI_assert(from.owner() == to.owner());
+  uint move_amount = std::min(from.used_size(), to.unused_capacity());
+
+  if (move_amount == 0) {
+    return;
+  }
+
+  AttributesRef from_ref = from.as_ref__all().slice(from.used_size() - move_amount, move_amount);
+  AttributesRef to_ref = to.as_ref__all().slice(to.used_size(), move_amount);
+
+  AttributesRef::RelocateUninitialized(from_ref, to_ref);
+
+  from.set_used_size(from.used_size() - move_amount);
+  to.set_used_size(to.used_size() + move_amount);
+}
+
+void AttributesBlock::Compress(MutableArrayRef<AttributesBlock *> blocks)
+{
+  std::sort(blocks.begin(), blocks.end(), [](AttributesBlock *a, AttributesBlock *b) {
+    return a->used_size() < b->used_size();
+  });
+
+  uint first_non_full_index = 0;
+  uint last_non_empty_index = blocks.size() - 1;
+
+  while (first_non_full_index < last_non_empty_index) {
+    AttributesBlock &first_non_full = *blocks[first_non_full_index];
+    AttributesBlock &last_non_empty = *blocks[last_non_empty_index];
+
+    if (first_non_full.used_size() == first_non_full.capacity()) {
+      first_non_full_index++;
+    }
+    else if (last_non_empty.used_size() == 0) {
+      last_non_empty_index--;
+    }
+    else {
+      AttributesBlock::MoveUntilFull(last_non_empty, first_non_full);
+    }
+  }
+}
+
 }  // namespace FN
diff --git a/source/blender/functions2/intern/attributes_ref.cc b/source/blender/functions2/intern/attributes_ref.cc
index deedc7b969a..1fd09cc635d 100644
--- a/source/blender/functions2/intern/attributes_ref.cc
+++ b/source/blender/functions2/intern/attributes_ref.cc
@@ -55,6 +55,19 @@ void AttributesRef::destruct_and_reorder(ArrayRef<uint> indices)
   }
 }
 
+void AttributesRef::RelocateUninitialized(AttributesRef from, AttributesRef to)
+{
+  BLI_assert(from.size() == to.size());
+  BLI_assert(&from.info() == &to.info());
+
+  for (uint attribute_index : from.info().indices()) {
+    GenericMutableArrayRef from_array = from.get(attribute_index);
+    GenericMutableArrayRef to_array = to.get(attribute_index);
+
+    GenericMutableArrayRef::RelocateUninitialized(from_array, to_array);
+  }
+}
+
 AttributesRefGroup::AttributesRefGroup(const AttributesInfo &info,
                                        Vector<ArrayRef<void *>> buffers,
                                        Vector<IndexRange> ranges)
diff --git a/source/blender/functions2/intern/cpp_types.cc b/source/blender/functions2/intern/cpp_types.cc
index f0c17c88735..d569efe2197 100644
--- a/source/blender/functions2/intern/cpp_types.cc
+++ b/source/blender/functions2/intern/cpp_types.cc
@@ -58,6 +58,10 @@ template<typename T> void RelocateToUninitialized_CB(void *src, void *dst)
 {
   BLI::uninitialized_relocate((T *)src, (T *)dst);
 }
+template<typename T> void RelocateToUninitializedN_CB(void *src, void *dst, uint n)
+{
+  BLI::uninitialized_relocate_n((T *)src, n, (T *)dst);
+}
 
 template<typename T> static std::unique_ptr<const CPPType> create_cpp_type(StringRef name)
 {
@@ -73,6 +77,7 @@ template<typename T> static std::unique_ptr<const CPPType> create_cpp_type(Strin
       CopyToUninitialized_CB<T>,
       RelocateToInitialized_CB<T>,
       RelocateToUninitialized_CB<T>,
+      RelocateToUninitializedN_CB<T>,
       nullptr);
   return std::unique_ptr<const CPPType>(type);
 }
diff --git a/source/blender/functions2/intern/generic_array_ref.cc b/source/blender/functions2/intern/generic_array_ref.cc
new file mode 100644
index 00000000000..fd6bb80e5c8
--- /dev/null
+++ b/source/blender/functions2/intern/generic_array_ref.cc
@@ -0,0 +1,14 @@
+#include "FN_generic_array_ref.h"
+
+namespace FN {
+
+void GenericMutableArrayRef::RelocateUninitialized(GenericMutableArrayRef from,
+                                                   GenericMutableArrayRef to)
+{
+  BLI_assert(from.size() == to.size());
+  BLI_assert(from.type() == to.type());
+
+  from.m_type->relocate_to_uninitialized_n(from.buffer(), to.buffer(), from.size());
+}
+
+}  // namespace FN



More information about the Bf-blender-cvs mailing list