2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \ingroup fn
|
|
|
|
*
|
|
|
|
* This file provides an MFParams and MFParamsBuilder structure.
|
|
|
|
*
|
|
|
|
* `MFParamsBuilder` is used by a function caller to be prepare all parameters that are passed into
|
|
|
|
* the function. `MFParams` is then used inside the called function to access the parameters.
|
|
|
|
*/
|
|
|
|
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
2022-03-19 08:26:29 +01:00
|
|
|
#include "BLI_generic_pointer.hh"
|
|
|
|
#include "BLI_generic_vector_array.hh"
|
|
|
|
#include "BLI_generic_virtual_vector_array.hh"
|
2021-04-01 15:55:08 +02:00
|
|
|
#include "BLI_resource_scope.hh"
|
2021-03-21 19:31:24 +01:00
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
#include "FN_multi_function_signature.hh"
|
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
namespace blender::fn {
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
class MFParamsBuilder {
|
|
|
|
private:
|
2021-04-01 15:55:08 +02:00
|
|
|
ResourceScope scope_;
|
2020-07-03 14:20:42 +02:00
|
|
|
const MFSignature *signature_;
|
2021-09-14 14:52:44 +02:00
|
|
|
IndexMask mask_;
|
2020-07-20 12:16:20 +02:00
|
|
|
int64_t min_array_size_;
|
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
|
|
|
Vector<GVArray> virtual_arrays_;
|
2020-07-03 14:20:42 +02:00
|
|
|
Vector<GMutableSpan> mutable_spans_;
|
2021-03-21 19:31:24 +01:00
|
|
|
Vector<const GVVectorArray *> virtual_vector_arrays_;
|
2020-07-03 14:20:42 +02:00
|
|
|
Vector<GVectorArray *> vector_arrays_;
|
2020-06-16 16:35:57 +02:00
|
|
|
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
std::mutex mutex_;
|
|
|
|
Vector<std::pair<int, GMutableSpan>> dummy_output_spans_;
|
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
friend class MFParams;
|
|
|
|
|
2021-09-14 14:52:44 +02:00
|
|
|
MFParamsBuilder(const MFSignature &signature, const IndexMask mask)
|
|
|
|
: signature_(&signature), mask_(mask), min_array_size_(mask.min_array_size())
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-14 14:52:44 +02:00
|
|
|
public:
|
|
|
|
MFParamsBuilder(const class MultiFunction &fn, int64_t size);
|
|
|
|
/**
|
|
|
|
* The indices referenced by the #mask has to live longer than the params builder. This is
|
|
|
|
* because the it might have to destruct elements for all masked indices in the end.
|
|
|
|
*/
|
|
|
|
MFParamsBuilder(const class MultiFunction &fn, const IndexMask *mask);
|
2020-06-16 16:35:57 +02:00
|
|
|
|
2021-08-20 11:43:54 +02:00
|
|
|
template<typename T> void add_readonly_single_input_value(T value, StringRef expected_name = "")
|
|
|
|
{
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
this->add_readonly_single_input(VArray<T>::ForSingle(std::move(value), min_array_size_),
|
|
|
|
expected_name);
|
2021-08-20 11:43:54 +02:00
|
|
|
}
|
2020-07-23 17:57:11 +02:00
|
|
|
template<typename T> void add_readonly_single_input(const T *value, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-09-14 16:08:09 +02:00
|
|
|
this->add_readonly_single_input(
|
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::ForSingleRef(CPPType::get<T>(), min_array_size_, value), expected_name);
|
2021-03-21 19:31:24 +01:00
|
|
|
}
|
|
|
|
void add_readonly_single_input(const GSpan span, StringRef expected_name = "")
|
|
|
|
{
|
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
|
|
|
this->add_readonly_single_input(GVArray::ForSpan(span), expected_name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-05-13 13:23:53 +02:00
|
|
|
void add_readonly_single_input(GPointer value, StringRef expected_name = "")
|
|
|
|
{
|
2021-09-14 16:08:09 +02:00
|
|
|
this->add_readonly_single_input(
|
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::ForSingleRef(*value.type(), min_array_size_, value.get()), expected_name);
|
2021-05-13 13:23:53 +02:00
|
|
|
}
|
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
|
|
|
void add_readonly_single_input(GVArray varray, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
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
|
|
|
this->assert_current_param_type(MFParamType::ForSingleInput(varray.type()), expected_name);
|
|
|
|
BLI_assert(varray.size() >= min_array_size_);
|
|
|
|
virtual_arrays_.append(varray);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name = "")
|
|
|
|
{
|
2021-09-14 16:08:09 +02:00
|
|
|
this->add_readonly_vector_input(scope_.construct<GVVectorArray_For_GVectorArray>(vector_array),
|
|
|
|
expected_name);
|
2021-03-21 19:31:24 +01:00
|
|
|
}
|
2021-08-20 11:43:54 +02:00
|
|
|
void add_readonly_vector_input(const GSpan single_vector, StringRef expected_name = "")
|
|
|
|
{
|
|
|
|
this->add_readonly_vector_input(
|
2021-09-14 16:08:09 +02:00
|
|
|
scope_.construct<GVVectorArray_For_SingleGSpan>(single_vector, min_array_size_),
|
2021-08-20 11:43:54 +02:00
|
|
|
expected_name);
|
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForVectorInput(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
2021-03-21 19:31:24 +01:00
|
|
|
virtual_vector_arrays_.append(&ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
template<typename T> void add_uninitialized_single_output(T *value, StringRef expected_name = "")
|
2020-07-21 17:20:05 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->add_uninitialized_single_output(GMutableSpan(CPPType::get<T>(), value, 1),
|
|
|
|
expected_name);
|
2020-07-21 17:20:05 +02:00
|
|
|
}
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_uninitialized_single_output(GMutableSpan ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForSingleOutput(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
|
|
|
mutable_spans_.append(ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-09-14 14:52:44 +02:00
|
|
|
void add_ignored_single_output(StringRef expected_name = "")
|
|
|
|
{
|
|
|
|
this->assert_current_param_name(expected_name);
|
|
|
|
const int param_index = this->current_param_index();
|
|
|
|
const MFParamType ¶m_type = signature_->param_types[param_index];
|
2022-04-26 17:12:34 +02:00
|
|
|
BLI_assert(param_type.category() == MFParamCategory::SingleOutput);
|
2021-09-14 14:52:44 +02:00
|
|
|
const CPPType &type = param_type.data_type().single_type();
|
|
|
|
/* An empty span indicates that this is ignored. */
|
|
|
|
const GMutableSpan dummy_span{type};
|
|
|
|
mutable_spans_.append(dummy_span);
|
|
|
|
}
|
2020-06-16 16:35:57 +02:00
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_vector_output(GVectorArray &vector_array, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForVectorOutput(vector_array.type()),
|
|
|
|
expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(vector_array.size() >= min_array_size_);
|
|
|
|
vector_arrays_.append(&vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_single_mutable(GMutableSpan ref, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForMutableSingle(ref.type()), expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ref.size() >= min_array_size_);
|
|
|
|
mutable_spans_.append(ref);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 17:57:11 +02:00
|
|
|
void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
this->assert_current_param_type(MFParamType::ForMutableVector(vector_array.type()),
|
|
|
|
expected_name);
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(vector_array.size() >= min_array_size_);
|
|
|
|
vector_arrays_.append(&vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan computed_array(int param_index)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ELEM(signature_->param_types[param_index].category(),
|
2022-04-26 17:12:34 +02:00
|
|
|
MFParamCategory::SingleOutput,
|
|
|
|
MFParamCategory::SingleMutable));
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return mutable_spans_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &computed_vector_array(int param_index)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(ELEM(signature_->param_types[param_index].category(),
|
2022-04-26 17:12:34 +02:00
|
|
|
MFParamCategory::VectorOutput,
|
|
|
|
MFParamCategory::VectorMutable));
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-04-01 15:55:08 +02:00
|
|
|
ResourceScope &resource_scope()
|
2021-03-21 19:31:24 +01:00
|
|
|
{
|
2021-04-01 15:55:08 +02:00
|
|
|
return scope_;
|
2021-03-21 19:31:24 +01:00
|
|
|
}
|
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
private:
|
2020-07-23 17:57:11 +02:00
|
|
|
void assert_current_param_type(MFParamType param_type, StringRef expected_name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2020-07-23 17:57:11 +02:00
|
|
|
UNUSED_VARS_NDEBUG(param_type, expected_name);
|
2020-06-16 16:35:57 +02:00
|
|
|
#ifdef DEBUG
|
2020-07-20 12:16:20 +02:00
|
|
|
int param_index = this->current_param_index();
|
2020-07-23 17:57:11 +02:00
|
|
|
|
|
|
|
if (expected_name != "") {
|
|
|
|
StringRef actual_name = signature_->param_names[param_index];
|
|
|
|
BLI_assert(actual_name == expected_name);
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParamType expected_type = signature_->param_types[param_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
BLI_assert(expected_type == param_type);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-14 14:52:44 +02:00
|
|
|
void assert_current_param_name(StringRef expected_name)
|
|
|
|
{
|
|
|
|
UNUSED_VARS_NDEBUG(expected_name);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (expected_name.is_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int param_index = this->current_param_index();
|
|
|
|
StringRef actual_name = signature_->param_names[param_index];
|
|
|
|
BLI_assert(actual_name == expected_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
int current_param_index() const
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return virtual_arrays_.size() + mutable_spans_.size() + virtual_vector_arrays_.size() +
|
2020-07-03 14:20:42 +02:00
|
|
|
vector_arrays_.size();
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MFParams {
|
|
|
|
private:
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParamsBuilder *builder_;
|
2020-06-16 16:35:57 +02:00
|
|
|
|
|
|
|
public:
|
2020-07-03 14:20:42 +02:00
|
|
|
MFParams(MFParamsBuilder &builder) : builder_(&builder)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
template<typename T> VArray<T> readonly_single_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
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
|
|
|
const GVArray &varray = this->readonly_single_input(param_index, name);
|
|
|
|
return varray.typed<T>();
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVArray &readonly_single_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::SingleInput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_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
|
|
|
return builder_->virtual_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 14:52:44 +02:00
|
|
|
/**
|
|
|
|
* \return True when the caller provided a buffer for this output parameter. This allows the
|
|
|
|
* called multi-function to skip some computation. It is still valid to call
|
|
|
|
* #uninitialized_single_output when this returns false. In this case a new temporary buffer is
|
|
|
|
* allocated.
|
|
|
|
*/
|
|
|
|
bool single_output_is_required(int param_index, StringRef name = "")
|
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::SingleOutput);
|
2021-09-14 14:52:44 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
|
|
|
return !builder_->mutable_spans_[data_index].is_empty();
|
|
|
|
}
|
|
|
|
|
2020-06-16 16:35:57 +02:00
|
|
|
template<typename T>
|
2020-07-20 12:16:20 +02:00
|
|
|
MutableSpan<T> uninitialized_single_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
return this->uninitialized_single_output(param_index, name).typed<T>();
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan uninitialized_single_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::SingleOutput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2021-09-14 14:52:44 +02:00
|
|
|
GMutableSpan span = builder_->mutable_spans_[data_index];
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
if (!span.is_empty()) {
|
|
|
|
return span;
|
2021-09-14 14:52:44 +02:00
|
|
|
}
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
/* The output is ignored by the caller, but the multi-function does not handle this case. So
|
|
|
|
* create a temporary buffer that the multi-function can write to. */
|
|
|
|
return this->ensure_dummy_single_output(data_index);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:12:25 +02:00
|
|
|
/**
|
|
|
|
* Same as #uninitialized_single_output, but returns an empty span when the output is not
|
|
|
|
* required.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
MutableSpan<T> uninitialized_single_output_if_required(int param_index, StringRef name = "")
|
|
|
|
{
|
|
|
|
return this->uninitialized_single_output_if_required(param_index, name).typed<T>();
|
|
|
|
}
|
|
|
|
GMutableSpan uninitialized_single_output_if_required(int param_index, StringRef name = "")
|
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::SingleOutput);
|
2021-09-20 13:12:25 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
|
|
|
return builder_->mutable_spans_[data_index];
|
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
const VVectorArray<T> &readonly_vector_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVVectorArray &vector_array = this->readonly_vector_input(param_index, name);
|
2021-09-14 16:08:09 +02:00
|
|
|
return builder_->scope_.construct<VVectorArray_For_GVVectorArray<T>>(vector_array);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2021-03-21 19:31:24 +01:00
|
|
|
const GVVectorArray &readonly_vector_input(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::VectorInput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2021-03-21 19:31:24 +01:00
|
|
|
return *builder_->virtual_vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
GVectorArray_TypedMutableRef<T> vector_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return {this->vector_output(param_index, name)};
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &vector_output(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::VectorOutput);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *builder_->vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
template<typename T> MutableSpan<T> single_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
return this->single_mutable(param_index, name).typed<T>();
|
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GMutableSpan single_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::SingleMutable);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return builder_->mutable_spans_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 19:31:24 +01:00
|
|
|
template<typename T>
|
|
|
|
GVectorArray_TypedMutableRef<T> vector_mutable(int param_index, StringRef name = "")
|
2020-06-22 15:48:08 +02:00
|
|
|
{
|
2021-03-21 19:31:24 +01:00
|
|
|
return {this->vector_mutable(param_index, name)};
|
2020-06-22 15:48:08 +02:00
|
|
|
}
|
2020-07-20 12:16:20 +02:00
|
|
|
GVectorArray &vector_mutable(int param_index, StringRef name = "")
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
2022-04-26 17:12:34 +02:00
|
|
|
this->assert_correct_param(param_index, name, MFParamCategory::VectorMutable);
|
2020-07-20 12:16:20 +02:00
|
|
|
int data_index = builder_->signature_->data_index(param_index);
|
2020-07-03 14:20:42 +02:00
|
|
|
return *builder_->vector_arrays_[data_index];
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-20 12:16:20 +02:00
|
|
|
void assert_correct_param(int param_index, StringRef name, MFParamType param_type)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
UNUSED_VARS_NDEBUG(param_index, name, param_type);
|
|
|
|
#ifdef DEBUG
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_types[param_index] == param_type);
|
2020-06-16 16:35:57 +02:00
|
|
|
if (name.size() > 0) {
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_names[param_index] == name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:12:34 +02:00
|
|
|
void assert_correct_param(int param_index, StringRef name, MFParamCategory category)
|
2020-06-16 16:35:57 +02:00
|
|
|
{
|
|
|
|
UNUSED_VARS_NDEBUG(param_index, name, category);
|
|
|
|
#ifdef DEBUG
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_types[param_index].category() == category);
|
2020-06-16 16:35:57 +02:00
|
|
|
if (name.size() > 0) {
|
2020-07-03 14:20:42 +02:00
|
|
|
BLI_assert(builder_->signature_->param_names[param_index] == name);
|
2020-06-16 16:35:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
Geometry Nodes: refactor multi-threading in field evaluation
Previously, there was a fixed grain size for all multi-functions. That was
not sufficient because some functions could benefit a lot from smaller
grain sizes.
This refactors adds a new `MultiFunction::call_auto` method which has the
same effect as just calling `MultiFunction::call` but additionally figures
out how to execute the specific multi-function efficiently. It determines
a good grain size and decides whether the mask indices should be shifted
or not.
Most multi-function evaluations benefit from this, but medium sized work
loads (1000 - 50000 elements) benefit from it the most. Especially when
expensive multi-functions (e.g. noise) is involved. This is because for
smaller work loads, threading is rarely used and for larger work loads
threading worked fine before already.
With this patch, multi-functions can specify execution hints, that allow
the caller to execute it most efficiently. These execution hints still
have to be added to more functions.
Some performance measurements of a field evaluation involving noise and
math nodes, ordered by the number of elements being evaluated:
```
1,000,000: 133 ms -> 120 ms
100,000: 30 ms -> 18 ms
10,000: 20 ms -> 2.7 ms
1,000: 4 ms -> 0.5 ms
100: 0.5 ms -> 0.4 ms
```
2021-11-26 11:05:47 +01:00
|
|
|
|
|
|
|
GMutableSpan ensure_dummy_single_output(int data_index);
|
2020-06-16 16:35:57 +02:00
|
|
|
};
|
|
|
|
|
2020-07-03 14:25:20 +02:00
|
|
|
} // namespace blender::fn
|