Geometry Nodes: new geometry nodes evaluator

The old geometry nodes evaluator was quite basic and missed many features.
It was useful to get the geometry nodes project started. However, nowadays
we run into its limitations from time to time.

The new evaluator is more complex, but comes with new capabilities.
The two most important capabilities are that it can now execute nodes in
parallel and it supports lazy evaluation.

The performance improvement by multi-threading depends a lot on the specific
node tree. In our demo files, the speedup is measurable but not huge. This
is mainly because they are bottlenecked by one or two nodes that have to be
executed one after the other (often the Boolean or Attribute Proximity nodes)
or because the bottleneck is multi-threaded already (often openvdb nodes).

Lazy evaluation of inputs is only supported by the Switch node for now.
Previously, geometry nodes would always compute both inputs and then just
discard the one that is not used. Now, only the input that is required
is computed.

For some more details read D11191, T87620 and the in-code documentation.

Differential Revision: https://developer.blender.org/D11191
This commit is contained in:
Jacques Lucke 2021-05-20 11:34:47 +02:00
parent 44e7192745
commit b084b57fbf
Notes: blender-bot 2023-02-13 18:54:52 +01:00
Referenced by issue #88719, Attribute Remove Node Input Field Does Nothing
Referenced by issue #88588, Geometry node crash when muting a node
Referenced by issue #88578, Geometry Nodes : Empty string input to Attribute Remove causes Error
Referenced by issue #87620, Geometry Nodes Evaluator Improvements
5 changed files with 1480 additions and 268 deletions

View File

@ -325,6 +325,7 @@ typedef struct bNodeType {
/* Execute a geometry node. */
NodeGeometryExecFunction geometry_node_execute;
bool geometry_node_execute_supports_lazyness;
/* RNA integration */
ExtensionRNA rna_ext;

File diff suppressed because it is too large Load Diff

View File

@ -96,7 +96,18 @@ class GeoNodeExecParamsProvider {
* Prepare a memory buffer for an output value of the node. The returned memory has to be
* initialized by the caller. The identifier and type are expected to be correct.
*/
virtual GMutablePointer alloc_output_value(StringRef identifier, const CPPType &type) = 0;
virtual GMutablePointer alloc_output_value(const CPPType &type) = 0;
/**
* The value has been allocated with #alloc_output_value.
*/
virtual void set_output(StringRef identifier, GMutablePointer value) = 0;
/* A description for these methods is provided in GeoNodeExecParams. */
virtual void set_input_unused(StringRef identifier) = 0;
virtual bool output_is_required(StringRef identifier) const = 0;
virtual bool lazy_require_input(StringRef identifier) = 0;
virtual bool lazy_output_is_required(StringRef identifier) const = 0;
};
class GeoNodeExecParams {
@ -174,8 +185,48 @@ class GeoNodeExecParams {
#ifdef DEBUG
this->check_output_access(identifier, type);
#endif
GMutablePointer gvalue = provider_->alloc_output_value(identifier, type);
GMutablePointer gvalue = provider_->alloc_output_value(type);
new (gvalue.get()) StoredT(std::forward<T>(value));
provider_->set_output(identifier, gvalue);
}
/**
* Tell the evaluator that a specific input won't be used anymore.
*/
void set_input_unused(StringRef identifier)
{
provider_->set_input_unused(identifier);
}
/**
* Returns true when the output has to be computed.
* Nodes that support lazyness could use the #lazy_output_is_required variant to possibly avoid
* some computations.
*/
bool output_is_required(StringRef identifier) const
{
return provider_->output_is_required(identifier);
}
/**
* Tell the evaluator that a specific input is required.
* This returns true when the input will only be available in the next execution.
* False is returned if the input is available already.
* This can only be used when the node supports lazyness.
*/
bool lazy_require_input(StringRef identifier)
{
return provider_->lazy_require_input(identifier);
}
/**
* Asks the evaluator if a specific output is required right now. If this returns false, the
* value might still need to be computed later.
* This can only be used when the node supports lazyness.
*/
bool lazy_output_is_required(StringRef identifier)
{
return provider_->lazy_output_is_required(identifier);
}
/**

View File

@ -135,15 +135,27 @@ static GeometrySet separate_geometry_set(const GeometrySet &set_in,
static void geo_node_point_separate_exec(GeoNodeExecParams params)
{
const std::string mask_attribute_name = params.extract_input<std::string>("Mask");
GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
bool wait_for_inputs = false;
wait_for_inputs |= params.lazy_require_input("Geometry");
wait_for_inputs |= params.lazy_require_input("Mask");
if (wait_for_inputs) {
return;
}
const std::string mask_attribute_name = params.get_input<std::string>("Mask");
GeometrySet geometry_set = params.get_input<GeometrySet>("Geometry");
/* TODO: This is not necessary-- the input geometry set can be read only,
* but it must be rewritten to handle instance groups. */
geometry_set = geometry_set_realize_instances(geometry_set);
params.set_output("Geometry 1", separate_geometry_set(geometry_set, mask_attribute_name, true));
params.set_output("Geometry 2", separate_geometry_set(geometry_set, mask_attribute_name, false));
if (params.lazy_output_is_required("Geometry 1")) {
params.set_output("Geometry 1",
separate_geometry_set(geometry_set, mask_attribute_name, true));
}
if (params.lazy_output_is_required("Geometry 2")) {
params.set_output("Geometry 2",
separate_geometry_set(geometry_set, mask_attribute_name, false));
}
}
} // namespace blender::nodes
@ -155,5 +167,6 @@ void register_node_type_geo_point_separate()
geo_node_type_base(&ntype, GEO_NODE_POINT_SEPARATE, "Point Separate", NODE_CLASS_GEOMETRY, 0);
node_type_socket_templates(&ntype, geo_node_point_instance_in, geo_node_point_instance_out);
ntype.geometry_node_execute = blender::nodes::geo_node_point_separate_exec;
ntype.geometry_node_execute_supports_lazyness = true;
nodeRegisterType(&ntype);
}

