[Bf-blender-cvs] [1c86d32fa7d] master: Nodes: deduplicate ping pong math operation

Jacques Lucke noreply at git.blender.org
Wed Nov 25 12:27:28 CET 2020


Commit: 1c86d32fa7d4c367370d06b7983410f4c33d58a2
Author: Jacques Lucke
Date:   Wed Nov 25 12:24:32 2020 +0100
Branches: master
https://developer.blender.org/rB1c86d32fa7d4c367370d06b7983410f4c33d58a2

Nodes: deduplicate ping pong math operation

The formula did not change. The only side effect of this change should be
that the compositor node now does not divide by zero in some cases.

===================================================================

M	source/blender/blenlib/intern/math_base_inline.c
M	source/blender/compositor/operations/COM_MathBaseOperation.cpp
M	source/blender/nodes/texture/nodes/node_texture_math.c

===================================================================

diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c
index 8cc5c31a4c7..1dc7c21f1d4 100644
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@ -387,6 +387,14 @@ MINLINE float wrapf(float value, float max, float min)
   return (range != 0.0f) ? value - (range * floorf((value - min) / range)) : min;
 }
 
+MINLINE float pingpongf(float value, float scale)
+{
+  if (scale == 0.0f) {
+    return 0.0f;
+  }
+  return fabsf(fractf((value - scale) / (scale * 2.0f)) * scale * 2.0f - scale);
+}
+
 // Square.
 
 MINLINE int square_s(short a)
diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.cpp b/source/blender/compositor/operations/COM_MathBaseOperation.cpp
index edd5bb6d139..dbec6dd1874 100644
--- a/source/blender/compositor/operations/COM_MathBaseOperation.cpp
+++ b/source/blender/compositor/operations/COM_MathBaseOperation.cpp
@@ -671,9 +671,7 @@ void MathPingpongOperation::executePixelSampled(float output[4],
   this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler);
   this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler);
 
-  output[0] = fabsf(fractf((inputValue1[0] - inputValue2[0]) / (inputValue2[0] * 2.0f)) *
-                        inputValue2[0] * 2.0f -
-                    inputValue2[0]);
+  output[0] = pingpongf(inputValue1[0], inputValue2[0]);
 
   clampIfNeeded(output);
 }
diff --git a/source/blender/nodes/texture/nodes/node_texture_math.c b/source/blender/nodes/texture/nodes/node_texture_math.c
index 134a0ebb093..53022c9e120 100644
--- a/source/blender/nodes/texture/nodes/node_texture_math.c
+++ b/source/blender/nodes/texture/nodes/node_texture_math.c
@@ -282,12 +282,7 @@ static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, shor
     }
 
     case NODE_MATH_PINGPONG: {
-      if (in1 == 0.0f) {
-        *out = 0.0f;
-      }
-      else {
-        *out = fabsf(fractf((in0 - in1) / (in1 * 2.0f)) * in1 * 2.0f - in1);
-      }
+      *out = pingpongf(in0, in1);
       break;
     }



More information about the Bf-blender-cvs mailing list