[Bf-blender-cvs] [2309fa20af4] master: Cleanup: Add macro and functions for node storage

Hans Goudey noreply at git.blender.org
Tue Dec 7 15:09:37 CET 2021


Commit: 2309fa20af416d479fc220d0841483eb3bcf55b0
Author: Hans Goudey
Date:   Tue Dec 7 09:09:30 2021 -0500
Branches: master
https://developer.blender.org/rB2309fa20af416d479fc220d0841483eb3bcf55b0

Cleanup: Add macro and functions for node storage

The `node_storage` functions to retrieve const and mutable structs
from a node are generated by a short macro that can be placed at the
top of each relevant file. I use this in D8286 to make code snippets
in the socket declarations much shorter, but I thought it would be
good to use it consistently everywhere else too.

The functions are also useful to avoid copy and paste errors,
like the one corrected in the cylinder node in this commit.

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

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/nodes/function/nodes/node_fn_random_value.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc
M	source/blender/nodes/geometry/nodes/node_geo_collection_info.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_fill.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_circle.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_line.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_primitive_quadrilateral.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_sample.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_set_handles.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_spline_type.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_to_points.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
M	source/blender/nodes/geometry/nodes/node_geo_delete_geometry.cc
M	source/blender/nodes/geometry/nodes/node_geo_image_texture.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cylinder.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_line.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_to_points.cc
M	source/blender/nodes/geometry/nodes/node_geo_object_info.cc
M	source/blender/nodes/geometry/nodes/node_geo_points_to_volume.cc
M	source/blender/nodes/geometry/nodes/node_geo_proximity.cc
M	source/blender/nodes/geometry/nodes/node_geo_raycast.cc
M	source/blender/nodes/geometry/nodes/node_geo_separate_geometry.cc
M	source/blender/nodes/geometry/nodes/node_geo_set_curve_handles.cc
M	source/blender/nodes/geometry/nodes/node_geo_string_to_curves.cc
M	source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc
M	source/blender/nodes/geometry/nodes/node_geo_switch.cc
M	source/blender/nodes/geometry/nodes/node_geo_transfer_attribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_viewer.cc
M	source/blender/nodes/geometry/nodes/node_geo_volume_to_mesh.cc
M	source/blender/nodes/shader/nodes/node_shader_tex_musgrave.cc
M	source/blender/nodes/shader/nodes/node_shader_tex_noise.cc
M	source/blender/nodes/shader/nodes/node_shader_tex_voronoi.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 34bb011fef1..f460aaf0d6a 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1759,3 +1759,13 @@ extern struct bNodeSocketType NodeSocketTypeUndefined;
 #ifdef __cplusplus
 }
 #endif
