diff options
author | Hans Goudey <h.goudey@me.com> | 2021-12-07 17:09:30 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-12-07 17:09:30 +0300 |
commit | 2309fa20af416d479fc220d0841483eb3bcf55b0 (patch) | |
tree | 5ece68f40841756e8a2293f926f8f93fcebbe7c5 /source/blender/nodes/shader | |
parent | 6a9775ec6fd38c26e66ca513cf2914d7344b8f51 (diff) |
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
Diffstat (limited to 'source/blender/nodes/shader')
3 files changed, 48 insertions, 39 deletions
diff --git a/source/blender/nodes/shader/nodes/node_shader_tex_musgrave.cc b/source/blender/nodes/shader/nodes/node_shader_tex_musgrave.cc index e426d9cc49c..9aee3c50689 100644 --- a/source/blender/nodes/shader/nodes/node_shader_tex_musgrave.cc +++ b/source/blender/nodes/shader/nodes/node_shader_tex_musgrave.cc @@ -21,6 +21,8 @@ #include "BLI_noise.hh" +NODE_STORAGE_FUNCS(NodeTexMusgrave) + namespace blender::nodes { static void sh_node_tex_musgrave_declare(NodeDeclarationBuilder &b) @@ -106,23 +108,23 @@ static int node_shader_gpu_tex_musgrave(GPUMaterial *mat, static void node_shader_update_tex_musgrave(bNodeTree *ntree, bNode *node) { - NodeTexMusgrave *tex = (NodeTexMusgrave *)node->storage; + const NodeTexMusgrave &storage = node_storage(*node); bNodeSocket *inVectorSock = nodeFindSocket(node, SOCK_IN, "Vector"); bNodeSocket *inWSock = nodeFindSocket(node, SOCK_IN, "W"); bNodeSocket *inOffsetSock = nodeFindSocket(node, SOCK_IN, "Offset"); bNodeSocket *inGainSock = nodeFindSocket(node, SOCK_IN, "Gain"); - nodeSetSocketAvailability(ntree, inVectorSock, tex->dimensions != 1); - nodeSetSocketAvailability(ntree, inWSock, tex->dimensions == 1 || tex->dimensions == 4); + nodeSetSocketAvailability(ntree, inVectorSock, storage.dimensions != 1); + nodeSetSocketAvailability(ntree, inWSock, storage.dimensions == 1 || storage.dimensions == 4); nodeSetSocketAvailability(ntree, inOffsetSock, - tex->musgrave_type != SHD_MUSGRAVE_MULTIFRACTAL && - tex->musgrave_type != SHD_MUSGRAVE_FBM); + storage.musgrave_type != SHD_MUSGRAVE_MULTIFRACTAL && + storage.musgrave_type != SHD_MUSGRAVE_FBM); nodeSetSocketAvailability(ntree, inGainSock, - tex->musgrave_type == SHD_MUSGRAVE_HYBRID_MULTIFRACTAL || - tex->musgrave_type == SHD_MUSGRAVE_RIDGED_MULTIFRACTAL); + storage.musgrave_type == SHD_MUSGRAVE_HYBRID_MULTIFRACTAL || + storage.musgrave_type == SHD_MUSGRAVE_RIDGED_MULTIFRACTAL); bNodeSocket *outFacSock = nodeFindSocket(node, SOCK_OUT, "Fac"); node_sock_label(outFacSock, "Height"); diff --git a/source/blender/nodes/shader/nodes/node_shader_tex_noise.cc b/source/blender/nodes/shader/nodes/node_shader_tex_noise.cc index 5f846207673..b4e7e52964a 100644 --- a/source/blender/nodes/shader/nodes/node_shader_tex_noise.cc +++ b/source/blender/nodes/shader/nodes/node_shader_tex_noise.cc @@ -21,6 +21,8 @@ #include "BLI_noise.hh" +NODE_STORAGE_FUNCS(NodeTexNoise) + namespace blender::nodes { static void sh_node_tex_noise_declare(NodeDeclarationBuilder &b) @@ -71,8 +73,8 @@ static int node_shader_gpu_tex_noise(GPUMaterial *mat, node_shader_gpu_default_tex_coord(mat, node, &in[0].link); node_shader_gpu_tex_mapping(mat, node, in, out); - NodeTexNoise *tex = (NodeTexNoise *)node->storage; - const char *name = gpu_shader_get_name(tex->dimensions); + const NodeTexNoise &storage = node_storage(*node); + const char *name = gpu_shader_get_name(storage.dimensions); return GPU_stack_link(mat, node, name, in, out); } @@ -81,9 +83,9 @@ static void node_shader_update_tex_noise(bNodeTree *ntree, bNode *node) bNodeSocket *sockVector = nodeFindSocket(node, SOCK_IN, "Vector"); bNodeSocket *sockW = nodeFindSocket(node, SOCK_IN, "W"); - NodeTexNoise *tex = (NodeTexNoise *)node->storage; - nodeSetSocketAvailability(ntree, sockVector, tex->dimensions != 1); - nodeSetSocketAvailability(ntree, sockW, tex->dimensions == 1 || tex->dimensions == 4); + const NodeTexNoise &storage = node_storage(*node); + nodeSetSocketAvailability(ntree, sockVector, storage.dimensions != 1); + nodeSetSocketAvailability(ntree, sockW, storage.dimensions == 1 || storage.dimensions == 4); } namespace blender::nodes { @@ -241,9 +243,8 @@ class NoiseFunction : public fn::MultiFunction { static void sh_node_noise_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder) { - bNode &node = builder.node(); - NodeTexNoise *tex = (NodeTexNoise *)node.storage; - builder.construct_and_set_matching_fn<NoiseFunction>(tex->dimensions); + const NodeTexNoise &storage = node_storage(builder.node()); + builder.construct_and_set_matching_fn<NoiseFunction>(storage.dimensions); } } // namespace blender::nodes diff --git a/source/blender/nodes/shader/nodes/node_shader_tex_voronoi.cc b/source/blender/nodes/shader/nodes/node_shader_tex_voronoi.cc index 5d2654f61c8..dcb1c97f0e0 100644 --- a/source/blender/nodes/shader/nodes/node_shader_tex_voronoi.cc +++ b/source/blender/nodes/shader/nodes/node_shader_tex_voronoi.cc @@ -21,6 +21,8 @@ #include "BLI_noise.hh" +NODE_STORAGE_FUNCS(NodeTexVoronoi) + namespace blender::nodes { static void sh_node_tex_voronoi_declare(NodeDeclarationBuilder &b) @@ -136,33 +138,34 @@ static void node_shader_update_tex_voronoi(bNodeTree *ntree, bNode *node) bNodeSocket *outWSock = nodeFindSocket(node, SOCK_OUT, "W"); bNodeSocket *outRadiusSock = nodeFindSocket(node, SOCK_OUT, "Radius"); - NodeTexVoronoi *tex = (NodeTexVoronoi *)node->storage; + const NodeTexVoronoi &storage = node_storage(*node); - nodeSetSocketAvailability(ntree, inWSock, tex->dimensions == 1 || tex->dimensions == 4); - nodeSetSocketAvailability(ntree, inVectorSock, tex->dimensions != 1); + nodeSetSocketAvailability(ntree, inWSock, storage.dimensions == 1 || storage.dimensions == 4); + nodeSetSocketAvailability(ntree, inVectorSock, storage.dimensions != 1); nodeSetSocketAvailability( ntree, inExponentSock, - tex->distance == SHD_VORONOI_MINKOWSKI && tex->dimensions != 1 && - !ELEM(tex->feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS)); - nodeSetSocketAvailability(ntree, inSmoothnessSock, tex->feature == SHD_VORONOI_SMOOTH_F1); + storage.distance == SHD_VORONOI_MINKOWSKI && storage.dimensions != 1 && + !ELEM(storage.feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS)); + nodeSetSocketAvailability(ntree, inSmoothnessSock, storage.feature == SHD_VORONOI_SMOOTH_F1); - nodeSetSocketAvailability(ntree, outDistanceSock, tex->feature != SHD_VORONOI_N_SPHERE_RADIUS); + nodeSetSocketAvailability( + ntree, outDistanceSock, storage.feature != SHD_VORONOI_N_SPHERE_RADIUS); nodeSetSocketAvailability(ntree, outColorSock, - tex->feature != SHD_VORONOI_DISTANCE_TO_EDGE && - tex->feature != SHD_VORONOI_N_SPHERE_RADIUS); + storage.feature != SHD_VORONOI_DISTANCE_TO_EDGE && + storage.feature != SHD_VORONOI_N_SPHERE_RADIUS); nodeSetSocketAvailability(ntree, outPositionSock, - tex->feature != SHD_VORONOI_DISTANCE_TO_EDGE && - tex->feature != SHD_VORONOI_N_SPHERE_RADIUS && - tex->dimensions != 1); + storage.feature != SHD_VORONOI_DISTANCE_TO_EDGE && + storage.feature != SHD_VORONOI_N_SPHERE_RADIUS && + storage.dimensions != 1); nodeSetSocketAvailability(ntree, outWSock, - tex->feature != SHD_VORONOI_DISTANCE_TO_EDGE && - tex->feature != SHD_VORONOI_N_SPHERE_RADIUS && - (ELEM(tex->dimensions, 1, 4))); - nodeSetSocketAvailability(ntree, outRadiusSock, tex->feature == SHD_VORONOI_N_SPHERE_RADIUS); + storage.feature != SHD_VORONOI_DISTANCE_TO_EDGE && + storage.feature != SHD_VORONOI_N_SPHERE_RADIUS && + (ELEM(storage.dimensions, 1, 4))); + nodeSetSocketAvailability(ntree, outRadiusSock, storage.feature == SHD_VORONOI_N_SPHERE_RADIUS); } namespace blender::nodes { @@ -1304,20 +1307,23 @@ class VoronoiEdgeFunction : public fn::MultiFunction { static void sh_node_voronoi_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder) { - bNode &node = builder.node(); - NodeTexVoronoi *tex = (NodeTexVoronoi *)node.storage; - bool minowski = (tex->distance == SHD_VORONOI_MINKOWSKI && tex->dimensions != 1 && - !ELEM(tex->feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS)); - bool dist_radius = ELEM(tex->feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS); + const NodeTexVoronoi &storage = node_storage(builder.node()); + bool minowski = + (storage.distance == SHD_VORONOI_MINKOWSKI && storage.dimensions != 1 && + !ELEM(storage.feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS)); + bool dist_radius = ELEM( + storage.feature, SHD_VORONOI_DISTANCE_TO_EDGE, SHD_VORONOI_N_SPHERE_RADIUS); if (dist_radius) { - builder.construct_and_set_matching_fn<VoronoiEdgeFunction>(tex->dimensions, tex->feature); + builder.construct_and_set_matching_fn<VoronoiEdgeFunction>(storage.dimensions, + storage.feature); } else if (minowski) { - builder.construct_and_set_matching_fn<VoronoiMinowskiFunction>(tex->dimensions, tex->feature); + builder.construct_and_set_matching_fn<VoronoiMinowskiFunction>(storage.dimensions, + storage.feature); } else { builder.construct_and_set_matching_fn<VoronoiMetricFunction>( - tex->dimensions, tex->feature, tex->distance); + storage.dimensions, storage.feature, storage.distance); } } |