From 324537287f62cd6876faf32e83f7e9795f8110c6 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 1 Nov 2019 19:05:09 +0100 Subject: initial functions2 folder --- source/blender/CMakeLists.txt | 1 + source/blender/functions/FN_all-c.h | 4 +- source/blender/functions/initialize.cpp | 4 +- source/blender/functions2/CMakeLists.txt | 39 +++++ source/blender/functions2/FN_cpp_type.h | 160 +++++++++++++++++++++ source/blender/functions2/FN_initialize.h | 10 ++ source/blender/functions2/intern/cpp_type.cc | 9 ++ source/blender/functions2/intern/cpp_types.cc | 120 ++++++++++++++++ source/blender/functions2/intern/cpp_types.h | 11 ++ source/blender/functions2/intern/initialize.cc | 12 ++ source/blender/modifiers/CMakeLists.txt | 1 + source/blender/windowmanager/CMakeLists.txt | 1 + source/blender/windowmanager/intern/wm_init_exit.c | 2 + source/creator/CMakeLists.txt | 2 + source/creator/creator.c | 2 + 15 files changed, 374 insertions(+), 4 deletions(-) create mode 100644 source/blender/functions2/CMakeLists.txt create mode 100644 source/blender/functions2/FN_cpp_type.h create mode 100644 source/blender/functions2/FN_initialize.h create mode 100644 source/blender/functions2/intern/cpp_type.cc create mode 100644 source/blender/functions2/intern/cpp_types.cc create mode 100644 source/blender/functions2/intern/cpp_types.h create mode 100644 source/blender/functions2/intern/initialize.cc diff --git a/source/blender/CMakeLists.txt b/source/blender/CMakeLists.txt index 98a93af93c7..4cbc31d3f1b 100644 --- a/source/blender/CMakeLists.txt +++ b/source/blender/CMakeLists.txt @@ -112,6 +112,7 @@ add_subdirectory(modifiers) add_subdirectory(gpencil_modifiers) add_subdirectory(shader_fx) add_subdirectory(functions) +add_subdirectory(functions2) add_subdirectory(simulations) add_subdirectory(makesdna) add_subdirectory(makesrna) diff --git a/source/blender/functions/FN_all-c.h b/source/blender/functions/FN_all-c.h index b970d8be2d5..71f8e2ed387 100644 --- a/source/blender/functions/FN_all-c.h +++ b/source/blender/functions/FN_all-c.h @@ -11,8 +11,8 @@ extern "C" { #endif -void FN_initialize(void); -void FN_exit(void); +void FN_old_initialize(void); +void FN_old_exit(void); #ifdef __cplusplus } diff --git a/source/blender/functions/initialize.cpp b/source/blender/functions/initialize.cpp index a7a17469c89..b2f7171c666 100644 --- a/source/blender/functions/initialize.cpp +++ b/source/blender/functions/initialize.cpp @@ -1,12 +1,12 @@ #include "FN_all.hpp" -void FN_initialize() +void FN_old_initialize() { FN::initialize_llvm(); FN::Types::initialize_types(); } -void FN_exit() +void FN_old_exit() { FN::Types::uninitialize_types(); } diff --git a/source/blender/functions2/CMakeLists.txt b/source/blender/functions2/CMakeLists.txt new file mode 100644 index 00000000000..2f07f0a9930 --- /dev/null +++ b/source/blender/functions2/CMakeLists.txt @@ -0,0 +1,39 @@ +set(INC + . + ../blenlib + ../makesdna + ../makesrna + ../blenkernel + ../depsgraph + ../windowmanager + ../../../intern/guardedalloc +) + +set(INC_SYS + ${LLVM_INCLUDE_DIRS} + ${PYTHON_INCLUDE_DIRS} +) + +if(WITH_PYTHON) + add_definitions(-DWITH_PYTHON) + list(APPEND INC + ../python + ) +endif() + +set(SRC + intern/cpp_type.cc + intern/cpp_types.cc + intern/initialize.cc + + FN_cpp_type.h + FN_initialize.h + + intern/cpp_types.h +) + +set(LIB + bf_blenlib +) + +blender_add_lib(bf_functions2 "${SRC}" "${INC}" "${INC_SYS}" "${LIB}") diff --git a/source/blender/functions2/FN_cpp_type.h b/source/blender/functions2/FN_cpp_type.h new file mode 100644 index 00000000000..8ab1e9f5ce4 --- /dev/null +++ b/source/blender/functions2/FN_cpp_type.h @@ -0,0 +1,160 @@ +#ifndef __FN_CPP_TYPE_H__ +#define __FN_CPP_TYPE_H__ + +#include "BLI_string_ref.h" +#include "BLI_utility_mixins.h" +#include "BLI_vector.h" + +namespace FN { + +using BLI::StringRef; +using BLI::StringRefNull; + +class CPPType { + public: + using ConstructDefaultF = void (*)(const CPPType *self, void *ptr); + using DestructF = void (*)(void *ptr); + using CopyToInitializedF = void (*)(const void *src, void *dst); + using CopyToUninitializedF = void (*)(const void *src, void *dst); + using RelocateToInitializedF = void (*)(void *src, void *dst); + using RelocateToUninitializedF = void (*)(void *src, void *dst); + + CPPType(std::string name, + uint size, + uint alignment, + bool trivially_destructible, + ConstructDefaultF construct_default, + DestructF destruct, + CopyToInitializedF copy_to_initialized, + CopyToUninitializedF copy_to_uninitialized, + RelocateToInitializedF relocate_to_initialized, + RelocateToUninitializedF relocate_to_uninitialized, + const CPPType *generalization) + : m_size(size), + m_alignment(alignment), + m_trivially_destructible(trivially_destructible), + m_construct_default(construct_default), + m_destruct(destruct), + m_copy_to_initialized(copy_to_initialized), + m_copy_to_uninitialized(copy_to_uninitialized), + m_relocate_to_initialized(relocate_to_initialized), + m_relocate_to_uninitialized(relocate_to_uninitialized), + m_generalization(generalization), + m_name(name) + { + BLI_assert(is_power_of_2_i(m_alignment)); + BLI_assert(generalization == nullptr || + (generalization->size() == size && generalization->alignment() <= alignment)); + + m_alignment_mask = m_alignment - 1; + } + + virtual ~CPPType(); + + StringRefNull name() const + { + return m_name; + } + + uint size() const + { + return m_size; + } + + uint alignment() const + { + return m_alignment; + } + + const CPPType *generalization() const + { + return m_generalization; + } + + bool trivially_destructible() const + { + return m_trivially_destructible; + } + + bool pointer_has_valid_alignment(const void *ptr) const + { + return (POINTER_AS_UINT(ptr) & m_alignment_mask) == 0; + } + + void construct_default(void *ptr) const + { + BLI_assert(this->pointer_has_valid_alignment(ptr)); + + m_construct_default(this, ptr); + } + + void destruct(void *ptr) const + { + BLI_assert(this->pointer_has_valid_alignment(ptr)); + + m_destruct(ptr); + } + + void copy_to_initialized(const void *src, void *dst) const + { + BLI_assert(this->pointer_has_valid_alignment(src)); + BLI_assert(this->pointer_has_valid_alignment(dst)); + + m_copy_to_initialized(src, dst); + } + + void copy_to_uninitialized(const void *src, void *dst) const + { + BLI_assert(this->pointer_has_valid_alignment(src)); + BLI_assert(this->pointer_has_valid_alignment(dst)); + + m_copy_to_uninitialized(src, dst); + } + + void relocate_to_initialized(void *src, void *dst) const + { + BLI_assert(this->pointer_has_valid_alignment(src)); + BLI_assert(this->pointer_has_valid_alignment(dst)); + + m_relocate_to_initialized(src, dst); + } + + void relocate_to_uninitialized(void *src, void *dst) const + { + BLI_assert(this->pointer_has_valid_alignment(src)); + BLI_assert(this->pointer_has_valid_alignment(dst)); + + m_relocate_to_uninitialized(src, dst); + } + + bool is_same_or_generalization(const CPPType &other) const + { + if (&other == this) { + return true; + } + if (m_generalization == nullptr) { + return false; + } + return m_generalization->is_same_or_generalization(other); + } + + private: + uint m_size; + uint m_alignment; + uint m_alignment_mask; + bool m_trivially_destructible; + ConstructDefaultF m_construct_default; + DestructF m_destruct; + CopyToInitializedF m_copy_to_initialized; + CopyToUninitializedF m_copy_to_uninitialized; + RelocateToInitializedF m_relocate_to_initialized; + RelocateToUninitializedF m_relocate_to_uninitialized; + const CPPType *m_generalization; + std::string m_name; +}; + +template const CPPType &GET_TYPE(); + +} // namespace FN + +#endif /* __FN_CPP_TYPE_H__ */ diff --git a/source/blender/functions2/FN_initialize.h b/source/blender/functions2/FN_initialize.h new file mode 100644 index 00000000000..10e8a68f02d --- /dev/null +++ b/source/blender/functions2/FN_initialize.h @@ -0,0 +1,10 @@ +#ifdef __cplusplus +extern "C" { +#endif + +void FN_initialize(void); +void FN_exit(void); + +#ifdef __cplusplus +} +#endif diff --git a/source/blender/functions2/intern/cpp_type.cc b/source/blender/functions2/intern/cpp_type.cc new file mode 100644 index 00000000000..2e04b405a22 --- /dev/null +++ b/source/blender/functions2/intern/cpp_type.cc @@ -0,0 +1,9 @@ +#include "FN_cpp_type.h" + +namespace FN { + +CPPType::~CPPType() +{ +} + +} // namespace FN diff --git a/source/blender/functions2/intern/cpp_types.cc b/source/blender/functions2/intern/cpp_types.cc new file mode 100644 index 00000000000..54d78882991 --- /dev/null +++ b/source/blender/functions2/intern/cpp_types.cc @@ -0,0 +1,120 @@ +#include "FN_cpp_type.h" +#include "cpp_types.h" + +#include "DNA_object_types.h" + +#include "BLI_math_cxx.h" +#include "BLI_vector.h" + +namespace FN { + +using BLI::Vector; + +static Vector allocated_types; + +void free_cpp_types() +{ + for (CPPType *type : allocated_types) { + delete type; + } +} + +template void ConstructDefault_CB(const CPPType *UNUSED(self), void *ptr) +{ + BLI::construct_default((T *)ptr); +} + +template struct DefaultConstructor; +template struct DefaultConstructor { + static CPPType::ConstructDefaultF get_callback() + { + return ConstructDefault_CB; + } +}; +template struct DefaultConstructor { + static CPPType::ConstructDefaultF get_callback() + { + return nullptr; + } +}; + +template void Destruct_CB(void *ptr) +{ + BLI::destruct((T *)ptr); +} +template void CopyToInitialized_CB(const void *src, void *dst) +{ + *(T *)dst = *(T *)src; +} +template void CopyToUninitialized_CB(const void *src, void *dst) +{ + BLI::uninitialized_copy_n((T *)src, 1, (T *)dst); +} +template void RelocateToInitialized_CB(void *src, void *dst) +{ + BLI::relocate((T *)src, (T *)dst); +} +template void RelocateToUninitialized_CB(void *src, void *dst) +{ + BLI::uninitialized_relocate((T *)src, (T *)dst); +} + +#define CPP_TYPE_DECLARE(IDENTIFIER) static CPPType *TYPE_##IDENTIFIER = nullptr + +CPP_TYPE_DECLARE(float); +CPP_TYPE_DECLARE(bool); +CPP_TYPE_DECLARE(ObjectPtr); +CPP_TYPE_DECLARE(int32); +CPP_TYPE_DECLARE(rgba_f); +CPP_TYPE_DECLARE(float3); +CPP_TYPE_DECLARE(string); + +#undef CPP_TYPE_DECLARE + +void init_cpp_types() +{ + +#define CPP_TYPE_CONSTRUCTION(IDENTIFIER, TYPE_NAME) \ + TYPE_##IDENTIFIER = new CPPType( \ + STRINGIFY(IDENTIFIER), \ + sizeof(TYPE_NAME), \ + alignof(TYPE_NAME), \ + std::is_trivially_destructible::value, \ + DefaultConstructor::value>::get_callback(), \ + Destruct_CB, \ + CopyToInitialized_CB, \ + CopyToUninitialized_CB, \ + RelocateToInitialized_CB, \ + RelocateToUninitialized_CB, \ + nullptr); \ + allocated_types.append(TYPE_##IDENTIFIER) + + CPP_TYPE_CONSTRUCTION(float, float); + CPP_TYPE_CONSTRUCTION(bool, bool); + CPP_TYPE_CONSTRUCTION(ObjectPtr, Object *); + CPP_TYPE_CONSTRUCTION(int32, int32_t); + CPP_TYPE_CONSTRUCTION(rgba_f, BLI::rgba_f); + CPP_TYPE_CONSTRUCTION(float3, BLI::float3); + CPP_TYPE_CONSTRUCTION(string, std::string); + +#undef CPP_TYPE_CONSTRUCTION +} + +#define CPP_TYPE_GETTER(IDENTIFIER, TYPE_NAME) \ + template<> const CPPType &GET_TYPE() \ + { \ + return *TYPE_##IDENTIFIER; \ + } + +CPP_TYPE_GETTER(float, float) +CPP_TYPE_GETTER(bool, bool) +CPP_TYPE_GETTER(ObjectPtr, Object *) +CPP_TYPE_GETTER(int32, int32_t) +CPP_TYPE_GETTER(rgba_f, BLI::rgba_f) +CPP_TYPE_GETTER(float3, BLI::float3) +CPP_TYPE_GETTER(string, std::string) + +#undef CPP_TYPE_GETTER + +} // namespace FN diff --git a/source/blender/functions2/intern/cpp_types.h b/source/blender/functions2/intern/cpp_types.h new file mode 100644 index 00000000000..34299f20beb --- /dev/null +++ b/source/blender/functions2/intern/cpp_types.h @@ -0,0 +1,11 @@ +#ifndef __FN_CPP_TYPES_H__ +#define __FN_CPP_TYPES_H__ + +namespace FN { + +void init_cpp_types(); +void free_cpp_types(); + +} // namespace FN + +#endif /* __FN_CPP_TYPES_H__ */ diff --git a/source/blender/functions2/intern/initialize.cc b/source/blender/functions2/intern/initialize.cc new file mode 100644 index 00000000000..61f3214c4b2 --- /dev/null +++ b/source/blender/functions2/intern/initialize.cc @@ -0,0 +1,12 @@ +#include "FN_initialize.h" +#include "cpp_types.h" + +void FN_initialize(void) +{ + FN::init_cpp_types(); +} + +void FN_exit(void) +{ + FN::free_cpp_types(); +} diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt index f062d99f4fe..3cfbac5ad9f 100644 --- a/source/blender/modifiers/CMakeLists.txt +++ b/source/blender/modifiers/CMakeLists.txt @@ -119,6 +119,7 @@ set(LIB bf_blenkernel bf_blenlib bf_functions + bf_functions2 bf_simulations ) diff --git a/source/blender/windowmanager/CMakeLists.txt b/source/blender/windowmanager/CMakeLists.txt index 8e3f0cba7fe..73b0b09dd5c 100644 --- a/source/blender/windowmanager/CMakeLists.txt +++ b/source/blender/windowmanager/CMakeLists.txt @@ -37,6 +37,7 @@ set(INC ../makesrna ../nodes ../functions + ../functions2 ../render/extern/include ../../../intern/clog ../../../intern/ghost diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c index 6b83a053e71..7627196fb48 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.c +++ b/source/blender/windowmanager/intern/wm_init_exit.c @@ -133,6 +133,7 @@ #include "DRW_engine.h" #include "FN_all-c.h" +#include "FN_initialize.h" #ifdef WITH_OPENSUBDIV # include "BKE_subsurf.h" @@ -661,6 +662,7 @@ void WM_exit_ex(bContext *C, const bool do_python) BKE_blender_atexit(); FN_exit(); + FN_old_exit(); if (MEM_get_memory_blocks_in_use() != 0) { size_t mem_in_use = MEM_get_memory_in_use() + MEM_get_memory_in_use(); diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt index f78a32a6599..0b6f8766bc4 100644 --- a/source/creator/CMakeLists.txt +++ b/source/creator/CMakeLists.txt @@ -30,6 +30,7 @@ blender_include_dirs( ../blender/depsgraph ../blender/editors/include ../blender/functions + ../blender/functions2 ../blender/imbuf ../blender/makesrna ../blender/render/extern/include @@ -46,6 +47,7 @@ set(LIB bf_dna bf_editor_datafiles bf_functions + bf_functions2 bf_imbuf bf_intern_clog bf_intern_guardedalloc diff --git a/source/creator/creator.c b/source/creator/creator.c index 887f7176de7..d21adaff244 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -74,6 +74,7 @@ #include "RNA_define.h" #include "FN_all-c.h" +#include "FN_initialize.h" #ifdef WITH_FREESTYLE # include "FRS_freestyle.h" @@ -356,6 +357,7 @@ int main(int argc, BKE_shaderfx_init(); DEG_register_node_types(); FN_initialize(); + FN_old_initialize(); BKE_brush_system_init(); RE_texture_rng_init(); -- cgit v1.2.3