[Bf-blender-cvs] [555d29a0fee] functions: initialize particles with IDs

Jacques Lucke noreply at git.blender.org
Mon Jul 29 17:57:46 CEST 2019


Commit: 555d29a0fee78222285f1b5ca6127e3356724da4
Author: Jacques Lucke
Date:   Mon Jul 29 16:39:35 2019 +0200
Branches: functions
https://developer.blender.org/rB555d29a0fee78222285f1b5ca6127e3356724da4

initialize particles with IDs

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

M	release/scripts/startup/nodes/bparticle_nodes/particle_info.py
M	source/blender/simulations/bparticles/particle_allocator.cpp
M	source/blender/simulations/bparticles/particle_allocator.hpp
M	source/blender/simulations/bparticles/particles_container.cpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/particle_info.py b/release/scripts/startup/nodes/bparticle_nodes/particle_info.py
index 683a881b503..747eb708bac 100644
--- a/release/scripts/startup/nodes/bparticle_nodes/particle_info.py
+++ b/release/scripts/startup/nodes/bparticle_nodes/particle_info.py
@@ -8,6 +8,7 @@ class ParticleInfoNode(bpy.types.Node, BParticlesNode):
     bl_label = "Particle Info"
 
     def declaration(self, builder : NodeBuilder):
+        builder.fixed_output("id", "ID", "Integer")
         builder.fixed_output("position", "Position", "Vector")
         builder.fixed_output("velocity", "Velocity", "Vector")
         builder.fixed_output("birth_time", "Birth Time", "Float")
diff --git a/source/blender/simulations/bparticles/particle_allocator.cpp b/source/blender/simulations/bparticles/particle_allocator.cpp
index 0ce2abd4806..718a9e0962a 100644
--- a/source/blender/simulations/bparticles/particle_allocator.cpp
+++ b/source/blender/simulations/bparticles/particle_allocator.cpp
@@ -45,15 +45,27 @@ void ParticleAllocator::allocate_block_ranges(StringRef particle_type_name,
     r_blocks.append(&block);
     r_ranges.append(range);
 
-    AttributeArrays attributes = block.attributes_slice(range);
-    for (uint i : attributes.info().attribute_indices()) {
-      attributes.init_default(i);
-    }
+    this->initialize_new_particles(block, range);
 
     remaining_size -= size_to_use;
   }
 }
 
+void ParticleAllocator::initialize_new_particles(ParticlesBlock &block, Range<uint> pindices)
+{
+  AttributeArrays attributes = block.attributes_slice(pindices);
+  for (uint i : attributes.info().attribute_indices()) {
+    attributes.init_default(i);
+  }
+
+  ArrayRef<int32_t> particle_ids = block.attributes_all().get_integer("ID");
+  Range<uint> new_ids = block.container().new_particle_ids(pindices.size());
+  for (uint i = 0; i < pindices.size(); i++) {
+    uint pindex = pindices[i];
+    particle_ids[pindex] = new_ids[i];
+  }
+}
+
 AttributesInfo &ParticleAllocator::attributes_info(StringRef particle_type_name)
 {
   return m_state.particle_container(particle_type_name).attributes_info();
diff --git a/source/blender/simulations/bparticles/particle_allocator.hpp b/source/blender/simulations/bparticles/particle_allocator.hpp
index f9986984c6d..33a06a02863 100644
--- a/source/blender/simulations/bparticles/particle_allocator.hpp
+++ b/source/blender/simulations/bparticles/particle_allocator.hpp
@@ -48,6 +48,8 @@ class ParticleAllocator {
                              Vector<Range<uint>> &r_ranges);
 
   AttributesInfo &attributes_info(StringRef particle_type_name);
+
+  void initialize_new_particles(ParticlesBlock &block, Range<uint> pindices);
 };
 
 /* ParticleAllocator inline functions
diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index 08017cd806a..62a47795995 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -6,15 +6,13 @@
 
 namespace BParticles {
 
-ParticlesBlock::ParticlesBlock(ParticlesContainer &container,
-                               AttributeArraysCore &attributes_core,
-                               uint id)
-    : m_container(container), m_attributes_core(attributes_core), m_id(id)
+ParticlesBlock::ParticlesBlock(ParticlesContainer &container, AttributeArraysCore &attributes_core)
+    : m_container(container), m_attributes_core(attributes_core)
 {
 }
 
 ParticlesContainer::ParticlesContainer(AttributesInfo attributes, uint block_size)
-    : m_attributes_info(std::move(attributes)), m_block_size(block_size)
+    : m_attributes_info(std::move(attributes)), m_block_size(block_size), m_next_particle_id(0)
 {
 }
 
@@ -66,7 +64,7 @@ ParticlesBlock *ParticlesContainer::allocate_block()
 {
   AttributeArraysCore attributes_core = AttributeArraysCore::NewWithSeparateAllocations(
       m_attributes_info, m_block_size);
-  ParticlesBlock *block = new ParticlesBlock(*this, attributes_core, m_next_block_id++);
+  ParticlesBlock *block = new ParticlesBlock(*this, attributes_core);
   return block;
 }
 
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index 10aac200c5a..04f38544814 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <mutex>
+#include <atomic>
 
 #include "BLI_map.hpp"
 #include "BLI_stack.hpp"
@@ -31,7 +32,7 @@ class ParticlesContainer {
   Stack<ParticlesBlock *> m_cached_blocks;
   uint m_block_size;
   std::mutex m_blocks_mutex;
-  uint m_next_block_id = 0;
+  std::atomic<uint> m_next_particle_id;
 
  public:
   ParticlesContainer(AttributesInfo attributes, uint block_size);
@@ -60,6 +61,11 @@ class ParticlesContainer {
    */
   void update_attributes(AttributesInfo new_info);
 
+  /**
+   * Request a range of unique particle ids. This method is thread-safe.
+   */
+  Range<uint> new_particle_ids(uint amount);
+
   /**
    * Get a read-only buffer of all the blocks currently in use.
    */
@@ -104,12 +110,11 @@ class ParticlesBlock {
   ParticlesContainer &m_container;
   AttributeArraysCore m_attributes_core;
   uint m_active_amount = 0;
-  uint m_id;
 
   friend ParticlesContainer;
 
  public:
-  ParticlesBlock(ParticlesContainer &container, AttributeArraysCore &attributes_core, uint id);
+  ParticlesBlock(ParticlesContainer &container, AttributeArraysCore &attributes_core);
 
   /**
    * Get the range of attribute indices that contain active particles.
@@ -217,6 +222,12 @@ inline uint ParticlesContainer::count_active() const
   return count;
 }
 
+inline Range<uint> ParticlesContainer::new_particle_ids(uint amount)
+{
+  uint start = m_next_particle_id.fetch_add(amount);
+  return Range<uint>(start, start + amount);
+}
+
 inline AttributesInfo &ParticlesContainer::attributes_info()
 {
   return m_attributes_info;
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index fd07d10f6ac..54640f6b6d1 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -555,6 +555,7 @@ BLI_NOINLINE static AttributesInfo build_attribute_info_for_type(ParticleType &t
   }
 
   builder.add_byte("Kill State", 0);
+  builder.add_integer("ID", 0);
   builder.add_float("Birth Time", 0);
 
   return AttributesInfo(builder);



More information about the Bf-blender-cvs mailing list