Blender  V3.3
node_shader_curves.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include "node_shader_util.hh"
9 
11 
13 {
14  b.is_function_node();
15  b.add_input<decl::Float>(N_("Fac")).min(0.0f).max(1.0f).default_value(1.0f).subtype(PROP_FACTOR);
16  b.add_input<decl::Vector>(N_("Vector")).min(-1.0f).max(1.0f);
17  b.add_output<decl::Vector>(N_("Vector"));
18 }
19 
21 {
22  node->storage = BKE_curvemapping_add(3, -1.0f, -1.0f, 1.0f, 1.0f);
23 }
24 
26  bNode *node,
27  bNodeExecData *UNUSED(execdata),
28  GPUNodeStack *in,
30 {
31  CurveMapping *curve_mapping = (CurveMapping *)node->storage;
32 
33  BKE_curvemapping_init(curve_mapping);
34  float *band_values;
35  int band_size;
36  BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
37  float band_layer;
38  GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
39 
40  float start_slopes[CM_TOT];
41  float end_slopes[CM_TOT];
42  BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
43  float range_minimums[CM_TOT];
44  BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
45  float range_dividers[CM_TOT];
46  BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
47 
48  return GPU_stack_link(mat,
49  node,
50  "curves_vector_mixed",
51  in,
52  out,
53  band_texture,
54  GPU_constant(&band_layer),
55  GPU_uniform(range_minimums),
56  GPU_uniform(range_dividers),
57  GPU_uniform(start_slopes),
58  GPU_uniform(end_slopes));
59 }
60 
62  private:
63  const CurveMapping &cumap_;
64 
65  public:
66  CurveVecFunction(const CurveMapping &cumap) : cumap_(cumap)
67  {
69  this->set_signature(&signature);
70  }
71 
73  {
74  fn::MFSignatureBuilder signature{"Curve Vec"};
75  signature.single_input<float>("Fac");
76  signature.single_input<float3>("Vector");
77  signature.single_output<float3>("Vector");
78  return signature.build();
79  }
80 
82  {
83  const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
84  const VArray<float3> &vec_in = params.readonly_single_input<float3>(1, "Vector");
85  MutableSpan<float3> vec_out = params.uninitialized_single_output<float3>(2, "Vector");
86 
87  for (int64_t i : mask) {
88  BKE_curvemapping_evaluate3F(&cumap_, vec_out[i], vec_in[i]);
89  if (fac[i] != 1.0f) {
90  interp_v3_v3v3(vec_out[i], vec_in[i], vec_out[i], fac[i]);
91  }
92  }
93  }
94 };
95 
97 {
98  bNode &bnode = builder.node();
99  CurveMapping *cumap = (CurveMapping *)bnode.storage;
100  BKE_curvemapping_init(cumap);
102 }
103 
104 } // namespace blender::nodes::node_shader_curves_cc
105 
107 {
108  namespace file_ns = blender::nodes::node_shader_curves_cc;
109 
110  static bNodeType ntype;
111 
116  node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves);
119 
120  nodeRegisterType(&ntype);
121 }
122 
123 /* **************** CURVE RGB ******************** */
124 
126 
128 {
129  b.is_function_node();
130  b.add_input<decl::Float>(N_("Fac")).min(0.0f).max(1.0f).default_value(1.0f).subtype(PROP_FACTOR);
131  b.add_input<decl::Color>(N_("Color")).default_value({1.0f, 1.0f, 1.0f, 1.0f});
132  b.add_output<decl::Color>(N_("Color"));
133 }
134 
136 {
137  node->storage = BKE_curvemapping_add(4, 0.0f, 0.0f, 1.0f, 1.0f);
138 }
139 
141  bNode *node,
142  bNodeExecData *UNUSED(execdata),
143  GPUNodeStack *in,
144  GPUNodeStack *out)
145 {
146  CurveMapping *curve_mapping = (CurveMapping *)node->storage;
147 
148  BKE_curvemapping_init(curve_mapping);
149  float *band_values;
150  int band_size;
151  BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
152  float band_layer;
153  GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
154 
155  float start_slopes[CM_TOT];
156  float end_slopes[CM_TOT];
157  BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
158  float range_minimums[CM_TOT];
159  BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
160  float range_dividers[CM_TOT];
161  BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
162 
163  /* Shader nodes don't do white balancing. */
164  float black_level[4] = {0.0f, 0.0f, 0.0f, 1.0f};
165  float white_level[4] = {1.0f, 1.0f, 1.0f, 1.0f};
166 
167  /* If the RGB curves do nothing, use a function that skips RGB computations. */
168  if (BKE_curvemapping_is_map_identity(curve_mapping, 0) &&
169  BKE_curvemapping_is_map_identity(curve_mapping, 1) &&
170  BKE_curvemapping_is_map_identity(curve_mapping, 2)) {
171  return GPU_stack_link(mat,
172  node,
173  "curves_combined_only",
174  in,
175  out,
176  GPU_constant(black_level),
177  GPU_constant(white_level),
178  band_texture,
179  GPU_constant(&band_layer),
180  GPU_uniform(&range_minimums[3]),
181  GPU_uniform(&range_dividers[3]),
182  GPU_uniform(&start_slopes[3]),
183  GPU_uniform(&end_slopes[3]));
184  }
185 
186  return GPU_stack_link(mat,
187  node,
188  "curves_combined_rgb",
189  in,
190  out,
191  GPU_constant(black_level),
192  GPU_constant(white_level),
193  band_texture,
194  GPU_constant(&band_layer),
195  GPU_uniform(range_minimums),
196  GPU_uniform(range_dividers),
197  GPU_uniform(start_slopes),
198  GPU_uniform(end_slopes));
199 }
200 
202  private:
203  const CurveMapping &cumap_;
204 
205  public:
206  CurveRGBFunction(const CurveMapping &cumap) : cumap_(cumap)
207  {
209  this->set_signature(&signature);
210  }
211 
213  {
214  fn::MFSignatureBuilder signature{"Curve RGB"};
215  signature.single_input<float>("Fac");
216  signature.single_input<ColorGeometry4f>("Color");
217  signature.single_output<ColorGeometry4f>("Color");
218  return signature.build();
219  }
220 
222  {
223  const VArray<float> &fac = params.readonly_single_input<float>(0, "Fac");
224  const VArray<ColorGeometry4f> &col_in = params.readonly_single_input<ColorGeometry4f>(1,
225  "Color");
226  MutableSpan<ColorGeometry4f> col_out = params.uninitialized_single_output<ColorGeometry4f>(
227  2, "Color");
228 
229  for (int64_t i : mask) {
230  BKE_curvemapping_evaluateRGBF(&cumap_, col_out[i], col_in[i]);
231  if (fac[i] != 1.0f) {
232  interp_v3_v3v3(col_out[i], col_in[i], col_out[i], fac[i]);
233  }
234  }
235  }
236 };
237 
239 {
240  bNode &bnode = builder.node();
241  CurveMapping *cumap = (CurveMapping *)bnode.storage;
242  BKE_curvemapping_init(cumap);
244 }
245 
246 } // namespace blender::nodes::node_shader_curves_cc
247 
249 {
250  namespace file_ns = blender::nodes::node_shader_curves_cc;
251 
252  static bNodeType ntype;
253 
258  node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves);
261 
262  nodeRegisterType(&ntype);
263 }
264 
265 /* **************** CURVE FLOAT ******************** */
266 
268 
270 {
271  b.is_function_node();
272  b.add_input<decl::Float>(N_("Factor"))
273  .min(0.0f)
274  .max(1.0f)
275  .default_value(1.0f)
276  .subtype(PROP_FACTOR);
277  b.add_input<decl::Float>(N_("Value")).default_value(1.0f).is_default_link_socket();
278  b.add_output<decl::Float>(N_("Value"));
279 }
280 
282 {
283  node->storage = BKE_curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
284 }
285 
287  bNode *node,
288  bNodeExecData *UNUSED(execdata),
289  GPUNodeStack *in,
290  GPUNodeStack *out)
291 {
292  CurveMapping *curve_mapping = (CurveMapping *)node->storage;
293 
294  BKE_curvemapping_init(curve_mapping);
295  float *band_values;
296  int band_size;
297  BKE_curvemapping_table_RGBA(curve_mapping, &band_values, &band_size);
298  float band_layer;
299  GPUNodeLink *band_texture = GPU_color_band(mat, band_size, band_values, &band_layer);
300 
301  float start_slopes[CM_TOT];
302  float end_slopes[CM_TOT];
303  BKE_curvemapping_compute_slopes(curve_mapping, start_slopes, end_slopes);
304  float range_minimums[CM_TOT];
305  BKE_curvemapping_get_range_minimums(curve_mapping, range_minimums);
306  float range_dividers[CM_TOT];
307  BKE_curvemapping_compute_range_dividers(curve_mapping, range_dividers);
308 
309  return GPU_stack_link(mat,
310  node,
311  "curves_float_mixed",
312  in,
313  out,
314  band_texture,
315  GPU_constant(&band_layer),
316  GPU_uniform(range_minimums),
317  GPU_uniform(range_dividers),
318  GPU_uniform(start_slopes),
319  GPU_uniform(end_slopes));
320 }
321 
323  private:
324  const CurveMapping &cumap_;
325 
326  public:
327  CurveFloatFunction(const CurveMapping &cumap) : cumap_(cumap)
328  {
330  this->set_signature(&signature);
331  }
332 
334  {
335  fn::MFSignatureBuilder signature{"Curve Float"};
336  signature.single_input<float>("Factor");
337  signature.single_input<float>("Value");
338  signature.single_output<float>("Value");
339  return signature.build();
340  }
341 
343  {
344  const VArray<float> &fac = params.readonly_single_input<float>(0, "Factor");
345  const VArray<float> &val_in = params.readonly_single_input<float>(1, "Value");
346  MutableSpan<float> val_out = params.uninitialized_single_output<float>(2, "Value");
347 
348  for (int64_t i : mask) {
349  val_out[i] = BKE_curvemapping_evaluateF(&cumap_, 0, val_in[i]);
350  if (fac[i] != 1.0f) {
351  val_out[i] = (1.0f - fac[i]) * val_in[i] + fac[i] * val_out[i];
352  }
353  }
354  }
355 };
356 
358 {
359  bNode &bnode = builder.node();
360  CurveMapping *cumap = (CurveMapping *)bnode.storage;
361  BKE_curvemapping_init(cumap);
363 }
364 
365 } // namespace blender::nodes::node_shader_curves_cc
366 
368 {
369  namespace file_ns = blender::nodes::node_shader_curves_cc;
370 
371  static bNodeType ntype;
372 
377  node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves);
380 
381  nodeRegisterType(&ntype);
382 }
void BKE_curvemapping_evaluate3F(const struct CurveMapping *cumap, float vecout[3], const float vecin[3])
bool BKE_curvemapping_is_map_identity(const struct CurveMapping *curve_mapping, int index)
void BKE_curvemapping_init(struct CurveMapping *cumap)
Definition: colortools.c:1235
void BKE_curvemapping_evaluateRGBF(const struct CurveMapping *cumap, float vecout[3], const float vecin[3])
void BKE_curvemapping_get_range_minimums(const struct CurveMapping *curve_mapping, float minimums[4])
float BKE_curvemapping_evaluateF(const struct CurveMapping *cumap, int cur, float value)
void BKE_curvemapping_compute_slopes(const struct CurveMapping *curve_mapping, float start_slopes[4], float end_slopes[4])
void BKE_curvemapping_table_RGBA(const struct CurveMapping *cumap, float **array, int *size)
void BKE_curvemapping_compute_range_dividers(const struct CurveMapping *curve_mapping, float dividers[4])
struct CurveMapping * BKE_curvemapping_add(int tot, float minx, float miny, float maxx, float maxy)
Definition: colortools.c:72
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
#define NODE_CLASS_CONVERTER
Definition: BKE_node.h:351
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4408
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4426
#define NODE_CLASS_OP_VECTOR
Definition: BKE_node.h:348
#define SH_NODE_CURVE_RGB
Definition: BKE_node.h:1090
#define NODE_CLASS_OP_COLOR
Definition: BKE_node.h:347
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
@ NODE_SIZE_LARGE
Definition: BKE_node.h:367
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
Definition: math_vector.c:29
#define UNUSED(x)
#define CM_TOT
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color SH_NODE_CURVE_VEC
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color or the default fallback if none is specified Separate Split a vector into its and Z components Generates normals with round corners and may slow down renders Vector Displace the surface along an arbitrary direction White Return a random value or color based on an input seed SH_NODE_CURVE_FLOAT
@ PROP_FACTOR
Definition: RNA_types.h:144
void set_signature(const MFSignature *signature)
const MFSignature & signature() const
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static void sh_node_curve_rgb_build_multi_function(NodeMultiFunctionBuilder &builder)
static int gpu_shader_curve_vec(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_curve_vec(bNodeTree *UNUSED(ntree), bNode *node)
static void sh_node_curve_float_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_curve_float(bNodeTree *UNUSED(ntree), bNode *node)
static void sh_node_curve_vec_declare(NodeDeclarationBuilder &b)
static void sh_node_curve_vec_build_multi_function(NodeMultiFunctionBuilder &builder)
static void sh_node_curve_rgb_declare(NodeDeclarationBuilder &b)
static void sh_node_curve_float_declare(NodeDeclarationBuilder &b)
static int gpu_shader_curve_float(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_curve_rgb(bNodeTree *UNUSED(ntree), bNode *node)
static int gpu_shader_curve_rgb(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_sh_curve_float()
void register_node_type_sh_curve_rgb()
void register_node_type_sh_curve_vec()
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void node_copy_curves(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:50
void node_free_curves(bNode *node)
Definition: node_util.c:38
#define min(a, b)
Definition: sort.c:35
__int64 int64_t
Definition: stdint.h:89
Defines a node type.
Definition: BKE_node.h:226
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
void * storage
#define N_(msgid)