2022-02-10 23:07:11 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-03-21 19:31:24 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
2022-03-19 08:26:29 +01:00
|
|
|
* \ingroup bli
|
2021-03-21 19:31:24 +01:00
|
|
|
*
|
2022-03-19 08:26:29 +01:00
|
|
|
* A generic virtual vector array is essentially the same as a virtual vector array, but its data
|
|
|
|
* type is only known at runtime.
|
2021-03-21 19:31:24 +01:00
|
|
|
*/
|
|
|
|
|
2022-03-19 08:26:29 +01:00
|
|
|
#include "BLI_generic_virtual_array.hh"
|
2021-03-21 19:31:24 +01:00
|
|
|
#include "BLI_virtual_vector_array.hh"
|
|
|
|
|
2022-03-19 08:26:29 +01:00
|
|
|
namespace blender {
|
2021-03-21 19:31:24 +01:00
|
|
|
|
|
|
|
/* A generically typed version of `VVectorArray`. */
|
|
|
|
class GVVectorArray {
|
|
|
|
protected:
|
|
|
|
const CPPType *type_;
|
|
|
|
int64_t size_;
|
|
|
|
|
|
|
|
public:
|
2023-03-29 16:50:54 +02:00
|
|
|
GVVectorArray(const CPPType &type, const int64_t size) : type_(&type), size_(size) {}
|
2021-03-21 19:31:24 +01:00
|
|
|
|
|
|
|
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:
|
2022-01-07 01:38:08 +01:00
|
|
|
virtual int64_t get_vector_size_impl(int64_t index) const = 0;
|
2021-03-21 19:31:24 +01:00
|
|
|
|
2022-01-07 01:38:08 +01:00
|
|
|
virtual void get_vector_element_impl(int64_t index,
|
|
|
|
int64_t index_in_vector,
|
2021-03-21 19:31:24 +01:00
|
|
|
void *r_value) const = 0;
|
|
|
|
|
|
|
|
virtual bool is_single_vector_impl() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
class GVArray_For_GVVectorArrayIndex : public GVArrayImpl {
|
2021-03-21 19:31:24 +01:00
|
|
|
private:
|
|
|
|
const GVVectorArray &vector_array_;
|
|
|
|
const int64_t index_;
|
|
|
|
|
|
|
|
public:
|
2021-04-17 15:13:20 +02:00
|
|
|
GVArray_For_GVVectorArrayIndex(const GVVectorArray &vector_array, const int64_t index)
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
: GVArrayImpl(vector_array.type(), vector_array.get_vector_size(index)),
|
2021-03-21 19:31:24 +01:00
|
|
|
vector_array_(vector_array),
|
|
|
|
index_(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-01-07 01:38:08 +01:00
|
|
|
void get(int64_t index_in_vector, void *r_value) const override;
|
|
|
|
void get_to_uninitialized(int64_t index_in_vector, void *r_value) const override;
|
2021-03-21 19:31:24 +01:00
|
|
|
};
|
|
|
|
|
2021-04-17 15:13:20 +02:00
|
|
|
class GVVectorArray_For_SingleGVArray : public GVVectorArray {
|
2021-03-21 19:31:24 +01:00
|
|
|
private:
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
GVArray varray_;
|
2021-03-21 19:31:24 +01:00
|
|
|
|
|
|
|
public:
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
GVVectorArray_For_SingleGVArray(GVArray varray, const int64_t size)
|
|
|
|
: GVVectorArray(varray.type(), size), varray_(std::move(varray))
|
2021-03-21 19:31:24 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-01-07 01:38:08 +01:00
|
|
|
int64_t get_vector_size_impl(int64_t index) const override;
|
|
|
|
void get_vector_element_impl(int64_t index,
|
|
|
|
int64_t index_in_vector,
|
2021-03-21 19:31:24 +01:00
|
|
|
void *r_value) const override;
|
|
|
|
|
|
|
|
bool is_single_vector_impl() const override;
|
|
|
|
};
|
|
|
|
|
2021-04-17 15:13:20 +02:00
|
|
|
class GVVectorArray_For_SingleGSpan : public GVVectorArray {
|
2021-03-21 19:31:24 +01:00
|
|
|
private:
|
|
|
|
const GSpan span_;
|
|
|
|
|
|
|
|
public:
|
2021-04-17 15:13:20 +02:00
|
|
|
GVVectorArray_For_SingleGSpan(const GSpan span, const int64_t size)
|
2021-03-21 19:31:24 +01:00
|
|
|
: GVVectorArray(span.type(), size), span_(span)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-10-04 00:37:25 +02:00
|
|
|
int64_t get_vector_size_impl(int64_t /*index*/) const override;
|
|
|
|
void get_vector_element_impl(int64_t /*index*/,
|
2022-01-07 01:38:08 +01:00
|
|
|
int64_t index_in_vector,
|
2021-03-21 19:31:24 +01:00
|
|
|
void *r_value) const override;
|
|
|
|
|
|
|
|
bool is_single_vector_impl() const override;
|
|
|
|
};
|
|
|
|
|
2021-04-17 15:13:20 +02:00
|
|
|
template<typename T> class VVectorArray_For_GVVectorArray : public VVectorArray<T> {
|
2021-03-21 19:31:24 +01:00
|
|
|
private:
|
|
|
|
const GVVectorArray &vector_array_;
|
|
|
|
|
|
|
|
public:
|
2021-04-17 15:13:20 +02:00
|
|
|
VVectorArray_For_GVVectorArray(const GVVectorArray &vector_array)
|
2021-03-21 19:31:24 +01:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
2021-03-21 19:49:29 +01:00
|
|
|
bool is_single_vector_impl() const override
|
2021-03-21 19:31:24 +01:00
|
|
|
{
|
|
|
|
return vector_array_.is_single_vector();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-19 08:26:29 +01:00
|
|
|
} // namespace blender
|