From 3fbc984b069fed441cccdd3416ec71e064235e36 Mon Sep 17 00:00:00 2001 From: Matt Heimlich Date: Wed, 7 May 2014 16:20:17 +0200 Subject: [PATCH] Nodes: add absolute value operation to all math nodes Reviewed By: dingto, brecht Differential Revision: https://developer.blender.org/D507 --- intern/cycles/kernel/shaders/node_math.osl | 2 ++ intern/cycles/kernel/svm/svm_math.h | 2 ++ intern/cycles/kernel/svm/svm_types.h | 1 + intern/cycles/render/nodes.cpp | 1 + intern/cycles/util/util_math.h | 2 +- source/blender/compositor/nodes/COM_MathNode.cpp | 3 +++ .../compositor/operations/COM_MathBaseOperation.cpp | 11 +++++++++++ .../compositor/operations/COM_MathBaseOperation.h | 6 ++++++ source/blender/gpu/shaders/gpu_shader_material.glsl | 5 +++++ source/blender/makesrna/intern/rna_nodetree.c | 3 ++- source/blender/nodes/shader/nodes/node_shader_math.c | 7 ++++++- .../blender/nodes/texture/nodes/node_texture_math.c | 9 ++++++++- 12 files changed, 48 insertions(+), 4 deletions(-) diff --git a/intern/cycles/kernel/shaders/node_math.osl b/intern/cycles/kernel/shaders/node_math.osl index 066e5f8dbe1..abb6a359e75 100644 --- a/intern/cycles/kernel/shaders/node_math.osl +++ b/intern/cycles/kernel/shaders/node_math.osl @@ -93,6 +93,8 @@ shader node_math( Value = Value1 > Value2; else if (type == "Modulo") Value = safe_modulo(Value1, Value2); + else if (type == "Absolute") + Value = fabs(Value1); if (Clamp) Value = clamp(Value, 0.0, 1.0); diff --git a/intern/cycles/kernel/svm/svm_math.h b/intern/cycles/kernel/svm/svm_math.h index bb46d443a6b..1ce9386e40e 100644 --- a/intern/cycles/kernel/svm/svm_math.h +++ b/intern/cycles/kernel/svm/svm_math.h @@ -56,6 +56,8 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2) Fac = Fac1 > Fac2; else if(type == NODE_MATH_MODULO) Fac = safe_modulo(Fac1, Fac2); + else if(type == NODE_MATH_ABSOLUTE) + Fac = fabsf(Fac1); else if(type == NODE_MATH_CLAMP) Fac = clamp(Fac1, 0.0f, 1.0f); else diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index a7deb0bb913..80972ec82bc 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -221,6 +221,7 @@ typedef enum NodeMath { NODE_MATH_LESS_THAN, NODE_MATH_GREATER_THAN, NODE_MATH_MODULO, + NODE_MATH_ABSOLUTE, NODE_MATH_CLAMP /* used for the clamp UI option */ } NodeMath; diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index e269074763c..a53e0b39435 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -3544,6 +3544,7 @@ static ShaderEnum math_type_init() enm.insert("Less Than", NODE_MATH_LESS_THAN); enm.insert("Greater Than", NODE_MATH_GREATER_THAN); enm.insert("Modulo", NODE_MATH_MODULO); + enm.insert("Absolute", NODE_MATH_ABSOLUTE); return enm; } diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h index ded75762cd2..69c0d84c163 100644 --- a/intern/cycles/util/util_math.h +++ b/intern/cycles/util/util_math.h @@ -183,7 +183,7 @@ ccl_device_inline float signf(float f) ccl_device_inline float nonzerof(float f, float eps) { - if(fabsf(f) < eps) + if(fabsf(f) < eps) return signf(f)*eps; else return f; diff --git a/source/blender/compositor/nodes/COM_MathNode.cpp b/source/blender/compositor/nodes/COM_MathNode.cpp index 42a8cb032ce..ac1c2b3c159 100644 --- a/source/blender/compositor/nodes/COM_MathNode.cpp +++ b/source/blender/compositor/nodes/COM_MathNode.cpp @@ -83,6 +83,9 @@ void MathNode::convertToOperations(NodeConverter &converter, const CompositorCon case 17: /* Modulo */ operation = new MathModuloOperation(); break; + case 18: /* Absolute Value */ + operation = new MathAbsoluteOperation(); + break; } if (operation) { diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.cpp b/source/blender/compositor/operations/COM_MathBaseOperation.cpp index 9da55df476d..cbc60b5091d 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.cpp +++ b/source/blender/compositor/operations/COM_MathBaseOperation.cpp @@ -333,3 +333,14 @@ void MathModuloOperation::executePixelSampled(float output[4], float x, float y, clampIfNeeded(output); } +void MathAbsoluteOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = fabs(inputValue1[0]); + + clampIfNeeded(output); +} + diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.h b/source/blender/compositor/operations/COM_MathBaseOperation.h index 4ea7c43a67d..05d2bb054d3 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.h +++ b/source/blender/compositor/operations/COM_MathBaseOperation.h @@ -163,4 +163,10 @@ public: void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); }; +class MathAbsoluteOperation : public MathBaseOperation { +public: + MathAbsoluteOperation() : MathBaseOperation() {} + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + #endif diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl index 4452d4e06c3..a224f8e979a 100644 --- a/source/blender/gpu/shaders/gpu_shader_material.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material.glsl @@ -299,6 +299,11 @@ void math_modulo(float val1, float val2, out float outval) outval = mod(val1, val2); } +void math_abs(float val1, out float outval) +{ + outval = abs(val1); +} + void squeeze(float val, float width, float center, out float outval) { outval = 1.0/(1.0 + pow(2.71828183, -((val-center)*width))); diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index 0f2380cbad4..c39d3826d8b 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -129,7 +129,8 @@ EnumPropertyItem node_math_items[] = { {14, "ROUND", 0, "Round", ""}, {15, "LESS_THAN", 0, "Less Than", ""}, {16, "GREATER_THAN", 0, "Greater Than", ""}, - {17, "MODULO", 0, "Modulo", ""}, + {17, "MODULO", 0, "Modulo", ""}, + {18, "ABSOLUTE", 0, "Absolute", ""}, {0, NULL, 0, NULL, NULL} }; diff --git a/source/blender/nodes/shader/nodes/node_shader_math.c b/source/blender/nodes/shader/nodes/node_shader_math.c index b6c755571c0..ed88e800d33 100644 --- a/source/blender/nodes/shader/nodes/node_shader_math.c +++ b/source/blender/nodes/shader/nodes/node_shader_math.c @@ -216,6 +216,11 @@ static void node_shader_exec_math(void *UNUSED(data), int UNUSED(thread), bNode r = fmod(a, b); break; } + case 18: /* Absolute */ + { + r = fabs(a); + break; + } } out[0]->vec[0] = r; @@ -226,7 +231,7 @@ static int gpu_shader_math(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED( static const char *names[] = {"math_add", "math_subtract", "math_multiply", "math_divide", "math_sine", "math_cosine", "math_tangent", "math_asin", "math_acos", "math_atan", "math_pow", "math_log", "math_min", "math_max", - "math_round", "math_less_than", "math_greater_than", "math_modulo"}; + "math_round", "math_less_than", "math_greater_than", "math_modulo", "math_absolute"}; switch (node->custom1) { case 0: diff --git a/source/blender/nodes/texture/nodes/node_texture_math.c b/source/blender/nodes/texture/nodes/node_texture_math.c index 8d69d79d847..86d693f7dfa 100644 --- a/source/blender/nodes/texture/nodes/node_texture_math.c +++ b/source/blender/nodes/texture/nodes/node_texture_math.c @@ -174,7 +174,7 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor break; } - case 17: /* Modulo */ + case 17: /* Modulo */ { if (in1 == 0.0f) *out = 0.0f; @@ -182,6 +182,13 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor *out = fmod(in0, in1); break; } + + case 18: /* Absolute */ + { + *out = fabs(in0); + break; + } + default: { BLI_assert(0);