This refactors how a geometry nodes node tree is converted to a lazy-function graph. Previously, all nodes were inserted into a single graph. This was fine because every node was evaluated at most once per node group evaluation. However, loops (#108896) break this assumption since now nodes may be evaluated multiple times and thus a single flat graph does not work anymore. Now, a separate lazy-function is build for every zone which gives us much more flexibility for what can happen in a zone. Right now, the change only applies to simulation zones since that's the only kind of zone we have. Technically, those zones could be inlined, but turning them into a separate lazy-function also does not hurt and makes it possible to test this refactor without implementing loops first. Also, having them as separate functions might help in the future if we integrate a substep loop directly into the simulation zone. The most tricky part here is to just link everything up correctly, especially with respect to deterministic anonymous attribute lifetimes. Fortunately, correctness can be checked visually by looking at the generated graphs. The logging/viewer system also had to be refactored a bit, because now there can be multiple different `ComputeContext` in a single node tree. Each zone is in a separate `ComputeContext`. To make it work, the `ViewerPath` system now explicitly supports zones and drawing code will look up the right logger for showing inspection data. No functional changes are expected, except that the spreadsheet now shows "Simulation Zone" in the context path if the viewer is in a simulation.
92 lines
3.0 KiB
C++
92 lines
3.0 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Foundation
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "DNA_node_types.h"
|
|
|
|
#include "BKE_compute_contexts.hh"
|
|
|
|
namespace blender::bke {
|
|
|
|
ModifierComputeContext::ModifierComputeContext(const ComputeContext *parent,
|
|
std::string modifier_name)
|
|
: ComputeContext(s_static_type, parent), modifier_name_(std::move(modifier_name))
|
|
{
|
|
hash_.mix_in(s_static_type, strlen(s_static_type));
|
|
hash_.mix_in(modifier_name_.data(), modifier_name_.size());
|
|
}
|
|
|
|
void ModifierComputeContext::print_current_in_line(std::ostream &stream) const
|
|
{
|
|
stream << "Modifier: " << modifier_name_;
|
|
}
|
|
|
|
NodeGroupComputeContext::NodeGroupComputeContext(
|
|
const ComputeContext *parent,
|
|
const int32_t node_id,
|
|
const std::optional<ComputeContextHash> &cached_hash)
|
|
: ComputeContext(s_static_type, parent), node_id_(node_id)
|
|
{
|
|
if (cached_hash.has_value()) {
|
|
hash_ = *cached_hash;
|
|
}
|
|
else {
|
|
/* Mix static type and node id into a single buffer so that only a single call to #mix_in is
|
|
* necessary. */
|
|
const int type_size = strlen(s_static_type);
|
|
const int buffer_size = type_size + 1 + sizeof(int32_t);
|
|
DynamicStackBuffer<64, 8> buffer_owner(buffer_size, 8);
|
|
char *buffer = static_cast<char *>(buffer_owner.buffer());
|
|
memcpy(buffer, s_static_type, type_size + 1);
|
|
memcpy(buffer + type_size + 1, &node_id_, sizeof(int32_t));
|
|
hash_.mix_in(buffer, buffer_size);
|
|
}
|
|
}
|
|
|
|
NodeGroupComputeContext::NodeGroupComputeContext(const ComputeContext *parent, const bNode &node)
|
|
: NodeGroupComputeContext(parent, node.identifier)
|
|
{
|
|
#ifdef DEBUG
|
|
debug_node_name_ = node.name;
|
|
#endif
|
|
}
|
|
|
|
void NodeGroupComputeContext::print_current_in_line(std::ostream &stream) const
|
|
{
|
|
#ifdef DEBUG
|
|
if (!debug_node_name_.empty()) {
|
|
stream << "Node: " << debug_node_name_;
|
|
return;
|
|
}
|
|
#endif
|
|
stream << "Node ID: " << node_id_;
|
|
}
|
|
|
|
SimulationZoneComputeContext::SimulationZoneComputeContext(const ComputeContext *parent,
|
|
const int32_t output_node_id)
|
|
: ComputeContext(s_static_type, parent), output_node_id_(output_node_id)
|
|
{
|
|
/* Mix static type and node id into a single buffer so that only a single call to #mix_in is
|
|
* necessary. */
|
|
const int type_size = strlen(s_static_type);
|
|
const int buffer_size = type_size + 1 + sizeof(int32_t);
|
|
DynamicStackBuffer<64, 8> buffer_owner(buffer_size, 8);
|
|
char *buffer = static_cast<char *>(buffer_owner.buffer());
|
|
memcpy(buffer, s_static_type, type_size + 1);
|
|
memcpy(buffer + type_size + 1, &output_node_id_, sizeof(int32_t));
|
|
hash_.mix_in(buffer, buffer_size);
|
|
}
|
|
|
|
SimulationZoneComputeContext::SimulationZoneComputeContext(const ComputeContext *parent,
|
|
const bNode &node)
|
|
: SimulationZoneComputeContext(parent, node.identifier)
|
|
{
|
|
}
|
|
|
|
void SimulationZoneComputeContext::print_current_in_line(std::ostream &stream) const
|
|
{
|
|
stream << "Simulation Zone ID: " << output_node_id_;
|
|
}
|
|
|
|
} // namespace blender::bke
|