From 19740b25c778ccd3fccd0ab33c7efd9f761dc001 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sun, 17 Oct 2021 15:56:37 +0200 Subject: Geometry Nodes: bring back lazy evaluation for field types in Switch node Differential Revision: https://developer.blender.org/D12878 --- .../nodes/geometry/nodes/node_geo_switch.cc | 48 +++++++++++++++------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'source') diff --git a/source/blender/nodes/geometry/nodes/node_geo_switch.cc b/source/blender/nodes/geometry/nodes/node_geo_switch.cc index 05df927fb39..c01fcf5bb5f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_switch.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_switch.cc @@ -157,23 +157,43 @@ template void switch_fields(GeoNodeExecParams ¶ms, const StringR const std::string name_true = "True" + suffix; const std::string name_output = "Output" + suffix; - /* TODO: Allow for Laziness when the switch field is constant. */ - const bool require_false = params.lazy_require_input(name_false); - const bool require_true = params.lazy_require_input(name_true); - if (require_false | require_true) { - return; - } + Field switches_field = params.get_input>("Switch"); + if (switches_field.node().depends_on_input()) { + /* The switch has to be incorporated into the field. Both inputs have to be evaluated. */ + const bool require_false = params.lazy_require_input(name_false); + const bool require_true = params.lazy_require_input(name_true); + if (require_false | require_true) { + return; + } - Field switches_field = params.extract_input>("Switch"); - Field falses_field = params.extract_input>(name_false); - Field trues_field = params.extract_input>(name_true); + Field falses_field = params.extract_input>(name_false); + Field trues_field = params.extract_input>(name_true); - auto switch_fn = std::make_unique>(); - auto switch_op = std::make_shared(FieldOperation( - std::move(switch_fn), - {std::move(switches_field), std::move(falses_field), std::move(trues_field)})); + auto switch_fn = std::make_unique>(); + auto switch_op = std::make_shared(FieldOperation( + std::move(switch_fn), + {std::move(switches_field), std::move(falses_field), std::move(trues_field)})); - params.set_output(name_output, Field(switch_op, 0)); + params.set_output(name_output, Field(switch_op, 0)); + } + else { + /* The switch input is constant, so just evaluate and forward one of the inputs. */ + const bool switch_value = fn::evaluate_constant_field(switches_field); + if (switch_value) { + params.set_input_unused(name_false); + if (params.lazy_require_input(name_true)) { + return; + } + params.set_output(name_output, params.extract_input>(name_true)); + } + else { + params.set_input_unused(name_true); + if (params.lazy_require_input(name_false)) { + return; + } + params.set_output(name_output, params.extract_input>(name_false)); + } + } } template void switch_no_fields(GeoNodeExecParams ¶ms, const StringRef suffix) -- cgit v1.2.3