[Bf-blender-cvs] [e985b20a775] functions: more generic way to add per element and global contexts
Jacques Lucke
noreply at git.blender.org
Wed Dec 11 12:19:01 CET 2019
Commit: e985b20a775558faabfe0a404effc6c84b0cf06e
Author: Jacques Lucke
Date: Wed Dec 11 11:36:40 2019 +0100
Branches: functions
https://developer.blender.org/rBe985b20a775558faabfe0a404effc6c84b0cf06e
more generic way to add per element and global contexts
===================================================================
A source/blender/blenkernel/BKE_id_data_cache.h
M source/blender/blenkernel/CMakeLists.txt
A source/blender/blenkernel/intern/id_data_cache.cc
M source/blender/blenkernel/intern/id_handle.cc
M source/blender/functions/FN_multi_function_common_contexts.h
M source/blender/functions/FN_multi_function_context.h
M source/blender/functions/intern/multi_function_common_contexts.cc
M source/blender/functions/intern/multi_functions/mixed.cc
M source/blender/modifiers/intern/MOD_functiondeform_cxx.cc
M source/blender/modifiers/intern/MOD_functionpoints_cxx.cc
M source/blender/simulations/bparticles/emitters.cpp
M source/blender/simulations/bparticles/emitters.hpp
M source/blender/simulations/bparticles/node_frontend.cpp
M source/blender/simulations/bparticles/particle_function.cpp
M source/blender/simulations/bparticles/particle_function.hpp
===================================================================
diff --git a/source/blender/blenkernel/BKE_id_data_cache.h b/source/blender/blenkernel/BKE_id_data_cache.h
new file mode 100644
index 00000000000..fc952c96b38
--- /dev/null
+++ b/source/blender/blenkernel/BKE_id_data_cache.h
@@ -0,0 +1,34 @@
+#ifndef __BKE_ID_DATA_CACHE_H__
+#define __BKE_ID_DATA_CACHE_H__
+
+#include <mutex>
+
+#include "BLI_map.h"
+#include "BLI_kdopbvh.h"
+#include "BLI_kdtree.h"
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_object_types.h"
+
+#include "BKE_bvhutils.h"
+
+namespace BKE {
+
+using BLI::Map;
+
+class IDDataCache {
+ private:
+ mutable Map<Object *, BVHTreeFromMesh *> m_bvh_trees;
+ mutable std::mutex m_bvt_trees_mutex;
+
+ public:
+ IDDataCache() = default;
+ ~IDDataCache();
+
+ BVHTreeFromMesh *get_bvh_tree(Object *object) const;
+};
+
+} // namespace BKE
+
+#endif /* __BKE_ID_DATA_CACHE_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 806b0191766..3c912a4da70 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -125,6 +125,7 @@ set(SRC
intern/idcode.c
intern/idprop.c
intern/idprop_utils.c
+ intern/id_data_cache.cc
intern/id_handle.cc
intern/image.c
intern/image_gen.c
@@ -294,6 +295,7 @@ set(SRC
BKE_icons.h
BKE_idcode.h
BKE_idprop.h
+ BKE_id_data_cache.h
BKE_id_handle.h
BKE_image.h
BKE_image_save.h
diff --git a/source/blender/blenkernel/intern/id_data_cache.cc b/source/blender/blenkernel/intern/id_data_cache.cc
new file mode 100644
index 00000000000..187808fbeef
--- /dev/null
+++ b/source/blender/blenkernel/intern/id_data_cache.cc
@@ -0,0 +1,31 @@
+#include "BKE_id_data_cache.h"
+
+namespace BKE {
+
+IDDataCache::~IDDataCache()
+{
+ for (auto bvhtree : m_bvh_trees.values()) {
+ if (bvhtree != nullptr) {
+ free_bvhtree_from_mesh(bvhtree);
+ delete bvhtree;
+ }
+ }
+}
+
+BVHTreeFromMesh *IDDataCache::get_bvh_tree(Object *object) const
+{
+ BLI_assert(object != nullptr);
+
+ std::lock_guard<std::mutex> lock(m_bvt_trees_mutex);
+
+ return m_bvh_trees.lookup_or_add(object, [&]() -> BVHTreeFromMesh * {
+ if (object->type != OB_MESH) {
+ return nullptr;
+ }
+ BVHTreeFromMesh *bvhtree_data = new BVHTreeFromMesh();
+ BKE_bvhtree_from_mesh_get(bvhtree_data, (Mesh *)object->data, BVHTREE_FROM_LOOPTRI, 2);
+ return bvhtree_data;
+ });
+}
+
+} // namespace BKE
diff --git a/source/blender/blenkernel/intern/id_handle.cc b/source/blender/blenkernel/intern/id_handle.cc
index 81298dbf86c..ddef61cb0b1 100644
--- a/source/blender/blenkernel/intern/id_handle.cc
+++ b/source/blender/blenkernel/intern/id_handle.cc
@@ -30,4 +30,4 @@ const IDHandleLookup &IDHandleLookup::Empty()
return empty_id_handle_lookup;
}
-} // namespace BKE
\ No newline at end of file
+} // namespace BKE
diff --git a/source/blender/functions/FN_multi_function_common_contexts.h b/source/blender/functions/FN_multi_function_common_contexts.h
index eb2049c66b9..c61e2c05177 100644
--- a/source/blender/functions/FN_multi_function_common_contexts.h
+++ b/source/blender/functions/FN_multi_function_common_contexts.h
@@ -8,14 +8,6 @@
#include "BLI_math_cxx.h"
#include "BLI_map.h"
-#include "BLI_kdopbvh.h"
-#include "BLI_kdtree.h"
-
-#include "BKE_bvhutils.h"
-
-#include "DNA_mesh_types.h"
-#include "DNA_meshdata_types.h"
-#include "DNA_object_types.h"
namespace FN {
@@ -40,41 +32,6 @@ class ParticleAttributesContext {
}
};
-class ExternalDataCacheContext {
- private:
- mutable Map<Object *, BVHTreeFromMesh *> m_bvh_trees;
- mutable std::mutex m_bvt_trees_mutex;
-
- public:
- ExternalDataCacheContext() = default;
-
- ~ExternalDataCacheContext()
- {
- for (auto bvhtree : m_bvh_trees.values()) {
- if (bvhtree != nullptr) {
- free_bvhtree_from_mesh(bvhtree);
- delete bvhtree;
- }
- }
- }
-
- BVHTreeFromMesh *get_bvh_tree(Object *object) const
- {
- BLI_assert(object != nullptr);
-
- std::lock_guard<std::mutex> lock(m_bvt_trees_mutex);
-
- return m_bvh_trees.lookup_or_add(object, [&]() -> BVHTreeFromMesh * {
- if (object->type != OB_MESH) {
- return nullptr;
- }
- BVHTreeFromMesh *bvhtree_data = new BVHTreeFromMesh();
- BKE_bvhtree_from_mesh_get(bvhtree_data, (Mesh *)object->data, BVHTREE_FROM_LOOPTRI, 2);
- return bvhtree_data;
- });
- }
-};
-
} // namespace FN
#endif /* __FN_MULTI_FUNCTION_COMMON_CONTEXTS_H__ */
diff --git a/source/blender/functions/FN_multi_function_context.h b/source/blender/functions/FN_multi_function_context.h
index d2b1ce358d8..8d1816cc1e8 100644
--- a/source/blender/functions/FN_multi_function_context.h
+++ b/source/blender/functions/FN_multi_function_context.h
@@ -22,7 +22,7 @@ using BLI::VirtualListRef;
class MFElementContexts {
private:
- Vector<uintptr_t> m_ids;
+ Vector<BLI::class_id_t> m_ids;
Vector<const void *> m_contexts;
Vector<VirtualListRef<uint>> m_indices;
@@ -38,7 +38,7 @@ class MFElementContexts {
template<typename T> Optional<TypedContext<T>> try_find() const
{
- uintptr_t context_id = BLI::get_class_id<T>();
+ BLI::class_id_t context_id = BLI::get_class_id<T>();
for (uint i : m_contexts.index_iterator()) {
if (m_ids[i] == context_id) {
const T *context = (const T *)m_contexts[i];
@@ -49,17 +49,38 @@ class MFElementContexts {
}
};
+class MFGlobalContexts {
+ private:
+ Vector<BLI::class_id_t> m_ids;
+ Vector<const void *> m_contexts;
+
+ friend class MFContextBuilder;
+
+ public:
+ MFGlobalContexts() = default;
+
+ template<typename T> const T *try_find() const
+ {
+ BLI::class_id_t context_id = BLI::get_class_id<T>();
+ for (uint i : m_contexts.index_iterator()) {
+ if (m_ids[i] == context_id) {
+ const T *context = (const T *)m_contexts[i];
+ return context;
+ }
+ }
+ return nullptr;
+ }
+};
+
class MFContextBuilder : BLI::NonCopyable, BLI::NonMovable {
private:
MFElementContexts m_element_contexts;
- const IDHandleLookup &m_id_handle_lookup;
+ MFGlobalContexts m_global_contexts;
friend class MFContext;
public:
- MFContextBuilder(const IDHandleLookup *id_handle_lookup = nullptr)
- : m_id_handle_lookup((id_handle_lookup == nullptr) ? IDHandleLookup::Empty() :
- *id_handle_lookup)
+ MFContextBuilder()
{
}
@@ -76,10 +97,10 @@ class MFContextBuilder : BLI::NonCopyable, BLI::NonMovable {
VirtualListRef<uint>::FromFullArray(indices.as_array_ref()));
}
- template<typename T> void add_element_context(const T &context)
+ template<typename T> void add_global_context(const T &context)
{
- static uint dummy_index = 0;
- this->add_element_context(context, VirtualListRef<uint>::FromSingle_MaxSize(&dummy_index));
+ m_global_contexts.m_ids.append(BLI::get_class_id<T>());
+ m_global_contexts.m_contexts.append((const void *)&context);
}
};
@@ -92,14 +113,14 @@ class MFContext {
{
}
- const IDHandleLookup &id_handle_lookup() const
+ template<typename T> Optional<MFElementContexts::TypedContext<T>> try_find_per_element() const
{
- return m_builder->m_id_handle_lookup;
+ return m_builder->m_element_contexts.try_find<T>();
}
- const MFElementContexts &element_contexts() const
+ template<typename T> const T *try_find_global() const
{
- return m_builder->m_element_contexts;
+ return m_builder->m_global_contexts.try_find<T>();
}
};
diff --git a/source/blender/functions/intern/multi_function_common_contexts.cc b/source/blender/functions/intern/multi_function_common_contexts.cc
index 265ac7d2492..5cc18e7301b 100644
--- a/source/blender/functions/intern/multi_function_common_contexts.cc
+++ b/source/blender/functions/intern/multi_function_common_contexts.cc
@@ -1,9 +1,13 @@
#include "FN_multi_function_common_contexts.h"
+#include "BKE_id_data_cache.h"
+#include "BKE_id_handle.h"
+
BLI_CREATE_CLASS_ID(FN::VertexPositionArray)
BLI_CREATE_CLASS_ID(FN::SceneTimeContext)
BLI_CREATE_CLASS_ID(FN::ParticleAttributesContext)
-BLI_CREATE_CLASS_ID(FN::ExternalDataCacheContext)
+BLI_CREATE_CLASS_ID(BKE::IDHandleLookup)
+BLI_CREATE_CLASS_ID(BKE::IDDataCache)
namespace FN {
diff --git a/source/blender/functions/intern/multi_functions/mixed.cc b/source/blender/functions/intern/multi_functions/mixed.cc
index 52777647ea3..6112e0f664e 100644
--- a/source/blender/functions/intern/multi_functions/mixed.cc
+++ b/source/blender/functions/intern/multi_functions/mixed.cc
@@ -22,6 +22,7 @@
#include "BKE_surface_hook.h"
#include "BKE_mesh_runtime.h"
#include "BKE_deform.h"
+#include "BKE_id_data_cache.h"
namespace FN {
@@ -259,8 +260,13 @@ void MF_ObjectVertexPositions::call(MFMask mask, MFParams params, MFContext cont
"Object");
auto positions = params.vector_output<float3>(1, "Positions");
+ auto *id_handle_lookup = context.try_find_global<BKE::IDHandleLookup>();
+ if (id_handle_lookup == nullptr) {
+ return;
+ }
+
for (uint i : mask.indices()) {
- Object *object = context.id_handle_lookup().lookup(objects[i]);
+ Object *object = id_handle_lookup->lookup(objects[i]);
if (object == nullptr || object->type != OB_MESH) {
continue;
}
@@ -290,16 +296,24 @@ void MF_GetPositionOnSurface::call(MFMask mask, MFParams params, MFContext conte
0, "Surface Hook");
MutableArrayRef<float3> r_positions = params.uninitialized_single_output<float3>(1, "Position");
+ float3 fallback = {0, 0, 0};
+
+ auto *id_handle_lookup = context.try_find_global<BKE::IDHandleLookup>();
+ if (id_handle_lookup == nullptr) {
+ r_positions.fill_indices(mask.indices(), fallback);
+ return;
+ }
+
for (uint i : mask.indices()) {
SurfaceHook location = locations[i];
if (location.type() != BKE::SurfaceHookType::MeshObject) {
- r_positions[i] = {0, 0, 0};
+ r_positions[i] = fallback;
continue;
}
- Object *object = context.id_handle_lookup().lookup(location.object_handle());
+ Object *object = id_handl
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list