From 0a52f2203ec9e1888c00a9f5db2f0c910db75b6a Mon Sep 17 00:00:00 2001 From: Omar Emara Date: Mon, 28 Oct 2024 12:56:10 +0300 Subject: [PATCH] Compositor: Implement Luminance Matte node for new CPU compositor Reference #125968. --- .../nodes/node_composite_luma_matte.cc | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/source/blender/nodes/composite/nodes/node_composite_luma_matte.cc b/source/blender/nodes/composite/nodes/node_composite_luma_matte.cc index d9edef9d6e9..76ac6f4df47 100644 --- a/source/blender/nodes/composite/nodes/node_composite_luma_matte.cc +++ b/source/blender/nodes/composite/nodes/node_composite_luma_matte.cc @@ -6,6 +6,8 @@ * \ingroup cmpnodes */ +#include "BLI_math_base.hh" +#include "BLI_math_vector.hh" #include "BLI_math_vector_types.hh" #include "FN_multi_function_builder.hh" @@ -59,6 +61,16 @@ static void node_composit_buts_luma_matte(uiLayout *layout, bContext * /*C*/, Po using namespace blender::realtime_compositor; +static float get_high(const bNode &node) +{ + return node_storage(node).t1; +} + +static float get_low(const bNode &node) +{ + return node_storage(node).t2; +} + class LuminanceMatteShaderNode : public ShaderNode { public: using ShaderNode::ShaderNode; @@ -68,8 +80,8 @@ class LuminanceMatteShaderNode : public ShaderNode { GPUNodeStack *inputs = get_inputs_array(); GPUNodeStack *outputs = get_outputs_array(); - const float high = get_high(); - const float low = get_low(); + const float high = get_high(bnode()); + const float low = get_low(bnode()); float luminance_coefficients[3]; IMB_colormanagement_get_luminance_coefficients(luminance_coefficients); @@ -82,16 +94,6 @@ class LuminanceMatteShaderNode : public ShaderNode { GPU_uniform(&low), GPU_constant(luminance_coefficients)); } - - float get_high() - { - return node_storage(bnode()).t1; - } - - float get_low() - { - return node_storage(bnode()).t2; - } }; static ShaderNode *get_compositor_shader_node(DNode node) @@ -101,15 +103,22 @@ static ShaderNode *get_compositor_shader_node(DNode node) static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder) { - /* Not yet implemented. Return zero. */ - static auto function = mf::build::SI1_SO2( - "Luminance Key", - [](const float4 & /*color*/, float4 &output_color, float &matte) -> void { - output_color = float4(0.0f); - matte = 0.0f; - }, - mf::build::exec_presets::AllSpanOrSingle()); - builder.set_matching_fn(function); + const float high = get_high(builder.node()); + const float low = get_low(builder.node()); + float3 luminance_coefficients; + IMB_colormanagement_get_luminance_coefficients(luminance_coefficients); + + builder.construct_and_set_matching_fn_cb([=]() { + return mf::build::SI1_SO2( + "Luminance Key", + [=](const float4 &color, float4 &result, float &matte) -> void { + float luminance = math::dot(color.xyz(), luminance_coefficients); + float alpha = math::clamp((luminance - low) / (high - low), 0.0f, 1.0f); + matte = math::min(alpha, color.w); + result = color * matte; + }, + mf::build::exec_presets::AllSpanOrSingle()); + }); } } // namespace blender::nodes::node_composite_luma_matte_cc