[Bf-blender-cvs] [882e7e49031] functions: cleanup usage of temporary allocator

Jacques Lucke noreply at git.blender.org
Thu Sep 5 19:12:02 CEST 2019


Commit: 882e7e49031dc22d49b0f20af299dbc982ca4f15
Author: Jacques Lucke
Date:   Thu Sep 5 15:17:43 2019 +0200
Branches: functions
https://developer.blender.org/rB882e7e49031dc22d49b0f20af299dbc982ca4f15

cleanup usage of temporary allocator

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

M	source/blender/blenlib/BLI_array_ref.hpp
A	source/blender/blenlib/BLI_temporary_allocator.hpp
M	source/blender/blenlib/CMakeLists.txt
M	source/blender/simulations/bparticles/action_contexts.cpp
M	source/blender/simulations/bparticles/action_contexts.hpp
M	source/blender/simulations/bparticles/particle_function_input_providers.cpp

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

diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index 582a07b0dbb..5c8501e540f 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -405,6 +405,11 @@ template<typename T> class MutableArrayRef {
     BLI_assert(n <= this->size());
     return this->slice(this->size() - n, n);
   }
+
+  ArrayRef<T> as_ref() const
+  {
+    return ArrayRef<T>(m_start, m_size);
+  }
 };
 
 /**
diff --git a/source/blender/blenlib/BLI_temporary_allocator.hpp b/source/blender/blenlib/BLI_temporary_allocator.hpp
new file mode 100644
index 00000000000..ea18f3b053a
--- /dev/null
+++ b/source/blender/blenlib/BLI_temporary_allocator.hpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#include "BLI_temporary_allocator.h"
+
+namespace BLI {
+
+template<typename T> class MutableArrayRef;
+
+template<typename T> MutableArrayRef<T> temporary_allocate_array(uint size)
+{
+  void *ptr = BLI_temporary_allocate(sizeof(T) * size);
+  return MutableArrayRef<T>((T *)ptr, size);
+}
+
+};  // namespace BLI
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 823c2066a37..6f501854be0 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -261,6 +261,7 @@ set(SRC
   BLI_string_map.hpp
   BLI_string_ref.hpp
   BLI_temporary_allocator.h
+  BLI_temporary_allocator.hpp
   intern/BLI_temporary_allocator.cpp
   BLI_timeit.hpp
   BLI_vector_adaptor.hpp
diff --git a/source/blender/simulations/bparticles/action_contexts.cpp b/source/blender/simulations/bparticles/action_contexts.cpp
index 39d626c0aae..a62cbaaa033 100644
--- a/source/blender/simulations/bparticles/action_contexts.cpp
+++ b/source/blender/simulations/bparticles/action_contexts.cpp
@@ -14,7 +14,7 @@ void MeshSurfaceContext::compute_barycentric_coords(ArrayRef<uint> pindices)
 {
   BLI_assert(m_object->type == OB_MESH);
   uint size = m_local_positions.size();
-  float3 *barycentric_coords_buffer = (float3 *)BLI_temporary_allocate(sizeof(float3) * size);
+  auto barycentric_coords = BLI::temporary_allocate_array<float3>(size);
 
   Mesh *mesh = (Mesh *)m_object->data;
   const MLoopTri *triangles = BKE_mesh_runtime_looptri_ensure(mesh);
@@ -31,11 +31,11 @@ void MeshSurfaceContext::compute_barycentric_coords(ArrayRef<uint> pindices)
     float3 weights;
     interp_weights_tri_v3(weights, v1, v2, v3, position);
 
-    barycentric_coords_buffer[pindex] = weights;
+    barycentric_coords[pindex] = weights;
   }
 
-  m_barycentric_coords = ArrayRef<float3>(barycentric_coords_buffer, size);
-  m_buffers_to_free.append(barycentric_coords_buffer);
+  m_barycentric_coords = barycentric_coords;
+  m_buffers_to_free.append(barycentric_coords.begin());
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/action_contexts.hpp b/source/blender/simulations/bparticles/action_contexts.hpp
index 465723a8353..d34df4a4f2f 100644
--- a/source/blender/simulations/bparticles/action_contexts.hpp
+++ b/source/blender/simulations/bparticles/action_contexts.hpp
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "BLI_temporary_allocator.hpp"
+
 #include "action_interface.hpp"
 
 struct Object;
@@ -52,23 +54,22 @@ class MeshSurfaceContext : public ActionContext {
         m_looptri_indices(looptri_indices)
   {
     uint size = local_positions.size();
-    float4x4 *world_transforms_buffer = (float4x4 *)BLI_temporary_allocate(sizeof(float4x4) *
-                                                                           size);
-    float3 *world_normals_buffer = (float3 *)BLI_temporary_allocate(sizeof(float3) * size);
-    float3 *surface_velocities_buffer = (float3 *)BLI_temporary_allocate(sizeof(float3) * size);
+    auto world_transforms = BLI::temporary_allocate_array<float4x4>(size);
+    auto world_normals = BLI::temporary_allocate_array<float3>(size);
+    auto surface_velocities = BLI::temporary_allocate_array<float3>(size);
 
     for (uint pindex : pindices) {
-      world_transforms_buffer[pindex] = world_transform;
-      world_normals_buffer[pindex] = local_normals[pindex];
-      surface_velocities_buffer[pindex] = float3(0, 0, 0);
+      world_transforms[pindex] = world_transform;
+      world_normals[pindex] = world_transform.transform_direction(local_normals[pindex]);
+      surface_velocities[pindex] = float3(0, 0, 0);
     }
 
-    m_world_transforms = ArrayRef<float4x4>(world_transforms_buffer, size);
-    m_world_normals = ArrayRef<float3>(world_normals_buffer, size);
-    m_world_surface_velocities = ArrayRef<float3>(surface_velocities_buffer, size);
+    m_world_transforms = world_transforms;
+    m_world_normals = world_normals;
+    m_world_surface_velocities = surface_velocities;
 
     m_buffers_to_free.extend(
-        {world_transforms_buffer, world_normals_buffer, surface_velocities_buffer});
+        {world_transforms.begin(), world_normals.begin(), surface_velocities.begin()});
 
     this->compute_barycentric_coords(pindices);
   }
diff --git a/source/blender/simulations/bparticles/particle_function_input_providers.cpp b/source/blender/simulations/bparticles/particle_function_input_providers.cpp
index c420570e2a7..cb9e09afc4e 100644
--- a/source/blender/simulations/bparticles/particle_function_input_providers.cpp
+++ b/source/blender/simulations/bparticles/particle_function_input_providers.cpp
@@ -56,8 +56,7 @@ Optional<ParticleFunctionInputArray> SurfaceVelocityInputProvider::get(
 Optional<ParticleFunctionInputArray> AgeInputProvider::get(InputProviderInterface &interface)
 {
   auto birth_times = interface.attributes().get<float>("Birth Time");
-  float *ages_buffer = (float *)BLI_temporary_allocate(sizeof(float) * birth_times.size());
-  MutableArrayRef<float> ages(ages_buffer, birth_times.size());
+  auto ages = BLI::temporary_allocate_array<float>(birth_times.size());
 
   ParticleTimes &times = interface.particle_times();
   if (times.type() == ParticleTimes::Type::Current) {
@@ -76,7 +75,7 @@ Optional<ParticleFunctionInputArray> AgeInputProvider::get(InputProviderInterfac
   else {
     BLI_assert(false);
   }
-  return ParticleFunctionInputArray(ArrayRef<float>(ages), true);
+  return ParticleFunctionInputArray(ages.as_ref(), true);
 }
 
 SurfaceImageInputProvider::SurfaceImageInputProvider(Image *image) : m_image(image)
@@ -130,8 +129,7 @@ Optional<ParticleFunctionInputArray> SurfaceImageInputProvider::compute_colors(
   rgba_b *pixel_buffer = (rgba_b *)m_ibuf->rect;
 
   uint size = interface.attributes().size();
-  rgba_f *colors_buffer = (rgba_f *)BLI_temporary_allocate(sizeof(rgba_f) * size);
-  MutableArrayRef<rgba_f> colors{colors_buffer, size};
+  auto colors = BLI::temporary_allocate_array<rgba_f>(size);
 
   for (uint pindex : interface.pindices()) {
     uint source_index = surface_info_mapping[pindex];
@@ -151,7 +149,7 @@ Optional<ParticleFunctionInputArray> SurfaceImageInputProvider::compute_colors(
     uint y = uv.y * (m_ibuf->y - 1);
     colors[pindex] = pixel_buffer[y * m_ibuf->x + x];
   }
-  return ParticleFunctionInputArray(ArrayRef<rgba_f>(colors), true);
+  return ParticleFunctionInputArray(colors.as_ref(), true);
 }
 
 Optional<ParticleFunctionInputArray> VertexWeightInputProvider::get(
@@ -192,8 +190,7 @@ Optional<ParticleFunctionInputArray> VertexWeightInputProvider::compute_weights(
   ArrayRef<float3> barycentric_coords = surface_info->barycentric_coords();
 
   uint size = interface.attributes().size();
-  float *weight_buffer = (float *)BLI_temporary_allocate(sizeof(float) * size);
-  MutableArrayRef<float> weights(weight_buffer, size);
+  auto weights = BLI::temporary_allocate_array<float>(size);
 
   for (uint pindex : interface.pindices()) {
     uint source_index = surface_info_mapping[pindex];
@@ -214,7 +211,7 @@ Optional<ParticleFunctionInputArray> VertexWeightInputProvider::compute_weights(
     weights[pindex] = weight;
   }
 
-  return ParticleFunctionInputArray(ArrayRef<float>(weights), true);
+  return ParticleFunctionInputArray(weights.as_ref(), true);
 }
 
 }  // namespace BParticles



More information about the Bf-blender-cvs mailing list