Refactoring: Geometry Nodes: Vectors of indices for zone socket mapping #116939

Merged
Jacques Lucke merged 6 commits from mod_moder/blender:tmp_indices_in_zones into main 2024-01-09 18:16:51 +01:00

View File

@ -1445,9 +1445,9 @@ struct BuildGraphParams {
struct ZoneFunctionIndices {
struct {
IndexRange main;
IndexRange border_links;
IndexRange output_usages;
Vector<int> main;
Vector<int> border_links;
Vector<int> output_usages;
/**
* Some attribute sets are input into the body of a zone from the outside. These two
* maps indicate which zone function inputs corresponds to attribute set. Attribute sets are
@ -1457,9 +1457,9 @@ struct ZoneFunctionIndices {
Map<int, int> attributes_by_caller_propagation_index;
} inputs;
struct {
IndexRange main;
IndexRange border_link_usages;
IndexRange input_usages;
Vector<int> main;
Vector<int> border_link_usages;
Vector<int> input_usages;
} outputs;
};
@ -1582,52 +1582,51 @@ class LazyFunctionForRepeatZone : public LazyFunction {
{
debug_name_ = "Repeat Zone";
inputs_.append_as("Iterations", CPPType::get<SocketValueVariant>(), lf::ValueUsage::Used);
zone_info.indices.inputs.main.append(inputs_.append_and_get_index_as(
"Iterations", CPPType::get<SocketValueVariant>(), lf::ValueUsage::Used));
for (const bNodeSocket *socket : zone.input_node->input_sockets().drop_front(1).drop_back(1)) {
inputs_.append_as(
socket->name, *socket->typeinfo->geometry_nodes_cpp_type, lf::ValueUsage::Maybe);
zone_info.indices.inputs.main.append(inputs_.append_and_get_index_as(
socket->name, *socket->typeinfo->geometry_nodes_cpp_type, lf::ValueUsage::Maybe));
}
zone_info.indices.inputs.main = inputs_.index_range();
for (const bNodeLink *link : zone.border_links) {
inputs_.append_as(link->fromsock->name,
*link->tosock->typeinfo->geometry_nodes_cpp_type,
lf::ValueUsage::Maybe);
zone_info.indices.inputs.border_links.append(
inputs_.append_and_get_index_as(link->fromsock->name,
*link->tosock->typeinfo->geometry_nodes_cpp_type,
lf::ValueUsage::Maybe));
}
zone_info.indices.inputs.border_links = inputs_.index_range().take_back(
zone.border_links.size());
for (const bNodeSocket *socket : zone.output_node->output_sockets().drop_back(1)) {
inputs_.append_as("Usage", CPPType::get<bool>(), lf::ValueUsage::Maybe);
outputs_.append_as(socket->name, *socket->typeinfo->geometry_nodes_cpp_type);
zone_info.indices.inputs.output_usages.append(
inputs_.append_and_get_index_as("Usage", CPPType::get<bool>(), lf::ValueUsage::Maybe));
zone_info.indices.outputs.main.append(outputs_.append_and_get_index_as(
socket->name, *socket->typeinfo->geometry_nodes_cpp_type));
}
zone_info.indices.inputs.output_usages = inputs_.index_range().take_back(
zone.output_node->output_sockets().drop_back(1).size());
zone_info.indices.outputs.main = outputs_.index_range();
for ([[maybe_unused]] const bNodeSocket *socket :
zone.input_node->input_sockets().drop_back(1))
{
outputs_.append_as("Usage", CPPType::get<bool>());
zone_info.indices.outputs.input_usages.append(
outputs_.append_and_get_index_as("Usage", CPPType::get<bool>()));
}
zone_info.indices.outputs.input_usages = outputs_.index_range().take_back(
zone.input_node->input_sockets().drop_back(1).size());
for ([[maybe_unused]] const bNodeLink *link : zone.border_links) {
outputs_.append_as("Border Link Usage", CPPType::get<bool>());
zone_info.indices.outputs.border_link_usages.append(
outputs_.append_and_get_index_as("Border Link Usage", CPPType::get<bool>()));
}
zone_info.indices.outputs.border_link_usages = outputs_.index_range().take_back(
zone.border_links.size());
for (const auto item : body_fn_.indices.inputs.attributes_by_field_source_index.items()) {
const int index = inputs_.append_and_get_index_as(
"Attribute Set", CPPType::get<bke::AnonymousAttributeSet>(), lf::ValueUsage::Maybe);
zone_info.indices.inputs.attributes_by_field_source_index.add_new(item.key, index);
zone_info.indices.inputs.attributes_by_field_source_index.add_new(
item.key,
inputs_.append_and_get_index_as(
"Attribute Set", CPPType::get<bke::AnonymousAttributeSet>(), lf::ValueUsage::Maybe));
}
for (const auto item : body_fn_.indices.inputs.attributes_by_caller_propagation_index.items())
{
const int index = inputs_.append_and_get_index_as(
"Attribute Set", CPPType::get<bke::AnonymousAttributeSet>(), lf::ValueUsage::Maybe);
zone_info.indices.inputs.attributes_by_caller_propagation_index.add_new(item.key, index);
zone_info.indices.inputs.attributes_by_caller_propagation_index.add_new(
item.key,
inputs_.append_and_get_index_as(
"Attribute Set", CPPType::get<bke::AnonymousAttributeSet>(), lf::ValueUsage::Maybe));
}
}
@ -2181,38 +2180,37 @@ struct GeometryNodesLazyFunctionBuilder {
const auto &sim_output_storage = *static_cast<const NodeGeometrySimulationOutput *>(
zone.output_node->storage);
Vector<lf::GraphInputSocket *> lf_zone_main_inputs;
Vector<lf::GraphOutputSocket *> lf_zone_main_input_usages;
Vector<lf::GraphInputSocket *> lf_zone_inputs;
Vector<lf::GraphOutputSocket *> lf_zone_outputs;
if (zone.input_node != nullptr) {
for (const bNodeSocket *bsocket : zone.input_node->input_sockets().drop_back(1)) {
lf_zone_main_inputs.append(
&lf_graph.add_input(*bsocket->typeinfo->geometry_nodes_cpp_type, bsocket->name));
lf_zone_main_input_usages.append(
&lf_graph.add_output(CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name)));
zone_info.indices.inputs.main.append(lf_zone_inputs.append_and_get_index(
&lf_graph.add_input(*bsocket->typeinfo->geometry_nodes_cpp_type, bsocket->name)));
zone_info.indices.outputs.input_usages.append(lf_zone_outputs.append_and_get_index(
&lf_graph.add_output(CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name))));
}
}
Vector<lf::GraphInputSocket *> lf_border_link_inputs = this->build_zone_border_links_inputs(
zone, lf_graph);
Vector<lf::GraphOutputSocket *> lf_zone_main_outputs;
Vector<lf::GraphInputSocket *> lf_zone_main_output_usages;
this->build_zone_border_links_inputs(
zone, lf_graph, lf_zone_inputs, zone_info.indices.inputs.border_links);
this->build_zone_border_link_input_usages(
zone, lf_graph, lf_zone_outputs, zone_info.indices.outputs.border_link_usages);
for (const bNodeSocket *bsocket : zone.output_node->output_sockets().drop_back(1)) {
lf_zone_main_outputs.append(
&lf_graph.add_output(*bsocket->typeinfo->geometry_nodes_cpp_type, bsocket->name));
lf_zone_main_output_usages.append(
&lf_graph.add_input(CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name)));
zone_info.indices.outputs.main.append(lf_zone_outputs.append_and_get_index(
&lf_graph.add_output(*bsocket->typeinfo->geometry_nodes_cpp_type, bsocket->name)));
zone_info.indices.inputs.output_usages.append(lf_zone_inputs.append_and_get_index(
&lf_graph.add_input(CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name))));
}
Vector<lf::GraphOutputSocket *> lf_border_link_usages =
this->build_zone_border_link_input_usages(zone, lf_graph);
lf::Node &lf_simulation_usage_node = [&]() -> lf::Node & {
auto &lazy_function = scope_.construct<LazyFunctionForSimulationInputsUsage>(
*zone.output_node);
lf::Node &lf_node = lf_graph.add_function(lazy_function);
for (const int i : lf_zone_main_input_usages.index_range()) {
lf_graph.add_link(lf_node.output(0), *lf_zone_main_input_usages[i]);
for (const int i : zone_info.indices.outputs.input_usages) {
lf_graph.add_link(lf_node.output(0), *lf_zone_outputs[i]);
}
return lf_node;
@ -2253,30 +2251,24 @@ struct GeometryNodesLazyFunctionBuilder {
this->insert_links_from_socket(*item.key, *item.value, graph_params);
}
this->link_border_link_inputs_and_usages(
zone, lf_border_link_inputs, lf_border_link_usages, graph_params);
this->link_border_link_inputs_and_usages(zone,
lf_zone_inputs,
zone_info.indices.inputs.border_links,
lf_zone_outputs,
zone_info.indices.outputs.border_link_usages,
graph_params);
for (const int i : lf_zone_main_inputs.index_range()) {
lf_graph.add_link(*lf_zone_main_inputs[i], lf_simulation_input->input(i));
for (const int i : zone_info.indices.inputs.main) {
lf_graph.add_link(*lf_zone_inputs[i], lf_simulation_input->input(i));
}
for (const int i : lf_zone_main_outputs.index_range()) {
lf_graph.add_link(lf_simulation_output.output(i), *lf_zone_main_outputs[i]);
for (const int i : zone_info.indices.outputs.main.index_range()) {
lf_graph.add_link(lf_simulation_output.output(i),
*lf_zone_outputs[zone_info.indices.outputs.main[i]]);
}
this->add_default_inputs(graph_params);
Vector<const lf::GraphInputSocket *, 16> lf_zone_inputs;
lf_zone_inputs.extend(lf_zone_main_inputs);
zone_info.indices.inputs.main = lf_zone_inputs.index_range();
lf_zone_inputs.extend(lf_border_link_inputs);
zone_info.indices.inputs.border_links = lf_zone_inputs.index_range().take_back(
lf_border_link_inputs.size());
lf_zone_inputs.extend(lf_zone_main_output_usages);
zone_info.indices.inputs.output_usages = lf_zone_inputs.index_range().take_back(
lf_zone_main_output_usages.size());
Map<int, lf::OutputSocket *> lf_attribute_set_by_field_source_index;
Map<int, lf::OutputSocket *> lf_attribute_set_by_caller_propagation_index;
this->build_attribute_set_inputs_for_zone(graph_params,
@ -2285,17 +2277,15 @@ struct GeometryNodesLazyFunctionBuilder {
for (const auto item : lf_attribute_set_by_field_source_index.items()) {
lf::OutputSocket &lf_attribute_set_socket = *item.value;
if (lf_attribute_set_socket.node().is_interface()) {
const int zone_input_index = lf_zone_inputs.append_and_get_index(&lf_attribute_set_socket);
zone_info.indices.inputs.attributes_by_field_source_index.add_new(item.key,
zone_input_index);
zone_info.indices.inputs.attributes_by_field_source_index.add_new(
item.key, lf_zone_inputs.append_and_get_index(&lf_attribute_set_socket));
}
}
for (const auto item : lf_attribute_set_by_caller_propagation_index.items()) {
lf::OutputSocket &lf_attribute_set_socket = *item.value;
if (lf_attribute_set_socket.node().is_interface()) {
const int zone_input_index = lf_zone_inputs.append_and_get_index(&lf_attribute_set_socket);
zone_info.indices.inputs.attributes_by_caller_propagation_index.add_new(item.key,
zone_input_index);
zone_info.indices.inputs.attributes_by_caller_propagation_index.add_new(
item.key, lf_zone_inputs.append_and_get_index(&lf_attribute_set_socket));
}
}
this->link_attribute_set_inputs(lf_graph,
@ -2304,25 +2294,17 @@ struct GeometryNodesLazyFunctionBuilder {
lf_attribute_set_by_caller_propagation_index);
this->fix_link_cycles(lf_graph, graph_params.socket_usage_inputs);
Vector<const lf::GraphOutputSocket *, 16> lf_zone_outputs;
lf_zone_outputs.extend(lf_zone_main_outputs);
zone_info.indices.outputs.main = lf_zone_outputs.index_range();
lf_zone_outputs.extend(lf_zone_main_input_usages);
zone_info.indices.outputs.input_usages = lf_zone_outputs.index_range().take_back(
lf_zone_main_input_usages.size());
lf_zone_outputs.extend(lf_border_link_usages);
zone_info.indices.outputs.border_link_usages = lf_zone_outputs.index_range().take_back(
lf_border_link_usages.size());
lf_graph.update_node_indices();
auto &logger = scope_.construct<GeometryNodesLazyFunctionLogger>(*lf_graph_info_);
auto &side_effect_provider = scope_.construct<GeometryNodesLazyFunctionSideEffectProvider>();
const auto &lf_graph_fn = scope_.construct<lf::GraphExecutor>(
lf_graph, lf_zone_inputs, lf_zone_outputs, &logger, &side_effect_provider, nullptr);
const auto &lf_graph_fn = scope_.construct<lf::GraphExecutor>(lf_graph,
lf_zone_inputs.as_span(),
lf_zone_outputs.as_span(),
&logger,
&side_effect_provider,
nullptr);
const auto &zone_function = scope_.construct<LazyFunctionForSimulationZone>(*zone.output_node,
lf_graph_fn);
zone_info.lazy_function = &zone_function;
@ -2352,32 +2334,25 @@ struct GeometryNodesLazyFunctionBuilder {
BuildGraphParams graph_params{lf_body_graph};
Vector<const lf::GraphInputSocket *, 16> lf_body_inputs;
Vector<const lf::GraphOutputSocket *, 16> lf_body_outputs;
Vector<lf::GraphInputSocket *> lf_body_inputs;
Vector<lf::GraphOutputSocket *> lf_body_outputs;
ZoneBodyFunction &body_fn = scope_.construct<ZoneBodyFunction>();
Vector<lf::GraphInputSocket *> lf_main_inputs;
Vector<lf::GraphOutputSocket *> lf_main_input_usages;
for (const bNodeSocket *bsocket : zone.input_node->output_sockets().drop_back(1)) {
lf::GraphInputSocket &lf_input = lf_body_graph.add_input(
*bsocket->typeinfo->geometry_nodes_cpp_type, bsocket->name);
lf::GraphOutputSocket &lf_input_usage = lf_body_graph.add_output(
CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name));
lf_main_inputs.append(&lf_input);
lf_main_input_usages.append(&lf_input_usage);
body_fn.indices.inputs.main.append(lf_body_inputs.append_and_get_index(&lf_input));
body_fn.indices.outputs.input_usages.append(
lf_body_outputs.append_and_get_index(&lf_input_usage));
graph_params.lf_output_by_bsocket.add_new(bsocket, &lf_input);
}
lf_body_inputs.extend(lf_main_inputs);
body_fn.indices.inputs.main = lf_body_inputs.index_range().take_back(lf_main_inputs.size());
lf_body_outputs.extend(lf_main_input_usages);
body_fn.indices.outputs.input_usages = lf_body_outputs.index_range().take_back(
lf_main_input_usages.size());
Vector<lf::GraphOutputSocket *> lf_main_outputs;
Vector<lf::GraphInputSocket *> lf_main_output_usages;
this->build_zone_border_links_inputs(
zone, lf_body_graph, lf_body_inputs, body_fn.indices.inputs.border_links);
this->build_zone_border_link_input_usages(
zone, lf_body_graph, lf_body_outputs, body_fn.indices.outputs.border_link_usages);
for (const bNodeSocket *bsocket : zone.output_node->input_sockets().drop_back(1)) {
lf::GraphOutputSocket &lf_output = lf_body_graph.add_output(
@ -2386,36 +2361,19 @@ struct GeometryNodesLazyFunctionBuilder {
CPPType::get<bool>(), "Usage: " + StringRef(bsocket->name));
graph_params.lf_inputs_by_bsocket.add(bsocket, &lf_output);
graph_params.usage_by_bsocket.add(bsocket, &lf_output_usage);
lf_main_outputs.append(&lf_output);
lf_main_output_usages.append(&lf_output_usage);
body_fn.indices.outputs.main.append(lf_body_outputs.append_and_get_index(&lf_output));
body_fn.indices.inputs.output_usages.append(
lf_body_inputs.append_and_get_index(&lf_output_usage));
}
lf_body_outputs.extend(lf_main_outputs);
body_fn.indices.outputs.main = lf_body_outputs.index_range().take_back(lf_main_outputs.size());
lf_body_inputs.extend(lf_main_output_usages);
body_fn.indices.inputs.output_usages = lf_body_inputs.index_range().take_back(
lf_main_output_usages.size());
Vector<lf::GraphInputSocket *> lf_border_link_inputs = this->build_zone_border_links_inputs(
zone, lf_body_graph);
lf_body_inputs.extend(lf_border_link_inputs);
body_fn.indices.inputs.border_links = lf_body_inputs.index_range().take_back(
lf_border_link_inputs.size());
Vector<lf::GraphOutputSocket *> lf_border_link_usages =
this->build_zone_border_link_input_usages(zone, lf_body_graph);
lf_body_outputs.extend(lf_border_link_usages);
body_fn.indices.outputs.border_link_usages = lf_body_outputs.index_range().take_back(
lf_border_link_usages.size());
this->insert_nodes_and_zones(zone.child_nodes, zone.child_zones, graph_params);
this->build_output_socket_usages(*zone.input_node, graph_params);
for (const int i : zone.input_node->output_sockets().drop_back(1).index_range()) {
const bNodeSocket &bsocket = zone.input_node->output_socket(i);
lf::OutputSocket *lf_usage = graph_params.usage_by_bsocket.lookup_default(&bsocket, nullptr);
lf::GraphOutputSocket &lf_usage_output = *lf_main_input_usages[i];
lf::GraphOutputSocket &lf_usage_output =
*lf_body_outputs[body_fn.indices.outputs.input_usages[i]];
if (lf_usage) {
lf_body_graph.add_link(*lf_usage, lf_usage_output);
}
@ -2429,8 +2387,12 @@ struct GeometryNodesLazyFunctionBuilder {
this->insert_links_from_socket(*item.key, *item.value, graph_params);
}
this->link_border_link_inputs_and_usages(
zone, lf_border_link_inputs, lf_border_link_usages, graph_params);
this->link_border_link_inputs_and_usages(zone,
lf_body_inputs,
body_fn.indices.inputs.border_links,
lf_body_outputs,
body_fn.indices.outputs.border_link_usages,
graph_params);
this->add_default_inputs(graph_params);
@ -2443,17 +2405,15 @@ struct GeometryNodesLazyFunctionBuilder {
for (const auto item : lf_attribute_set_by_field_source_index.items()) {
lf::OutputSocket &lf_attribute_set_socket = *item.value;
if (lf_attribute_set_socket.node().is_interface()) {
const int body_input_index = lf_body_inputs.append_and_get_index(&lf_attribute_set_socket);
body_fn.indices.inputs.attributes_by_field_source_index.add_new(item.key,
body_input_index);
body_fn.indices.inputs.attributes_by_field_source_index.add_new(
item.key, lf_body_inputs.append_and_get_index(&lf_attribute_set_socket));
}
}
for (const auto item : lf_attribute_set_by_caller_propagation_index.items()) {
lf::OutputSocket &lf_attribute_set_socket = *item.value;
if (lf_attribute_set_socket.node().is_interface()) {
const int body_input_index = lf_body_inputs.append_and_get_index(&lf_attribute_set_socket);
body_fn.indices.inputs.attributes_by_caller_propagation_index.add_new(item.key,
body_input_index);
body_fn.indices.inputs.attributes_by_caller_propagation_index.add_new(
item.key, lf_body_inputs.append_and_get_index(&lf_attribute_set_socket));
}
}
this->link_attribute_set_inputs(lf_body_graph,
@ -2466,35 +2426,40 @@ struct GeometryNodesLazyFunctionBuilder {
auto &logger = scope_.construct<GeometryNodesLazyFunctionLogger>(*lf_graph_info_);
auto &side_effect_provider = scope_.construct<GeometryNodesLazyFunctionSideEffectProvider>();
body_fn.function = &scope_.construct<lf::GraphExecutor>(
lf_body_graph, lf_body_inputs, lf_body_outputs, &logger, &side_effect_provider, nullptr);
body_fn.function = &scope_.construct<lf::GraphExecutor>(lf_body_graph,
lf_body_inputs.as_span(),
lf_body_outputs.as_span(),
&logger,
&side_effect_provider,
nullptr);
// std::cout << "\n\n" << lf_body_graph.to_dot() << "\n\n";
return body_fn;
}
Vector<lf::GraphInputSocket *> build_zone_border_links_inputs(const bNodeTreeZone &zone,
lf::Graph &lf_graph)
void build_zone_border_links_inputs(const bNodeTreeZone &zone,
lf::Graph &lf_graph,
Vector<lf::GraphInputSocket *> &r_lf_graph_inputs,
Vector<int> &r_indices)
{
Vector<lf::GraphInputSocket *> lf_graph_inputs;
for (const bNodeLink *border_link : zone.border_links) {
lf_graph_inputs.append(
r_indices.append(r_lf_graph_inputs.append_and_get_index(
&lf_graph.add_input(*border_link->tosock->typeinfo->geometry_nodes_cpp_type,
StringRef("Link from ") + border_link->fromsock->name));
StringRef("Link from ") + border_link->fromsock->name)));
}
return lf_graph_inputs;
}
Vector<lf::GraphOutputSocket *> build_zone_border_link_input_usages(const bNodeTreeZone &zone,
lf::Graph &lf_graph)
void build_zone_border_link_input_usages(const bNodeTreeZone &zone,
lf::Graph &lf_graph,
Vector<lf::GraphOutputSocket *> &r_lf_graph_outputs,
Vector<int> &r_indices)
{
Vector<lf::GraphOutputSocket *> lf_graph_outputs;
for (const bNodeLink *border_link : zone.border_links) {
lf_graph_outputs.append(&lf_graph.add_output(
CPPType::get<bool>(), StringRef("Usage: Link from ") + border_link->fromsock->name));
r_indices.append(r_lf_graph_outputs.append_and_get_index(&lf_graph.add_output(
CPPType::get<bool>(), StringRef("Usage: Link from ") + border_link->fromsock->name)));
}
return lf_graph_outputs;
}
void build_attribute_set_inputs_for_zone(
@ -2878,22 +2843,24 @@ struct GeometryNodesLazyFunctionBuilder {
}
}
void link_border_link_inputs_and_usages(
const bNodeTreeZone &zone,
const Span<lf::GraphInputSocket *> lf_border_link_inputs,
const Span<lf::GraphOutputSocket *> lf_border_link_usages,
BuildGraphParams &graph_params)
void link_border_link_inputs_and_usages(const bNodeTreeZone &zone,
const Span<lf::GraphInputSocket *> lf_inputs,
const Span<int> lf_border_link_input_indices,
const Span<lf::GraphOutputSocket *> lf_usages,
const Span<int> lf_border_link_usage_indices,
BuildGraphParams &graph_params)
{
lf::Graph &lf_graph = graph_params.lf_graph;
for (const int border_link_i : zone.border_links.index_range()) {
const bNodeLink &border_link = *zone.border_links[border_link_i];
lf::GraphInputSocket &lf_from = *lf_border_link_inputs[border_link_i];
lf::GraphInputSocket &lf_from = *lf_inputs[lf_border_link_input_indices[border_link_i]];
const Vector<lf::InputSocket *> lf_link_targets = this->find_link_targets(border_link,
graph_params);
for (lf::InputSocket *lf_to : lf_link_targets) {
lf_graph.add_link(lf_from, *lf_to);
}
lf::GraphOutputSocket &lf_usage_output = *lf_border_link_usages[border_link_i];
lf::GraphOutputSocket &lf_usage_output =
*lf_usages[lf_border_link_usage_indices[border_link_i]];
if (lf::OutputSocket *lf_usage = graph_params.usage_by_bsocket.lookup_default(
border_link.tosock, nullptr))
{