Blender V5.3
node_shader_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10
11#include "BKE_colortools.hh"
12
13#include "BLI_math_vector_c.hh"
14
15#include "NOD_multi_function.hh"
16
17#include "node_util.hh"
18
19namespace blender {
20
22
24{
25 b.is_function_node();
26 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
27 .min(0.0f)
28 .max(1.0f)
29 .default_value(1.0f)
30 .subtype(PROP_FACTOR)
31 .no_muted_links()
32 .description("Amount of influence the node exerts on the output vector")
33 .compositor_domain_priority(1);
34 b.add_input<decl::Vector>("Vector"_ustr)
35 .min(-1.0f)
36 .max(1.0f)
37 .description("Vector which would be mapped to the curve")
38 .compositor_domain_priority(0);
39 b.add_output<decl::Vector>("Vector"_ustr);
40}
41
43{
44 node->storage = BKE_curvemapping_add(3, -1.0f, -1.0f, 1.0f, 1.0f);
45}
46
48 bNode *node,
49 bNodeExecData * /*execdata*/,
52{
53 CurveMapping *curve_mapping = static_cast<CurveMapping *>(node->storage);
54
55 BKE_curvemapping_init(curve_mapping);
56 float *band_values;
57 int band_size;
58 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
59 float band_layer;
60 GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
61
62 float start_slopes[CM_TOT];
63 float end_slopes[CM_TOT];
64 BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
65 float range_minimums[CM_TOT];
66 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
67 float range_dividers[CM_TOT];
68 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
69
70 return GPU_stack_link(mat,
71 node,
72 "curves_vector_mixed",
73 in,
74 out,
75 band_texture,
76 GPU_constant(&band_layer),
77 GPU_uniform(range_minimums),
78 GPU_uniform(range_dividers),
79 GPU_uniform(start_slopes),
80 GPU_uniform(end_slopes));
81}
82
83class CurveVecFunction : public mf::MultiFunction {
84 private:
86 std::shared_ptr<const bNodeTree> tree_;
87 const CurveMapping &cumap_;
88
89 public:
90 CurveVecFunction(const CurveMapping &cumap, std::shared_ptr<const bNodeTree> tree)
91 : tree_(std::move(tree)), cumap_(cumap)
92 {
93 static const mf::Signature signature = []() {
94 mf::Signature signature;
95 mf::SignatureBuilder builder{"Curve Vec", signature};
96 builder.single_input<float>("Fac");
97 builder.single_input<float3>("Vector");
98 builder.single_output<float3>("Vector");
99 return signature;
100 }();
101 this->set_signature(&signature);
102 }
103
104 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
105 {
106 const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
107 const VArray<float3> &vec_in = params.readonly_single_input<float3>(1, "Vector");
108 MutableSpan<float3> vec_out = params.uninitialized_single_output<float3>(2, "Vector");
109
110 mask.foreach_index([&](const int64_t i) {
111 BKE_curvemapping_evaluate3F(&cumap_, vec_out[i], vec_in[i]);
112 if (fac[i] != 1.0f) {
113 interp_v3_v3v3(vec_out[i], vec_in[i], vec_out[i], fac[i]);
114 }
115 });
116 }
117
118 void hash_unique(UniqueHashBytes &hash) const override
119 {
120 static constexpr int8_t id = 0;
121 hash.add(&id);
122 hash.add(&cumap_);
123 }
124};
125
127{
128 const bNode &bnode = builder.node();
129 CurveMapping *cumap = static_cast<CurveMapping *>(bnode.storage);
132}
133
135#ifdef WITH_MATERIALX
136{
137 /* TODO: implement */
138 return get_input_value("Vector", NodeItem::Type::Vector3);
139}
140#endif
142
143} // namespace nodes::node_shader_curves_cc::vec
144
146{
147 namespace file_ns = nodes::node_shader_curves_cc::vec;
148
149 static bke::bNodeType ntype;
150
151 common_node_type_base(&ntype, "ShaderNodeVectorCurve"_ustr, SH_NODE_CURVE_VEC);
152 ntype.ui_name = "Vector Curves";
153 ntype.ui_description = "Map input vector components with curves";
154 ntype.enum_name_legacy = "CURVE_VEC";
156 ntype.declare = file_ns::sh_node_curve_vec_declare;
157 ntype.initfunc = file_ns::node_shader_init_curve_vec;
160 ntype.gpu_fn = file_ns::gpu_shader_curve_vec;
161 ntype.build_multi_function = file_ns::sh_node_curve_vec_build_multi_function;
162 ntype.materialx_fn = file_ns::node_shader_materialx;
163
165}
166
167/* **************** CURVE RGB ******************** */
168
170
172{
173 b.is_function_node();
174 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
175 .min(0.0f)
176 .max(1.0f)
177 .default_value(1.0f)
178 .subtype(PROP_FACTOR)
179 .no_muted_links()
180 .description("Amount of influence the node exerts on the output color")
181 .compositor_domain_priority(1);
182 b.add_input<decl::Color>("Color"_ustr)
183 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
184 .description("Color input on which correction will be applied")
186 b.add_output<decl::Color>("Color"_ustr);
187}
188
190{
191 node->storage = BKE_curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
192}
193
195 bNode *node,
196 bNodeExecData * /*execdata*/,
199{
200 CurveMapping *curve_mapping = static_cast<CurveMapping *>(node->storage);
201
202 BKE_curvemapping_init(curve_mapping);
203 float *band_values;
204 int band_size;
205 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
206 float band_layer;
207 GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
208
209 float start_slopes[CM_TOT];
210 float end_slopes[CM_TOT];
211 BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
212 float range_minimums[CM_TOT];
213 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
214 float range_dividers[CM_TOT];
215 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
216
217 /* Shader nodes don't do white balancing. */
218 float black_level[4] = {0.0f, 0.0f, 0.0f, 1.0f};
219 float white_level[4] = {1.0f, 1.0f, 1.0f, 1.0f};
220
221 /* If the RGB curves do nothing, use a function that skips RGB computations. */
222 if (BKE_curvemapping_is_map_identity(curve_mapping, 0) &&
223 BKE_curvemapping_is_map_identity(curve_mapping, 1) &&
224 BKE_curvemapping_is_map_identity(curve_mapping, 2))
225 {
226 return GPU_stack_link(mat,
227 node,
228 "curves_combined_only",
229 in,
230 out,
231 GPU_constant(black_level),
232 GPU_constant(white_level),
233 band_texture,
234 GPU_constant(&band_layer),
235 GPU_uniform(&range_minimums[3]),
236 GPU_uniform(&range_dividers[3]),
237 GPU_uniform(&start_slopes[3]),
238 GPU_uniform(&end_slopes[3]));
239 }
240
241 return GPU_stack_link(mat,
242 node,
243 "curves_combined_rgb",
244 in,
245 out,
246 GPU_constant(black_level),
247 GPU_constant(white_level),
248 band_texture,
249 GPU_constant(&band_layer),
250 GPU_uniform(range_minimums),
251 GPU_uniform(range_dividers),
252 GPU_uniform(start_slopes),
253 GPU_uniform(end_slopes));
254}
255
256class CurveRGBFunction : public mf::MultiFunction {
257 private:
259 std::shared_ptr<const bNodeTree> tree_;
260 const CurveMapping &cumap_;
261
262 public:
263 CurveRGBFunction(const CurveMapping &cumap, std::shared_ptr<const bNodeTree> tree)
264 : tree_(std::move(tree)), cumap_(cumap)
265 {
266 static const mf::Signature signature = []() {
267 mf::Signature signature;
268 mf::SignatureBuilder builder{"Curve RGB", signature};
269 builder.single_input<float>("Fac");
270 builder.single_input<ColorGeometry4f>("Color");
271 builder.single_output<ColorGeometry4f>("Color");
272 return signature;
273 }();
274 this->set_signature(&signature);
275 }
276
277 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
278 {
279 const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
280 const VArray<ColorGeometry4f> &col_in = params.readonly_single_input<ColorGeometry4f>(1,
281 "Color");
282 MutableSpan<ColorGeometry4f> col_out = params.uninitialized_single_output<ColorGeometry4f>(
283 2, "Color");
284
285 mask.foreach_index([&](const int64_t i) {
286 BKE_curvemapping_evaluateRGBF(&cumap_, col_out[i], col_in[i]);
287 if (fac[i] != 1.0f) {
288 interp_v3_v3v3(col_out[i], col_in[i], col_out[i], fac[i]);
289 }
290 col_out[i].a = 1.0f;
291 });
292 }
293
294 void hash_unique(UniqueHashBytes &hash) const override
295 {
296 static constexpr int8_t id = 0;
297 hash.add(&id);
298 hash.add(&cumap_);
299 }
300};
301
303{
304 const bNode &bnode = builder.node();
305 CurveMapping *cumap = static_cast<CurveMapping *>(bnode.storage);
308}
309
311#ifdef WITH_MATERIALX
312{
313 /* TODO: implement */
314 return get_input_value("Color", NodeItem::Type::Color3);
315}
316#endif
318
319} // namespace nodes::node_shader_curves_cc::rgb
320
322{
323 namespace file_ns = nodes::node_shader_curves_cc::rgb;
324
325 static bke::bNodeType ntype;
326
327 common_node_type_base(&ntype, "ShaderNodeRGBCurve"_ustr, SH_NODE_CURVE_RGB);
328 ntype.ui_name = "RGB Curves";
329 ntype.ui_description = "Apply color corrections for each color channel";
330 ntype.enum_name_legacy = "CURVE_RGB";
332 ntype.declare = file_ns::sh_node_curve_rgb_declare;
333 ntype.initfunc = file_ns::node_shader_init_curve_rgb;
336 ntype.gpu_fn = file_ns::gpu_shader_curve_rgb;
337 ntype.build_multi_function = file_ns::sh_node_curve_rgb_build_multi_function;
338 ntype.materialx_fn = file_ns::node_shader_materialx;
339
341}
342
343/* **************** CURVE FLOAT ******************** */
344
346
348{
349 b.is_function_node();
350 b.add_input<decl::Float>("Factor"_ustr)
351 .min(0.0f)
352 .max(1.0f)
353 .default_value(1.0f)
354 .subtype(PROP_FACTOR)
355 .no_muted_links()
356 .compositor_domain_priority(1);
357 b.add_input<decl::Float>("Value"_ustr)
358 .default_value(1.0f)
360 .compositor_domain_priority(0);
361 b.add_output<decl::Float>("Value"_ustr);
362}
363
365{
366 node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
367}
368
370 bNode *node,
371 bNodeExecData * /*execdata*/,
374{
375 CurveMapping *curve_mapping = static_cast<CurveMapping *>(node->storage);
376
377 BKE_curvemapping_init(curve_mapping);
378 float *band_values;
379 int band_size;
380 BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
381 float band_layer;
382 GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
383
384 float start_slopes[CM_TOT];
385 float end_slopes[CM_TOT];
386 BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
387 float range_minimums[CM_TOT];
388 BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
389 float range_dividers[CM_TOT];
390 BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
391
392 return GPU_stack_link(mat,
393 node,
394 "curves_float_mixed",
395 in,
396 out,
397 band_texture,
398 GPU_constant(&band_layer),
399 GPU_uniform(range_minimums),
400 GPU_uniform(range_dividers),
401 GPU_uniform(start_slopes),
402 GPU_uniform(end_slopes));
403}
404
405class CurveFloatFunction : public mf::MultiFunction {
406 private:
408 std::shared_ptr<const bNodeTree> tree_;
409 const CurveMapping &cumap_;
410
411 public:
412 CurveFloatFunction(const CurveMapping &cumap, std::shared_ptr<const bNodeTree> tree)
413 : tree_(std::move(tree)), cumap_(cumap)
414 {
415 static const mf::Signature signature = []() {
416 mf::Signature signature;
417 mf::SignatureBuilder builder{"Curve Float", signature};
418 builder.single_input<float>("Factor");
419 builder.single_input<float>("Value");
420 builder.single_output<float>("Value");
421 return signature;
422 }();
423 this->set_signature(&signature);
424 }
425
426 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
427 {
428 const VArray<float> &fac = params.readonly_single_input<float>(0, "Factor");
429 const VArray<float> &val_in = params.readonly_single_input<float>(1, "Value");
430 MutableSpan<float> val_out = params.uninitialized_single_output<float>(2, "Value");
431
432 mask.foreach_index([&](const int64_t i) {
433 val_out[i] = BKE_curvemapping_evaluateF(&cumap_, 0, val_in[i]);
434 if (fac[i] != 1.0f) {
435 val_out[i] = (1.0f - fac[i]) * val_in[i] + fac[i] * val_out[i];
436 }
437 });
438 }
439
440 void hash_unique(UniqueHashBytes &hash) const override
441 {
442 static constexpr int8_t id = 0;
443 hash.add(&id);
444 hash.add(&cumap_);
445 }
446};
447
449{
450 const bNode &bnode = builder.node();
451 CurveMapping *cumap = static_cast<CurveMapping *>(bnode.storage);
454}
455
457#ifdef WITH_MATERIALX
458{
459 /* TODO: implement */
460 return get_input_value("Value", NodeItem::Type::Float);
461}
462#endif
464
465} // namespace nodes::node_shader_curves_cc::flt
466
468{
469 namespace file_ns = nodes::node_shader_curves_cc::flt;
470
471 static bke::bNodeType ntype;
472
473 common_node_type_base(&ntype, "ShaderNodeFloatCurve"_ustr, SH_NODE_CURVE_FLOAT);
474 ntype.ui_name = "Float Curve";
475 ntype.ui_description = "Map an input float to a curve and outputs a float value";
476 ntype.enum_name_legacy = "CURVE_FLOAT";
478 ntype.declare = file_ns::sh_node_curve_float_declare;
479 ntype.initfunc = file_ns::node_shader_init_curve_float;
482 ntype.gpu_fn = file_ns::gpu_shader_curve_float;
483 ntype.build_multi_function = file_ns::sh_node_curve_float_build_multi_function;
484 ntype.materialx_fn = file_ns::node_shader_materialx;
485
487}
488
489} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_CLASS_OP_VECTOR
Definition BKE_node.hh:481
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define SH_NODE_CURVE_FLOAT
#define SH_NODE_CURVE_VEC
#define SH_NODE_CURVE_RGB
#define CM_TOT
void set_signature(const Signature *signature)
const std::shared_ptr< const bNodeTree > & shared_tree()
CurveFloatFunction(const CurveMapping &cumap, std::shared_ptr< const bNodeTree > tree)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
CurveRGBFunction(const CurveMapping &cumap, std::shared_ptr< const bNodeTree > tree)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
CurveVecFunction(const CurveMapping &cumap, std::shared_ptr< const bNodeTree > tree)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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:6166
static void sh_node_curve_float_declare(NodeDeclarationBuilder &b)
static void sh_node_curve_float_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_curve_float(bNodeTree *, bNode *node)
static int gpu_shader_curve_float(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_curve_rgb_build_multi_function(NodeMultiFunctionBuilder &builder)
static int gpu_shader_curve_rgb(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_curve_rgb_declare(NodeDeclarationBuilder &b)
static void node_shader_init_curve_rgb(bNodeTree *, bNode *node)
static void node_shader_init_curve_vec(bNodeTree *, bNode *node)
static int gpu_shader_curve_vec(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_curve_vec_declare(NodeDeclarationBuilder &b)
static void sh_node_curve_vec_build_multi_function(NodeMultiFunctionBuilder &builder)
BMesh Iterators.
CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition colortools.cc:99
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
void BKE_curvemapping_evaluate3F(const CurveMapping *cumap, float vecout[3], const float vecin[3])
void BKE_curvemapping_evaluateRGBF(const CurveMapping *cumap, float vecout[3], const float vecin[3])
void register_node_type_sh_curve_vec()
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
void BKE_curvemapping_init(CurveMapping *cumap)
GPUNodeLink * GPU_constant(const float *num)
void node_free_curves(bNode *node)
Definition node_util.cc:40
void register_node_type_sh_curve_float()
void node_copy_curves(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:52
void BKE_curvemapping_table_RGBA(const CurveMapping *cumap, float **array, int *size)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void BKE_curvemapping_compute_slopes(const CurveMapping *curve_mapping, float start_slopes[4], float end_slopes[4])
void BKE_curvemapping_compute_range_dividers(const CurveMapping *curve_mapping, float dividers[4])
bool BKE_curvemapping_is_map_identity(const CurveMapping *curve_mapping, int index)
void register_node_type_sh_curve_rgb()
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
@ PROP_FACTOR
Definition RNA_types.hh:308
void BKE_curvemapping_get_range_minimums(const CurveMapping *curve_mapping, float minimums[4])
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
signed char int8_t
Definition BKE_text.h:76
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define hash
Definition noise_c.cc:156
#define min(a, b)
Definition sort.cc:38
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393