[Bf-blender-cvs] [617d35315c1] functions: test dynamic composition instead of inheritance for functions
Jacques Lucke
noreply at git.blender.org
Sun Feb 10 20:25:45 CET 2019
Commit: 617d35315c12784df124549447b4e708fd30f4b7
Author: Jacques Lucke
Date: Wed Jan 30 16:37:39 2019 +0100
Branches: functions
https://developer.blender.org/rB617d35315c12784df124549447b4e708fd30f4b7
test dynamic composition instead of inheritance for functions
===================================================================
M source/blender/functions/FN_functions.h
M source/blender/functions/c_wrapper.cpp
M source/blender/functions/core/core.hpp
M source/blender/functions/core/cpu.hpp
M source/blender/modifiers/intern/MOD_functiondeform.c
===================================================================
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index 5b337dc7ef7..fd244e4e9ee 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -7,14 +7,16 @@
extern "C" {
#endif
-typedef struct OpaqueCPUFunction *FnCPUFunction;
+typedef struct OpaqueFnFunction *FnFunction;
typedef struct OpaqueFnType *FnTypeRef;
typedef struct OpaqueFnTuple *FnTuple;
+typedef struct OpaqueFnCallTuple *FnCallTuple;
-void FN_function_call(FnCPUFunction fn, FnTuple fn_in, FnTuple fn_out);
+FnCallTuple FN_function_get_tuple_call(FnFunction fn);
+void FN_function_call_tuple(FnCallTuple call, FnTuple fn_in, FnTuple fn_out);
-FnTuple FN_tuple_for_input(FnCPUFunction fn);
-FnTuple FN_tuple_for_output(FnCPUFunction fn);
+FnTuple FN_tuple_for_input(FnFunction fn);
+FnTuple FN_tuple_for_output(FnFunction fn);
void FN_tuple_free(FnTuple tuple);
@@ -29,7 +31,7 @@ FnTypeRef FN_type_get_float(void);
FnTypeRef FN_type_get_int32(void);
FnTypeRef FN_type_get_float_vector_3d(void);
-FnCPUFunction FN_get_deform_function(void);
+FnFunction FN_get_deform_function(int type);
#ifdef __cplusplus
}
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 1fe8c051747..2e7ff0fc403 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -3,33 +3,31 @@
#include "./types/types.hpp"
-inline FN::CPUFunction *unwrap(FnCPUFunction fn)
-{
- return (FN::CPUFunction *)fn;
-}
+#define WRAPPERS(T1, T2) \
+ inline T1 unwrap(T2 value) { return (T1)value; } \
+ inline T2 wrap(T1 value) { return (T2)value; }
-inline FN::Tuple *unwrap(FnTuple tuple)
-{
- return (FN::Tuple *)tuple;
-}
+WRAPPERS(const FN::Function *, FnFunction);
+WRAPPERS(FN::Tuple *, FnTuple);
+WRAPPERS(const FN::TupleCallBody *, FnCallTuple);
-inline FnTuple wrap(FN::Tuple *tuple)
+void FN_function_call_tuple(FnCallTuple fn_call, FnTuple fn_in, FnTuple fn_out)
{
- return (FnTuple)tuple;
+ unwrap(fn_call)->call(*unwrap(fn_in), *unwrap(fn_out));
}
-void FN_function_call(FnCPUFunction fn, FnTuple fn_in, FnTuple fn_out)
+FnCallTuple FN_function_get_tuple_call(FnFunction fn)
{
- unwrap(fn)->call(*unwrap(fn_in), *unwrap(fn_out));
+ return wrap(unwrap(fn)->body<FN::TupleCallBody>());
}
-FnTuple FN_tuple_for_input(FnCPUFunction fn)
+FnTuple FN_tuple_for_input(FnFunction fn)
{
auto tuple = new FN::Tuple(unwrap(fn)->signature().inputs());
return wrap(tuple);
}
-FnTuple FN_tuple_for_output(FnCPUFunction fn)
+FnTuple FN_tuple_for_output(FnFunction fn)
{
auto tuple = new FN::Tuple(unwrap(fn)->signature().outputs());
return wrap(tuple);
@@ -77,34 +75,48 @@ FnTypeRef FN_type_get_float_vector_3d()
#include <cmath>
#include <algorithm>
-class DeformFunction : public FN::CPUFunction {
-private:
- DeformFunction(FN::Signature sig)
- : CPUFunction(sig) {}
+
+class Deform1 : public FN::TupleCallBody {
public:
- static DeformFunction *Create()
+ virtual void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) const override
{
- FN::Signature sig({FN::Types::floatvec3d_ty, FN::Types::float_ty}, {FN::Types::floatvec3d_ty});
- return new DeformFunction(sig);
+ Vector vec = fn_in.get<Vector>(0);
+ float control = fn_in.get<float>(1);
+
+ Vector result;
+
+ result.x = vec.x * control;
+ result.y = vec.y;// / std::max(control, 0.1f);
+ result.z = vec.z;
+
+ fn_out.set<Vector>(0, result);
}
+};
- void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) override
+class Deform2 : public FN::TupleCallBody {
+public:
+ virtual void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) const override
{
Vector vec = fn_in.get<Vector>(0);
float control = fn_in.get<float>(1);
Vector result;
- result.x = vec.x * control;
- result.y = vec.y / std::max(control, 0.1f);
+ result.x = vec.x;
+ result.y = vec.y * control;// / std::max(control, 0.1f);
result.z = vec.z;
fn_out.set<Vector>(0, result);
}
};
-FnCPUFunction FN_get_deform_function()
+FnFunction FN_get_deform_function(int type)
{
- return (FnCPUFunction)DeformFunction::Create();
+ FN::Signature signature({FN::Types::floatvec3d_ty, FN::Types::float_ty}, {FN::Types::floatvec3d_ty});
+ FN::FunctionBodies bodies;
+ if (type == 0) bodies.add(new Deform1());
+ else bodies.add(new Deform2());
+
+ return wrap(new FN::Function(signature, bodies));
}
\ No newline at end of file
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index dfbf3eacb42..75f0a94773e 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -3,6 +3,7 @@
#include <string>
#include "BLI_small_vector.hpp"
+#include "BLI_small_map.hpp"
namespace FN {
@@ -52,10 +53,41 @@ namespace FN {
const SmallTypeVector m_outputs;
};
+ class FunctionBodies {
+ private:
+ BLI::SmallMap<uint64_t, void *> m_bodies;
+
+ public:
+ template<typename T>
+ void add(const T *body)
+ {
+ this->m_bodies.add(this->get_key<T>(), (void *)body);
+ }
+
+ template<typename T>
+ inline const T *get() const
+ {
+ uint64_t key = this->get_key<T>();
+ if (this->m_bodies.contains(key)) {
+ return (T *)this->m_bodies.lookup(key);
+ }
+ else {
+ return nullptr;
+ }
+ }
+
+ private:
+ template<typename T>
+ static uint64_t get_key()
+ {
+ return (uint64_t)T::identifier;
+ }
+ };
+
class Function {
public:
- Function(const Signature &signature)
- : m_signature(signature) {}
+ Function(const Signature &signature, const FunctionBodies &bodies)
+ : m_signature(signature), m_bodies(bodies) {}
virtual ~Function() {}
@@ -64,8 +96,15 @@ namespace FN {
return this->m_signature;
}
+ template<typename T>
+ inline const T *body() const
+ {
+ return this->m_bodies.get<T>();
+ }
+
private:
const Signature m_signature;
+ const FunctionBodies m_bodies;
};
} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/cpu.hpp b/source/blender/functions/core/cpu.hpp
index 5a7f372c745..21ff5bdd97a 100644
--- a/source/blender/functions/core/cpu.hpp
+++ b/source/blender/functions/core/cpu.hpp
@@ -80,12 +80,11 @@ namespace FN {
void *data;
};
- class CPUFunction : public Function {
+ class TupleCallBody {
public:
- CPUFunction(const Signature &signature)
- : Function(signature) {}
+ static constexpr const char *identifier = "Tuple Call Body";
- virtual void call(const Tuple &fn_in, Tuple &fn_out) = 0;
+ virtual void call(const Tuple &fn_in, Tuple &fn_out) const = 0;
};
} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/modifiers/intern/MOD_functiondeform.c b/source/blender/modifiers/intern/MOD_functiondeform.c
index d6dd7be15ca..82f7c04ceb3 100644
--- a/source/blender/modifiers/intern/MOD_functiondeform.c
+++ b/source/blender/modifiers/intern/MOD_functiondeform.c
@@ -53,7 +53,8 @@ static void do_deformation(
float (*vertexCos)[3],
int numVerts)
{
- FnCPUFunction fn = FN_get_deform_function();
+ FnFunction fn = FN_get_deform_function(fdmd->control2);
+ FnCallTuple fn_call = FN_function_get_tuple_call(fn);
FnTuple fn_in = FN_tuple_for_input(fn);
FnTuple fn_out = FN_tuple_for_output(fn);
@@ -63,7 +64,7 @@ static void do_deformation(
for (int i = 0; i < numVerts; i++) {
FN_tuple_set_float_vector_3(fn_in, 0, vertexCos[i]);
- FN_function_call(fn, fn_in, fn_out);
+ FN_function_call_tuple(fn_call, fn_in, fn_out);
FN_tuple_get_float_vector_3(fn_out, 0, vertexCos[i]);
}
More information about the Bf-blender-cvs
mailing list