[Bf-blender-cvs] [5aaa435ac73] geometry-nodes-simulation: Simulation output and input nodes

Hans Goudey noreply at git.blender.org
Thu Nov 24 00:20:06 CET 2022


Commit: 5aaa435ac73e89ceae6843c8f82b93547f501d2f
Author: Hans Goudey
Date:   Tue Nov 22 18:23:26 2022 -0600
Branches: geometry-nodes-simulation
https://developer.blender.org/rB5aaa435ac73e89ceae6843c8f82b93547f501d2f

Simulation output and input nodes

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

M	release/scripts/startup/bl_ui/node_add_menu_geometry.py
A	source/blender/blenkernel/BKE_compute_cache.hh
M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.cc
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/nodes/NOD_geometry.h
M	source/blender/nodes/NOD_geometry_nodes_lazy_function.hh
M	source/blender/nodes/NOD_static_types.h
M	source/blender/nodes/geometry/CMakeLists.txt
A	source/blender/nodes/geometry/nodes/node_geo_simulation_input.cc
A	source/blender/nodes/geometry/nodes/node_geo_simulation_output.cc

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

diff --git a/release/scripts/startup/bl_ui/node_add_menu_geometry.py b/release/scripts/startup/bl_ui/node_add_menu_geometry.py
index 83448f8e32a..bf014e52634 100644
--- a/release/scripts/startup/bl_ui/node_add_menu_geometry.py
+++ b/release/scripts/startup/bl_ui/node_add_menu_geometry.py
@@ -291,6 +291,17 @@ class NODE_MT_category_GEO_POINT(Menu):
         node_add_menu.draw_assets_for_catalog(layout, self.bl_label)
 
 
+class NODE_MT_category_simulation(Menu):
+    bl_idname = "NODE_MT_category_simulation"
+    bl_label = "Simulation"
+
+    def draw(self, _context):
+        layout = self.layout
+        node_add_menu.add_node_type(layout, "GeometryNodeSimulationInput")
+        node_add_menu.add_node_type(layout, "GeometryNodeSimulationOutput")
+        node_add_menu.draw_assets_for_catalog(layout, self.bl_label)
+
+
 class NODE_MT_category_GEO_TEXT(Menu):
     bl_idname = "NODE_MT_category_GEO_TEXT"
     bl_label = "Text"
@@ -428,6 +439,7 @@ class NODE_MT_geometry_node_add_all(Menu):
         layout.menu("NODE_MT_geometry_node_mesh_topology")
         layout.menu("NODE_MT_category_GEO_OUTPUT")
         layout.menu("NODE_MT_category_GEO_POINT")
+        layout.menu("NODE_MT_category_simulation")
         layout.menu("NODE_MT_category_GEO_TEXT")
         layout.menu("NODE_MT_category_GEO_TEXTURE")
         layout.menu("NODE_MT_category_GEO_UTILITIES")
