[Bf-blender-cvs] [a708562177b] functions-experimental-refactor: move attribute defaults back into AttributesInfo

Jacques Lucke noreply at git.blender.org
Fri Nov 8 20:36:43 CET 2019


Commit: a708562177b1c14bbbcc2c9609d4122bfe714dbc
Author: Jacques Lucke
Date:   Fri Nov 8 19:48:04 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rBa708562177b1c14bbbcc2c9609d4122bfe714dbc

move attribute defaults back into AttributesInfo

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

M	source/blender/functions/FN_attributes_block_container.h
M	source/blender/functions/FN_attributes_ref.h
M	source/blender/functions/intern/attributes_block_container.cc
M	source/blender/functions/intern/attributes_ref.cc
M	source/blender/simulations/bparticles/integrator.cpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particle_allocator.cpp
M	source/blender/simulations/bparticles/particle_allocator.hpp
M	source/blender/simulations/bparticles/simulate.cpp
M	source/blender/simulations/bparticles/simulate.hpp

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

diff --git a/source/blender/functions/FN_attributes_block_container.h b/source/blender/functions/FN_attributes_block_container.h
index 27cd95c55ae..b7e07820534 100644
--- a/source/blender/functions/FN_attributes_block_container.h
+++ b/source/blender/functions/FN_attributes_block_container.h
@@ -40,8 +40,7 @@ class AttributesBlockContainer : BLI::NonCopyable, BLI::NonMovable {
   template<typename T> Vector<T> flatten_attribute(StringRef name) const;
   void flatten_attribute(StringRef name, GenericMutableArrayRef dst) const;
 
-  void update_attributes(const AttributesInfoBuilder &new_info_builder,
-                         const AttributesDefaults &defaults);
+  void update_attributes(const AttributesInfoBuilder &new_info_builder);
 
   AttributesBlock &new_block();
   void release_block(AttributesBlock &block);
diff --git a/source/blender/functions/FN_attributes_ref.h b/source/blender/functions/FN_attributes_ref.h
index 9ae1b077eea..82870f9f15e 100644
--- a/source/blender/functions/FN_attributes_ref.h
+++ b/source/blender/functions/FN_attributes_ref.h
@@ -25,23 +25,29 @@ using BLI::VectorSet;
 
 class AttributesInfo;
 
-class AttributesInfoBuilder {
+class AttributesInfoBuilder : BLI::NonCopyable, BLI::NonMovable {
  private:
+  MonotonicAllocator<32> m_allocator;
   VectorSet<std::string> m_names;
   Vector<const CPPType *> m_types;
+  Vector<void *> m_defaults;
 
  public:
   AttributesInfoBuilder() = default;
+  ~AttributesInfoBuilder();
 
-  template<typename T> void add(StringRef name)
+  template<typename T> void add(StringRef name, const T &default_value)
   {
-    this->add(name, CPP_TYPE<T>());
+    this->add(name, CPP_TYPE<T>(), (const void *)&default_value);
   }
 
-  void add(StringRef name, const CPPType &type)
+  void add(StringRef name, const CPPType &type, const void *default_value)
   {
     if (m_names.add(name)) {
       m_types.append(&type);
+      void *dst = m_allocator.allocate(type.size(), type.alignment());
+      type.copy_to_uninitialized(default_value, dst);
+      m_defaults.append(dst);
     }
     else {
       BLI_assert(m_types[m_names.index(name)] == &type);
@@ -63,19 +69,27 @@ class AttributesInfoBuilder {
     return m_types;
   }
 
+  ArrayRef<const void *> defaults() const
+  {
+    return ArrayRef<const void *>(m_defaults.begin(), m_defaults.size());
+  }
+
   void add(const AttributesInfoBuilder &other);
   void add(const AttributesInfo &other);
 };
 
 class AttributesInfo : BLI::NonCopyable, BLI::NonMovable {
  private:
+  MonotonicAllocator<32> m_allocator;
   StringMap<int> m_index_by_name;
   Vector<std::string> m_name_by_index;
   Vector<const CPPType *> m_type_by_index;
+  Vector<void *> m_defaults;
 
  public:
   AttributesInfo() = default;
   AttributesInfo(const AttributesInfoBuilder &builder);
+  ~AttributesInfo();
 
   uint size() const
   {
@@ -92,6 +106,16 @@ class AttributesInfo : BLI::NonCopyable, BLI::NonMovable {
     return m_index_by_name.lookup(name);
   }
 
+  const void *default_of(uint index) const
+  {
+    return m_defaults[index];
+  }
+
+  const void *default_of(StringRef name) const
+  {
+    return this->default_of(this->index_of(name));
+  }
+
   int index_of_try(StringRef name, const CPPType &type) const
   {
     int index = this->index_of_try(name);
@@ -332,58 +356,15 @@ class AttributesRefGroup {
   }
 };
 
-class AttributesDefaults : BLI::NonCopyable, BLI::NonMovable {
- private:
-  StringMap<uint> m_index_by_name;
-  Vector<const CPPType *> m_type_by_index;
-  MonotonicAllocator<> m_allocator;
-  Vector<void *> m_values;
-
- public:
-  template<typename T> void add(StringRef name, T value)
-  {
-    if (m_index_by_name.contains(name)) {
-      /* TODO: Check if different handling of this case works better. */
-      BLI_assert(false);
-    }
-    else {
-      uint index = m_type_by_index.size();
-      m_index_by_name.add_new(name, index);
-      const CPPType &type = CPP_TYPE<T>();
-      m_type_by_index.append(&type);
-      void *value_buffer = m_allocator.allocate(type.size(), type.alignment());
-      new (value_buffer) T(std::move(value));
-      m_values.append(value_buffer);
-    }
-  }
-
-  const void *get(StringRef name, const CPPType &expected_type) const
-  {
-    uint index = m_index_by_name.lookup(name);
-    BLI_assert(*m_type_by_index[index] == expected_type);
-    UNUSED_VARS_NDEBUG(expected_type);
-    return m_values[index];
-  }
-
-  template<typename T> const T &get(StringRef name) const
-  {
-    const void *value = this->get(name, CPP_TYPE<T>());
-    return *(const T *)value;
-  }
-};
-
 class AttributesInfoDiff {
  private:
   const AttributesInfo *m_old_info;
   const AttributesInfo *m_new_info;
   Array<int> m_old_to_new_mapping;
   Array<int> m_new_to_old_mapping;
-  Array<const void *> m_default_buffers;
 
  public:
-  AttributesInfoDiff(const AttributesInfo &old_info,
-                     const AttributesInfo &new_info,
-                     const AttributesDefaults &defaults);
+  AttributesInfoDiff(const AttributesInfo &old_info, const AttributesInfo &new_info);
 
   void update(uint capacity,
               uint used_size,
diff --git a/source/blender/functions/intern/attributes_block_container.cc b/source/blender/functions/intern/attributes_block_container.cc
index 1fbbf97fb70..e772584cfda 100644
--- a/source/blender/functions/intern/attributes_block_container.cc
+++ b/source/blender/functions/intern/attributes_block_container.cc
@@ -41,12 +41,11 @@ void AttributesBlockContainer::flatten_attribute(StringRef name, GenericMutableA
   }
 }
 
-void AttributesBlockContainer::update_attributes(const AttributesInfoBuilder &new_info_builder,
-                                                 const AttributesDefaults &defaults)
+void AttributesBlockContainer::update_attributes(const AttributesInfoBuilder &new_info_builder)
 {
   auto new_info = BLI::make_unique<AttributesInfo>(new_info_builder);
 
-  AttributesInfoDiff diff{*m_info, *new_info, defaults};
+  AttributesInfoDiff diff{*m_info, *new_info};
   for (AttributesBlock *block : m_active_blocks) {
     Vector<void *> new_buffers{diff.new_buffer_amount()};
     diff.update(m_block_size, block->m_used_size, block->m_buffers, new_buffers);
diff --git a/source/blender/functions/intern/attributes_ref.cc b/source/blender/functions/intern/attributes_ref.cc
index a3a6c5e7220..7460b9730c9 100644
--- a/source/blender/functions/intern/attributes_ref.cc
+++ b/source/blender/functions/intern/attributes_ref.cc
@@ -2,26 +2,48 @@
 
 namespace FN {
 
+AttributesInfoBuilder::~AttributesInfoBuilder()
+{
+  for (uint i = 0; i < m_defaults.size(); i++) {
+    m_types[i]->destruct(m_defaults[i]);
+  }
+}
+
 void AttributesInfoBuilder::add(const AttributesInfoBuilder &other)
 {
   for (uint i = 0; i < other.size(); i++) {
-    this->add(other.m_names[i], *other.m_types[i]);
+    this->add(other.m_names[i], *other.m_types[i], other.m_defaults[i]);
   }
 }
 
 void AttributesInfoBuilder::add(const AttributesInfo &other)
 {
   for (uint i = 0; i < other.size(); i++) {
-    this->add(other.name_of(i), other.type_of(i));
+    this->add(other.name_of(i), other.type_of(i), other.default_of(i));
   }
 }
 
 AttributesInfo::AttributesInfo(const AttributesInfoBuilder &builder)
 {
   for (uint i = 0; i < builder.size(); i++) {
-    m_index_by_name.add_new(builder.names()[i], i);
-    m_name_by_index.append(builder.names()[i]);
-    m_type_by_index.append(builder.types()[i]);
+    StringRef name = builder.names()[i];
+    const CPPType &type = *builder.types()[i];
+    const void *default_value = builder.defaults()[i];
+
+    m_index_by_name.add_new(name, i);
+    m_name_by_index.append(name);
+    m_type_by_index.append(&type);
+
+    void *dst = m_allocator.allocate(type.size(), type.alignment());
+    type.copy_to_uninitialized(default_value, dst);
+    m_defaults.append(dst);
+  }
+}
+
+AttributesInfo::~AttributesInfo()
+{
+  for (uint i = 0; i < m_defaults.size(); i++) {
+    m_type_by_index[i]->destruct(m_defaults[i]);
   }
 }
 
@@ -96,20 +118,11 @@ static Array<int> map_attribute_indices(const AttributesInfo &from_info,
 }
 
 AttributesInfoDiff::AttributesInfoDiff(const AttributesInfo &old_info,
-                                       const AttributesInfo &new_info,
-                                       const AttributesDefaults &defaults)
+                                       const AttributesInfo &new_info)
     : m_old_info(&old_info), m_new_info(&new_info)
 {
   m_old_to_new_mapping = map_attribute_indices(old_info, new_info);
   m_new_to_old_mapping = map_attribute_indices(new_info, old_info);
-  m_default_buffers = Array<const void *>(new_info.size(), nullptr);
-
-  for (uint i : new_info.indices()) {
-    if (m_new_to_old_mapping[i] == -1) {
-      const void *default_buffer = defaults.get(new_info.name_of(i), new_info.type_of(i));
-      m_default_buffers[i] = default_buffer;
-    }
-  }
 }
 
 void AttributesInfoDiff::update(uint capacity,
@@ -128,7 +141,7 @@ void AttributesInfoDiff::update(uint capacity,
       void *new_buffer = MEM_mallocN_aligned(capacity * type.size(), type.alignment(), __func__);
 
       GenericMutableArrayRef{type, new_buffer, used_size}.fill__uninitialized(
-          m_default_buffers[new_index]);
+          m_new_info->default_of(new_index));
 
       new_buffers[new_index] = new_buffer;
     }
diff --git a/source/blender/simulations/bparticles/integrator.cpp b/source/blender/simulations/bparticles/integrator.cpp
index 125d44bb0be..ad8543932c5 100644
--- a/source/blender/simulations/bparticles/integrator.cpp
+++ b/source/blender/simulations/bparticles/integrator.cpp
@@ -7,7 +7,7 @@ using BLI::float3;
 ConstantVelocityIntegrator::ConstantVelocityIntegrator()
 {
   FN::AttributesInfoBuilder builder;
-  builder.add<float3>("Position");
+  builder.add<float3>("Position", {0, 0, 0});
   m_offset_attributes_info = BLI::make_unique<AttributesInfo>(builder);
 }
 
@@ -30,8 +30,8 @@ void ConstantVelocityIntegrator::integrate(IntegratorInterface &interface)
 EulerIntegrator::EulerIntegrator(ArrayRef<Force *> forces) : m_forces(forces)
 {
   FN::AttributesInfoBuilder builder;
-  builder.add<float3>("Position");
-  builder.add<float3>("Velocity");
+  builder.add<float3>("Position", {0, 0, 0})

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list