From 0406eb110332a863811d7fb6228f9a7ca514e022 Mon Sep 17 00:00:00 2001 From: Charlie Jolly Date: Thu, 5 Dec 2019 23:02:05 +0000 Subject: Maths Node: Additional functions When creating shaders and using maths functions it is expected that Blender should match functions in other DCC applications, game engines and shading languages such as GLSL and OSL. This patch adds missing functions to the Blender maths node. Ideally, it would be nice to have these functions available to vectors too but that is not part of this patch. This patch adds the following functions trunc, snap, wrap, compare, pingpong, sign, radians, degrees, cosh, sinh, tanh, exp, smoothmin and inversesqrt. Sign function is based on GLSL and OSL functions and returns zero when x == 0. Differential Revision: https://developer.blender.org/D5957 --- source/blender/compositor/nodes/COM_MathNode.cpp | 49 ++++ .../operations/COM_MathBaseOperation.cpp | 271 +++++++++++++++++++++ .../compositor/operations/COM_MathBaseOperation.h | 126 ++++++++++ 3 files changed, 446 insertions(+) (limited to 'source/blender/compositor') diff --git a/source/blender/compositor/nodes/COM_MathNode.cpp b/source/blender/compositor/nodes/COM_MathNode.cpp index d13b34bb6b5..5497d4a4755 100644 --- a/source/blender/compositor/nodes/COM_MathNode.cpp +++ b/source/blender/compositor/nodes/COM_MathNode.cpp @@ -56,6 +56,15 @@ void MathNode::convertToOperations(NodeConverter &converter, case NODE_MATH_ARCTANGENT: operation = new MathArcTangentOperation(); break; + case NODE_MATH_SINH: + operation = new MathHyperbolicSineOperation(); + break; + case NODE_MATH_COSH: + operation = new MathHyperbolicCosineOperation(); + break; + case NODE_MATH_TANH: + operation = new MathHyperbolicTangentOperation(); + break; case NODE_MATH_POWER: operation = new MathPowerOperation(); break; @@ -83,6 +92,12 @@ void MathNode::convertToOperations(NodeConverter &converter, case NODE_MATH_ABSOLUTE: operation = new MathAbsoluteOperation(); break; + case NODE_MATH_RADIANS: + operation = new MathRadiansOperation(); + break; + case NODE_MATH_DEGREES: + operation = new MathDegreesOperation(); + break; case NODE_MATH_ARCTAN2: operation = new MathArcTan2Operation(); break; @@ -98,6 +113,39 @@ void MathNode::convertToOperations(NodeConverter &converter, case NODE_MATH_SQRT: operation = new MathSqrtOperation(); break; + case NODE_MATH_INV_SQRT: + operation = new MathInverseSqrtOperation(); + break; + case NODE_MATH_SIGN: + operation = new MathSignOperation(); + break; + case NODE_MATH_EXPONENT: + operation = new MathExponentOperation(); + break; + case NODE_MATH_TRUNC: + operation = new MathTruncOperation(); + break; + case NODE_MATH_SNAP: + operation = new MathSnapOperation(); + break; + case NODE_MATH_WRAP: + operation = new MathWrapOperation(); + break; + case NODE_MATH_PINGPONG: + operation = new MathPingpongOperation(); + break; + case NODE_MATH_COMPARE: + operation = new MathCompareOperation(); + break; + case NODE_MATH_MULTIPLY_ADD: + operation = new MathMultiplyAddOperation(); + break; + case NODE_MATH_SMOOTH_MIN: + operation = new MathSmoothMinOperation(); + break; + case NODE_MATH_SMOOTH_MAX: + operation = new MathSmoothMaxOperation(); + break; } if (operation) { @@ -107,6 +155,7 @@ void MathNode::convertToOperations(NodeConverter &converter, converter.mapInputSocket(getInputSocket(0), operation->getInputSocket(0)); converter.mapInputSocket(getInputSocket(1), operation->getInputSocket(1)); + converter.mapInputSocket(getInputSocket(2), operation->getInputSocket(2)); converter.mapOutputSocket(getOutputSocket(0), operation->getOutputSocket()); } } diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.cpp b/source/blender/compositor/operations/COM_MathBaseOperation.cpp index 15dbd4e2ac9..d103cc3cf0d 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.cpp +++ b/source/blender/compositor/operations/COM_MathBaseOperation.cpp @@ -23,11 +23,13 @@ extern "C" { MathBaseOperation::MathBaseOperation() : NodeOperation() { + this->addInputSocket(COM_DT_VALUE); this->addInputSocket(COM_DT_VALUE); this->addInputSocket(COM_DT_VALUE); this->addOutputSocket(COM_DT_VALUE); this->m_inputValue1Operation = NULL; this->m_inputValue2Operation = NULL; + this->m_inputValue3Operation = NULL; this->m_useClamp = false; } @@ -35,12 +37,14 @@ void MathBaseOperation::initExecution() { this->m_inputValue1Operation = this->getInputSocketReader(0); this->m_inputValue2Operation = this->getInputSocketReader(1); + this->m_inputValue3Operation = this->getInputSocketReader(2); } void MathBaseOperation::deinitExecution() { this->m_inputValue1Operation = NULL; this->m_inputValue2Operation = NULL; + this->m_inputValue3Operation = NULL; } void MathBaseOperation::determineResolution(unsigned int resolution[2], @@ -182,6 +186,54 @@ void MathTangentOperation::executePixelSampled(float output[4], clampIfNeeded(output); } +void MathHyperbolicSineOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + output[0] = sinh(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathHyperbolicCosineOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + output[0] = cosh(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathHyperbolicTangentOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + output[0] = tanh(inputValue1[0]); + + clampIfNeeded(output); +} + void MathArcSineOperation::executePixelSampled(float output[4], float x, float y, @@ -404,6 +456,34 @@ void MathAbsoluteOperation::executePixelSampled(float output[4], clampIfNeeded(output); } +void MathRadiansOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = DEG2RADF(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathDegreesOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = RAD2DEGF(inputValue1[0]); + + clampIfNeeded(output); +} + void MathArcTan2Operation::executePixelSampled(float output[4], float x, float y, @@ -480,3 +560,194 @@ void MathSqrtOperation::executePixelSampled(float output[4], clampIfNeeded(output); } + +void MathInverseSqrtOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + if (inputValue1[0] > 0) { + output[0] = 1.0f / sqrt(inputValue1[0]); + } + else { + output[0] = 0.0f; + } + + clampIfNeeded(output); +} + +void MathSignOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = compatible_signf(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathExponentOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = expf(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathTruncOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + + output[0] = (inputValue1[0] >= 0.0f) ? floor(inputValue1[0]) : ceil(inputValue1[0]); + + clampIfNeeded(output); +} + +void MathSnapOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + if (inputValue1[0] == 0 || inputValue2[0] == 0) { /* We don't want to divide by zero. */ + output[0] = 0.0f; + } + else { + output[0] = floorf(inputValue1[0] / inputValue2[0]) * inputValue2[0]; + } + + clampIfNeeded(output); +} + +void MathWrapOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + float inputValue3[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + this->m_inputValue3Operation->readSampled(inputValue3, x, y, sampler); + + output[0] = wrapf(inputValue1[0], inputValue2[0], inputValue3[0]); + + clampIfNeeded(output); +} + +void MathPingpongOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + + output[0] = output[0] = fabsf( + fractf((inputValue1[0] - inputValue2[0]) / (inputValue2[0] * 2.0f)) * inputValue2[0] * 2.0f - + inputValue2[0]); + + clampIfNeeded(output); +} + +void MathCompareOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + float inputValue3[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + this->m_inputValue3Operation->readSampled(inputValue3, x, y, sampler); + + output[0] = (fabsf(inputValue1[0] - inputValue2[0]) <= MAX2(inputValue3[0], 1e-5f)) ? 1.0f : + 0.0f; + + clampIfNeeded(output); +} + +void MathMultiplyAddOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + float inputValue3[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + this->m_inputValue3Operation->readSampled(inputValue3, x, y, sampler); + + output[0] = inputValue1[0] * inputValue2[0] + inputValue3[0]; + + clampIfNeeded(output); +} + +void MathSmoothMinOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + float inputValue3[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + this->m_inputValue3Operation->readSampled(inputValue3, x, y, sampler); + + output[0] = smoothminf(inputValue1[0], inputValue2[0], inputValue3[0]); + + clampIfNeeded(output); +} + +void MathSmoothMaxOperation::executePixelSampled(float output[4], + float x, + float y, + PixelSampler sampler) +{ + float inputValue1[4]; + float inputValue2[4]; + float inputValue3[4]; + + this->m_inputValue1Operation->readSampled(inputValue1, x, y, sampler); + this->m_inputValue2Operation->readSampled(inputValue2, x, y, sampler); + this->m_inputValue3Operation->readSampled(inputValue3, x, y, sampler); + + output[0] = -smoothminf(-inputValue1[0], -inputValue2[0], inputValue3[0]); + + clampIfNeeded(output); +} diff --git a/source/blender/compositor/operations/COM_MathBaseOperation.h b/source/blender/compositor/operations/COM_MathBaseOperation.h index 7c11ea8f45b..199b59d8649 100644 --- a/source/blender/compositor/operations/COM_MathBaseOperation.h +++ b/source/blender/compositor/operations/COM_MathBaseOperation.h @@ -31,6 +31,7 @@ class MathBaseOperation : public NodeOperation { */ SocketReader *m_inputValue1Operation; SocketReader *m_inputValue2Operation; + SocketReader *m_inputValue3Operation; bool m_useClamp; @@ -119,6 +120,28 @@ class MathTangentOperation : public MathBaseOperation { void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); }; +class MathHyperbolicSineOperation : public MathBaseOperation { + public: + MathHyperbolicSineOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; +class MathHyperbolicCosineOperation : public MathBaseOperation { + public: + MathHyperbolicCosineOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; +class MathHyperbolicTangentOperation : public MathBaseOperation { + public: + MathHyperbolicTangentOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + class MathArcSineOperation : public MathBaseOperation { public: MathArcSineOperation() : MathBaseOperation() @@ -206,6 +229,22 @@ class MathAbsoluteOperation : public MathBaseOperation { void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); }; +class MathRadiansOperation : public MathBaseOperation { + public: + MathRadiansOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathDegreesOperation : public MathBaseOperation { + public: + MathDegreesOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + class MathArcTan2Operation : public MathBaseOperation { public: MathArcTan2Operation() : MathBaseOperation() @@ -246,4 +285,91 @@ class MathSqrtOperation : public MathBaseOperation { void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); }; +class MathInverseSqrtOperation : public MathBaseOperation { + public: + MathInverseSqrtOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathSignOperation : public MathBaseOperation { + public: + MathSignOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathExponentOperation : public MathBaseOperation { + public: + MathExponentOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathTruncOperation : public MathBaseOperation { + public: + MathTruncOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathSnapOperation : public MathBaseOperation { + public: + MathSnapOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathWrapOperation : public MathBaseOperation { + public: + MathWrapOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathPingpongOperation : public MathBaseOperation { + public: + MathPingpongOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathCompareOperation : public MathBaseOperation { + public: + MathCompareOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathMultiplyAddOperation : public MathBaseOperation { + public: + MathMultiplyAddOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathSmoothMinOperation : public MathBaseOperation { + public: + MathSmoothMinOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; + +class MathSmoothMaxOperation : public MathBaseOperation { + public: + MathSmoothMaxOperation() : MathBaseOperation() + { + } + void executePixelSampled(float output[4], float x, float y, PixelSampler sampler); +}; #endif -- cgit v1.2.3