From ce0b3d98205dabaed87f6212aa0f0f1f2656092b Mon Sep 17 00:00:00 2001 From: Omar Emara Date: Thu, 24 Oct 2024 11:13:07 +0300 Subject: [PATCH] Refactor: Move common code into small functions --- .../nodes/node_composite_brightness.cc | 14 +++++----- .../nodes/node_composite_map_range.cc | 14 +++++----- .../nodes/node_composite_map_value.cc | 28 +++++++++---------- .../composite/nodes/node_composite_normal.cc | 23 +++++++-------- 4 files changed, 38 insertions(+), 41 deletions(-) diff --git a/source/blender/nodes/composite/nodes/node_composite_brightness.cc b/source/blender/nodes/composite/nodes/node_composite_brightness.cc index 4c9129169bb..3807664a74c 100644 --- a/source/blender/nodes/composite/nodes/node_composite_brightness.cc +++ b/source/blender/nodes/composite/nodes/node_composite_brightness.cc @@ -50,6 +50,11 @@ static void node_composit_buts_brightcontrast(uiLayout *layout, bContext * /*C*/ using namespace blender::realtime_compositor; +static bool get_use_premultiply(const bNode &node) +{ + return node.custom1; +} + class BrightContrastShaderNode : public ShaderNode { public: using ShaderNode::ShaderNode; @@ -59,7 +64,7 @@ class BrightContrastShaderNode : public ShaderNode { GPUNodeStack *inputs = get_inputs_array(); GPUNodeStack *outputs = get_outputs_array(); - const float use_premultiply = get_use_premultiply(); + const float use_premultiply = get_use_premultiply(bnode()); GPU_stack_link(material, &bnode(), @@ -68,11 +73,6 @@ class BrightContrastShaderNode : public ShaderNode { outputs, GPU_constant(&use_premultiply)); } - - bool get_use_premultiply() - { - return bnode().custom1; - } }; static ShaderNode *get_compositor_shader_node(DNode node) @@ -131,7 +131,7 @@ static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder & }, mf::build::exec_presets::SomeSpanOrSingle<0>()); - const bool use_premultiply = builder.node().custom1; + const bool use_premultiply = get_use_premultiply(builder.node()); if (use_premultiply) { builder.set_matching_fn(premultiply_used_function); } diff --git a/source/blender/nodes/composite/nodes/node_composite_map_range.cc b/source/blender/nodes/composite/nodes/node_composite_map_range.cc index 45c132c6040..39fcaa47b12 100644 --- a/source/blender/nodes/composite/nodes/node_composite_map_range.cc +++ b/source/blender/nodes/composite/nodes/node_composite_map_range.cc @@ -65,6 +65,11 @@ static void node_composit_buts_map_range(uiLayout *layout, bContext * /*C*/, Poi using namespace blender::realtime_compositor; +static bool get_should_clamp(const bNode &node) +{ + return node.custom1; +} + class MapRangeShaderNode : public ShaderNode { public: using ShaderNode::ShaderNode; @@ -74,7 +79,7 @@ class MapRangeShaderNode : public ShaderNode { GPUNodeStack *inputs = get_inputs_array(); GPUNodeStack *outputs = get_outputs_array(); - const float should_clamp = get_should_clamp(); + const float should_clamp = get_should_clamp(bnode()); GPU_stack_link(material, &bnode(), @@ -83,11 +88,6 @@ class MapRangeShaderNode : public ShaderNode { outputs, GPU_constant(&should_clamp)); } - - bool get_should_clamp() - { - return bnode().custom1; - } }; static ShaderNode *get_compositor_shader_node(DNode node) @@ -158,7 +158,7 @@ static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder & }, mf::build::exec_presets::SomeSpanOrSingle<0>()); - const bool should_clamp = builder.node().custom1; + const bool should_clamp = get_should_clamp(builder.node()); if (should_clamp) { builder.set_matching_fn(clamp_function); } diff --git a/source/blender/nodes/composite/nodes/node_composite_map_value.cc b/source/blender/nodes/composite/nodes/node_composite_map_value.cc index f34f4230195..5454f0169be 100644 --- a/source/blender/nodes/composite/nodes/node_composite_map_value.cc +++ b/source/blender/nodes/composite/nodes/node_composite_map_value.cc @@ -67,6 +67,16 @@ static void node_composit_buts_map_value(uiLayout *layout, bContext * /*C*/, Poi using namespace blender::realtime_compositor; +static bool get_use_min(const bNode &node) +{ + return node_storage(node).flag & TEXMAP_CLIP_MIN; +} + +static bool get_use_max(const bNode &node) +{ + return node_storage(node).flag & TEXMAP_CLIP_MAX; +} + class MapValueShaderNode : public ShaderNode { public: using ShaderNode::ShaderNode; @@ -78,8 +88,8 @@ class MapValueShaderNode : public ShaderNode { const TexMapping &texture_mapping = node_storage(bnode()); - const float use_min = get_use_min(); - const float use_max = get_use_max(); + const float use_min = get_use_min(bnode()); + const float use_max = get_use_max(bnode()); GPU_stack_link(material, &bnode(), @@ -93,16 +103,6 @@ class MapValueShaderNode : public ShaderNode { GPU_constant(&use_max), GPU_uniform(texture_mapping.max)); } - - bool get_use_min() - { - return node_storage(bnode()).flag & TEXMAP_CLIP_MIN; - } - - bool get_use_max() - { - return node_storage(bnode()).flag & TEXMAP_CLIP_MAX; - } }; static ShaderNode *get_compositor_shader_node(DNode node) @@ -138,8 +138,8 @@ static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder & const float size = texture_mapping.size[0]; const float min = texture_mapping.min[0]; const float max = texture_mapping.max[0]; - const bool use_min = texture_mapping.flag & TEXMAP_CLIP_MIN; - const bool use_max = texture_mapping.flag & TEXMAP_CLIP_MAX; + const bool use_min = get_use_min(builder.node()); + const bool use_max = get_use_max(builder.node()); if (use_min) { if (use_max) { diff --git a/source/blender/nodes/composite/nodes/node_composite_normal.cc b/source/blender/nodes/composite/nodes/node_composite_normal.cc index 8009ea86a13..d6928c159b8 100644 --- a/source/blender/nodes/composite/nodes/node_composite_normal.cc +++ b/source/blender/nodes/composite/nodes/node_composite_normal.cc @@ -41,6 +41,14 @@ static void cmp_node_normal_declare(NodeDeclarationBuilder &b) using namespace blender::realtime_compositor; +/* The vector value is stored in the default value of the output socket. */ +static float3 get_vector_value(const bNode &node) +{ + const bNodeSocket &normal_output = node.output_by_identifier("Normal"); + const float3 node_normal = normal_output.default_value_typed()->value; + return math::normalize(node_normal); +} + class NormalShaderNode : public ShaderNode { public: using ShaderNode::ShaderNode; @@ -55,16 +63,7 @@ class NormalShaderNode : public ShaderNode { "node_composite_normal", inputs, outputs, - GPU_uniform(get_vector_value())); - } - - /* The vector value is stored in the default value of the output socket. */ - const float *get_vector_value() - { - return node() - .output_by_identifier("Normal") - ->default_value_typed() - ->value; + GPU_uniform(get_vector_value(bnode()))); } }; @@ -75,9 +74,7 @@ static ShaderNode *get_compositor_shader_node(DNode node) static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder) { - const bNodeSocket &normal_output = builder.node().output_by_identifier("Normal"); - const float3 node_normal = normal_output.default_value_typed()->value; - const float3 normalized_node_normal = math::normalize(node_normal); + const float3 normalized_node_normal = get_vector_value(builder.node()); builder.construct_and_set_matching_fn_cb([=]() { return mf::build::SI1_SO2(