[Bf-blender-cvs] [f2ed497129b] functions: initialize attributes with default values

Jacques Lucke noreply at git.blender.org
Thu Jun 27 15:50:01 CEST 2019


Commit: f2ed497129bea76eeb03f3e15c54acb1136621eb
Author: Jacques Lucke
Date:   Thu Jun 27 13:46:03 2019 +0200
Branches: functions
https://developer.blender.org/rBf2ed497129bea76eeb03f3e15c54acb1136621eb

initialize attributes with default values

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

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

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

diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index a953a6ab31f..2e21b1ca729 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -170,6 +170,9 @@ class AttributeArrays {
 
   void *get_ptr(uint index) const;
 
+  void init_default(uint index);
+  void init_default(StringRef name);
+
   ArrayRef<uint8_t> get_byte(uint index) const;
   ArrayRef<uint8_t> get_byte(StringRef name);
   ArrayRef<float> get_float(uint index) const;
@@ -246,6 +249,23 @@ inline void *AttributeArrays::get_ptr(uint index) const
   return POINTER_OFFSET(ptr, m_start * size);
 }
 
+inline void AttributeArrays::init_default(uint index)
+{
+  void *default_value = m_core.info().default_value_ptr(index);
+  void *dst = this->get_ptr(index);
+  AttributeType type = m_core.get_type(index);
+  uint element_size = size_of_attribute_type(type);
+
+  for (uint i = 0; i < m_size; i++) {
+    memcpy(POINTER_OFFSET(dst, element_size * i), default_value, element_size);
+  }
+}
+
+inline void AttributeArrays::init_default(StringRef name)
+{
+  this->init_default(this->attribute_index(name));
+}
+
 inline ArrayRef<uint8_t> AttributeArrays::get_byte(uint index) const
 {
   BLI_assert(m_core.get_type(index) == AttributeType::Byte);
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 0bda6770598..590010fd9e8 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -74,11 +74,17 @@ EmitTarget &EmitterInterface::request(uint particle_type_id, uint size)
   while (remaining_size > 0) {
     ParticlesBlock &block = m_allocator.get_non_full_block(particle_type_id);
 
-    uint size_to_use = std::min(block.size(), remaining_size);
+    uint size_to_use = std::min(block.inactive_amount(), remaining_size);
+    Range<uint> range(block.active_amount(), block.active_amount() + size_to_use);
     block.active_amount() += size_to_use;
 
     blocks.append(&block);
-    ranges.append(Range<uint>(0, size_to_use));
+    ranges.append(range);
+
+    AttributeArrays attributes = block.slice(range);
+    for (uint i : attributes.info().attribute_indices()) {
+      attributes.init_default(i);
+    }
 
     remaining_size -= size_to_use;
   }
diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index 990709c4992..50b78b51506 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -82,6 +82,13 @@ void ParticlesContainer::update_attributes(AttributesInfo new_info)
     }
   }
 
+  SmallVector<uint> indices_to_allocate;
+  for (uint i = 0; i < new_to_old_mapping.size(); i++) {
+    if (new_to_old_mapping[i] == -1) {
+      indices_to_allocate.append(i);
+    }
+  }
+
   m_attributes_info = new_info;
 
   SmallVector<void *> arrays;
@@ -94,13 +101,7 @@ void ParticlesContainer::update_attributes(AttributesInfo new_info)
       AttributeType type = new_info.type_of(new_index);
 
       if (old_index == -1) {
-        void *array = MEM_malloc_arrayN(m_block_size, size_of_attribute_type(type), __func__);
-        uint value_size = size_of_attribute_type(type);
-        void *default_ptr = new_info.default_value_ptr(new_index);
-        for (uint i = 0; i < m_block_size; i++) {
-          memcpy(POINTER_OFFSET(array, i * value_size), default_ptr, value_size);
-        }
-        arrays.append(array);
+        arrays.append(MEM_malloc_arrayN(m_block_size, size_of_attribute_type(type), __func__));
       }
       else {
         arrays.append(block->attributes_core().get_ptr((uint)old_index));
@@ -113,6 +114,10 @@ void ParticlesContainer::update_attributes(AttributesInfo new_info)
     }
 
     block->m_attributes_core = AttributeArraysCore(m_attributes_info, arrays, m_block_size);
+
+    for (uint new_index : indices_to_allocate) {
+      block->m_attributes_core.slice_all().init_default(new_index);
+    }
   }
 }



More information about the Bf-blender-cvs mailing list