+
+#define NODE_STORAGE_FUNCS(StorageT) \
+  [[maybe_unused]] static StorageT &node_storage(bNode &node) \
+  { \
+    return *static_cast<StorageT *>(node.storage); \
+  } \
+  [[maybe_unused]] static const StorageT &node_storage(const bNode &node) \
+  { \
+    return *static_cast<const StorageT *>(node.storage); \
+  }
diff --git a/source/blender/nodes/function/nodes/node_fn_random_value.cc b/source/blender/nodes/function/nodes/node_fn_random_value.cc
index 9720a39b740..61c3aeeb1f4 100644
--- a/source/blender/nodes/function/nodes/node_fn_random_value.cc
+++ b/source/blender/nodes/function/nodes/node_fn_random_value.cc
@@ -24,6 +24,8 @@
 
 namespace blender::nodes {
 
+NODE_STORAGE_FUNCS(NodeRandomValue)
+
 static void fn_node_random_value_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Vector>(N_("Min")).supports_field();
@@ -65,7 +67,7 @@ static void fn_node_random_value_init(bNodeTree *UNUSED(tree), bNode *node)
 
 static void fn_node_random_value_update(bNodeTree *ntree, bNode *node)
 {
-  const NodeRandomValue &storage = *(const NodeRandomValue *)node->storage;
+  const NodeRandomValue &storage = node_storage(*node);
   const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
 
   bNodeSocket *sock_min_vector = (bNodeSocket *)node->inputs.first;
@@ -251,7 +253,7 @@ class RandomBoolFunction : public fn::MultiFunction {
 
 static void fn_node_random_value_build_multi_function(NodeMultiFunctionBuilder &builder)
 {
-  const NodeRandomValue &storage = *(const NodeRandomValue *)builder.node().storage;
+  const NodeRandomValue &storage = node_storage(builder.node());
   const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
 
   switch (data_type) {
diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc
index 7229fda7a5c..cf20ddacca7 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_capture.cc
@@ -23,6 +23,8 @@
 
 namespace blender::nodes::node_geo_attribute_capture_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryAttributeCapture)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Geometry>(N_("Geometry"));
@@ -60,8 +62,7 @@ static void node_init(bNodeTree *UNUSED(tree), bNode *node)
 
 static void node_update(bNodeTree *ntree, bNode *node)
 {
-  const NodeGeometryAttributeCapture &storage = *(const NodeGeometryAttributeCapture *)
-                                                     node->storage;
+  const NodeGeometryAttributeCapture &storage = node_storage(*node);
   const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
 
   bNodeSocket *socket_value_geometry = (bNodeSocket *)node->inputs.first;
@@ -115,9 +116,7 @@ static void node_geo_exec(GeoNodeExecParams params)
 {
   GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
 
-  const bNode &node = params.node();
-  const NodeGeometryAttributeCapture &storage = *(const NodeGeometryAttributeCapture *)
-                                                     node.storage;
+  const NodeGeometryAttributeCapture &storage = node_storage(params.node());
   const CustomDataType data_type = static_cast<CustomDataType>(storage.data_type);
   const AttributeDomain domain = static_cast<AttributeDomain>(storage.domain);
 
diff --git a/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc b/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc
index d44b42f9e0a..6b8c895879d 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_collection_info.cc
@@ -29,6 +29,8 @@
 
 namespace blender::nodes::node_geo_collection_info_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryCollectionInfo)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Collection>(N_("Collection")).hide_label();
@@ -78,9 +80,8 @@ static void node_geo_exec(GeoNodeExecParams params)
     return;
   }
 
-  const bNode &bnode = params.node();
-  NodeGeometryCollectionInfo *node_storage = (NodeGeometryCollectionInfo *)bnode.storage;
-  const bool use_relative_transform = (node_storage->transform_space ==
+  const NodeGeometryCollectionInfo &storage = node_storage(params.node());
+  const bool use_relative_transform = (storage.transform_space ==
                                        GEO_NODE_TRANSFORM_SPACE_RELATIVE);
 
   GeometrySet geometry_set_out;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_fill.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_fill.cc
index 4130cf99516..3aabf8e21eb 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_fill.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_fill.cc
@@ -33,6 +33,8 @@
 
 namespace blender::nodes::node_geo_curve_fill_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryCurveFill)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Geometry>(N_("Curve")).supported_type(GEO_COMPONENT_TYPE_CURVE);
@@ -151,7 +153,7 @@ static void node_geo_exec(GeoNodeExecParams params)
 {
   GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
 
-  const NodeGeometryCurveFill &storage = *(const NodeGeometryCurveFill *)params.node().storage;
+  const NodeGeometryCurveFill &storage = node_storage(params.node());
   const GeometryNodeCurveFillMode mode = (GeometryNodeCurveFillMode)storage.mode;
 
   geometry_set.modify_geometry_sets(
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc
index b279ad23ff7..7fbcc47c708 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_fillet.cc
@@ -27,6 +27,8 @@
 
 namespace blender::nodes::node_geo_curve_fillet_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryCurveFillet)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Geometry>(N_("Curve")).supported_type(GEO_COMPONENT_TYPE_CURVE);
@@ -78,8 +80,8 @@ struct FilletData {
 
 static void node_update(bNodeTree *ntree, bNode *node)
 {
-  NodeGeometryCurveFillet &node_storage = *(NodeGeometryCurveFillet *)node->storage;
-  const GeometryNodeCurveFilletMode mode = (GeometryNodeCurveFilletMode)node_storage.mode;
+  const NodeGeometryCurveFillet &storage = node_storage(*node);
+  const GeometryNodeCurveFilletMode mode = (GeometryNodeCurveFilletMode)storage.mode;
 
   bNodeSocket *poly_socket = ((bNodeSocket *)node->inputs.first)->next;
 
@@ -614,8 +616,8 @@ static void node_geo_exec(GeoNodeExecParams params)
 {
   GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
 
-  NodeGeometryCurveFillet &node_storage = *(NodeGeometryCurveFillet *)params.node().storage;
-  const GeometryNodeCurveFilletMode mode = (GeometryNodeCurveFilletMode)node_storage.mode;
+  const NodeGeometryCurveFillet &storage = node_storage(params.node());
+  const GeometryNodeCurveFilletMode mode = (GeometryNodeCurveFilletMode)storage.mode;
 
   Field<float> radius_field = params.extract_input<Field<float>>("Radius");
   const bool limit_radius = params.extract_input<bool>("Limit Radius");
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
index a4b080b11e7..381bb0fc1d0 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_handle_type_selection.cc
@@ -23,6 +23,8 @@
 
 namespace blender::nodes::node_geo_curve_handle_type_selection_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryCurveSelectHandles)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_output<decl::Bool>(N_("Selection")).field_source();
@@ -136,11 +138,10 @@ class HandleTypeFieldInput final : public GeometryFieldInput {
 
 static void node_geo_exec(GeoNodeExecParams params)
 {
-  const NodeGeometryCurveSelectHandles *storage =
-      (const NodeGeometryCurveSelectHandles *)params.node().storage;
+  const NodeGeometryCurveSelectHandles &storage = node_storage(params.node());
   const BezierSpline::HandleType handle_type = handle_type_from_input_type(
-      (GeometryNodeCurveHandleType)storage->handle_type);
-  const GeometryNodeCurveHandleMode mode = (GeometryNodeCurveHandleMode)storage->mode;
+      (GeometryNodeCurveHandleType)storage.handle_type);
+  const GeometryNodeCurveHandleMode mode = (GeometryNodeCurveHandleMode)storage.mode;
 
   Field<bool> selection_field{std::make_shared<HandleTypeFieldInput>(handle_type, mode)};
   params.set_output("Selection", std::move(selection_field));
diff --git a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
index a2ca41d8e19..4299b5cc022 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_curve_primitive_bezier_segment.cc
@@ -23,6 +23,8 @@
 
 namespace blender::nodes::node_geo_curve_primitive_bezier_segment_cc {
 
+NODE_STORAGE_FUNCS(NodeGeometryCurvePrimitiveBezierSegment)
+
 static void node_declare(NodeDeclarationBuilder &b)
 {
   b.add_input<decl::Int>(N_("Resolution"))
@@ -120,10 +122,9 @@ static std::unique_ptr<CurveEval> create_bezier_segment_curve(
 
 static void node_geo_exec(GeoNodeExecParams params)
 {
-  const NodeGeometryCurvePrimitiveBezierSegment *node_storage =
-      (NodeGeometryCurvePrimitiveBezierSegment *)params.node().storage;
+  const NodeGeometryCurvePrimitiveBezierSegment &storage = node_storage(params.node());
   const GeometryNodeCurvePrimitiveBezierSegmentMode mode =
-      (const GeometryNodeCurvePrimitiveBezierSegmentMode)node_storage->mo

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list