@@ -455,6 +467,7 @@ classes = (
     NODE_MT_geometry_node_mesh_topology,
     NODE_MT_category_GEO_OUTPUT,
     NODE_MT_category_GEO_POINT,
+    NODE_MT_category_simulation,
     NODE_MT_category_GEO_TEXT,
     NODE_MT_category_GEO_TEXTURE,
     NODE_MT_category_GEO_UTILITIES,
diff --git a/source/blender/blenkernel/BKE_compute_cache.hh b/source/blender/blenkernel/BKE_compute_cache.hh
new file mode 100644
index 00000000000..2dc07594a5c
--- /dev/null
+++ b/source/blender/blenkernel/BKE_compute_cache.hh
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include "BLI_compute_context.hh"
+#include "BLI_map.hh"
+
+#include "BKE_geometry_set.hh"
+
+namespace blender::bke {
+
+struct CacheData {
+  Map<int, GeometrySet> geometry_per_frame;
+};
+
+struct ComputeCaches {
+  Map<ComputeContextHash, CacheData> cache_per_context;
+};
+
+}  // namespace blender::bke
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index dd035dbf537..96adbbef507 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1396,6 +1396,9 @@ struct TexResult;
 /** \name Geometry Nodes
  * \{ */
 
+#define GEO_NODE_SIMULATION_INPUT 1198
+#define GEO_NODE_SIMULATION_OUTPUT 1199
+
 #define GEO_NODE_TRIANGULATE 1000
 #define GEO_NODE_TRANSFORM 1002
 #define GEO_NODE_MESH_BOOLEAN 1003
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index d8451cab0f5..958f532eb28 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -2213,7 +2213,7 @@ bNode *nodeAddNode(const struct bContext *C, bNodeTree *ntree, const char *idnam
 
   BKE_ntree_update_tag_node_new(ntree, node);
 
-  if (ELEM(node->type, GEO_NODE_INPUT_SCENE_TIME, GEO_NODE_SELF_OBJECT)) {
+  if (ELEM(node->type, GEO_NODE_INPUT_SCENE_TIME, GEO_NODE_SELF_OBJECT, GEO_NODE_SIMULATION_INPUT)) {
     DEG_relations_tag_update(CTX_data_main(C));
   }
 
@@ -3061,7 +3061,10 @@ void nodeRemoveNode(Main *bmain, bNodeTree *ntree, bNode *node, bool do_id_user)
 
   /* Also update relations for the scene time node, which causes a dependency
    * on time that users expect to be removed when the node is removed. */
-  if (node_has_id || ELEM(node->type, GEO_NODE_INPUT_SCENE_TIME, GEO_NODE_SELF_OBJECT)) {
+  if (node_has_id || ELEM(node->type,
+                          GEO_NODE_INPUT_SCENE_TIME,
+                          GEO_NODE_SELF_OBJECT,
+                          GEO_NODE_SIMULATION_INPUT)) {
     if (bmain != nullptr) {
       DEG_relations_tag_update(bmain);
     }
@@ -4771,6 +4774,8 @@ static void registerGeometryNodes()
   register_node_type_geo_set_shade_smooth();
   register_node_type_geo_set_spline_cyclic();
   register_node_type_geo_set_spline_resolution();
+  register_node_type_geo_simulation_input();
+  register_node_type_geo_simulation_output();
   register_node_type_geo_store_named_attribute();
   register_node_type_geo_string_join();
   register_node_type_geo_string_to_curves();
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index c4180071352..05ca4a6a7ae 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -10,6 +10,15 @@
 #include "DNA_listBase.h"
 #include "DNA_session_uuid_types.h"
 
+#ifdef __cplusplus
+namespace blender::bke {
+struct ComputeCaches;
+}
+using ComputeCachesHandle = blender::bke::ComputeCaches;
+#else
+typedef struct ComputeCachesHandle ComputeCachesHandle;
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -2295,7 +2304,9 @@ typedef struct NodesModifierData {
    * This can be used to help the user to debug a node tree.
    */
   void *runtime_eval_log;
-  void *_pad1;
+
+  /** #ComputeCaches. */
+  ComputeCachesHandle *simulation_caches;
 } NodesModifierData;
 
 typedef struct MeshToVolumeModifierData {
diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc
index c2b14262b4f..7dee87cc239 100644
--- a/source/blender/modifiers/intern/MOD_nodes.cc
+++ b/source/blender/modifiers/intern/MOD_nodes.cc
@@ -37,6 +37,7 @@
 #include "DNA_windowmanager_types.h"
 
 #include "BKE_attribute_math.hh"
+#include "BKE_compute_cache.hh"
 #include "BKE_compute_contexts.hh"
 #include "BKE_customdata.h"
 #include "BKE_geometry_fields.hh"
@@ -336,7 +337,7 @@ static bool check_tree_for_time_node(const bNodeTree &tree,
     return false;
   }
   LISTBASE_FOREACH (const bNode *, node, &tree.nodes) {
-    if (node->type == GEO_NODE_INPUT_SCENE_TIME) {
+    if (ELEM(node->type, GEO_NODE_INPUT_SCENE_TIME, GEO_NODE_SIMULATION_INPUT)) {
       return true;
     }
     if (node->type == NODE_GROUP) {
@@ -1106,6 +1107,7 @@ static GeometrySet compute_geometry(
     const blender::nodes::GeometryNodesLazyFunctionGraphInfo &lf_graph_info,
     const bNode &output_node,
     GeometrySet input_geometry_set,
+    blender::bke::ComputeCaches &compute_caches,
     NodesModifierData *nmd,
     const ModifierEvalContext *ctx)
 {
@@ -1133,6 +1135,7 @@ static GeometrySet compute_geometry(
   blender::nodes::GeoNodesModifierData geo_nodes_modifier_data;
   geo_nodes_modifier_data.depsgraph = ctx->depsgraph;
   geo_nodes_modifier_data.self_object = ctx->object;
+  geo_nodes_modifier_data.cache_per_frame = &compute_caches;
   auto eval_log = std::make_unique<GeoModifierLog>();
   if (logging_enabled(ctx)) {
     geo_nodes_modifier_data.eval_log = eval_log.get();
@@ -1301,8 +1304,24 @@ static void modifyGeometry(ModifierData *md,
     use_orig_index_polys = CustomData_has_layer(&mesh.pdata, CD_ORIGINDEX);
   }
 
-  geometry_set = compute_geometry(
-      tree, *lf_graph_info, *output_node, std::move(geometry_set), nmd, ctx);
+  NodesModifierData *orig_nmd = reinterpret_cast<NodesModifierData *>(
+      BKE_modifier_get_original(ctx->object, md));
+  if (!orig_nmd->simulation_caches) {
+    orig_nmd->simulation_caches = new blender::bke::ComputeCaches();
+  }
+
+  geometry_set = compute_geometry(tree,
+                                  *lf_graph_info,
+                                  *output_node,
+                                  std::move(geometry_set),
+                                  *orig_nmd->simulation_caches,
+                                  nmd,
+                                  ctx);
+
+  if (orig_nmd->simulation_caches->cache_per_context.is_empty()) {
+    delete orig_nmd->simulation_caches;
+    orig_nmd->simulation_caches = nullptr;
+  }
 
   if (geometry_set.has_mesh()) {
     /* Add #CD_ORIGINDEX layers if they don't exist already. This is required because the
@@ -1827,6 +1846,7 @@ static void blendWrite(BlendWriter *writer, const ID * /*id_owner*/, const Modif
      * and don't necessarily need to be written, but we can't just free them. */
     IDP_BlendWrite(writer, nmd->settings.properties);
   }
+  /* TODO: Write cached geometry. */
 }
 
 static void blendRead(BlendDataReader *reader, ModifierData *md)
@@ -1840,6 +1860,8 @@ static void blendRead(BlendDataReader *reader, ModifierData *md)
     IDP_BlendDataRead(reader, &nmd->settings.properties);
   }
   nmd->runtime_eval_log = nullptr;
+  /* TODO: Read cached geometry. */
+  nmd->simulation_caches = nullptr;
 }
 
 static void copyData(const ModifierData *md, ModifierData *target, const int flag)
@@ -1850,6 +1872,11 @@ static void copyData(const ModifierData *md, ModifierData *target, const int fla
   BKE_modifier_copydata_generic(md, target, flag);
 
   tnmd->runtime_eval_log = nullptr;
+  if (nmd->simulation_caches) {
+    const blender::bke::ComputeCaches &src_caches = *static_cast<blender::bke::ComputeCaches *>(
+        nmd->simulation_caches);
+    tnmd->simulation_caches = new blender::bke::ComputeCaches(src_caches);
+  }
 
   if (nmd->settings.properties != nullptr) {
     tnmd->settings.properties = IDP_CopyProperty_ex(nmd->settings.properties, flag);
@@ -1864,6 +1891,8 @@ static void freeData(ModifierData *md)
     nmd->settings.properties = nullptr;
   }
 
+  delete static_cast<blender::bke::ComputeCaches *>(nmd->simulation_caches);
+
   clear_runtime_data(nmd);
 }
 
diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h
index 6b886f01b0e..59b8e0bee73 100644
--- a/source/blender/nodes/NOD_geometry.h
+++ b/source/blender/nodes/NOD_geometry.h
@@ -151,6 +151,8 @@ void register_node_type_geo_set_position(void);
 void register_node_type_geo_set_shade_smooth(void);
 void register_node_type_geo_set_spline_cyclic(void);
 void register_node_type_geo_set_spline_resolution(void);
+void register_node_type_geo_simulation_input(void);
+void register_node_type_geo_simulation_output(void);
 void register_node_type_geo_store_named_attribute(void);
 void register_node_type_geo_string_join(void);
 void register_node_type_geo_string_t

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list