[Bf-blender-cvs] [dcd84a10519] functions: remove limit for index range arrays

Jacques Lucke noreply at git.blender.org
Mon Sep 9 18:02:08 CEST 2019


Commit: dcd84a10519ef6d576e1ffa7ec2bf808267ca225
Author: Jacques Lucke
Date:   Mon Sep 9 17:58:43 2019 +0200
Branches: functions
https://developer.blender.org/rBdcd84a10519ef6d576e1ffa7ec2bf808267ca225

remove limit for index range arrays

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

M	source/blender/blenlib/BLI_index_range.hpp
M	source/blender/blenlib/BLI_vector.hpp
M	source/blender/blenlib/intern/BLI_index_range.cpp
M	source/blender/simulations/bparticles/node_frontend.cpp

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

diff --git a/source/blender/blenlib/BLI_index_range.hpp b/source/blender/blenlib/BLI_index_range.hpp
index 270686ed611..96c3d22c4e5 100644
--- a/source/blender/blenlib/BLI_index_range.hpp
+++ b/source/blender/blenlib/BLI_index_range.hpp
@@ -30,8 +30,6 @@
 
 #include "BLI_utildefines.h"
 
-#define RANGE_AS_ARRAY_REF_MAX_LEN 10000
-
 namespace BLI {
 
 template<typename T> class ArrayRef;
@@ -181,8 +179,7 @@ class IndexRange {
   }
 
   /**
-   * Get read-only access to a memory buffer that contains the range as actual numbers. This only
-   * works for some ranges. The range must be within [0, RANGE_AS_ARRAY_REF_MAX_LEN].
+   * Get read-only access to a memory buffer that contains the range as actual numbers.
    */
   ArrayRef<uint> as_array_ref() const;
 
diff --git a/source/blender/blenlib/BLI_vector.hpp b/source/blender/blenlib/BLI_vector.hpp
index 6d2765ae66d..4c32f2c9746 100644
--- a/source/blender/blenlib/BLI_vector.hpp
+++ b/source/blender/blenlib/BLI_vector.hpp
@@ -303,6 +303,12 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     return *(m_end - 1);
   }
 
+  T &last()
+  {
+    BLI_assert(this->size() > 0);
+    return *(m_end - 1);
+  }
+
   /**
    * Replace every element with a new value.
    */
diff --git a/source/blender/blenlib/intern/BLI_index_range.cpp b/source/blender/blenlib/intern/BLI_index_range.cpp
index db7e14c4a89..662272527f9 100644
--- a/source/blender/blenlib/intern/BLI_index_range.cpp
+++ b/source/blender/blenlib/intern/BLI_index_range.cpp
@@ -14,24 +14,46 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#include <mutex>
+
 #include "BLI_index_range.hpp"
 #include "BLI_array_ref.hpp"
+#include "BLI_array.hpp"
+#include "BLI_vector.hpp"
 
 namespace BLI {
 
-static bool array_is_initialized = false;
-static uint array[RANGE_AS_ARRAY_REF_MAX_LEN];
+static Vector<Array<uint, RawAllocator>, 0, RawAllocator> arrays;
+static uint current_array_size = 0;
+static uint *current_array = nullptr;
+static std::mutex current_array_mutex;
 
 ArrayRef<uint> IndexRange::as_array_ref() const
 {
-  BLI_assert(m_size <= RANGE_AS_ARRAY_REF_MAX_LEN);
-  if (!array_is_initialized) {
-    for (uint i = 0; i < RANGE_AS_ARRAY_REF_MAX_LEN; i++) {
-      array[i] = i;
-    }
-    array_is_initialized = true;
+  uint min_required_size = m_start + m_size;
+
+  if (min_required_size <= current_array_size) {
+    return ArrayRef<uint>(current_array + m_start, m_size);
+  }
+
+  std::lock_guard<std::mutex> lock(current_array_mutex);
+
+  if (min_required_size <= current_array_size) {
+    return ArrayRef<uint>(current_array + m_start, m_size);
   }
-  return ArrayRef<uint>(array + m_start, this->size());
+
+  uint new_size = std::max<uint>(1000, power_of_2_max_u(min_required_size));
+  Array<uint, RawAllocator> new_array(new_size);
+  for (uint i = 0; i < new_size; i++) {
+    new_array[i] = i;
+  }
+  arrays.append(std::move(new_array));
+
+  current_array = arrays.last().begin();
+  std::atomic_thread_fence(std::memory_order_seq_cst);
+  current_array_size = new_size;
+
+  return ArrayRef<uint>(current_array + m_start, m_size);
 }
 
 }  // namespace BLI
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index b3dc9500055..c965cd95265 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -330,10 +330,8 @@ static Vector<float> compute_emitter_vertex_weights(VirtualNode *vnode,
     float4x4 transform = object->obmat;
 
     TemporaryArray<float3> vertex_positions(mesh->totvert);
-    TemporaryVector<uint> indices(mesh->totvert);
     for (uint i = 0; i < mesh->totvert; i++) {
       vertex_positions[i] = transform.transform_position(mesh->mvert[i].co);
-      indices[i] = i;
     }
     AttributesDeclaration info_declaration;
     info_declaration.add<float3>("Position", {0, 0, 0});
@@ -341,7 +339,7 @@ static Vector<float> compute_emitter_vertex_weights(VirtualNode *vnode,
 
     std::array<void *, 1> buffers = {(void *)vertex_positions.begin()};
     AttributesRef attributes{info, buffers, (uint)mesh->totvert};
-    falloff->compute(attributes, indices, vertex_weights);
+    falloff->compute(attributes, IndexRange(mesh->totvert).as_array_ref(), vertex_weights);
   }
 
   return vertex_weights;



More information about the Bf-blender-cvs mailing list