View File

@ -86,23 +86,36 @@ static void geo_node_switch_update(bNodeTree *UNUSED(ntree), bNode *node)
}
template<typename T>
void output_input(GeoNodeExecParams &params,
const bool input,
const StringRef input_suffix,
const StringRef output_identifier)
static void output_input(GeoNodeExecParams &params,
const bool input,
const StringRef input_suffix,
const StringRef output_identifier)
{
const std::string name_a = "A" + input_suffix;
const std::string name_b = "B" + input_suffix;
if (input) {
params.set_output(output_identifier, params.extract_input<T>("B" + input_suffix));
params.set_input_unused(name_a);
if (params.lazy_require_input(name_b)) {
return;
}
params.set_output(output_identifier, params.extract_input<T>(name_b));
}
else {
params.set_output(output_identifier, params.extract_input<T>("A" + input_suffix));
params.set_input_unused(name_b);
if (params.lazy_require_input(name_a)) {
return;
}
params.set_output(output_identifier, params.extract_input<T>(name_a));
}
}
static void geo_node_switch_exec(GeoNodeExecParams params)
{
if (params.lazy_require_input("Switch")) {
return;
}
const NodeSwitch &storage = *(const NodeSwitch *)params.node().storage;
const bool input = params.extract_input<bool>("Switch");
const bool input = params.get_input<bool>("Switch");
switch ((eNodeSocketDatatype)storage.input_type) {
case SOCK_FLOAT: {
output_input<float>(params, input, "", "Output");
@ -158,6 +171,7 @@ void register_node_type_geo_switch()
node_type_update(&ntype, blender::nodes::geo_node_switch_update);
node_type_storage(&ntype, "NodeSwitch", node_free_standard_storage, node_copy_standard_storage);
ntype.geometry_node_execute = blender::nodes::geo_node_switch_exec;
ntype.geometry_node_execute_supports_lazyness = true;
ntype.draw_buttons = geo_node_switch_layout;
nodeRegisterType(&ntype);
}