Commit: 0b15817ad2bf0111750bbfcd92ead6a0d86de54b Author: artem ivanov Date: Mon Aug 12 16:36:31 2019 +0200 Branches: functions https://developer.blender.org/rB0b15817ad2bf0111750bbfcd92ead6a0d86de54b
More Float Math operations Differential Revision: https://developer.blender.org/D5433 =================================================================== M release/scripts/startup/nodes/function_nodes/float_math.py M source/blender/functions/backends/llvm/builder.hpp M source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp M source/blender/functions/functions/scalar_math.cpp M source/blender/functions/functions/scalar_math.hpp =================================================================== diff --git a/release/scripts/startup/nodes/function_nodes/float_math.py b/release/scripts/startup/nodes/function_nodes/float_math.py index f3d597f58ff..89f4454288e 100644 --- a/release/scripts/startup/nodes/function_nodes/float_math.py +++ b/release/scripts/startup/nodes/function_nodes/float_math.py @@ -4,15 +4,48 @@ from .. base import FunctionNode from .. node_builder import NodeBuilder operation_items = [ - ("ADD", "Add", "", "", 1), - ("MULTIPLY", "Multiply", "", "", 2), - ("MIN", "Minimum", "", "", 3), - ("MAX", "Maximum", "", "", 4), - ("SIN", "Sin", "", "", 5), + ("ADD", "Add", "", "", 1), + ("SUB", "Subtract", "", "", 2), + ("MULTIPLY", "Multiply", "", "", 3), + ("DIVIDE", "Divide", "", "", 4), + None, + ("POWER", "Power", "", "", 5), + ("LOG", "Logarithm", "", "", 6), + ("SQRT", "Square Root", "", "", 7), + None, + ("ABS", "Absolute", "", "", 8), + ("MIN", "Minimum", "", "", 9), + ("MAX", "Maximum", "", "", 10), + ("MOD", "Modulo", "", "", 18), + None, + ("SIN", "Sine", "", "", 11), + ("COS", "Cosine", "", "", 12), + ("TAN", "Tangent", "", "", 13), + ("ASIN", "Arcsine", "", "", 14), + ("ACOS", "Arccosine", "", "", 15), + ("ATAN", "Arctangent", "", "", 16), + ("ATAN2", "Arctan2", "", "", 17), + None, + ("FRACT", "Fract", "", "", 19), + ("CEIL", "Ceil", "", "", 20), + ("FLOOR", "Floor", "", "", 21), + ("ROUND", "Round", "", "", 22), + ("SNAP", "Snap", "", "", 23), ] single_value_operations = { + "SQRT", + "ABS", "SIN", + "COS", + "TAN", + "ASIN", + "ACOS", + "ATAN", + "FRACT", + "CEIL", + "FLOOR", + "ROUND" } class FloatMathNode(bpy.types.Node, FunctionNode): @@ -21,7 +54,9 @@ class FloatMathNode(bpy.types.Node, FunctionNode): search_terms = ( ("Add Floats", {"operation" : "ADD"}), + ("Subtract Floats", {"operation" : "SUB"}), ("Multiply Floats", {"operation" : "MULTIPLY"}), + ("Divide Floats", {"operation" : "DIVIDE"}), ) operation: EnumProperty( diff --git a/source/blender/functions/backends/llvm/builder.hpp b/source/blender/functions/backends/llvm/builder.hpp index 5a17201bd23..f87278edc9d 100644 --- a/source/blender/functions/backends/llvm/builder.hpp +++ b/source/blender/functions/backends/llvm/builder.hpp @@ -425,6 +425,13 @@ class CodeBuilder { return m_builder.CreateCall(function, value); } + llvm::Value *CreateCos(llvm::Value *value) + { + auto *function = llvm::Intrinsic::getDeclaration( + this->getModule(), llvm::Intrinsic::cos, value->getType()); + return m_builder.CreateCall(function, value); + } + llvm::Value *CreateSIntMax(llvm::Value *a, llvm::Value *b) { llvm::Value *a_is_larger = m_builder.CreateICmpSGE(a, b); diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp index 997048e8279..cb7e4fafd7b 100644 --- a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp +++ b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp @@ -73,13 +73,49 @@ static SharedFunction &get_float_math_function(int operation) case 1: return Functions::GET_FN_add_floats(); case 2: - return Functions::GET_FN_multiply_floats(); + return Functions::GET_FN_sub_floats(); case 3: - return Functions::GET_FN_min_floats(); + return Functions::GET_FN_multiply_floats(); case 4: - return Functions::GET_FN_max_floats(); + return Functions::GET_FN_divide_floats(); case 5: + return Functions::GET_FN_power_floats(); + case 6: + return Functions::GET_FN_log_floats(); + case 7: + return Functions::GET_FN_sqrt_float(); + case 8: + return Functions::GET_FN_abs_float(); + case 9: + return Functions::GET_FN_min_floats(); + case 10: + return Functions::GET_FN_max_floats(); + case 11: return Functions::GET_FN_sin_float(); + case 12: + return Functions::GET_FN_cos_float(); + case 13: + return Functions::GET_FN_tan_float(); + case 14: + return Functions::GET_FN_asin_float(); + case 15: + return Functions::GET_FN_acos_float(); + case 16: + return Functions::GET_FN_atan_float(); + case 17: + return Functions::GET_FN_atan2_floats(); + case 18: + return Functions::GET_FN_mod_floats(); + case 19: + return Functions::GET_FN_fract_float(); + case 20: + return Functions::GET_FN_ceil_float(); + case 21: + return Functions::GET_FN_floor_float(); + case 22: + return Functions::GET_FN_round_float(); + case 23: + return Functions::GET_FN_snap_floats(); default: BLI_assert(false); return *(SharedFunction *)nullptr; diff --git a/source/blender/functions/functions/scalar_math.cpp b/source/blender/functions/functions/scalar_math.cpp index c9dbcfc172c..cc1dfcceb20 100644 --- a/source/blender/functions/functions/scalar_math.cpp +++ b/source/blender/functions/functions/scalar_math.cpp @@ -38,7 +38,7 @@ class AddFloats : public TupleCallBody { } }; -class GenAddFloats : public LLVMBuildIRBody { +class AddFloatsGen : public LLVMBuildIRBody { void build_ir(CodeBuilder &builder, CodeInterface &interface, const BuildIRSettings &UNUSED(settings)) const override @@ -51,8 +51,35 @@ class GenAddFloats : public LLVMBuildIRBody { BLI_LAZY_INIT(SharedFunction, GET_FN_add_floats) { auto fn = get_math_function__two_inputs("Add Floats"); - // fn->add_body<AddFloats>(); - fn->add_body<GenAddFloats>(); + fn->add_body<AddFloats>(); + fn->add_body<AddFloatsGen>(); + return fn; +} + +class SubFloats : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + float b = fn_in.get<float>(1); + fn_out.set<float>(0, a - b); + } +}; + +class SubFloatsGen : public LLVMBuildIRBody { + void build_ir(CodeBuilder &builder, + CodeInterface &interface, + const BuildIRSettings &UNUSED(settings)) const override + { + auto output = builder.CreateFSub(interface.get_input(0), interface.get_input(1)); + interface.set_output(0, output); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_sub_floats) +{ + auto fn = get_math_function__two_inputs("Sub Floats"); + fn->add_body<SubFloats>(); + fn->add_body<SubFloatsGen>(); return fn; } @@ -83,6 +110,100 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_multiply_floats) return fn; } +class DivideFloats : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + float b = fn_in.get<float>(1); + float result = 0.0f; + if (b == 0.0f) { + result = 0.0f; + } + else { + result = a / b; + } + fn_out.set<float>(0, result); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_divide_floats) +{ + auto fn = get_math_function__two_inputs("Divide Floats"); + fn->add_body<DivideFloats>(); + return fn; +} + +class PowerFloats : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + float b = fn_in.get<float>(1); + float result = 0.0f; + float integral_part = 0.0f; + if (a != 0.0f && (a > 0.0f || modff(b, &integral_part) == 0.0f)) { + result = powf(a, b); + } + fn_out.set<float>(0, result); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_power_floats) +{ + auto fn = get_math_function__two_inputs("Power Floats"); + fn->add_body<PowerFloats>(); + return fn; +} + +class LogarithmFloats : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + float b = fn_in.get<float>(1); + float result = 0.0f; + if (a > 0.0f && b > 0.0f && b != 1.0f) { + result = logf(a) / logf(b); + } + fn_out.set<float>(0, result); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_log_floats) +{ + auto fn = get_math_function__two_inputs("Logarithm"); + fn->add_body<LogarithmFloats>(); + return fn; +} + +class SqrtFloat : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + fn_out.set<float>(0, sasqrtf(a)); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_sqrt_float) +{ + auto fn = get_math_function__one_input("Square Root"); + fn->add_body<SqrtFloat>(); + return fn; +} + +class AbsFloat : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + fn_out.set<float>(0, fabsf(a)); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_abs_float) +{ + auto fn = get_math_function__one_input("Absolute Float"); + fn->add_body<AbsFloat>(); + return fn; +} + class MinFloats : public TupleCallBody { void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override { @@ -115,6 +236,241 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_max_floats) return fn; } +class SinFloat : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float a = fn_in.get<float>(0); + fn_out.set<float>(0, std::sin(a)); + } +}; + +class SinFloatGen : public LLVMBuildIRBody { + void build_ir(CodeBuilder &builder, + CodeInterface &interface, + const BuildIRSettings &UNUSED(settings)) const override + { + auto output = builder.CreateSin(interface.get_input(0)); + interface.set_output(0, output); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_sin_float) +{ + auto fn = get_math_function__one_input("Sin"); + fn->add_body<SinFloat>(); + fn->add_body<SinFloatGen>(); + return fn @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] https://lists.blender.org/mailman/listinfo/bf-blender-cvs
