Blender V5.0
node_composite_curves.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_base.hh"
10#include "BLI_math_vector.hh"
12
14
15#include "NOD_multi_function.hh"
16
17#include "DNA_color_types.h"
18
19#include "BKE_colortools.hh"
20#include "BKE_node.hh"
21
22#include "GPU_material.hh"
23
24#include "COM_node_operation.hh"
26
28
29/* **************** CURVE Time ******************** */
30
32
34{
35 b.add_input<decl::Int>("Start Frame").default_value(1);
36 b.add_input<decl::Int>("End Frame").default_value(250);
37
38 b.add_output<decl::Float>("Factor", "Fac");
39}
40
41static void node_composit_init_curves_time(bNodeTree * /*ntree*/, bNode *node)
42{
43 node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
44}
45
46using namespace blender::compositor;
47
49 public:
51
52 void execute() override
53 {
54 Result &result = this->get_result("Fac");
55 result.allocate_single_value();
56
57 CurveMapping *curve_mapping = const_cast<CurveMapping *>(this->get_curve_mapping());
58 BKE_curvemapping_init(curve_mapping);
59 const float time = BKE_curvemapping_evaluateF(
60 curve_mapping, 0, this->compute_normalized_time());
61 result.set_single_value(math::clamp(time, 0.0f, 1.0f));
62 }
63
65 {
66 const int frame_number = this->context().get_frame_number();
67 if (frame_number < this->get_start_frame()) {
68 return 0.0f;
69 }
70 if (frame_number > this->get_end_frame()) {
71 return 1.0f;
72 }
73 if (this->get_start_frame() == this->get_end_frame()) {
74 return 0.0f;
75 }
76 return float(frame_number - this->get_start_frame()) /
77 float(this->get_end_frame() - this->get_start_frame());
78 }
79
81 {
82 return this->get_input("Start Frame").get_single_value_default(1);
83 }
84
86 {
87 return this->get_input("End Frame").get_single_value_default(250);
88 }
89
91 {
92 return static_cast<const CurveMapping *>(bnode().storage);
93 }
94};
95
97{
98 return new TimeCurveOperation(context, node);
99}
100
101} // namespace blender::nodes::node_composite_time_curves_cc
102
104{
106
107 static blender::bke::bNodeType ntype;
108
109 cmp_node_type_base(&ntype, "CompositorNodeTime", CMP_NODE_TIME);
110 ntype.ui_name = "Time Curve";
111 ntype.ui_description =
112 "Generate a factor value (from 0.0 to 1.0) between scene start and end time, using a curve "
113 "mapping";
114 ntype.enum_name_legacy = "TIME";
115 ntype.nclass = NODE_CLASS_INPUT;
116 ntype.declare = file_ns::cmp_node_time_declare;
117 blender::bke::node_type_size(ntype, 200, 140, 320);
118 ntype.initfunc = file_ns::node_composit_init_curves_time;
120 ntype.get_compositor_operation = file_ns::get_compositor_operation;
121
123}
125
126/* **************** CURVE RGB ******************** */
127
129
131{
132 b.is_function_node();
133 b.add_input<decl::Color>("Image")
134 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
135 .description("Image/Color input on which RGB color transformation will be applied");
136 b.add_input<decl::Float>("Factor", "Fac")
137 .default_value(1.0f)
138 .min(0.0f)
139 .max(1.0f)
141 .description("Amount of influence the node exerts on the image");
142 b.add_input<decl::Color>("Black Level")
143 .default_value({0.0f, 0.0f, 0.0f, 1.0f})
144 .description("Input color that should be mapped to black");
145 b.add_input<decl::Color>("White Level")
146 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
147 .description("Input color that should be mapped to white");
148 b.add_output<decl::Color>("Image");
149}
150
151static void node_composit_init_curve_rgb(bNodeTree * /*ntree*/, bNode *node)
152{
153 node->storage = BKE_curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
154}
155
156using namespace blender::compositor;
157
159{
160 return static_cast<CurveMapping *>(node.storage);
161}
162
163static int node_gpu_material(GPUMaterial *material,
164 bNode *node,
165 bNodeExecData * /*execdata*/,
168{
169 CurveMapping *curve_mapping = get_curve_mapping(*node);
170
171 BKE_curvemapping_init(curve_mapping);
172 float *band_values;
173 int band_size;
174 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
175 float band_layer;
176 GPUNodeLink *band_texture = GPU_color_band(material, band_size, band_values, &band_layer);
177
178 float start_slopes[CM_TOT];
179 float end_slopes[CM_TOT];
180 BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
181 float range_minimums[CM_TOT];
182 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
183 float range_dividers[CM_TOT];
184 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
185
186 if (curve_mapping->tone == CURVE_TONE_FILMLIKE) {
187 return GPU_stack_link(material,
188 node,
189 "curves_film_like_compositor",
190 inputs,
191 outputs,
192 band_texture,
193 GPU_constant(&band_layer),
194 GPU_uniform(&range_minimums[3]),
195 GPU_uniform(&range_dividers[3]),
196 GPU_uniform(&start_slopes[3]),
197 GPU_uniform(&end_slopes[3]));
198 }
199
200 const float min = 0.0f;
201 const float max = 1.0f;
202 GPU_link(material,
203 "clamp_value",
204 get_shader_node_input_link(*node, inputs, "Fac"),
207 &get_shader_node_input(*node, inputs, "Fac").link);
208
209 /* If the RGB curves do nothing, use a function that skips RGB computations. */
210 if (BKE_curvemapping_is_map_identity(curve_mapping, 0) &&
211 BKE_curvemapping_is_map_identity(curve_mapping, 1) &&
212 BKE_curvemapping_is_map_identity(curve_mapping, 2))
213 {
214 return GPU_stack_link(material,
215 node,
216 "curves_combined_only_compositor",
217 inputs,
218 outputs,
219 band_texture,
220 GPU_constant(&band_layer),
221 GPU_uniform(&range_minimums[3]),
222 GPU_uniform(&range_dividers[3]),
223 GPU_uniform(&start_slopes[3]),
224 GPU_uniform(&end_slopes[3]));
225 }
226
227 return GPU_stack_link(material,
228 node,
229 "curves_combined_rgb_compositor",
230 inputs,
231 outputs,
232 band_texture,
233 GPU_constant(&band_layer),
234 GPU_uniform(range_minimums),
235 GPU_uniform(range_dividers),
236 GPU_uniform(start_slopes),
237 GPU_uniform(end_slopes));
238}
239
241{
242 CurveMapping *curve_mapping = get_curve_mapping(builder.node());
243 BKE_curvemapping_init(curve_mapping);
244 BKE_curvemapping_premultiply(curve_mapping, false);
245
247 return mf::build::SI4_SO<float4, float, float4, float4, float4>(
248 "RGB Curves",
249 [=](const float4 &color, const float factor, const float4 &black, const float4 &white)
250 -> float4 {
251 float3 black_white_scale;
252 BKE_curvemapping_set_black_white_ex(black, white, black_white_scale);
253
256 curve_mapping, result, color, black, black_white_scale);
257 return float4(math::interpolate(color.xyz(), result, math::clamp(factor, 0.0f, 1.0f)),
258 color.w);
259 },
260 mf::build::exec_presets::SomeSpanOrSingle<0>());
261 });
262}
263
264} // namespace blender::nodes::node_composite_rgb_curves_cc
265
267{
269
270 static blender::bke::bNodeType ntype;
271
272 cmp_node_type_base(&ntype, "CompositorNodeCurveRGB", CMP_NODE_CURVE_RGB);
273 ntype.ui_name = "RGB Curves";
274 ntype.ui_description = "Perform level adjustments on each color channel of an image";
275 ntype.enum_name_legacy = "CURVE_RGB";
277 ntype.declare = file_ns::cmp_node_rgbcurves_declare;
278 blender::bke::node_type_size(ntype, 200, 140, 320);
279 ntype.initfunc = file_ns::node_composit_init_curve_rgb;
281 ntype.gpu_fn = file_ns::node_gpu_material;
282 ntype.build_multi_function = file_ns::node_build_multi_function;
283
285}
void BKE_curvemapping_compute_range_dividers(const CurveMapping *curve_mapping, float dividers[4])
void BKE_curvemapping_premultiply(CurveMapping *cumap, bool restore)
void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[4])
void BKE_curvemapping_evaluate_premulRGBF_ex(const CurveMapping *cumap, float vecout[3], const float vecin[3], const float black[3], const float bwmul[3])
bool BKE_curvemapping_is_map_identity(const CurveMapping *curve_mapping, int index)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_set_black_white_ex(const float black[3], const float white[3], float r_bwmul[3])
void BKE_curvemapping_init(CurveMapping *cumap)
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:89
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
void BKE_curvemapping_compute_slopes(const CurveMapping *curve_mapping, float start_slopes[4], float end_slopes[4])
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:449
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define CMP_NODE_TIME
#define CMP_NODE_CURVE_RGB
#define CM_TOT
@ CURVE_TONE_FILMLIKE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_uniform(const float *num)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FACTOR
Definition RNA_types.hh:251
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
T get_single_value_default(const T &default_value) const
void set_single_value(const T &value)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
nullptr float
VecBase< float, 4 > float4
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5384
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:5414
GPUNodeStack & get_shader_node_input(const bNode &node, GPUNodeStack inputs[], 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)
T interpolate(const T &a, const T &b, const FactorT &t)
static CurveMapping * get_curve_mapping(const bNode &node)
static void cmp_node_rgbcurves_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(blender::nodes::NodeMultiFunctionBuilder &builder)
static void node_composit_init_curve_rgb(bNodeTree *, bNode *node)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void cmp_node_time_declare(NodeDeclarationBuilder &b)
static void node_composit_init_curves_time(bNodeTree *, bNode *node)
VecBase< float, 4 > float4
VecBase< float, 3 > float3
static void register_node_type_cmp_curve_time()
static void register_node_type_cmp_curve_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_copy_curves(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:49
void node_free_curves(bNode *node)
Definition node_util.cc:37
#define min(a, b)
Definition sort.cc:36
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
max
Definition text_draw.cc:251