Blender V4.5
node_composite_mixrgb.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_assert.h"
10#include "BLI_math_vector.hh"
12
14
15#include "DNA_material_types.h"
16
17#include "BKE_material.hh"
18
19#include "GPU_material.hh"
20
22
23#include "NOD_multi_function.hh"
25
26#include "RNA_enum_types.hh"
27
29
30/* **************** MIX RGB ******************** */
31
33
35{
36 b.add_input<decl::Float>("Fac")
37 .default_value(1.0f)
38 .min(0.0f)
39 .max(1.0f)
41 .compositor_domain_priority(2);
42 b.add_input<decl::Color>("Image")
43 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
44 .compositor_domain_priority(0);
45 b.add_input<decl::Color>("Image", "Image_001")
46 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
47 .compositor_domain_priority(1);
48 b.add_output<decl::Color>("Image");
49}
50
51using namespace blender::compositor;
52
53static int get_mode(const bNode &node)
54{
55 return node.custom1;
56}
57
58static bool get_use_alpha(const bNode &node)
59{
60 return node.custom2 & SHD_MIXRGB_USE_ALPHA;
61}
62
63static bool get_should_clamp(const bNode &node)
64{
65 return node.custom2 & SHD_MIXRGB_CLAMP;
66}
67
68static const char *get_shader_function_name(const bNode &node)
69{
70 switch (get_mode(node)) {
71 case MA_RAMP_BLEND:
72 return "mix_blend";
73 case MA_RAMP_ADD:
74 return "mix_add";
75 case MA_RAMP_MULT:
76 return "mix_mult";
77 case MA_RAMP_SUB:
78 return "mix_sub";
79 case MA_RAMP_SCREEN:
80 return "mix_screen";
81 case MA_RAMP_DIV:
82 return "mix_div";
83 case MA_RAMP_DIFF:
84 return "mix_diff";
86 return "mix_exclusion";
87 case MA_RAMP_DARK:
88 return "mix_dark";
89 case MA_RAMP_LIGHT:
90 return "mix_light";
91 case MA_RAMP_OVERLAY:
92 return "mix_overlay";
93 case MA_RAMP_DODGE:
94 return "mix_dodge";
95 case MA_RAMP_BURN:
96 return "mix_burn";
97 case MA_RAMP_HUE:
98 return "mix_hue";
99 case MA_RAMP_SAT:
100 return "mix_sat";
101 case MA_RAMP_VAL:
102 return "mix_val";
103 case MA_RAMP_COLOR:
104 return "mix_color";
105 case MA_RAMP_SOFT:
106 return "mix_soft";
107 case MA_RAMP_LINEAR:
108 return "mix_linear";
109 }
110
112 return nullptr;
113}
114
115static int node_gpu_material(GPUMaterial *material,
116 bNode *node,
117 bNodeExecData * /*execdata*/,
120{
121 if (get_use_alpha(*node)) {
122 GPU_link(material,
123 "multiply_by_alpha",
124 get_shader_node_input_link(*node, inputs, "Fac"),
125 get_shader_node_input_link(*node, inputs, "Image_001"),
126 &get_shader_node_input(*node, inputs, "Fac").link);
127 }
128
129 const bool is_valid = GPU_stack_link(
130 material, node, get_shader_function_name(*node), inputs, outputs);
131
132 if (!is_valid || !get_should_clamp(*node)) {
133 return is_valid;
134 }
135
136 const float min[4] = {0.0f, 0.0f, 0.0f, 0.0f};
137 const float max[4] = {1.0f, 1.0f, 1.0f, 1.0f};
138 return GPU_link(material,
139 "clamp_color",
140 get_shader_node_output(*node, outputs, "Image").link,
143 &get_shader_node_output(*node, outputs, "Image").link);
144}
145
147{
148 const int mode = get_mode(builder.node());
149
150 if (get_use_alpha(builder.node())) {
151 if (get_should_clamp(builder.node())) {
153 return mf::build::SI3_SO<float, float4, float4, float4>(
154 "Mix RGB Alpha Clamp",
155 [=](const float factor, const float4 &color1, const float4 &color2) -> float4 {
156 const float alpha_factor = factor * color2.w;
157 float4 result = color1;
158 ramp_blend(mode, result, alpha_factor, color2);
159 return math::clamp(result, 0.0f, 1.0f);
160 },
161 mf::build::exec_presets::SomeSpanOrSingle<1, 2>());
162 });
163 }
164 else {
166 return mf::build::SI3_SO<float, float4, float4, float4>(
167 "Mix RGB Alpha",
168 [=](const float factor, const float4 &color1, const float4 &color2) -> float4 {
169 const float alpha_factor = factor * color2.w;
170 float4 result = color1;
171 ramp_blend(mode, result, alpha_factor, color2);
172 return result;
173 },
174 mf::build::exec_presets::SomeSpanOrSingle<1, 2>());
175 });
176 }
177 }
178 else {
179 if (get_should_clamp(builder.node())) {
181 return mf::build::SI3_SO<float, float4, float4, float4>(
182 "Mix RGB Clamp",
183 [=](const float factor, const float4 &color1, const float4 &color2) -> float4 {
184 float4 result = color1;
185 ramp_blend(mode, result, factor, color2);
186 return math::clamp(result, 0.0f, 1.0f);
187 },
188 mf::build::exec_presets::SomeSpanOrSingle<1, 2>());
189 });
190 }
191 else {
193 return mf::build::SI3_SO<float, float4, float4, float4>(
194 "Mix RGB",
195 [=](const float factor, const float4 &color1, const float4 &color2) -> float4 {
196 float4 result = color1;
197 ramp_blend(mode, result, factor, color2);
198 return result;
199 },
200 mf::build::exec_presets::SomeSpanOrSingle<1, 2>());
201 });
202 }
203 }
204}
205
206} // namespace blender::nodes::node_composite_mixrgb_cc
207
209{
211
212 static blender::bke::bNodeType ntype;
213
214 cmp_node_type_base(&ntype, "CompositorNodeMixRGB", CMP_NODE_MIX_RGB);
215 ntype.ui_name = "Mix";
216 ntype.ui_description = "Blend two images together using various blending modes";
217 ntype.enum_name_legacy = "MIX_RGB";
219 ntype.flag |= NODE_PREVIEW;
220 ntype.declare = file_ns::cmp_node_mixrgb_declare;
222 ntype.gpu_fn = file_ns::node_gpu_material;
223 ntype.build_multi_function = file_ns::node_build_multi_function;
224 ntype.gather_link_search_ops = nullptr;
225
227}
General operations, lookup, etc. for materials.
void ramp_blend(int type, float r_col[3], float fac, const float col[3])
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:435
#define CMP_NODE_MIX_RGB
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
@ MA_RAMP_LIGHT
@ MA_RAMP_COLOR
@ MA_RAMP_SAT
@ MA_RAMP_HUE
@ MA_RAMP_LINEAR
@ MA_RAMP_DIV
@ MA_RAMP_EXCLUSION
@ MA_RAMP_ADD
@ MA_RAMP_DODGE
@ MA_RAMP_SUB
@ MA_RAMP_SCREEN
@ MA_RAMP_SOFT
@ MA_RAMP_DARK
@ MA_RAMP_BURN
@ MA_RAMP_BLEND
@ MA_RAMP_VAL
@ MA_RAMP_OVERLAY
@ MA_RAMP_MULT
@ MA_RAMP_DIFF
@ NODE_PREVIEW
@ SHD_MIXRGB_USE_ALPHA
@ SHD_MIXRGB_CLAMP
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:239
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
GPUNodeStack & get_shader_node_input(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
GPUNodeStack & get_shader_node_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
GPUNodeLink * get_shader_node_input_link(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
T clamp(const T &a, const T &min, const T &max)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static const char * get_shader_function_name(const bNode &node)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static bool get_should_clamp(const bNode &node)
static void cmp_node_mixrgb_declare(NodeDeclarationBuilder &b)
static bool get_use_alpha(const bNode &node)
VecBase< float, 4 > float4
static void register_node_type_cmp_mix_rgb()
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[]
void node_blend_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:176
#define min(a, b)
Definition sort.cc:36
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:258
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
max
Definition text_draw.cc:251