2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-04-20 10:58:43 +02:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-05-15 15:14:22 +02:00
|
|
|
#include "BKE_node.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "NOD_geometry_exec.hh"
|
2023-08-09 22:01:03 +02:00
|
|
|
#include "NOD_register.hh"
|
2021-08-30 17:13:46 +02:00
|
|
|
#include "NOD_socket_declarations.hh"
|
2021-10-26 20:00:03 +02:00
|
|
|
#include "NOD_socket_declarations_geometry.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2023-05-03 14:21:14 +02:00
|
|
|
#include "node_util.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2023-03-19 11:21:08 +01:00
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
# include <openvdb/Types.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-23 13:56:35 -05:00
|
|
|
struct BVHTreeFromMesh;
|
2023-08-13 10:50:52 +03:00
|
|
|
struct GeometrySet;
|
2023-08-04 20:59:04 +02:00
|
|
|
namespace blender::nodes {
|
|
|
|
|
class GatherAddNodeSearchParams;
|
|
|
|
|
class GatherLinkSearchOpParams;
|
|
|
|
|
} // namespace blender::nodes
|
2022-09-23 13:56:35 -05:00
|
|
|
|
2023-08-03 01:11:28 +02:00
|
|
|
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass);
|
|
|
|
|
bool geo_node_poll_default(const bNodeType *ntype,
|
|
|
|
|
const bNodeTree *ntree,
|
2021-04-12 18:43:23 +02:00
|
|
|
const char **r_disabled_hint);
|
2020-12-09 16:20:48 +01:00
|
|
|
|
|
|
|
|
namespace blender::nodes {
|
2020-12-18 16:00:45 +01:00
|
|
|
|
2023-08-04 20:59:04 +02:00
|
|
|
bool check_tool_context_and_error(GeoNodeExecParams ¶ms);
|
|
|
|
|
void search_link_ops_for_tool_node(GatherLinkSearchOpParams ¶ms);
|
|
|
|
|
|
2021-10-15 14:20:53 -05:00
|
|
|
void transform_mesh(Mesh &mesh,
|
2021-03-16 17:35:12 -04:00
|
|
|
const float3 translation,
|
|
|
|
|
const float3 rotation,
|
|
|
|
|
const float3 scale);
|
|
|
|
|
|
2022-08-29 17:00:08 +02:00
|
|
|
void transform_geometry_set(GeoNodeExecParams ¶ms,
|
|
|
|
|
GeometrySet &geometry,
|
2021-10-15 14:20:53 -05:00
|
|
|
const float4x4 &transform,
|
|
|
|
|
const Depsgraph &depsgraph);
|
|
|
|
|
|
2021-10-11 08:38:02 -05:00
|
|
|
/**
|
|
|
|
|
* Returns the parts of the geometry that are on the selection for the given domain. If the domain
|
|
|
|
|
* is not applicable for the component, e.g. face domain for point cloud, nothing happens to that
|
|
|
|
|
* component. If no component can work with the domain, then `error_message` is set to true.
|
|
|
|
|
*/
|
|
|
|
|
void separate_geometry(GeometrySet &geometry_set,
|
2022-06-01 14:38:06 +10:00
|
|
|
eAttrDomain domain,
|
2022-01-07 11:38:08 +11:00
|
|
|
GeometryNodeDeleteGeometryMode mode,
|
2021-10-11 08:38:02 -05:00
|
|
|
const Field<bool> &selection_field,
|
2023-01-05 14:05:30 +01:00
|
|
|
const AnonymousAttributePropagationInfo &propagation_info,
|
2021-10-11 16:07:57 +02:00
|
|
|
bool &r_is_error);
|
2021-06-01 17:32:03 -04:00
|
|
|
|
2022-09-23 13:56:35 -05:00
|
|
|
void get_closest_in_bvhtree(BVHTreeFromMesh &tree_data,
|
|
|
|
|
const VArray<float3> &positions,
|
BLI: refactor IndexMask for better performance and memory usage
Goals of this refactor:
* Reduce memory consumption of `IndexMask`. The old `IndexMask` uses an
`int64_t` for each index which is more than necessary in pretty much all
practical cases currently. Using `int32_t` might still become limiting
in the future in case we use this to index e.g. byte buffers larger than
a few gigabytes. We also don't want to template `IndexMask`, because
that would cause a split in the "ecosystem", or everything would have to
be implemented twice or templated.
* Allow for more multi-threading. The old `IndexMask` contains a single
array. This is generally good but has the problem that it is hard to fill
from multiple-threads when the final size is not known from the beginning.
This is commonly the case when e.g. converting an array of bool to an
index mask. Currently, this kind of code only runs on a single thread.
* Allow for efficient set operations like join, intersect and difference.
It should be possible to multi-thread those operations.
* It should be possible to iterate over an `IndexMask` very efficiently.
The most important part of that is to avoid all memory access when iterating
over continuous ranges. For some core nodes (e.g. math nodes), we generate
optimized code for the cases of irregular index masks and simple index ranges.
To achieve these goals, a few compromises had to made:
* Slicing of the mask (at specific indices) and random element access is
`O(log #indices)` now, but with a low constant factor. It should be possible
to split a mask into n approximately equally sized parts in `O(n)` though,
making the time per split `O(1)`.
* Using range-based for loops does not work well when iterating over a nested
data structure like the new `IndexMask`. Therefor, `foreach_*` functions with
callbacks have to be used. To avoid extra code complexity at the call site,
the `foreach_*` methods support multi-threading out of the box.
The new data structure splits an `IndexMask` into an arbitrary number of ordered
`IndexMaskSegment`. Each segment can contain at most `2^14 = 16384` indices. The
indices within a segment are stored as `int16_t`. Each segment has an additional
`int64_t` offset which allows storing arbitrary `int64_t` indices. This approach
has the main benefits that segments can be processed/constructed individually on
multiple threads without a serial bottleneck. Also it reduces the memory
requirements significantly.
For more details see comments in `BLI_index_mask.hh`.
I did a few tests to verify that the data structure generally improves
performance and does not cause regressions:
* Our field evaluation benchmarks take about as much as before. This is to be
expected because we already made sure that e.g. add node evaluation is
vectorized. The important thing here is to check that changes to the way we
iterate over the indices still allows for auto-vectorization.
* Memory usage by a mask is about 1/4 of what it was before in the average case.
That's mainly caused by the switch from `int64_t` to `int16_t` for indices.
In the worst case, the memory requirements can be larger when there are many
indices that are very far away. However, when they are far away from each other,
that indicates that there aren't many indices in total. In common cases, memory
usage can be way lower than 1/4 of before, because sub-ranges use static memory.
* For some more specific numbers I benchmarked `IndexMask::from_bools` in
`index_mask_from_selection` on 10.000.000 elements at various probabilities for
`true` at every index:
```
Probability Old New
0 4.6 ms 0.8 ms
0.001 5.1 ms 1.3 ms
0.2 8.4 ms 1.8 ms
0.5 15.3 ms 3.0 ms
0.8 20.1 ms 3.0 ms
0.999 25.1 ms 1.7 ms
1 13.5 ms 1.1 ms
```
Pull Request: https://projects.blender.org/blender/blender/pulls/104629
2023-05-24 18:11:41 +02:00
|
|
|
const IndexMask &mask,
|
2022-09-23 13:56:35 -05:00
|
|
|
const MutableSpan<int> r_indices,
|
|
|
|
|
const MutableSpan<float> r_distances_sq,
|
|
|
|
|
const MutableSpan<float3> r_positions);
|
|
|
|
|
|
2022-09-28 14:38:27 -05:00
|
|
|
int apply_offset_in_cyclic_range(IndexRange range, int start_index, int offset);
|
|
|
|
|
|
2022-06-01 14:38:06 +10:00
|
|
|
std::optional<eCustomDataType> node_data_type_to_custom_data_type(eNodeSocketDatatype type);
|
|
|
|
|
std::optional<eCustomDataType> node_socket_to_custom_data_type(const bNodeSocket &socket);
|
2021-12-15 09:51:57 -06:00
|
|
|
|
2023-03-19 11:21:08 +01:00
|
|
|
#ifdef WITH_OPENVDB
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the VolumeComponent of a GeometrySet with a new Volume from points.
|
|
|
|
|
* The grid class should be either openvdb::GRID_FOG_VOLUME or openvdb::GRID_LEVEL_SET.
|
|
|
|
|
*/
|
|
|
|
|
void initialize_volume_component_from_points(GeoNodeExecParams ¶ms,
|
|
|
|
|
const NodeGeometryPointsToVolume &storage,
|
|
|
|
|
GeometrySet &r_geometry_set,
|
|
|
|
|
openvdb::GridClass gridClass);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-03 11:50:05 -04:00
|
|
|
class EvaluateAtIndexInput final : public bke::GeometryFieldInput {
|
2022-09-28 14:38:27 -05:00
|
|
|
private:
|
|
|
|
|
Field<int> index_field_;
|
|
|
|
|
GField value_field_;
|
|
|
|
|
eAttrDomain value_field_domain_;
|
|
|
|
|
|
|
|
|
|
public:
|
2023-05-03 11:50:05 -04:00
|
|
|
EvaluateAtIndexInput(Field<int> index_field, GField value_field, eAttrDomain value_field_domain);
|
2022-09-28 14:38:27 -05:00
|
|
|
|
|
|
|
|
GVArray get_varray_for_context(const bke::GeometryFieldContext &context,
|
BLI: refactor IndexMask for better performance and memory usage
Goals of this refactor:
* Reduce memory consumption of `IndexMask`. The old `IndexMask` uses an
`int64_t` for each index which is more than necessary in pretty much all
practical cases currently. Using `int32_t` might still become limiting
in the future in case we use this to index e.g. byte buffers larger than
a few gigabytes. We also don't want to template `IndexMask`, because
that would cause a split in the "ecosystem", or everything would have to
be implemented twice or templated.
* Allow for more multi-threading. The old `IndexMask` contains a single
array. This is generally good but has the problem that it is hard to fill
from multiple-threads when the final size is not known from the beginning.
This is commonly the case when e.g. converting an array of bool to an
index mask. Currently, this kind of code only runs on a single thread.
* Allow for efficient set operations like join, intersect and difference.
It should be possible to multi-thread those operations.
* It should be possible to iterate over an `IndexMask` very efficiently.
The most important part of that is to avoid all memory access when iterating
over continuous ranges. For some core nodes (e.g. math nodes), we generate
optimized code for the cases of irregular index masks and simple index ranges.
To achieve these goals, a few compromises had to made:
* Slicing of the mask (at specific indices) and random element access is
`O(log #indices)` now, but with a low constant factor. It should be possible
to split a mask into n approximately equally sized parts in `O(n)` though,
making the time per split `O(1)`.
* Using range-based for loops does not work well when iterating over a nested
data structure like the new `IndexMask`. Therefor, `foreach_*` functions with
callbacks have to be used. To avoid extra code complexity at the call site,
the `foreach_*` methods support multi-threading out of the box.
The new data structure splits an `IndexMask` into an arbitrary number of ordered
`IndexMaskSegment`. Each segment can contain at most `2^14 = 16384` indices. The
indices within a segment are stored as `int16_t`. Each segment has an additional
`int64_t` offset which allows storing arbitrary `int64_t` indices. This approach
has the main benefits that segments can be processed/constructed individually on
multiple threads without a serial bottleneck. Also it reduces the memory
requirements significantly.
For more details see comments in `BLI_index_mask.hh`.
I did a few tests to verify that the data structure generally improves
performance and does not cause regressions:
* Our field evaluation benchmarks take about as much as before. This is to be
expected because we already made sure that e.g. add node evaluation is
vectorized. The important thing here is to check that changes to the way we
iterate over the indices still allows for auto-vectorization.
* Memory usage by a mask is about 1/4 of what it was before in the average case.
That's mainly caused by the switch from `int64_t` to `int16_t` for indices.
In the worst case, the memory requirements can be larger when there are many
indices that are very far away. However, when they are far away from each other,
that indicates that there aren't many indices in total. In common cases, memory
usage can be way lower than 1/4 of before, because sub-ranges use static memory.
* For some more specific numbers I benchmarked `IndexMask::from_bools` in
`index_mask_from_selection` on 10.000.000 elements at various probabilities for
`true` at every index:
```
Probability Old New
0 4.6 ms 0.8 ms
0.001 5.1 ms 1.3 ms
0.2 8.4 ms 1.8 ms
0.5 15.3 ms 3.0 ms
0.8 20.1 ms 3.0 ms
0.999 25.1 ms 1.7 ms
1 13.5 ms 1.1 ms
```
Pull Request: https://projects.blender.org/blender/blender/pulls/104629
2023-05-24 18:11:41 +02:00
|
|
|
const IndexMask &mask) const final;
|
2022-09-28 14:38:27 -05:00
|
|
|
|
|
|
|
|
std::optional<eAttrDomain> preferred_domain(const GeometryComponent & /*component*/) const final
|
|
|
|
|
{
|
|
|
|
|
return value_field_domain_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
Geometry Nodes: add simulation support
This adds support for building simulations with geometry nodes. A new
`Simulation Input` and `Simulation Output` node allow maintaining a
simulation state across multiple frames. Together these two nodes form
a `simulation zone` which contains all the nodes that update the simulation
state from one frame to the next.
A new simulation zone can be added via the menu
(`Simulation > Simulation Zone`) or with the node add search.
The simulation state contains a geometry by default. However, it is possible
to add multiple geometry sockets as well as other socket types. Currently,
field inputs are evaluated and stored for the preceding geometry socket in
the order that the sockets are shown. Simulation state items can be added
by linking one of the empty sockets to something else. In the sidebar, there
is a new panel that allows adding, removing and reordering these sockets.
The simulation nodes behave as follows:
* On the first frame, the inputs of the `Simulation Input` node are evaluated
to initialize the simulation state. In later frames these sockets are not
evaluated anymore. The `Delta Time` at the first frame is zero, but the
simulation zone is still evaluated.
* On every next frame, the `Simulation Input` node outputs the simulation
state of the previous frame. Nodes in the simulation zone can edit that
data in arbitrary ways, also taking into account the `Delta Time`. The new
simulation state has to be passed to the `Simulation Output` node where it
is cached and forwarded.
* On a frame that is already cached or baked, the nodes in the simulation
zone are not evaluated, because the `Simulation Output` node can return
the previously cached data directly.
It is not allowed to connect sockets from inside the simulation zone to the
outside without going through the `Simulation Output` node. This is a necessary
restriction to make caching and sub-frame interpolation work. Links can go into
the simulation zone without problems though.
Anonymous attributes are not propagated by the simulation nodes unless they
are explicitly stored in the simulation state. This is unfortunate, but
currently there is no practical and reliable alternative. The core problem
is detecting which anonymous attributes will be required for the simulation
and afterwards. While we can detect this for the current evaluation, we can't
look into the future in time to see what data will be necessary. We intend to
make it easier to explicitly pass data through a simulation in the future,
even if the simulation is in a nested node group.
There is a new `Simulation Nodes` panel in the physics tab in the properties
editor. It allows baking all simulation zones on the selected objects. The
baking options are intentially kept at a minimum for this MVP. More features
for simulation baking as well as baking in general can be expected to be added
separately.
All baked data is stored on disk in a folder next to the .blend file. #106937
describes how baking is implemented in more detail. Volumes can not be baked
yet and materials are lost during baking for now. Packing the baked data into
the .blend file is not yet supported.
The timeline indicates which frames are currently cached, baked or cached but
invalidated by user-changes.
Simulation input and output nodes are internally linked together by their
`bNode.identifier` which stays the same even if the node name changes. They
are generally added and removed together. However, there are still cases where
"dangling" simulation nodes can be created currently. Those generally don't
cause harm, but would be nice to avoid this in more cases in the future.
Co-authored-by: Hans Goudey <h.goudey@me.com>
Co-authored-by: Lukas Tönne <lukas@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/104924
2023-05-03 13:18:51 +02:00
|
|
|
std::string socket_identifier_for_simulation_item(const NodeSimulationItem &item);
|
|
|
|
|
|
|
|
|
|
void socket_declarations_for_simulation_items(Span<NodeSimulationItem> items,
|
|
|
|
|
NodeDeclaration &r_declaration);
|
|
|
|
|
const CPPType &get_simulation_item_cpp_type(eNodeSocketDatatype socket_type);
|
|
|
|
|
const CPPType &get_simulation_item_cpp_type(const NodeSimulationItem &item);
|
Geometry Nodes: refactor simulation storage and how simulation nodes access it
Goals of the refactor:
* Internal support for baking individual simulation zones (not exposed in the UI yet).
* More well-defined access to simulation data in geometry nodes. Especially, it
should be more obvious where data is modified. A similar approach should also
work for the Bake node.
Previously, there were a bunch of simulation specific properties in `GeoNodesModifierData`
and then the simulation input and output nodes would have to figure out what to do with that
data. Now, there is a new `GeoNodesSimulationParams` which controls the behavior of
simulation zones. Contrary to before, different simulation zones can now be handled
independently, even if that is not really used yet. `GeoNodesSimulationParams` has to be
subclassed by a user of the geometry nodes API. The subclass controls what each simulation
input and output node does. This some of the logic that was part of the node before, into
the modifier.
The way we store simulation data is "transposed". Previously, we stored zone data per
frame, but now we store frame data per zone. This allows different zones to be more
independent. Consequently, the way the simulation cache is accessed changed. I kept
things simpler for now, avoiding many of the methods we had before, and directly
accessing the data more often which is often simple enough. This change also makes
it theoretically possible to store baked data for separate zones independently.
A downside of this is, that existing baked data can't be read anymore. We don't really
have compatibility guarantees for this format yet, so it's ok. Users will have to bake again.
The bake folder for the modifier now contains an extra subfolder for every zone.
Drawing the cached/baked frames in the timeline is less straight forward now. Currently,
it just draws the state of one of the zones, which usually is identical to that of all other
zones. This will change in the future though, and then the timeline drawing also needs
some new UI work.
Pull Request: https://projects.blender.org/blender/blender/pulls/111623
2023-08-31 16:28:03 +02:00
|
|
|
|
2023-09-03 13:37:24 +02:00
|
|
|
bke::bake::BakeState move_values_to_simulation_state(
|
Geometry Nodes: refactor simulation storage and how simulation nodes access it
Goals of the refactor:
* Internal support for baking individual simulation zones (not exposed in the UI yet).
* More well-defined access to simulation data in geometry nodes. Especially, it
should be more obvious where data is modified. A similar approach should also
work for the Bake node.
Previously, there were a bunch of simulation specific properties in `GeoNodesModifierData`
and then the simulation input and output nodes would have to figure out what to do with that
data. Now, there is a new `GeoNodesSimulationParams` which controls the behavior of
simulation zones. Contrary to before, different simulation zones can now be handled
independently, even if that is not really used yet. `GeoNodesSimulationParams` has to be
subclassed by a user of the geometry nodes API. The subclass controls what each simulation
input and output node does. This some of the logic that was part of the node before, into
the modifier.
The way we store simulation data is "transposed". Previously, we stored zone data per
frame, but now we store frame data per zone. This allows different zones to be more
independent. Consequently, the way the simulation cache is accessed changed. I kept
things simpler for now, avoiding many of the methods we had before, and directly
accessing the data more often which is often simple enough. This change also makes
it theoretically possible to store baked data for separate zones independently.
A downside of this is, that existing baked data can't be read anymore. We don't really
have compatibility guarantees for this format yet, so it's ok. Users will have to bake again.
The bake folder for the modifier now contains an extra subfolder for every zone.
Drawing the cached/baked frames in the timeline is less straight forward now. Currently,
it just draws the state of one of the zones, which usually is identical to that of all other
zones. This will change in the future though, and then the timeline drawing also needs
some new UI work.
Pull Request: https://projects.blender.org/blender/blender/pulls/111623
2023-08-31 16:28:03 +02:00
|
|
|
const Span<NodeSimulationItem> node_simulation_items, const Span<void *> input_values);
|
2023-07-11 14:03:12 +02:00
|
|
|
void move_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
|
2023-09-03 13:37:24 +02:00
|
|
|
bke::bake::BakeState zone_state,
|
2023-07-11 14:03:12 +02:00
|
|
|
const Object &self_object,
|
|
|
|
|
const ComputeContext &compute_context,
|
|
|
|
|
const bNode &sim_output_node,
|
|
|
|
|
Span<void *> r_output_values);
|
|
|
|
|
void copy_simulation_state_to_values(const Span<NodeSimulationItem> node_simulation_items,
|
2023-09-03 13:37:24 +02:00
|
|
|
const bke::bake::BakeStateRef &zone_state,
|
2023-07-11 14:03:12 +02:00
|
|
|
const Object &self_object,
|
|
|
|
|
const ComputeContext &compute_context,
|
|
|
|
|
const bNode &sim_output_node,
|
|
|
|
|
Span<void *> r_output_values);
|
Geometry Nodes: add simulation support
This adds support for building simulations with geometry nodes. A new
`Simulation Input` and `Simulation Output` node allow maintaining a
simulation state across multiple frames. Together these two nodes form
a `simulation zone` which contains all the nodes that update the simulation
state from one frame to the next.
A new simulation zone can be added via the menu
(`Simulation > Simulation Zone`) or with the node add search.
The simulation state contains a geometry by default. However, it is possible
to add multiple geometry sockets as well as other socket types. Currently,
field inputs are evaluated and stored for the preceding geometry socket in
the order that the sockets are shown. Simulation state items can be added
by linking one of the empty sockets to something else. In the sidebar, there
is a new panel that allows adding, removing and reordering these sockets.
The simulation nodes behave as follows:
* On the first frame, the inputs of the `Simulation Input` node are evaluated
to initialize the simulation state. In later frames these sockets are not
evaluated anymore. The `Delta Time` at the first frame is zero, but the
simulation zone is still evaluated.
* On every next frame, the `Simulation Input` node outputs the simulation
state of the previous frame. Nodes in the simulation zone can edit that
data in arbitrary ways, also taking into account the `Delta Time`. The new
simulation state has to be passed to the `Simulation Output` node where it
is cached and forwarded.
* On a frame that is already cached or baked, the nodes in the simulation
zone are not evaluated, because the `Simulation Output` node can return
the previously cached data directly.
It is not allowed to connect sockets from inside the simulation zone to the
outside without going through the `Simulation Output` node. This is a necessary
restriction to make caching and sub-frame interpolation work. Links can go into
the simulation zone without problems though.
Anonymous attributes are not propagated by the simulation nodes unless they
are explicitly stored in the simulation state. This is unfortunate, but
currently there is no practical and reliable alternative. The core problem
is detecting which anonymous attributes will be required for the simulation
and afterwards. While we can detect this for the current evaluation, we can't
look into the future in time to see what data will be necessary. We intend to
make it easier to explicitly pass data through a simulation in the future,
even if the simulation is in a nested node group.
There is a new `Simulation Nodes` panel in the physics tab in the properties
editor. It allows baking all simulation zones on the selected objects. The
baking options are intentially kept at a minimum for this MVP. More features
for simulation baking as well as baking in general can be expected to be added
separately.
All baked data is stored on disk in a folder next to the .blend file. #106937
describes how baking is implemented in more detail. Volumes can not be baked
yet and materials are lost during baking for now. Packing the baked data into
the .blend file is not yet supported.
The timeline indicates which frames are currently cached, baked or cached but
invalidated by user-changes.
Simulation input and output nodes are internally linked together by their
`bNode.identifier` which stays the same even if the node name changes. They
are generally added and removed together. However, there are still cases where
"dangling" simulation nodes can be created currently. Those generally don't
cause harm, but would be nice to avoid this in more cases in the future.
Co-authored-by: Hans Goudey <h.goudey@me.com>
Co-authored-by: Lukas Tönne <lukas@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/104924
2023-05-03 13:18:51 +02:00
|
|
|
|
2023-05-03 11:46:18 -04:00
|
|
|
void copy_with_checked_indices(const GVArray &src,
|
|
|
|
|
const VArray<int> &indices,
|
BLI: refactor IndexMask for better performance and memory usage
Goals of this refactor:
* Reduce memory consumption of `IndexMask`. The old `IndexMask` uses an
`int64_t` for each index which is more than necessary in pretty much all
practical cases currently. Using `int32_t` might still become limiting
in the future in case we use this to index e.g. byte buffers larger than
a few gigabytes. We also don't want to template `IndexMask`, because
that would cause a split in the "ecosystem", or everything would have to
be implemented twice or templated.
* Allow for more multi-threading. The old `IndexMask` contains a single
array. This is generally good but has the problem that it is hard to fill
from multiple-threads when the final size is not known from the beginning.
This is commonly the case when e.g. converting an array of bool to an
index mask. Currently, this kind of code only runs on a single thread.
* Allow for efficient set operations like join, intersect and difference.
It should be possible to multi-thread those operations.
* It should be possible to iterate over an `IndexMask` very efficiently.
The most important part of that is to avoid all memory access when iterating
over continuous ranges. For some core nodes (e.g. math nodes), we generate
optimized code for the cases of irregular index masks and simple index ranges.
To achieve these goals, a few compromises had to made:
* Slicing of the mask (at specific indices) and random element access is
`O(log #indices)` now, but with a low constant factor. It should be possible
to split a mask into n approximately equally sized parts in `O(n)` though,
making the time per split `O(1)`.
* Using range-based for loops does not work well when iterating over a nested
data structure like the new `IndexMask`. Therefor, `foreach_*` functions with
callbacks have to be used. To avoid extra code complexity at the call site,
the `foreach_*` methods support multi-threading out of the box.
The new data structure splits an `IndexMask` into an arbitrary number of ordered
`IndexMaskSegment`. Each segment can contain at most `2^14 = 16384` indices. The
indices within a segment are stored as `int16_t`. Each segment has an additional
`int64_t` offset which allows storing arbitrary `int64_t` indices. This approach
has the main benefits that segments can be processed/constructed individually on
multiple threads without a serial bottleneck. Also it reduces the memory
requirements significantly.
For more details see comments in `BLI_index_mask.hh`.
I did a few tests to verify that the data structure generally improves
performance and does not cause regressions:
* Our field evaluation benchmarks take about as much as before. This is to be
expected because we already made sure that e.g. add node evaluation is
vectorized. The important thing here is to check that changes to the way we
iterate over the indices still allows for auto-vectorization.
* Memory usage by a mask is about 1/4 of what it was before in the average case.
That's mainly caused by the switch from `int64_t` to `int16_t` for indices.
In the worst case, the memory requirements can be larger when there are many
indices that are very far away. However, when they are far away from each other,
that indicates that there aren't many indices in total. In common cases, memory
usage can be way lower than 1/4 of before, because sub-ranges use static memory.
* For some more specific numbers I benchmarked `IndexMask::from_bools` in
`index_mask_from_selection` on 10.000.000 elements at various probabilities for
`true` at every index:
```
Probability Old New
0 4.6 ms 0.8 ms
0.001 5.1 ms 1.3 ms
0.2 8.4 ms 1.8 ms
0.5 15.3 ms 3.0 ms
0.8 20.1 ms 3.0 ms
0.999 25.1 ms 1.7 ms
1 13.5 ms 1.1 ms
```
Pull Request: https://projects.blender.org/blender/blender/pulls/104629
2023-05-24 18:11:41 +02:00
|
|
|
const IndexMask &mask,
|
2023-05-03 11:46:18 -04:00
|
|
|
GMutableSpan dst);
|
|
|
|
|
|
2023-07-11 22:36:10 +02:00
|
|
|
void socket_declarations_for_repeat_items(const Span<NodeRepeatItem> items,
|
|
|
|
|
NodeDeclaration &r_declaration);
|
|
|
|
|
|
2023-08-29 12:15:40 +02:00
|
|
|
namespace enums {
|
|
|
|
|
|
|
|
|
|
const EnumPropertyItem *attribute_type_type_with_socket_fn(bContext * /*C*/,
|
|
|
|
|
PointerRNA * /*ptr*/,
|
|
|
|
|
PropertyRNA * /*prop*/,
|
|
|
|
|
bool *r_free);
|
|
|
|
|
|
|
|
|
|
bool generic_attribute_type_supported(const EnumPropertyItem &item);
|
|
|
|
|
|
|
|
|
|
} // namespace enums
|
|
|
|
|
|
2020-12-16 16:59:30 +01:00
|
|
|
} // namespace blender::nodes
|