Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-03-21 21:31:24 +0300
committerJacques Lucke <jacques@blender.org>2021-03-21 21:33:13 +0300
commit4fe8d0419c2f080a248f52b3924ce2a4e897e5cb (patch)
tree81aba45e26cca99578087835184ce5664362b791 /source/blender/functions/FN_generic_virtual_vector_array.hh
parent68c31c41e52caa1ac5b527f835b16f8e298dfd86 (diff)
Functions: refactor virtual array data structures
When a function is executed for many elements (e.g. per point) it is often the case that some parameters are different for every element and other parameters are the same (there are some more less common cases). To simplify writing such functions one can use a "virtual array". This is a data structure that has a value for every index, but might not be stored as an actual array internally. Instead, it might be just a single value or is computed on the fly. There are various tradeoffs involved when using this data structure which are mentioned in `BLI_virtual_array.hh`. It is called "virtual", because it uses inheritance and virtual methods. Furthermore, there is a new virtual vector array data structure, which is an array of vectors. Both these types have corresponding generic variants, which can be used when the data type is not known at compile time. This is typically the case when building a somewhat generic execution system. The function system used these virtual data structures before, but now they are more versatile. I've done this refactor in preparation for the attribute processor and other features of geometry nodes. I moved the typed virtual arrays to blenlib, so that they can be used independent of the function system. One open question for me is whether all the generic data structures (and `CPPType`) should be moved to blenlib as well. They are well isolated and don't really contain any business logic. That can be done later if necessary.
Diffstat (limited to 'source/blender/functions/FN_generic_virtual_vector_array.hh')
-rw-r--r--source/blender/functions/FN_generic_virtual_vector_array.hh188
1 files changed, 188 insertions, 0 deletions
diff --git a/source/blender/functions/FN_generic_virtual_vector_array.hh b/source/blender/functions/FN_generic_virtual_vector_array.hh
new file mode 100644
index 00000000000..22d1e5641e5
--- /dev/null
+++ b/source/blender/functions/FN_generic_virtual_vector_array.hh
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+/** \file
+ * \ingroup fn
+ *
+ * A generic virtual vector array is essentially the same as a virtual vector array from blenlib,
+ * but its data type is only known at runtime.
+ */
+
+#include "FN_generic_virtual_array.hh"
+
+#include "BLI_virtual_vector_array.hh"
+
+namespace blender::fn {
+
+/* A generically typed version of `VVectorArray`. */
+class GVVectorArray {
+ protected:
+ const CPPType *type_;
+ int64_t size_;
+
+ public:
+ GVVectorArray(const CPPType &type, const int64_t size) : type_(&type), size_(size)
+ {
+ }
+
+ virtual ~GVVectorArray() = default;
+
+ /* Returns the number of vectors in the vector array. */
+ int64_t size() const
+ {
+ return size_;
+ }
+
+ /* Returns true when there is no vector in the vector array. */
+ bool is_empty() const
+ {
+ return size_ == 0;
+ }
+
+ const CPPType &type() const
+ {
+ return *type_;
+ }
+
+ /* Returns the size of the vector at the given index. */
+ int64_t get_vector_size(const int64_t index) const
+ {
+ BLI_assert(index >= 0);
+ BLI_assert(index < size_);
+ return this->get_vector_size_impl(index);
+ }
+
+ /* Copies an element from one of the vectors into `r_value`, which is expected to point to
+ * initialized memory. */
+ void get_vector_element(const int64_t index, const int64_t index_in_vector, void *r_value) const
+ {
+ BLI_assert(index >= 0);
+ BLI_assert(index < size_);
+ BLI_assert(index_in_vector >= 0);
+ BLI_assert(index_in_vector < this->get_vector_size(index));
+ this->get_vector_element_impl(index, index_in_vector, r_value);
+ }
+
+ /* Returns true when the same vector is used at every index. */
+ bool is_single_vector() const
+ {
+ if (size_ == 1) {
+ return true;
+ }
+ return this->is_single_vector_impl();
+ }
+
+ protected:
+ virtual int64_t get_vector_size_impl(const int64_t index) const = 0;
+
+ virtual void get_vector_element_impl(const int64_t index,
+ const int64_t index_in_vector,
+ void *r_value) const = 0;
+
+ virtual bool is_single_vector_impl() const
+ {
+ return false;
+ }
+};
+
+class GVArrayForGVVectorArrayIndex : public GVArray {
+ private:
+ const GVVectorArray &vector_array_;
+ const int64_t index_;
+
+ public:
+ GVArrayForGVVectorArrayIndex(const GVVectorArray &vector_array, const int64_t index)
+ : GVArray(vector_array.type(), vector_array.get_vector_size(index)),
+ vector_array_(vector_array),
+ index_(index)
+ {
+ }
+
+ protected:
+ void get_impl(const int64_t index_in_vector, void *r_value) const override;
+ void get_to_uninitialized_impl(const int64_t index_in_vector, void *r_value) const override;
+};
+
+class GVVectorArrayForSingleGVArray : public GVVectorArray {
+ private:
+ const GVArray &array_;
+
+ public:
+ GVVectorArrayForSingleGVArray(const GVArray &array, const int64_t size)
+ : GVVectorArray(array.type(), size), array_(array)
+ {
+ }
+
+ protected:
+ int64_t get_vector_size_impl(const int64_t index) const override;
+ void get_vector_element_impl(const int64_t index,
+ const int64_t index_in_vector,
+ void *r_value) const override;
+
+ bool is_single_vector_impl() const override;
+};
+
+class GVVectorArrayForSingleGSpan : public GVVectorArray {
+ private:
+ const GSpan span_;
+
+ public:
+ GVVectorArrayForSingleGSpan(const GSpan span, const int64_t size)
+ : GVVectorArray(span.type(), size), span_(span)
+ {
+ }
+
+ protected:
+ int64_t get_vector_size_impl(const int64_t UNUSED(index)) const override;
+ void get_vector_element_impl(const int64_t UNUSED(index),
+ const int64_t index_in_vector,
+ void *r_value) const override;
+
+ bool is_single_vector_impl() const override;
+};
+
+template<typename T> class VVectorArrayForGVVectorArray : public VVectorArray<T> {
+ private:
+ const GVVectorArray &vector_array_;
+
+ public:
+ VVectorArrayForGVVectorArray(const GVVectorArray &vector_array)
+ : VVectorArray<T>(vector_array.size()), vector_array_(vector_array)
+ {
+ }
+
+ protected:
+ int64_t get_vector_size_impl(const int64_t index) const override
+ {
+ return vector_array_.get_vector_size(index);
+ }
+
+ T get_vector_element_impl(const int64_t index, const int64_t index_in_vector) const override
+ {
+ T value;
+ vector_array_.get_vector_element(index, index_in_vector, &value);
+ return value;
+ }
+
+ bool is_single_vector_impl() const
+ {
+ return vector_array_.is_single_vector();
+ }
+};
+
+} // namespace blender::fn