[Bf-blender-cvs] [d5e25680dfe] functions: separate actual core from cpu functions
Jacques Lucke
noreply at git.blender.org
Sun Feb 10 20:25:41 CET 2019
Commit: d5e25680dfe3d9fff833b5dd8380996d6d34cc84
Author: Jacques Lucke
Date: Mon Jan 28 15:11:46 2019 +0100
Branches: functions
https://developer.blender.org/rBd5e25680dfe3d9fff833b5dd8380996d6d34cc84
separate actual core from cpu functions
===================================================================
M source/blender/functions/CMakeLists.txt
M source/blender/functions/FN_functions.h
M source/blender/functions/FN_functions.hpp
A source/blender/functions/c_wrapper.cpp
A source/blender/functions/core/core.hpp
A source/blender/functions/core/cpu.hpp
D source/blender/functions/intern/c_wrapper.cpp
D source/blender/functions/intern/function.cpp
M source/blender/modifiers/intern/MOD_functiondeform.c
===================================================================
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 53596722345..fe5140ce88a 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -7,10 +7,12 @@ set(INC_SYS
)
set(SRC
- intern/c_wrapper.cpp
- intern/function.cpp
-
FN_functions.h
+ c_wrapper.cpp
+
+ core/core.hpp
+ core/cpu.hpp
+
FN_functions.hpp
types/numeric.hpp
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index be99a49165a..5b337dc7ef7 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -7,35 +7,21 @@
extern "C" {
#endif
-typedef struct OpaqueFunction *FunctionRef;
+typedef struct OpaqueCPUFunction *FnCPUFunction;
typedef struct OpaqueFnType *FnTypeRef;
-typedef struct OpaqueFnInputs *FnInputsRef;
-typedef struct OpaqueFnOutputs *FnOutputsRef;
+typedef struct OpaqueFnTuple *FnTuple;
-/* Call a function with the given input.
- * The function output will be written into fn_out.
- * Returns true on success. */
-bool FN_function_call(FunctionRef fn, FnInputsRef fn_in, FnOutputsRef fn_out);
+void FN_function_call(FnCPUFunction fn, FnTuple fn_in, FnTuple fn_out);
+FnTuple FN_tuple_for_input(FnCPUFunction fn);
+FnTuple FN_tuple_for_output(FnCPUFunction fn);
-/* Create a container to store function inputs. */
-FnInputsRef FN_inputs_new(FunctionRef fn);
+void FN_tuple_free(FnTuple tuple);
-/* Free a set of function inputs. */
-void FN_inputs_free(FnInputsRef fn_in);
+void FN_tuple_set_float(FnTuple tuple, uint index, float value);
+void FN_tuple_set_float_vector_3(FnTuple tuple, uint index, float vector[3]);
-/* Set a function input by index. Returns true on success. */
-void FN_inputs_set_float(FnInputsRef fn_in, uint index, float value);
-void FN_inputs_set_float_vector_3(FnInputsRef fn_in, uint index, float vector[3]);
-
-/* Create a container to store function outputs. */
-FnOutputsRef FN_outputs_new(FunctionRef fn);
-
-/* Free a set of output functions. */
-void FN_outputs_free(FnOutputsRef fn_out);
-
-/* Extract the result of an executed function by index. */
-void FN_outputs_get_float_vector_3(FnOutputsRef fn_out, uint index, float dst[3]);
+void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3]);
const char *FN_type_name(FnTypeRef type);
@@ -43,8 +29,7 @@ FnTypeRef FN_type_get_float(void);
FnTypeRef FN_type_get_int32(void);
FnTypeRef FN_type_get_float_vector_3d(void);
-FunctionRef FN_get_add_const_function(int value);
-FunctionRef FN_get_deform_function(void);
+FnCPUFunction FN_get_deform_function(void);
#ifdef __cplusplus
}
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 87102419b58..ea4e9ff7e42 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -1,151 +1,4 @@
#pragma once
-#include <string>
-#include <type_traits>
-
-#include "BLI_utildefines.h"
-#include "BLI_small_vector.hpp"
-
-namespace FN {
- using namespace BLI;
-
- class Type;
- class Inputs;
- class Outputs;
- class Signature;
- class Function;
-
- using SmallTypeVector = SmallVector<const Type *>;
-
- class Type {
- public:
- const std::string &name() const;
- const uint size() const;
-
- protected:
- std::string m_name;
- uint m_size;
- };
-
- class Tuple {
- public:
- Tuple() {}
- Tuple(const SmallTypeVector &types)
- : types(types)
- {
- int total_size = 0;
- for (const Type *type : types) {
- this->offsets.append(total_size);
- this->initialized.append(false);
- total_size += type->size();
- }
- this->offsets.append(total_size);
- this->data = std::malloc(total_size);
- }
-
- ~Tuple()
- {
- std::free(this->data);
- }
-
- template<typename T>
- inline void set(uint index, const T &value)
- {
- BLI_assert(index < this->types.size());
- BLI_assert(sizeof(T) == this->element_size(index));
-
- if (std::is_trivial<T>::value) {
- std::memcpy((char *)this->data + this->offsets[index], &value, sizeof(T));
- }
- else {
- const T *begin = &value;
- const T *end = begin + 1;
- T *dst = (T *)((char *)this->data + this->offsets[index]);
-
- if (this->initialized[index]) {
- std::copy(begin, end, dst);
- }
- else {
- std::uninitialized_copy(begin, end, dst);
- this->initialized[index] = true;
- }
- }
- }
-
- template<typename T>
- inline const T &get(uint index) const
- {
- BLI_assert(index < this->types.size());
- BLI_assert(sizeof(T) == this->element_size(index));
-
- if (!std::is_trivial<T>::value) {
- BLI_assert(this->initialized[index]);
- }
-
- return *(T *)((char *)this->data + this->offsets[index]);;
- }
-
- private:
- inline uint element_size(uint index) const
- {
- return this->offsets[index + 1] - this->offsets[index];
- }
-
- const SmallTypeVector types;
- SmallVector<uint> offsets;
- SmallVector<bool> initialized;
- void *data;
- };
-
- class Inputs : public Tuple {
- public:
- Inputs(const Function &fn);
-
- private:
- const Function &fn;
- };
-
- class Outputs : public Tuple {
- public:
- Outputs(const Function &fn);
-
- private:
- const Function &fn;
- };
-
- class Signature {
- public:
- Signature()
- : m_inputs({}), m_outputs({}) {}
-
- Signature(const SmallTypeVector &inputs, const SmallTypeVector &outputs)
- : m_inputs(inputs), m_outputs(outputs) {}
-
- ~Signature() {}
-
- inline const SmallTypeVector &inputs() const
- { return this->m_inputs; }
- inline const SmallTypeVector &outputs() const
- { return this->m_outputs; }
-
- private:
- const SmallTypeVector m_inputs;
- const SmallTypeVector m_outputs;
- };
-
- class Function {
- public:
- Function(const Signature &signature)
- : m_signature(signature) {}
-
- virtual ~Function();
-
- virtual bool call(const Inputs &fn_in, Outputs &fn_out) = 0;
-
- const Signature &signature() const;
-
- private:
- const Signature m_signature;
- };
-
-} /* namespace FN */
+#include "core/core.hpp"
+#include "core/cpu.hpp"
\ No newline at end of file
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
new file mode 100644
index 00000000000..1fe8c051747
--- /dev/null
+++ b/source/blender/functions/c_wrapper.cpp
@@ -0,0 +1,110 @@
+#include "FN_functions.h"
+#include "FN_functions.hpp"
+
+#include "./types/types.hpp"
+
+inline FN::CPUFunction *unwrap(FnCPUFunction fn)
+{
+ return (FN::CPUFunction *)fn;
+}
+
+inline FN::Tuple *unwrap(FnTuple tuple)
+{
+ return (FN::Tuple *)tuple;
+}
+
+inline FnTuple wrap(FN::Tuple *tuple)
+{
+ return (FnTuple)tuple;
+}
+
+void FN_function_call(FnCPUFunction fn, FnTuple fn_in, FnTuple fn_out)
+{
+ unwrap(fn)->call(*unwrap(fn_in), *unwrap(fn_out));
+}
+
+FnTuple FN_tuple_for_input(FnCPUFunction fn)
+{
+ auto tuple = new FN::Tuple(unwrap(fn)->signature().inputs());
+ return wrap(tuple);
+}
+
+FnTuple FN_tuple_for_output(FnCPUFunction fn)
+{
+ auto tuple = new FN::Tuple(unwrap(fn)->signature().outputs());
+ return wrap(tuple);
+}
+
+void FN_tuple_free(FnTuple tuple)
+{
+ delete unwrap(tuple);
+}
+
+void FN_tuple_set_float(FnTuple tuple, uint index, float value)
+{
+ unwrap(tuple)->set<float>(index, value);
+}
+
+struct Vector {
+ float x, y, z;
+};
+
+void FN_tuple_set_float_vector_3(FnTuple tuple, uint index, float value[3])
+{
+ unwrap(tuple)->set<Vector>(index, *(Vector *)value);
+}
+
+void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3])
+{
+ *(Vector *)dst = unwrap(tuple)->get<Vector>(index);
+}
+
+const char *FN_type_name(FnTypeRef type)
+{
+ return ((FN::Type *)type)->name().c_str();
+}
+
+FnTypeRef FN_type_get_float()
+{ return (FnTypeRef)FN::Types::float_ty; }
+
+FnTypeRef FN_type_get_int32()
+{ return (FnTypeRef)FN::Types::int32_ty; }
+
+FnTypeRef FN_type_get_float_vector_3d()
+{ return (FnTypeRef)FN::Types::floatvec3d_ty; }
+
+
+#include <cmath>
+#include <algorithm>
+
+class DeformFunction : public FN::CPUFunction {
+private:
+ DeformFunction(FN::Signature sig)
+ : CPUFunction(sig) {}
+
+public:
+ static DeformFunction *Create()
+ {
+ FN::Signature sig({FN::Types::floatvec3d_ty, FN::Types::float_ty}, {FN::Types::floatvec3d_ty});
+ return new DeformFunction(sig);
+ }
+
+ void call(const FN::Tuple &fn_in, FN::Tuple &fn_out) 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.z = vec.z;
+
+ fn_out.set<Vector>(0, result);
+ }
+};
+
+FnCPUFunction FN_get_deform_function()
+{
+ return (FnCPUFunction)DeformFunction::Create();
+}
\ No newline at end of file
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
new file mode 100644
index 00000000000..dfbf3eacb42
--- /dev/null
+++ b/source/blender/functions/core/core.hpp
@@ -0,0 +1,71 @@
+#pragma once
+
+#include <string>
+
+#include "BLI_small_vector.hpp"
+
+namespace FN {
+
+ using namespace BLI;
+
+ class Type;
+ class Signature;
+ class Function;
+
+ using SmallTypeVector = SmallVector<const Type *>;
+
+ class Type {
+ public:
+ const std::string &name() const
+ {
+ return this->m_name;
+ }
+
+ protected:
+ std::string m_name;
+
+ public:
+ /* will be removed */
+ uint m_size;
+ };
+
+ class Signature {
+ public:
+ Signature() = default;
+ ~Signature() = default;
+
+ Signature(const SmallTypeVector &inputs, const SmallTypeVector &outputs)
+ : m_inputs(inputs), m_outputs(outputs) {}
+
+ inline const SmallTypeVector &inputs() const
+ {
+ return this->m_inputs;
+ }
+
+ inline const SmallTypeVector &outputs() const
+ {
+ return this->m_outputs;
+ }
+
+ private:
+ const SmallTypeVector m_inputs;
+ const SmallTypeVector m_outputs;
+ };
+
+ class Function {
+ public:
+ Function(const Signature &signature)
+ : m_signature(signature) {}
+
+ virtual ~Function() {}
+
+ inline const Signature &signature() const
+ {
+ return this->m_signature;
+ }
+
+ private:
+ const Signature m_signature;
+ };
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/cpu.hpp b/source/blender/functions/core/cpu.hpp
new fil
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list