[Bf-blender-cvs] [4365de38700] master: Functions: Multi Function

Jacques Lucke noreply at git.blender.org
Tue Jun 16 16:42:22 CEST 2020


Commit: 4365de38700ccc05f98f601efdde0963de4645c1
Author: Jacques Lucke
Date:   Tue Jun 16 16:35:57 2020 +0200
Branches: master
https://developer.blender.org/rB4365de38700ccc05f98f601efdde0963de4645c1

Functions: Multi Function

This adds the `MultiFunction` type and some smallish utility types that it uses.
A `MultiFunction` encapsulates a function that is optimized for throughput by
always processing many elements at once.

This is an important part of the new particle system, because it allows us to
execute user generated node trees for many particles efficiently.

Reviewers: brecht

Differential Revision: https://developer.blender.org/D8030

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

M	source/blender/functions/CMakeLists.txt
A	source/blender/functions/FN_array_spans.hh
M	source/blender/functions/FN_cpp_type.hh
M	source/blender/functions/FN_cpp_types.hh
A	source/blender/functions/FN_generic_vector_array.hh
A	source/blender/functions/FN_multi_function.hh
A	source/blender/functions/FN_multi_function_context.hh
A	source/blender/functions/FN_multi_function_data_type.hh
A	source/blender/functions/FN_multi_function_param_type.hh
A	source/blender/functions/FN_multi_function_params.hh
A	source/blender/functions/FN_multi_function_signature.hh
A	source/blender/functions/FN_spans.hh
M	source/blender/functions/intern/cpp_types.cc
M	tests/gtests/functions/CMakeLists.txt
A	tests/gtests/functions/FN_array_spans_test.cc
M	tests/gtests/functions/FN_cpp_type_test.cc
A	tests/gtests/functions/FN_generic_vector_array_test.cc
A	tests/gtests/functions/FN_multi_function_test.cc
A	tests/gtests/functions/FN_spans_test.cc

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

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 9ce1d3ac2fe..dce7eb71376 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -29,8 +29,16 @@ set(INC_SYS
 set(SRC
   intern/cpp_types.cc
 
+  FN_array_spans.hh
   FN_cpp_type.hh
   FN_cpp_types.hh
+  FN_multi_function.hh
+  FN_multi_function_context.hh
+  FN_multi_function_data_type.hh
+  FN_multi_function_param_type.hh
+  FN_multi_function_params.hh
+  FN_multi_function_signature.hh
+  FN_spans.hh
 )
 
 set(LIB
diff --git a/source/blender/functions/FN_array_spans.hh b/source/blender/functions/FN_array_spans.hh
new file mode 100644
index 00000000000..ca818ad6b11
--- /dev/null
+++ b/source/blender/functions/FN_array_spans.hh
@@ -0,0 +1,220 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __FN_ARRAY_SPANS_HH__
+#define __FN_ARRAY_SPANS_HH__
+
+/** \file
+ * \ingroup fn
+ *
+ * An ArraySpan is a span where every element contains an array (instead of a single element as is
+ * the case in a normal span). It's main use case is to reference many small arrays.
+ */
+
+#include "FN_spans.hh"
+
+namespace blender {
+namespace fn {
+
+/**
+ * A virtual array span. Every element of this span contains a virtual span. So it behaves like a
+ * blender::Span, but might not be backed up by an actual array.
+ */
+template<typename T> class VArraySpan {
+ private:
+  /**
+   * Depending on the use case, the referenced data might have a different structure. More
+   * categories can be added when necessary.
+   */
+  enum Category {
+    SingleArray,
+    StartsAndSizes,
+  };
+
+  uint m_virtual_size;
+  Category m_category;
+
+  union {
+    struct {
+      const T *start;
+      uint size;
+    } single_array;
+    struct {
+      const T *const *starts;
+      const uint *sizes;
+    } starts_and_sizes;
+  } m_data;
+
+ public:
+  VArraySpan()
+  {
+    m_virtual_size = 0;
+    m_category = StartsAndSizes;
+    m_data.starts_and_sizes.starts = nullptr;
+    m_data.starts_and_sizes.sizes = nullptr;
+  }
+
+  VArraySpan(Span<T> span, uint virtual_size)
+  {
+    m_virtual_size = virtual_size;
+    m_category = SingleArray;
+    m_data.single_array.start = span.data();
+    m_data.single_array.size = span.size();
+  }
+
+  VArraySpan(Span<const T *> starts, Span<uint> sizes)
+  {
+    BLI_assert(starts.size() == sizes.size());
+    m_virtual_size = starts.size();
+    m_category = StartsAndSizes;
+    m_data.starts_and_sizes.starts = starts.begin();
+    m_data.starts_and_sizes.sizes = sizes.begin();
+  }
+
+  bool is_empty() const
+  {
+    return m_virtual_size == 0;
+  }
+
+  uint size() const
+  {
+    return m_virtual_size;
+  }
+
+  VSpan<T> operator[](uint index) const
+  {
+    BLI_assert(index < m_virtual_size);
+    switch (m_category) {
+      case SingleArray:
+        return VSpan<T>(Span<T>(m_data.single_array.start, m_data.single_array.size));
+      case StartsAndSizes:
+        return VSpan<T>(
+            Span<T>(m_data.starts_and_sizes.starts[index], m_data.starts_and_sizes.sizes[index]));
+    }
+    BLI_assert(false);
+    return {};
+  }
+};
+
+/**
+ * A generic virtual array span. It's just like a VArraySpan, but the type is only known at
+ * run-time.
+ */
+class GVArraySpan {
+ private:
+  /**
+   * Depending on the use case, the referenced data might have a different structure. More
+   * categories can be added when necessary.
+   */
+  enum Category {
+    SingleArray,
+    StartsAndSizes,
+  };
+
+  const CPPType *m_type;
+  uint m_virtual_size;
+  Category m_category;
+
+  union {
+    struct {
+      const void *values;
+      uint size;
+    } single_array;
+    struct {
+      const void *const *starts;
+      const uint *sizes;
+    } starts_and_sizes;
+  } m_data;
+
+  GVArraySpan() = default;
+
+ public:
+  GVArraySpan(const CPPType &type)
+  {
+    m_type = &type;
+    m_virtual_size = 0;
+    m_category = StartsAndSizes;
+    m_data.starts_and_sizes.starts = nullptr;
+    m_data.starts_and_sizes.sizes = nullptr;
+  }
+
+  GVArraySpan(GSpan array, uint virtual_size)
+  {
+    m_type = &array.type();
+    m_virtual_size = virtual_size;
+    m_category = SingleArray;
+    m_data.single_array.values = array.buffer();
+    m_data.single_array.size = array.size();
+  }
+
+  GVArraySpan(const CPPType &type, Span<const void *> starts, Span<uint> sizes)
+  {
+    BLI_assert(starts.size() == sizes.size());
+    m_type = &type;
+    m_virtual_size = starts.size();
+    m_category = StartsAndSizes;
+    m_data.starts_and_sizes.starts = starts.begin();
+    m_data.starts_and_sizes.sizes = sizes.begin();
+  }
+
+  bool is_empty() const
+  {
+    return m_virtual_size == 0;
+  }
+
+  uint size() const
+  {
+    return m_virtual_size;
+  }
+
+  const CPPType &type() const
+  {
+    return *m_type;
+  }
+
+  template<typename T> VArraySpan<T> typed() const
+  {
+    BLI_assert(CPPType::get<T>() == *m_type);
+    switch (m_category) {
+      case SingleArray:
+        return VArraySpan<T>(
+            Span<T>((const T *)m_data.single_array.values, m_data.single_array.size));
+      case StartsAndSizes:
+        return VArraySpan<T>(
+            Span<const T *>((const T *const *)m_data.starts_and_sizes.starts, m_virtual_size),
+            Span<uint>(m_data.starts_and_sizes.sizes, m_virtual_size));
+    }
+  }
+
+  GVSpan operator[](uint index) const
+  {
+    BLI_assert(index < m_virtual_size);
+    switch (m_category) {
+      case SingleArray:
+        return GVSpan(GSpan(*m_type, m_data.single_array.values, m_data.single_array.size));
+      case StartsAndSizes:
+        return GVSpan(GSpan(
+            *m_type, m_data.starts_and_sizes.starts[index], m_data.starts_and_sizes.sizes[index]));
+    }
+    BLI_assert(false);
+    return GVSpan(*m_type);
+  }
+};
+
+}  // namespace fn
+}  // namespace blender
+
+#endif /* __FN_ARRAY_SPANS_HH__ */
diff --git a/source/blender/functions/FN_cpp_type.hh b/source/blender/functions/FN_cpp_type.hh
index 1dc72e16e77..df5218ed5ba 100644
--- a/source/blender/functions/FN_cpp_type.hh
+++ b/source/blender/functions/FN_cpp_type.hh
@@ -71,11 +71,7 @@
 #include "BLI_string_ref.hh"
 
 namespace blender {
-namespace FN {
-
-using blender::IndexMask;
-using blender::StringRef;
-using blender::StringRefNull;
+namespace fn {
 
 class CPPType {
  public:
@@ -241,14 +237,14 @@ class CPPType {
 
   void construct_default_n(void *ptr, uint n) const
   {
-    BLI_assert(this->pointer_can_point_to_instance(ptr));
+    BLI_assert(this->pointer_has_valid_alignment(ptr));
 
     m_construct_default_n(ptr, n);
   }
 
   void construct_default_indices(void *ptr, IndexMask index_mask) const
   {
-    BLI_assert(this->pointer_can_point_to_instance(ptr));
+    BLI_assert(this->pointer_has_valid_alignment(ptr));
 
     m_construct_default_indices(ptr, index_mask);
   }
@@ -270,14 +266,14 @@ class CPPType {
 
   void destruct_n(void *ptr, uint n) const
   {
-    BLI_assert(this->pointer_can_point_to_instance(ptr));
+    BLI_assert(this->pointer_has_valid_alignment(ptr));
 
     m_destruct_n(ptr, n);
   }
 
   void destruct_indices(void *ptr, IndexMask index_mask) const
   {
-    BLI_assert(this->pointer_can_point_to_instance(ptr));
+    BLI_assert(this->pointer_has_valid_alignment(ptr));
 
     m_destruct_indices(ptr, index_mask);
   }
@@ -300,8 +296,8 @@ class CPPType {
   void copy_to_initialized_n(const void *src, void *dst, uint n) const
   {
     BLI_assert(src != dst);
-    BLI_assert(this->pointer_can_point_to_instance(src));
-    BLI_assert(this->pointer_can_point_to_instance(dst));
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
 
     m_copy_to_initialized_n(src, dst, n);
   }
@@ -309,8 +305,8 @@ class CPPType {
   void copy_to_initialized_indices(const void *src, void *dst, IndexMask index_mask) const
   {
     BLI_assert(src != dst);
-    BLI_assert(this->pointer_can_point_to_instance(src));
-    BLI_assert(this->pointer_can_point_to_instance(dst));
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
 
     m_copy_to_initialized_indices(src, dst, index_mask);
   }
@@ -335,8 +331,8 @@ class CPPType {
   void copy_to_uninitialized_n(const void *src, void *dst, uint n) const
   {
     BLI_assert(src != dst);
-    BLI_assert(this->pointer_can_point_to_instance(src));
-    BLI_assert(this->pointer_can_point_to_instance(dst));
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
 
     m_copy_to_uninitialized_n(src, dst, n);
   }
@@ -344,8 +340,8 @@ class CPPType {
   void copy_to_uninitialized_indices(const void *src, void *dst, IndexMask index_mask) const
   {
     BLI_assert(src != dst);
-    BLI_assert(this->pointer_can_point_to_instance(src));
-    BLI_assert(this->pointer_can_point_to_instance(dst));
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
 
     m_copy_to_uninitialized_indices(src, dst, index_mask);
   }
@@ -370,8 +366,8 @@ class CPPType {
   void relocate_to_initialized_n(void *src, void *dst, uint n) const
   {
     BLI_assert(src != dst);
-    BLI_assert(this->pointer_can_point_to_instance(src));
-    BLI_assert(this->pointer_can_point_to_instance(dst));
+    BLI_assert(this->pointer_has_valid_alignment(src));
+    BLI_assert(this->pointer_has_valid_alignment(dst));
 
     m_relocate_to_initialized_n(src, dst, n);


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list