Compositor: Implement Luminance Matte node for new CPU compositor

Reference #125968.
This commit is contained in:
Omar Emara
2024-10-28 12:56:10 +03:00
parent 1c19dc9b20
commit 0a52f2203e

View File

@@ -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<float4, float4, float>(
"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<float4, float4, float>(
"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