[Bf-blender-cvs] [19740b25c77] master: Geometry Nodes: bring back lazy evaluation for field types in Switch node

Jacques Lucke noreply at git.blender.org
Sun Oct 17 15:57:35 CEST 2021


Commit: 19740b25c778ccd3fccd0ab33c7efd9f761dc001
Author: Jacques Lucke
Date:   Sun Oct 17 15:56:37 2021 +0200
Branches: master
https://developer.blender.org/rB19740b25c778ccd3fccd0ab33c7efd9f761dc001

Geometry Nodes: bring back lazy evaluation for field types in Switch node

Differential Revision: https://developer.blender.org/D12878

===================================================================

M	source/blender/nodes/geometry/nodes/node_geo_switch.cc

===================================================================

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<typename T> void switch_fields(GeoNodeExecParams &params, 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<bool> switches_field = params.get_input<Field<bool>>("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<bool> switches_field = params.extract_input<Field<bool>>("Switch");
-  Field<T> falses_field = params.extract_input<Field<T>>(name_false);
-  Field<T> trues_field = params.extract_input<Field<T>>(name_true);
+    Field<T> falses_field = params.extract_input<Field<T>>(name_false);
+    Field<T> trues_field = params.extract_input<Field<T>>(name_true);
 
-  auto switch_fn = std::make_unique<SwitchFieldsFunction<T>>();
-  auto switch_op = std::make_shared<FieldOperation>(FieldOperation(
-      std::move(switch_fn),
-      {std::move(switches_field), std::move(falses_field), std::move(trues_field)}));
+    auto switch_fn = std::make_unique<SwitchFieldsFunction<T>>();
+    auto switch_op = std::make_shared<FieldOperation>(FieldOperation(
+        std::move(switch_fn),
+        {std::move(switches_field), std::move(falses_field), std::move(trues_field)}));
 
-  params.set_output(name_output, Field<T>(switch_op, 0));
+    params.set_output(name_output, Field<T>(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<Field<T>>(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<Field<T>>(name_false));
+    }
+  }
 }
 
 template<typename T> void switch_no_fields(GeoNodeExecParams &params, const StringRef suffix)



More information about the Bf-blender-cvs mailing list