From a9970d3cb92baa4991e7825b36033d8d69df43bd Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 20 Aug 2021 10:44:29 +0200 Subject: bring back clamping in math node --- .../blender/nodes/shader/nodes/node_shader_math.cc | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/source/blender/nodes/shader/nodes/node_shader_math.cc b/source/blender/nodes/shader/nodes/node_shader_math.cc index 8e20a922c92..c30f2948ab1 100644 --- a/source/blender/nodes/shader/nodes/node_shader_math.cc +++ b/source/blender/nodes/shader/nodes/node_shader_math.cc @@ -106,13 +106,42 @@ static const blender::fn::MultiFunction *get_base_multi_function(bNode &node) return nullptr; } +class ClampWrapperFunction : public blender::fn::MultiFunction { + private: + const blender::fn::MultiFunction &fn_; + + public: + ClampWrapperFunction(const blender::fn::MultiFunction &fn) : fn_(fn) + { + this->set_signature(&fn.signature()); + } + + void call(blender::IndexMask mask, + blender::fn::MFParams params, + blender::fn::MFContext context) const override + { + fn_.call(mask, params, context); + + /* Assumes the output parameter is the last one. */ + const int output_param_index = this->param_amount() - 1; + /* This has actually been initialized in the call above. */ + blender::MutableSpan results = params.uninitialized_single_output( + output_param_index); + + for (const int i : mask) { + float &value = results[i]; + CLAMP(value, 0.0f, 1.0f); + } + } +}; + static void sh_node_math_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder) { const blender::fn::MultiFunction *base_function = get_base_multi_function(builder.node()); const bool clamp_output = builder.node().custom2 != 0; if (clamp_output) { - /* TODO */ + builder.construct_and_set_matching_fn(*base_function); } else { builder.set_matching_fn(base_function); -- cgit v1.2.3