This adds a new type of zone to Geometry Nodes that allows executing some nodes
for each element in a geometry.
## Features
* The `Selection` input allows iterating over a subset of elements on the set
domain.
* Fields passed into the input node are available as single values inside of the
zone.
* The input geometry can be split up into separate (completely independent)
geometries for each element (on all domains except face corner).
* New attributes can be created on the input geometry by outputting a single
value from each iteration.
* New geometries can be generated in each iteration.
* All of these geometries are joined to form the final output.
* Attributes from the input geometry are propagated to the output
geometries.
## Evaluation
The evaluation strategy is similar to the one used for repeat zones. Namely, it
dynamically builds a `lazy_function::Graph` once it knows how many iterations
are necessary. It contains a separate node for each iteration. The inputs for
each iteration are hardcoded into the graph. The outputs of each iteration a
passed to a separate lazy-function that reduces all the values down to the final
outputs. This final output can have a huge number of inputs and that is not
ideal for multi-threading yet, but that can still be improved in the future.
## Performance
There is a non-neglilible amount of overhead for each iteration. The overhead is
way larger than the per-element overhead when just doing field evaluation.
Therefore, normal field evaluation should be preferred when possible. That can
partially still be optimized if there is only some number crunching going on in
the zone but that optimization is not implemented yet.
However, processing many small geometries (e.g. each hair of a character
separately) will likely **always be slower** than working on fewer larger
geoemtries. The additional flexibility you get by processing each element
separately comes at the cost that Blender can't optimize the operation as well.
For node groups that need to handle lots of geometry elements, we recommend
trying to design the node setup so that iteration over tiny sub-geometries is
not required.
An opposite point is true as well though. It can be faster to process more
medium sized geometries in parallel than fewer very large geometries because of
more multi-threading opportunities. The exact threshold between tiny, medium and
large geometries depends on a lot of factors though.
Overall, this initial version of the new zone does not implement all
optimization opportunities yet, but the points mentioned above will still hold
true later.
Pull Request: https://projects.blender.org/blender/blender/pulls/127331
168 lines
4.2 KiB
C++
168 lines
4.2 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
/**
|
|
* This file implements some specific compute contexts for concepts in Blender.
|
|
*/
|
|
|
|
#include <optional>
|
|
|
|
#include "BLI_compute_context.hh"
|
|
|
|
struct bNode;
|
|
struct bNodeTree;
|
|
|
|
namespace blender::bke {
|
|
|
|
class ModifierComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "MODIFIER";
|
|
|
|
/**
|
|
* Use modifier name instead of something like `session_uid` for now because:
|
|
* - It's more obvious that the name matches between the original and evaluated object.
|
|
* - We might want that the context hash is consistent between sessions in the future.
|
|
*/
|
|
std::string modifier_name_;
|
|
|
|
public:
|
|
ModifierComputeContext(const ComputeContext *parent, std::string modifier_name);
|
|
|
|
StringRefNull modifier_name() const
|
|
{
|
|
return modifier_name_;
|
|
}
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
class GroupNodeComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "NODE_GROUP";
|
|
|
|
int32_t node_id_;
|
|
/**
|
|
* The caller node tree and group node are not always necessary or even available, but storing
|
|
* them here simplifies "walking up" the compute context to the parent node groups.
|
|
*/
|
|
const bNodeTree *caller_tree_ = nullptr;
|
|
const bNode *caller_group_node_ = nullptr;
|
|
|
|
public:
|
|
GroupNodeComputeContext(const ComputeContext *parent,
|
|
int32_t node_id,
|
|
const std::optional<ComputeContextHash> &cached_hash = {});
|
|
GroupNodeComputeContext(const ComputeContext *parent,
|
|
const bNode &node,
|
|
const bNodeTree &caller_tree);
|
|
|
|
int32_t node_id() const
|
|
{
|
|
return node_id_;
|
|
}
|
|
|
|
const bNode *caller_group_node() const
|
|
{
|
|
return caller_group_node_;
|
|
}
|
|
|
|
const bNodeTree *caller_tree() const
|
|
{
|
|
return caller_tree_;
|
|
}
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
class SimulationZoneComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "SIMULATION_ZONE";
|
|
|
|
int32_t output_node_id_;
|
|
|
|
public:
|
|
SimulationZoneComputeContext(const ComputeContext *parent, int output_node_id);
|
|
SimulationZoneComputeContext(const ComputeContext *parent, const bNode &node);
|
|
|
|
int32_t output_node_id() const
|
|
{
|
|
return output_node_id_;
|
|
}
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
class RepeatZoneComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "REPEAT_ZONE";
|
|
|
|
int32_t output_node_id_;
|
|
int iteration_;
|
|
|
|
public:
|
|
RepeatZoneComputeContext(const ComputeContext *parent, int32_t output_node_id, int iteration);
|
|
RepeatZoneComputeContext(const ComputeContext *parent, const bNode &node, int iteration);
|
|
|
|
int32_t output_node_id() const
|
|
{
|
|
return output_node_id_;
|
|
}
|
|
|
|
int iteration() const
|
|
{
|
|
return iteration_;
|
|
}
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
class ForeachGeometryElementZoneComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "FOREACH_GEOMETRY_ELEMENT_ZONE";
|
|
|
|
int32_t output_node_id_;
|
|
int index_;
|
|
|
|
public:
|
|
ForeachGeometryElementZoneComputeContext(const ComputeContext *parent,
|
|
int32_t output_node_id,
|
|
int index);
|
|
ForeachGeometryElementZoneComputeContext(const ComputeContext *parent,
|
|
const bNode &node,
|
|
int index);
|
|
|
|
int32_t output_node_id() const
|
|
{
|
|
return output_node_id_;
|
|
}
|
|
|
|
int index() const
|
|
{
|
|
return index_;
|
|
}
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
class OperatorComputeContext : public ComputeContext {
|
|
private:
|
|
static constexpr const char *s_static_type = "OPERATOR";
|
|
|
|
public:
|
|
OperatorComputeContext();
|
|
OperatorComputeContext(const ComputeContext *parent);
|
|
|
|
private:
|
|
void print_current_in_line(std::ostream &stream) const override;
|
|
};
|
|
|
|
} // namespace blender::bke
|