[Bf-blender-cvs] [165580c6617] functions: Update attribute arrays based on which attributes are required

Jacques Lucke noreply at git.blender.org
Wed Jun 26 18:19:09 CEST 2019


Commit: 165580c661731f0220ffb1b1e87f1aa455345eb7
Author: Jacques Lucke
Date:   Wed Jun 26 12:51:58 2019 +0200
Branches: functions
https://developer.blender.org/rB165580c661731f0220ffb1b1e87f1aa455345eb7

Update attribute arrays based on which attributes are required

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

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

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

diff --git a/source/blender/simulations/bparticles/attributes.cpp b/source/blender/simulations/bparticles/attributes.cpp
index a4b6e25ba6c..dea29f58282 100644
--- a/source/blender/simulations/bparticles/attributes.cpp
+++ b/source/blender/simulations/bparticles/attributes.cpp
@@ -23,7 +23,7 @@ AttributesInfo::AttributesInfo(ArrayRef<std::string> byte_names,
 }
 
 AttributeArraysCore::AttributeArraysCore(AttributesInfo &info, ArrayRef<void *> arrays, uint size)
-    : m_info(info), m_arrays(arrays.to_small_vector()), m_size(size)
+    : m_info(&info), m_arrays(arrays.to_small_vector()), m_size(size)
 {
 }
 
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index a4711ca8d36..3928a2879ce 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -74,13 +74,23 @@ class AttributesInfo {
     return m_types;
   }
 
+  int attribute_index_try(StringRef name) const
+  {
+    return m_indices.index(name.to_std_string());
+  }
+
   uint attribute_index(StringRef name) const
   {
-    int index = m_indices.index(name.to_std_string());
+    int index = this->attribute_index_try(name);
     BLI_assert(index >= 0);
     return (uint)index;
   }
 
+  Range<uint> attribute_indices() const
+  {
+    return Range<uint>(0, m_indices.size());
+  }
+
   Range<uint> byte_attributes() const
   {
     return m_byte_attributes;
@@ -106,7 +116,7 @@ class AttributeArrays;
 
 class AttributeArraysCore {
  private:
-  AttributesInfo &m_info;
+  AttributesInfo *m_info;
   SmallVector<void *> m_arrays;
   uint m_size = 0;
 
@@ -181,7 +191,7 @@ class JoinedAttributeArrays {
 
 inline AttributesInfo &AttributeArraysCore::info()
 {
-  return m_info;
+  return *m_info;
 }
 
 inline void *AttributeArraysCore::get_ptr(uint index)
@@ -191,7 +201,7 @@ inline void *AttributeArraysCore::get_ptr(uint index)
 
 inline AttributeType AttributeArraysCore::get_type(uint index)
 {
-  return m_info.type_of(index);
+  return m_info->type_of(index);
 }
 
 inline AttributeArrays AttributeArraysCore::slice_all()
diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index acca5f9e687..53199689024 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -42,10 +42,69 @@ void ParticlesContainer::release_block(ParticlesBlock *block)
   delete block;
 }
 
+static SmallVector<int> map_attribute_indices(AttributesInfo &from_info, AttributesInfo &to_info)
+{
+  SmallVector<int> mapping;
+  mapping.reserve(from_info.amount());
+
+  for (uint from_index : from_info.attribute_indices()) {
+    StringRef name = from_info.name_of(from_index);
+    int to_index = to_info.attribute_index_try(name);
+    if (to_index == -1) {
+      mapping.append(-1);
+    }
+    else if (from_info.type_of(from_index) != to_info.type_of(to_index)) {
+      mapping.append(-1);
+    }
+    else {
+      mapping.append((uint)to_index);
+    }
+  }
+
+  return mapping;
+}
+
 void ParticlesContainer::update_attributes(AttributesInfo new_info)
 {
+
+  AttributesInfo &old_info = m_attributes_info;
+
+  SmallVector<int> new_to_old_mapping = map_attribute_indices(new_info, old_info);
+  SmallVector<int> old_to_new_mapping = map_attribute_indices(old_info, new_info);
+
+  SmallVector<uint> unused_old_indices;
+  for (uint i = 0; i < old_to_new_mapping.size(); i++) {
+    if (old_to_new_mapping[i] == -1) {
+      unused_old_indices.append(i);
+    }
+  }
+
   m_attributes_info = new_info;
-  /* TODO: actually update attributes */
+
+  SmallVector<void *> arrays;
+  arrays.reserve(new_info.amount());
+  for (ParticlesBlock *block : m_blocks) {
+    arrays.clear();
+
+    for (uint new_index : new_info.attribute_indices()) {
+      int old_index = new_to_old_mapping[new_index];
+      AttributeType type = new_info.type_of(new_index);
+
+      if (old_index == -1) {
+        arrays.append(MEM_calloc_arrayN(m_block_size, size_of_attribute_type(type), __func__));
+      }
+      else {
+        arrays.append(block->attributes_core().get_ptr((uint)old_index));
+      }
+    }
+
+    for (uint old_index : unused_old_indices) {
+      void *ptr = block->attributes_core().get_ptr(old_index);
+      MEM_freeN(ptr);
+    }
+
+    block->m_attributes_core = AttributeArraysCore(m_attributes_info, arrays, m_block_size);
+  }
 }
 
 void ParticlesBlock::MoveUntilFull(ParticlesBlock &from, ParticlesBlock &to)
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 1201f25c097..f4a598340c4 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -50,6 +50,8 @@ class ParticlesBlock {
   AttributeArraysCore m_attributes_core;
   uint m_active_amount = 0;
 
+  friend ParticlesContainer;
+
  public:
   ParticlesBlock(ParticlesContainer &container, AttributeArraysCore &attributes_core);



More information about the Bf-blender-cvs mailing list