Blender V5.3
node_shader_math.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#include "node_util.hh"
11
12#include "BKE_node.hh"
13
15#include "NOD_math_functions.hh"
18
19#include "RNA_enum_types.hh"
20
21namespace blender {
22
23/* **************** SCALAR MATH ******************** */
24
26
28{
29 b.is_function_node();
30 b.add_input<decl::Float>("Value"_ustr)
31 .default_value(0.5f)
32 .min(-10000.0f)
33 .max(10000.0f)
34 .label_fn([](const bNode &node) {
35 switch (node.custom1) {
36 case NODE_MATH_POWER:
37 return IFACE_("Base");
39 return IFACE_("Radians");
41 return IFACE_("Degrees");
42 default:
43 return IFACE_("Value");
44 }
45 });
46 b.add_input<decl::Float>("Value"_ustr, "Value_001"_ustr)
47 .default_value(0.5f)
48 .min(-10000.0f)
49 .max(10000.0f)
50 .label_fn([](const bNode &node) {
51 switch (node.custom1) {
52 case NODE_MATH_WRAP:
53 return IFACE_("Max");
55 return IFACE_("Multiplier");
58 return IFACE_("Threshold");
60 return IFACE_("Scale");
61 case NODE_MATH_SNAP:
62 return IFACE_("Increment");
63 case NODE_MATH_POWER:
64 return IFACE_("Exponent");
66 return IFACE_("Base");
67 default:
68 return IFACE_("Value");
69 }
70 });
71 b.add_input<decl::Float>("Value"_ustr, "Value_002"_ustr)
72 .default_value(0.5f)
73 .min(-10000.0f)
74 .max(10000.0f)
75 .label_fn([](const bNode &node) {
76 switch (node.custom1) {
77 case NODE_MATH_WRAP:
78 return IFACE_("Min");
80 return IFACE_("Addend");
82 return IFACE_("Epsilon");
85 return IFACE_("Distance");
86 default:
87 return IFACE_("Value");
88 }
89 });
90 b.add_output<decl::Float>("Value"_ustr);
91}
92
94{
95 bNodeSocket *socket_2 = bke::node_find_socket(node, SOCK_IN, "Value_001"_ustr);
96 BLI_assert(socket_2 != nullptr);
97 float &value_2 = socket_2->default_value_typed<bNodeSocketValueFloat>()->value;
98
99 bNodeSocket *socket_3 = bke::node_find_socket(node, SOCK_IN, "Value_002"_ustr);
100 BLI_assert(socket_3 != nullptr);
101 float &value_3 = socket_3->default_value_typed<bNodeSocketValueFloat>()->value;
102
103 switch (mode) {
105 case NODE_MATH_DIVIDE:
106 case NODE_MATH_POWER:
108 case NODE_MATH_MODULO:
110 value_2 = 1.0f;
111 break;
112 case NODE_MATH_ADD:
114 value_2 = 0.0f;
115 break;
117 value_2 = 1.0f;
118 value_3 = 0.0f;
119 break;
120
121 default:
122 /* Use the default defined in the node declaration otherwise. */
123 break;
124 }
125}
126
128 public:
132 {
133 bNode &node = params.add_node("ShaderNodeMath"_ustr);
134 node.custom1 = mode;
137 }
138};
139
141{
142 if (!params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_FLOAT)) {
143 return;
144 }
145
146 const bool is_compare_node_supported = ELEM(
148 const int weight = ELEM(params.other_socket().type, SOCK_FLOAT, SOCK_INT, SOCK_BOOLEAN) ? 0 : -1;
149
150 for (const EnumPropertyItem *item = rna_enum_node_math_items; item->identifier != nullptr;
151 item++)
152 {
153 if (item->name != nullptr && item->identifier[0] != '\0') {
154 if (is_compare_node_supported &&
156 {
157 /* Hide compare operations from the Math node if the Compare node is supported. */
158 continue;
159 }
161 SocketSearchOp{"Value"_ustr, NodeMathOperation(item->value)},
162 weight);
163 }
164 }
165}
166
167static const char *gpu_shader_get_name(int mode)
168{
170 if (!info) {
171 return nullptr;
172 }
173 if (info->shader_name.is_empty()) {
174 return nullptr;
175 }
176 return info->shader_name.c_str();
177}
178
180 bNode *node,
181 bNodeExecData * /*execdata*/,
184{
185 const char *name = gpu_shader_get_name(node->custom1);
186 if (name != nullptr) {
187 int ret = GPU_stack_link(mat, node, name, in, out);
188
189 if (ret && node->custom2 & SHD_MATH_CLAMP) {
190 float min[3] = {0.0f, 0.0f, 0.0f};
191 float max[3] = {1.0f, 1.0f, 1.0f};
192 GPU_link(
193 mat, "clamp_value", out[0].link, GPU_constant(min), GPU_constant(max), &out[0].link);
194 }
195 return ret;
196 }
197
198 return 0;
199}
200
202{
203 using namespace value_elem;
205 switch (op) {
206 case NODE_MATH_ADD:
209 case NODE_MATH_DIVIDE: {
210 FloatElem output_elem = params.get_input_elem<FloatElem>("Value"_ustr);
211 output_elem.merge(params.get_input_elem<FloatElem>("Value_001"_ustr));
212 params.set_output_elem("Value"_ustr, output_elem);
213 break;
214 }
215 default:
216 break;
217 }
218}
219
221{
223 switch (op) {
224 case NODE_MATH_ADD:
227 case NODE_MATH_DIVIDE: {
228 params.set_input_elem("Value"_ustr,
229 params.get_output_elem<value_elem::FloatElem>("Value"_ustr));
230 break;
231 }
232 default:
233 break;
234 }
235}
236
238{
240 const UString first_input_id = "Value"_ustr;
241 const UString second_input_id = "Value_001"_ustr;
242 const UString output_id = "Value"_ustr;
243 switch (op) {
244 case NODE_MATH_ADD: {
245 params.set_input(first_input_id,
246 params.get_output<float>(output_id) -
247 params.get_input<float>(second_input_id));
248 break;
249 }
250 case NODE_MATH_SUBTRACT: {
251 params.set_input(first_input_id,
252 params.get_output<float>(output_id) +
253 params.get_input<float>(second_input_id));
254 break;
255 }
256 case NODE_MATH_MULTIPLY: {
257 params.set_input(first_input_id,
258 math::safe_divide(params.get_output<float>(output_id),
259 params.get_input<float>(second_input_id)));
260 break;
261 }
262 case NODE_MATH_DIVIDE: {
263 params.set_input(first_input_id,
264 params.get_output<float>(output_id) *
265 params.get_input<float>(second_input_id));
266 break;
267 }
268 default: {
269 break;
270 }
271 }
272}
273
275#ifdef WITH_MATERIALX
276{
277 NodeMathOperation op = NodeMathOperation(node_->custom1);
278 NodeItem res = empty();
279
280 /* Single operand operations */
281 NodeItem x = get_input_value(0, NodeItem::Type::Float);
282
283 switch (op) {
284 case NODE_MATH_SINE:
285 res = x.sin();
286 break;
287 case NODE_MATH_COSINE:
288 res = x.cos();
289 break;
291 res = x.tan();
292 break;
294 res = x.asin();
295 break;
297 res = x.acos();
298 break;
300 res = x.atan();
301 break;
302 case NODE_MATH_ROUND:
303 res = (x + val(0.5f)).floor();
304 break;
306 res = x.abs();
307 break;
308 case NODE_MATH_FLOOR:
309 res = x.floor();
310 break;
311 case NODE_MATH_CEIL:
312 res = x.ceil();
313 break;
315 res = x % val(1.0f);
316 break;
317 case NODE_MATH_SQRT:
318 res = x.sqrt();
319 break;
321 res = val(1.0f) / x.sqrt();
322 break;
323 case NODE_MATH_SIGN:
324 res = x.sign();
325 break;
327 res = x.exp();
328 break;
330 res = x * val(float(M_PI) / 180.0f);
331 break;
333 res = x * val(180.0f * float(M_1_PI));
334 break;
335 case NODE_MATH_SINH:
336 res = x.sinh();
337 break;
338 case NODE_MATH_COSH:
339 res = x.cosh();
340 break;
341 case NODE_MATH_TANH:
342 res = x.tanh();
343 break;
344 case NODE_MATH_TRUNC:
345 res = x.sign() * x.abs().floor();
346 break;
347
348 default: {
349 /* 2-operand operations */
350 NodeItem y = get_input_value(1, NodeItem::Type::Float);
351
352 switch (op) {
353 case NODE_MATH_ADD:
354 res = x + y;
355 break;
357 res = x - y;
358 break;
360 res = x * y;
361 break;
362 case NODE_MATH_DIVIDE:
363 res = x / y;
364 break;
365 case NODE_MATH_POWER:
366 res = x ^ y;
367 break;
369 res = x.ln() / y.ln();
370 break;
372 res = x.min(y);
373 break;
375 res = x.max(y);
376 break;
378 res = x.if_else(NodeItem::CompareOp::Less, y, val(1.0f), val(0.0f));
379 break;
381 res = x.if_else(NodeItem::CompareOp::Greater, y, val(1.0f), val(0.0f));
382 break;
383 case NODE_MATH_MODULO:
384 res = x % y;
385 break;
387 res = x.atan2(y);
388 break;
389 case NODE_MATH_SNAP:
390 res = (x / y).floor() * y;
391 break;
392 case NODE_MATH_PINGPONG: {
393 NodeItem fract_part = (x - y) / (y * val(2.0f));
394 NodeItem if_branch = ((fract_part - fract_part.floor()) * y * val(2.0f) - y).abs();
395 res = y.if_else(NodeItem::CompareOp::NotEq, val(0.0f), if_branch, val(0.0f));
396 break;
397 }
399 res = y.if_else(
400 NodeItem::CompareOp::NotEq, val(0.0f), (x - (x / y).floor() * y), val(0.0f));
401 break;
402
403 default: {
404 /* 3-operand operations */
405 NodeItem z = get_input_value(2, NodeItem::Type::Float);
406
407 switch (op) {
408 case NODE_MATH_WRAP: {
409 NodeItem range = (y - z);
410 NodeItem if_branch = x - (range * ((x - z) / range).floor());
411 res = range.if_else(NodeItem::CompareOp::NotEq, val(0.0f), if_branch, z);
412 break;
413 }
415 res = z.if_else(NodeItem::CompareOp::Less, (x - y).abs(), val(1.0f), val(0.0f));
416 break;
418 res = x * y + z;
419 break;
422 auto make_smoothmin = [&](NodeItem a, NodeItem b, NodeItem k) {
423 NodeItem h = (k - (a - b).abs()).max(val(0.0f)) / k;
424 NodeItem if_branch = a.min(b) - h * h * h * k * val(1.0f / 6.0f);
425 return k.if_else(NodeItem::CompareOp::NotEq, val(0.0f), if_branch, a.min(b));
426 };
427 if (op == NODE_MATH_SMOOTH_MIN) {
428 res = make_smoothmin(x, y, z);
429 }
430 else {
431 res = -make_smoothmin(-x, -y, -z);
432 }
433 break;
434 }
435 default:
437 }
438 }
439 }
440 }
441 }
442
443 bool clamp_output = node_->custom2 != 0;
444 if (clamp_output && res) {
445 res = res.clamp();
446 }
447
448 return res;
449}
450#endif
452
453} // namespace nodes::node_shader_math_cc
454
456{
457 namespace file_ns = nodes::node_shader_math_cc;
458
459 static bke::bNodeType ntype;
460
461 common_node_type_base(&ntype, "ShaderNodeMath"_ustr, SH_NODE_MATH);
462 ntype.ui_name = "Math";
463 ntype.ui_description = "Perform math operations";
464 ntype.enum_name_legacy = "MATH";
466 ntype.declare = file_ns::sh_node_math_declare;
468 ntype.gpu_fn = file_ns::gpu_shader_math;
471 ntype.gather_link_search_ops = file_ns::sh_node_math_gather_link_searches;
472 ntype.materialx_fn = file_ns::node_shader_materialx;
473 ntype.eval_elem = file_ns::node_eval_elem;
474 ntype.eval_inverse_elem = file_ns::node_eval_inverse_elem;
475 ntype.eval_inverse = file_ns::node_eval_inverse;
476
478}
479
480} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define SH_NODE_MATH
#define M_1_PI
#define M_PI
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define BLI_assert_unreachable()
#define ELEM(...)
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr const char * c_str() const
constexpr bool is_empty() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
std::unique_ptr< CustomSocketLabelFn > label_fn
void set_input(const UString identifier, T value)
T get_input_elem(const UString identifier) const
void set_output_elem(const UString identifier, T elem)
void set_input_elem(const UString identifier, T elem)
#define abs
#define floor
@ NODE_MATH_SMOOTH_MIN
@ NODE_MATH_MULTIPLY_ADD
@ NODE_MATH_COMPARE
@ NODE_MATH_SMOOTH_MAX
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
T safe_divide(const T &a, const T &b)
static int gpu_shader_math(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_eval_inverse_elem(value_elem::InverseElemEvalParams &params)
static void sh_node_math_gather_link_searches(GatherLinkSearchOpParams &params)
static void math_input_defaults(bNode &node, const NodeMathOperation mode)
static const char * gpu_shader_get_name(int mode)
static void sh_node_math_declare(NodeDeclarationBuilder &b)
static void node_eval_elem(value_elem::ElemEvalParams &params)
static void node_eval_inverse(inverse_eval::InverseEvalParams &params)
void node_math_build_multi_function(NodeMultiFunctionBuilder &builder)
const FloatMathOperationInfo * get_float_math_operation_info(const int operation)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void node_math_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:158
void node_math_update(bNodeTree *ntree, bNode *node)
Definition node_util.cc:88
@ NODE_MATH_SMOOTH_MAX
@ NODE_MATH_LESS_THAN
@ NODE_MATH_GREATER_THAN
@ NODE_MATH_LOGARITHM
@ NODE_MATH_SMOOTH_MIN
@ NODE_MATH_FLOORED_MODULO
@ NODE_MATH_ARCCOSINE
@ NODE_MATH_ARCTANGENT
@ NODE_MATH_MULTIPLY_ADD
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
const EnumPropertyItem rna_enum_node_math_items[]
void register_node_type_sh_math()
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 min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
eNodeTree_Type type
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInverseElemEvalFunction eval_inverse_elem
Definition BKE_node.hh:434
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
NodeInverseEvalFunction eval_inverse
Definition BKE_node.hh:441
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeElemEvalFunction eval_elem
Definition BKE_node.hh:428
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393