[Bf-blender-cvs] [c23ccfc0ac7] functions: separate files for particle state and allocator

Jacques Lucke noreply at git.blender.org
Thu Jul 18 18:19:55 CEST 2019


Commit: c23ccfc0ac756a57ffa6147a45fd5e5f06b8d9cb
Author: Jacques Lucke
Date:   Thu Jul 18 16:59:31 2019 +0200
Branches: functions
https://developer.blender.org/rBc23ccfc0ac756a57ffa6147a45fd5e5f06b8d9cb

separate files for particle state and allocator

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

M	source/blender/simulations/CMakeLists.txt
M	source/blender/simulations/bparticles/core.cpp
M	source/blender/simulations/bparticles/core.hpp
A	source/blender/simulations/bparticles/particle_allocator.cpp
A	source/blender/simulations/bparticles/particle_allocator.hpp
A	source/blender/simulations/bparticles/particles_state.cpp
A	source/blender/simulations/bparticles/particles_state.hpp

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

diff --git a/source/blender/simulations/CMakeLists.txt b/source/blender/simulations/CMakeLists.txt
index a7fa907c7b5..76ce0a44c0b 100644
--- a/source/blender/simulations/CMakeLists.txt
+++ b/source/blender/simulations/CMakeLists.txt
@@ -45,6 +45,10 @@ set(SRC
   bparticles/node_frontend.cpp
   bparticles/particle_set.hpp
   bparticles/particle_set.cpp
+  bparticles/particles_state.hpp
+  bparticles/particles_state.cpp
+  bparticles/particle_allocator.hpp
+  bparticles/particle_allocator.cpp
 )
 
 set(LIB
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 8627d62ece8..89c4eecc96d 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -44,89 +44,6 @@ StepDescription::~StepDescription()
   }
 }
 
-ParticlesState::~ParticlesState()
-{
-  for (ParticlesContainer *container : m_container_by_id.values()) {
-    delete container;
-  }
-}
-
-/* Block Allocator
- ******************************************/
-
-ParticleAllocator::ParticleAllocator(ParticlesState &state) : m_state(state)
-{
-}
-
-ParticlesBlock &ParticleAllocator::get_non_full_block(StringRef particle_type_name)
-{
-  ParticlesContainer &container = m_state.particle_container(particle_type_name);
-
-  uint index = 0;
-  while (index < m_non_full_cache.size()) {
-    if (m_non_full_cache[index]->unused_amount() == 0) {
-      m_non_full_cache.remove_and_reorder(index);
-      continue;
-    }
-
-    if (m_non_full_cache[index]->container() == container) {
-      return *m_non_full_cache[index];
-    }
-    index++;
-  }
-
-  ParticlesBlock &block = container.new_block();
-  m_non_full_cache.append(&block);
-  m_allocated_blocks.append(&block);
-  return block;
-}
-
-void ParticleAllocator::allocate_block_ranges(StringRef particle_type_name,
-                                              uint size,
-                                              SmallVector<ParticlesBlock *> &r_blocks,
-                                              SmallVector<Range<uint>> &r_ranges)
-{
-  uint remaining_size = size;
-  while (remaining_size > 0) {
-    ParticlesBlock &block = this->get_non_full_block(particle_type_name);
-
-    uint size_to_use = std::min(block.unused_amount(), remaining_size);
-    Range<uint> range(block.active_amount(), block.active_amount() + size_to_use);
-    block.active_amount() += size_to_use;
-
-    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);
-    }
-
-    remaining_size -= size_to_use;
-  }
-}
-
-AttributesInfo &ParticleAllocator::attributes_info(StringRef particle_type_name)
-{
-  return m_state.particle_container(particle_type_name).attributes_info();
-}
-
-ParticleSets ParticleAllocator::request(StringRef particle_type_name, uint size)
-{
-  SmallVector<ParticlesBlock *> blocks;
-  SmallVector<Range<uint>> ranges;
-  this->allocate_block_ranges(particle_type_name, size, blocks, ranges);
-
-  AttributesInfo &attributes_info = this->attributes_info(particle_type_name);
-
-  SmallVector<ParticleSet> sets;
-  for (uint i = 0; i < blocks.size(); i++) {
-    sets.append(ParticleSet(*blocks[i], ranges[i].as_array_ref()));
-  }
-
-  return ParticleSets(particle_type_name, attributes_info, sets);
-}
-
 /* Emitter Interface
  ******************************************/
 
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 9b06e8b6742..3e6e0d2cc2d 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -17,11 +17,11 @@
 #include "particles_container.hpp"
 #include "particle_set.hpp"
 #include "time_span.hpp"
