Commit: e0ebaf8f3c076b1258e8cbfee6d1bacd6325bf2c Author: Eitan Date: Mon Aug 12 16:21:26 2019 +0200 Branches: functions https://developer.blender.org/rBe0ebaf8f3c076b1258e8cbfee6d1bacd6325bf2c
Add multiply and divide to vector math node Differential Revision: https://developer.blender.org/D5430 =================================================================== M release/scripts/startup/nodes/function_nodes/vector.py M source/blender/blenlib/BLI_math.hpp M source/blender/functions/backends/cpp/list.hpp 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/vectors.cpp M source/blender/functions/functions/vectors.hpp =================================================================== diff --git a/release/scripts/startup/nodes/function_nodes/vector.py b/release/scripts/startup/nodes/function_nodes/vector.py index 1bc333ab557..609256759fd 100644 --- a/release/scripts/startup/nodes/function_nodes/vector.py +++ b/release/scripts/startup/nodes/function_nodes/vector.py @@ -10,9 +10,11 @@ class VectorMathNode(bpy.types.Node, FunctionNode): operation_items = [ ("ADD", "Add", "", "", 1), - ("SUB", "Subtract", "", "", 2), - ("CROSS", "Cross Product", "", "", 3), - ("REFLECT", "Reflect", "", "", 4), + ("SUB", "Subtract", "", "", 2), + ("MUL", "Multiply", "", "", 3), + ("DIV", "Divide", "", "", 4), + ("CROSS", "Cross Product", "", "", 5), + ("REFLECT", "Reflect", "", "", 6), ] operation: EnumProperty( diff --git a/source/blender/blenlib/BLI_math.hpp b/source/blender/blenlib/BLI_math.hpp index aa226ea3538..6161d031c89 100644 --- a/source/blender/blenlib/BLI_math.hpp +++ b/source/blender/blenlib/BLI_math.hpp @@ -139,6 +139,15 @@ struct float3 { return result; } + static float3 safe_divide(const float3 a, const float3 b) + { + float3 result; + result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x; + result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y; + result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z; + return result; + } + void invert() { x = -x; @@ -202,6 +211,12 @@ struct float3 { return b * a; } + friend float3 operator/(float3 a, float3 b) + { + BLI_assert(!b.is_zero()); + return {a.x / b.x, a.y / b.y, a.z / b.z}; + } + friend float3 operator/(float3 a, float b) { BLI_assert(b != 0); diff --git a/source/blender/functions/backends/cpp/list.hpp b/source/blender/functions/backends/cpp/list.hpp index 240b66dac4a..357cc35164b 100644 --- a/source/blender/functions/backends/cpp/list.hpp +++ b/source/blender/functions/backends/cpp/list.hpp @@ -75,7 +75,6 @@ class List : public RefCounter { void get__dynamic_copy_to_tuple(uint element_index, Tuple &tuple, uint tuple_index) { - BLI_assert(this->is_mutable()); BLI_assert(&tuple.meta().type_info(tuple_index) == m_type_info); BLI_assert(element_index < m_size); void *src = POINTER_OFFSET(m_storage, element_index * m_type_info->size()); diff --git a/source/blender/functions/backends/llvm/builder.hpp b/source/blender/functions/backends/llvm/builder.hpp index ceaeed01258..5a17201bd23 100644 --- a/source/blender/functions/backends/llvm/builder.hpp +++ b/source/blender/functions/backends/llvm/builder.hpp @@ -281,16 +281,21 @@ class CodeBuilder { return m_builder.CreateFAdd(a, b); } - llvm::Value *CreateFSub(llvm::Value *a, llvm::Value *b) - { - return m_builder.CreateFSub(a, b); - } - + llvm::Value *CreateFSub(llvm::Value *a, llvm::Value *b) + { + return m_builder.CreateFSub(a, b); + } + llvm::Value *CreateFMul(llvm::Value *a, llvm::Value *b) { return m_builder.CreateFMul(a, b); } + llvm::Value *CreateFDiv(llvm::Value *a, llvm::Value *b) + { + return m_builder.CreateFDiv(a, b); + } + llvm::Value *CreateURem(llvm::Value *a, llvm::Value *b) { return m_builder.CreateURem(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 bffe7ae131e..997048e8279 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 @@ -116,10 +116,14 @@ static SharedFunction &get_vector_math_function(int operation) case 1: return Functions::GET_FN_add_vectors(); case 2: - return Functions::GET_FN_sub_vectors(); - case 3: + return Functions::GET_FN_sub_vectors(); + case 3: + return Functions::GET_FN_mul_vectors(); + case 4: + return Functions::GET_FN_div_vectors(); + case 5: return Functions::GET_FN_cross_vectors(); - case 4: + case 6: return Functions::GET_FN_reflect_vectors(); default: BLI_assert(false); diff --git a/source/blender/functions/functions/vectors.cpp b/source/blender/functions/functions/vectors.cpp index 1b8e8e06ade..9457c864f2d 100644 --- a/source/blender/functions/functions/vectors.cpp +++ b/source/blender/functions/functions/vectors.cpp @@ -123,107 +123,150 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_add_vectors) return fn; } -class SubVectors : public TupleCallBody { - void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override - { - float3 a = fn_in.get<float3>(0); - float3 b = fn_in.get<float3>(1); - fn_out.set<float3>(0, a - b); - } -}; - -class SubVectorsGen : public LLVMBuildIRBody { - void build_ir(CodeBuilder &builder, - CodeInterface &interface, - const BuildIRSettings &UNUSED(settings)) const override - { - llvm::Value *a = interface.get_input(0); - llvm::Value *b = interface.get_input(1); - llvm::Value *result = builder.CreateFSub(a, b); - interface.set_output(0, result); - } -}; - -BLI_LAZY_INIT(SharedFunction, GET_FN_sub_vectors) -{ - auto fn = get_math_function__two_inputs("Subtract Vectors"); - fn->add_body<SubVectors>(); - fn->add_body<SubVectorsGen>(); - return fn; -} - -class CrossProductVectors : public TupleCallBody { - void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override - { - float3 a = fn_in.get<float3>(0); - float3 b = fn_in.get<float3>(1); - float3 result; - cross_v3_v3v3_hi_prec(result, a, b); - fn_out.set<float3>(0, result); - } -}; - -class CrossProductVectorsGen : public LLVMBuildIRBody { - void build_ir(CodeBuilder &builder, - CodeInterface &interface, - const BuildIRSettings &UNUSED(settings)) const override - { - auto a = interface.get_input(0); - auto b = interface.get_input(1); - - auto a_x = builder.CreateExtractElement(a, 0); - auto a_y = builder.CreateExtractElement(a, 1); - auto a_z = builder.CreateExtractElement(a, 2); - - auto b_x = builder.CreateExtractElement(b, 0); - auto b_y = builder.CreateExtractElement(b, 1); - auto b_z = builder.CreateExtractElement(b, 2); - - auto mul_ay_bz = builder.CreateFMul(a_y, b_z); - auto mul_az_by = builder.CreateFMul(a_z, b_y); - auto result_x = builder.CreateFSub(mul_ay_bz, mul_az_by); - - auto mul_az_bx = builder.CreateFMul(a_z, b_x); - auto mul_ax_bz = builder.CreateFMul(a_x, b_z); - auto result_y = builder.CreateFSub(mul_az_bx, mul_ax_bz); - - auto mul_ax_by = builder.CreateFMul(a_x, b_y); - auto mul_ay_bx = builder.CreateFMul(a_y, b_x); - auto result_z = builder.CreateFSub(mul_ax_by, mul_ay_bx); - - auto result = static_cast<llvm::Value *>(builder.getUndef(a->getType())); - - result = builder.CreateInsertElement(result, result_x, 0); - result = builder.CreateInsertElement(result, result_y, 1); - result = builder.CreateInsertElement(result, result_z, 2); - - interface.set_output(0, result); - } -}; - -BLI_LAZY_INIT(SharedFunction, GET_FN_cross_vectors) -{ - auto fn = get_math_function__two_inputs("Cross Product"); - fn->add_body<CrossProductVectors>(); - fn->add_body<CrossProductVectorsGen>(); - return fn; -} - -class ReflectVectors : public TupleCallBody { - void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override - { - float3 a = fn_in.get<float3>(0); - float3 b = fn_in.get<float3>(1); - fn_out.set<float3>(0, a.reflected(b.normalized())); - } -}; - -BLI_LAZY_INIT(SharedFunction, GET_FN_reflect_vectors) -{ - auto fn = get_math_function__two_inputs("Reflect Vectors"); - fn->add_body<ReflectVectors>(); - return fn; -} - +class SubVectors : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float3 a = fn_in.get<float3>(0); + float3 b = fn_in.get<float3>(1); + fn_out.set<float3>(0, a - b); + } +}; + +class SubVectorsGen : public LLVMBuildIRBody { + void build_ir(CodeBuilder &builder, + CodeInterface &interface, + const BuildIRSettings &UNUSED(settings)) const override + { + llvm::Value *a = interface.get_input(0); + llvm::Value *b = interface.get_input(1); + llvm::Value *result = builder.CreateFSub(a, b); + interface.set_output(0, result); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_sub_vectors) +{ + auto fn = get_math_function__two_inputs("Subtract Vectors"); + fn->add_body<SubVectors>(); + fn->add_body<SubVectorsGen>(); + return fn; +} +class MultiplyVectors : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float3 a = fn_in.get<float3>(0); + float3 b = fn_in.get<float3>(1); + fn_out.set<float3>(0, a * b); + } +}; + +class MultiplyVectorsGen : public LLVMBuildIRBody { + void build_ir(CodeBuilder &builder, + CodeInterface &interface, + const BuildIRSettings &UNUSED(settings)) const override + { + llvm::Value *a = interface.get_input(0); + llvm::Value *b = interface.get_input(1); + llvm::Value *result = builder.CreateFMul(a, b); + interface.set_output(0, result); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_mul_vectors) +{ + auto fn = get_math_function__two_inputs("Multiply Vectors"); + fn->add_body<MultiplyVectors>(); + fn->add_body<MultiplyVectorsGen>(); + return fn; +} + +class DivideVectors : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float3 a = fn_in.get<float3>(0); + float3 b = fn_in.get<float3>(1); + fn_out.set<float3>(0, float3::safe_divide(a, b)); + } +}; + +BLI_LAZY_INIT(SharedFunction, GET_FN_div_vectors) +{ + auto fn = get_math_function__two_inputs("Divide Vectors"); + fn->add_body<DivideVectors>(); + return fn; +} +class CrossProductVectors : public TupleCallBody { + void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override + { + float3 a = fn_in.get<float3>(0); + float3 b = fn_in.get<float3>(1); + float3 result; + cross_v3_v3v3_hi_ @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] https://lists.blender.org/mailman/listinfo/bf-blender-cvs
