[Bf-blender-cvs] [d0cdadc81ea] functions: More vector math operations: Subtract, Cross Product, Reflect
artem ivanov
noreply at git.blender.org
Mon Aug 5 11:51:45 CEST 2019
Commit: d0cdadc81ea2a074d2d95b486dda42aaac038eb2
Author: artem ivanov
Date: Mon Aug 5 11:50:00 2019 +0200
Branches: functions
https://developer.blender.org/rBd0cdadc81ea2a074d2d95b486dda42aaac038eb2
More vector math operations: Subtract, Cross Product, Reflect
===================================================================
M release/scripts/startup/nodes/function_nodes/vector.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/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 4f7d7f1cda4..1bc333ab557 100644
--- a/release/scripts/startup/nodes/function_nodes/vector.py
+++ b/release/scripts/startup/nodes/function_nodes/vector.py
@@ -10,6 +10,9 @@ class VectorMathNode(bpy.types.Node, FunctionNode):
operation_items = [
("ADD", "Add", "", "", 1),
+ ("SUB", "Subtract", "", "", 2),
+ ("CROSS", "Cross Product", "", "", 3),
+ ("REFLECT", "Reflect", "", "", 4),
]
operation: EnumProperty(
diff --git a/source/blender/functions/backends/llvm/builder.hpp b/source/blender/functions/backends/llvm/builder.hpp
index 3c25135e687..ceaeed01258 100644
--- a/source/blender/functions/backends/llvm/builder.hpp
+++ b/source/blender/functions/backends/llvm/builder.hpp
@@ -281,6 +281,11 @@ 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 *CreateFMul(llvm::Value *a, llvm::Value *b)
{
return m_builder.CreateFMul(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 487375477af..bffe7ae131e 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
@@ -115,6 +115,12 @@ static SharedFunction &get_vector_math_function(int operation)
switch (operation) {
case 1:
return Functions::GET_FN_add_vectors();
+ case 2:
+ return Functions::GET_FN_sub_vectors();
+ case 3:
+ return Functions::GET_FN_cross_vectors();
+ case 4:
+ return Functions::GET_FN_reflect_vectors();
default:
BLI_assert(false);
return *(SharedFunction *)nullptr;
diff --git a/source/blender/functions/functions/vectors.cpp b/source/blender/functions/functions/vectors.cpp
index febeea82c87..1b8e8e06ade 100644
--- a/source/blender/functions/functions/vectors.cpp
+++ b/source/blender/functions/functions/vectors.cpp
@@ -123,5 +123,107 @@ 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;
+}
+
} // namespace Functions
} // namespace FN
diff --git a/source/blender/functions/functions/vectors.hpp b/source/blender/functions/functions/vectors.hpp
index 68e8469f922..e52e6f6e56f 100644
--- a/source/blender/functions/functions/vectors.hpp
+++ b/source/blender/functions/functions/vectors.hpp
@@ -9,6 +9,9 @@ SharedFunction &GET_FN_combine_vector();
SharedFunction &GET_FN_separate_vector();
SharedFunction &GET_FN_vector_distance();
SharedFunction &GET_FN_add_vectors();
+SharedFunction &GET_FN_sub_vectors();
+SharedFunction &GET_FN_cross_vectors();
+SharedFunction &GET_FN_reflect_vectors();
} // namespace Functions
} // namespace FN
More information about the Bf-blender-cvs
mailing list