+#include "particle_allocator.hpp"
+#include "particles_state.hpp"
 
 namespace BParticles {
 
-using BLI::StringMap;
-
 class EventFilterInterface;
 class EventExecuteInterface;
 class EmitterInterface;
@@ -207,101 +207,6 @@ class StepDescription {
 /* Classes used by the interface
  ***********************************************/
 
-/**
- * This holds the current state of an entire particle particle system. It only knows about the
- * particles and the current time, not how the system got there.
- *
- * The state can also be created independent of any particle system. It gets "fixed up" when it is
- * used in a simulation.
- */
-class ParticlesState {
- private:
-  StringMap<ParticlesContainer *> m_container_by_id;
-  float m_current_time = 0.0f;
-  uint m_current_step = 0;
-
- public:
-  ParticlesState() = default;
-  ParticlesState(ParticlesState &other) = delete;
-  ~ParticlesState();
-
-  /**
-   * Access the time since the simulation started.
-   */
-  float current_time() const;
-
-  /**
-   * Move current time forward.
-   */
-  void increase_time(float time_step);
-
-  /**
-   * Get the current simulation step.
-   */
-  uint current_step() const;
-
-  /**
-   * Access the mapping from particle type names to their corresponding containers.
-   */
-  StringMap<ParticlesContainer *> &particle_containers();
-
-  /**
-   * Get the container corresponding to a particle type name.
-   * Asserts when the container does not exist.
-   */
-  ParticlesContainer &particle_container(StringRef name);
-
-  /**
-   * Get the name of a container in the context of this particle state.
-   */
-  StringRefNull particle_container_name(ParticlesContainer &container);
-};
-
-/**
- * This class allows allocating new blocks from different particle containers.
- * A single instance is not thread safe, but multiple allocator instances can
- * be used by multiple threads at the same time.
- * It might hand out the same block more than once until it is full.
- */
-class ParticleAllocator {
- private:
-  ParticlesState &m_state;
-  SmallVector<ParticlesBlock *> m_non_full_cache;
-  SmallVector<ParticlesBlock *> m_allocated_blocks;
-
- public:
-  ParticleAllocator(ParticlesState &state);
-  ParticleAllocator(ParticleAllocator &other) = delete;
-  ParticleAllocator(ParticleAllocator &&other) = delete;
-
-  /**
-   * Access all blocks that have been allocated by this allocator.
-   */
-  ArrayRef<ParticlesBlock *> allocated_blocks();
-
-  ParticleSets request(StringRef particle_type_name, uint size);
-
-  ParticlesState &particles_state();
-
- private:
-  /**
-   * Return a block that can hold new particles. It might create an entirely new one or use a
-   * cached block.
-   */
-  ParticlesBlock &get_non_full_block(StringRef particle_type_name);
-
-  /**
-   * Allocate space for a given number of new particles. The attribute buffers might be distributed
-   * over multiple blocks.
-   */
-  void allocate_block_ranges(StringRef particle_type_name,
-                             uint size,
-                             SmallVector<ParticlesBlock *> &r_blocks,
-                             SmallVector<Range<uint>> &r_ranges);
-
-  AttributesInfo &attributes_info(StringRef particle_type_name);
-};
-
 /**
  * The interface between the simulation core and individual emitters.
  */
@@ -538,60 +443,6 @@ class OffsetHandlerInterface {
   TimeSpan time_span(uint pindex);
 };
 
-/* ParticlesState inline functions
- ********************************************/
-
-inline StringMap<ParticlesContainer *> &ParticlesState::particle_containers()
-{
-  return m_container_by_id;
-}
-
-inline ParticlesContainer &ParticlesState::particle_container(StringRef name)
-{
-  return *m_container_by_id.lookup(name.to_std_string());
-}
-
-inline StringRefNull ParticlesState::particle_container_name(ParticlesContainer &container)
-{
-  for (auto item : m_container_by_id.items()) {
-    if (item.value == &container) {
-      return item.key;
-    }
-  }
-  BLI_assert(false);
-  return *(StringRefNull *)nullptr;
-}
-
-inline float ParticlesState::current_time() const
-{
-  return m_current_time;
-}
-
-inline void ParticlesState::increase_time(float time_step)
-{
-  BLI_assert(time_step >= 0.0f);
-  m_current_time += time_step;
-  m_current_step++;
-}
-
-inline uint ParticlesState::current_step() const
-{
-  return m_current_step;
-}
-
-/* ParticleAllocator inline functions
- ********************************************/
-
-inline ParticlesState &ParticleAllocator::particles_state()
-{
-  return m_state;
-}
-
-inline ArrayRef<ParticlesBlock *> ParticleAllocator::allocated_blocks()
-{
-  return m_allocated_blocks;
-}
-
 /* EmitterInterface inline functions
  ***********************************************/
 
diff --git a/source/blender/simulations/bparticles/particle_allocator.cpp b/source/blender/simulations/bparticles/particle_allocator.cpp
new file mode 100644
index 00000000000..50454bdcdcf
--- /dev/null
+++ b/source/blender/simulations/bparticles/particle_allocator.cpp
@@ -0,0 +1,78 @@
+#include "particle_allocator.hpp"
+
+namespace BParticles {
+
+ParticleAllocator::ParticleAllocator(ParticlesState &state) : m_state(state)
+{
+}
+
+ParticlesBlock &ParticleAllocator::get_non_full_block(StringRef particle_type_name)
+{
+  ParticlesContainer &container = m_state.particle_container(particle_type_name);
+
+  uint index = 0;
+  while (index < m_non_full_cache.size()) {
+    if (m_non_full_cache[index]->unused_amount() == 0) {
+      m_non_full_cache.remove_and_reorder(index);
+      continue;
+    }
+
+    if (m_non_full_cache[index]->container() == container) {
+      return *m_non_full_cache[index];
+    }
+    index++;
+  }
+
+  ParticlesBlock &block = container.new_block();
+  m_non_full_cache.append(&block);
+  m_allocated_blocks.append(&block);
+  return block;
+}
+
+void ParticleAllocator::allocate_block_ranges(StringRef particle_type_name,
+                                              uint size,
+                                              SmallVector<ParticlesBlock *> &r_blocks,
+                                              SmallVector<Range<uint>> &r_ranges)
+{
+  uint remaining_size = size;
+  while (remaining_size > 0) {
+    ParticlesBlock &block = this->get_non_full_block(particle_type_name);
+
+    uint size_to_use = std::min(block.unused_amount(), remaining_size);
+    Range<uint> range(block.active_amount(), 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list