Blender V4.5
node_composite_alpha_over.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_vector.hh"
11
13
14#include "NOD_multi_function.hh"
15
16#include "UI_interface.hh"
17#include "UI_resources.hh"
18
19#include "GPU_material.hh"
20
22
23/* **************** ALPHAOVER ******************** */
24
26
28{
29 b.add_input<decl::Float>("Fac")
30 .default_value(1.0f)
31 .min(0.0f)
32 .max(1.0f)
34 .compositor_domain_priority(2);
35 b.add_input<decl::Color>("Image")
36 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
37 .compositor_domain_priority(0);
38 b.add_input<decl::Color>("Image", "Image_001")
39 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
40 .compositor_domain_priority(1);
41 b.add_input<decl::Bool>("Straight Alpha")
42 .default_value(false)
44 "Defines whether the foreground is in straight alpha form, which is necessary to know "
45 "for proper alpha compositing. Images in the compositor are in premultiplied alpha form "
46 "by default, so this should be false in most cases. But if, and only if, the foreground "
47 "was converted to straight alpha form for some reason, this should be set to true");
48 b.add_output<decl::Color>("Image");
49}
50
51using namespace blender::compositor;
52
53static int node_gpu_material(GPUMaterial *material,
54 bNode *node,
55 bNodeExecData * /*execdata*/,
58{
59 return GPU_stack_link(material, node, "node_composite_alpha_over", inputs, outputs);
60}
61
62/* Computes the Porter and Duff Over compositing operation. If straight_alpha is true, then the
63 * foreground is in straight alpha form and would need to be premultiplied. */
64static float4 alpha_over(const float factor,
65 const float4 &background,
66 const float4 &foreground,
67 const bool straight_alpha)
68{
69 /* Premultiply the alpha of the foreground if it is straight. */
70 const float alpha = math::clamp(foreground.w, 0.0f, 1.0f);
71 const float4 premultiplied_foreground = float4(foreground.xyz() * alpha, alpha);
72 const float4 foreground_color = straight_alpha ? premultiplied_foreground : foreground;
73
74 const float4 mix_result = background * (1.0f - alpha) + foreground_color;
75 return math::interpolate(background, mix_result, factor);
76}
77
79{
80 static auto function = mf::build::SI4_SO<float, float4, float4, bool, float4>(
81 "Alpha Over",
82 [=](const float factor,
83 const float4 &background,
84 const float4 &foreground,
85 const bool straight_alpha) -> float4 {
86 return alpha_over(factor, background, foreground, straight_alpha);
87 },
88 mf::build::exec_presets::SomeSpanOrSingle<1, 2>());
89 builder.set_matching_fn(function);
90}
91
92} // namespace blender::nodes::node_composite_alpha_over_cc
93
95{
97
98 static blender::bke::bNodeType ntype;
99
100 cmp_node_type_base(&ntype, "CompositorNodeAlphaOver", CMP_NODE_ALPHAOVER);
101 ntype.ui_name = "Alpha Over";
102 ntype.ui_description = "Overlay a foreground image onto a background image";
103 ntype.enum_name_legacy = "ALPHAOVER";
105 ntype.declare = file_ns::cmp_node_alphaover_declare;
106 ntype.gpu_fn = file_ns::node_gpu_material;
107 ntype.build_multi_function = file_ns::node_build_multi_function;
108
110}
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:435
#define CMP_NODE_ALPHAOVER
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
T clamp(const T &a, const T &min, const T &max)
T interpolate(const T &a, const T &b, const FactorT &t)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static float4 alpha_over(const float factor, const float4 &background, const float4 &foreground, const bool straight_alpha)
static void cmp_node_alphaover_declare(NodeDeclarationBuilder &b)
VecBase< float, 4 > float4
static void register_node_type_cmp_alphaover()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static blender::bke::bNodeSocketTemplate outputs[]
static blender::bke::bNodeSocketTemplate inputs[]
VecBase< T, 3 > xyz() const
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:344
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355