Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-07-21 18:20:05 +0300
committerJacques Lucke <jacques@blender.org>2020-07-21 18:35:09 +0300
commit8369adabc0ec7a1fce248b688bf20860ae0434bb (patch)
treeaf767efe8c6765c5876391a5eb96923fc8d6ac68 /source/blender/blenkernel/BKE_persistent_data_handle.hh
parent6c7e62ef9be564fbee279c7467c9b6adaf51b6c8 (diff)
Particles: initial object socket and emitter node support
Object sockets work now, but only the new Object Transforms and the Particle Mesh Emitter node use it. The emitter does not actually use the mesh surface yet. Instead, new particles are just emitted around the origin of the object. Internally, handles to object data blocks are passed around in the network, instead of raw object pointers. Using handles has a couple of benefits: * The caller of the function has control over which handles can be resolved and therefore limit access to specific data. The set of data blocks that is accessed by a node tree should be known statically. This is necessary for a proper integration with the dependency graph. * When the pointer to an object changes (e.g. after restarting Blender), all handles are still valid. * When an object is deleted, the handle is invalidated without causing crashes. * The handle is just an integer that can be stored per particle and can be cached easily. The mapping between handles and their corresponding data blocks is stored in the Simulation data block.
Diffstat (limited to 'source/blender/blenkernel/BKE_persistent_data_handle.hh')
-rw-r--r--source/blender/blenkernel/BKE_persistent_data_handle.hh132
1 files changed, 132 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_persistent_data_handle.hh b/source/blender/blenkernel/BKE_persistent_data_handle.hh
new file mode 100644
index 00000000000..721132560e3
--- /dev/null
+++ b/source/blender/blenkernel/BKE_persistent_data_handle.hh
@@ -0,0 +1,132 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __BKE_PERSISTENT_DATA_HANDLE_H__
+#define __BKE_PERSISTENT_DATA_HANDLE_H__
+
+/** \file
+ * \ingroup bke
+ *
+ * A PersistentDataHandle is a weak reference to some data in a Blender file. The handle itself is
+ * just a number. A PersistentDataHandleMap is used to convert between handles and the actual data.
+ */
+
+#include "BLI_map.hh"
+
+#include "DNA_ID.h"
+
+struct Object;
+
+namespace blender::bke {
+
+class PersistentDataHandleMap;
+
+class PersistentDataHandle {
+ private:
+ /* Negative values indicate that the handle is "empty". */
+ int32_t handle_;
+
+ friend PersistentDataHandleMap;
+
+ protected:
+ PersistentDataHandle(int handle) : handle_(handle)
+ {
+ }
+
+ public:
+ PersistentDataHandle() : handle_(-1)
+ {
+ }
+
+ friend bool operator==(const PersistentDataHandle &a, const PersistentDataHandle &b)
+ {
+ return a.handle_ == b.handle_;
+ }
+
+ friend bool operator!=(const PersistentDataHandle &a, const PersistentDataHandle &b)
+ {
+ return !(a == b);
+ }
+
+ friend std::ostream &operator<<(std::ostream &stream, const PersistentDataHandle &a)
+ {
+ stream << a.handle_;
+ return stream;
+ }
+
+ uint64_t hash() const
+ {
+ return (uint64_t)handle_;
+ }
+};
+
+class PersistentIDHandle : public PersistentDataHandle {
+ friend PersistentDataHandleMap;
+ using PersistentDataHandle::PersistentDataHandle;
+};
+
+class PersistentObjectHandle : public PersistentIDHandle {
+ friend PersistentDataHandleMap;
+ using PersistentIDHandle::PersistentIDHandle;
+};
+
+class PersistentDataHandleMap {
+ private:
+ Map<int32_t, const ID *> id_by_handle_;
+ Map<const ID *, int32_t> handle_by_id_;
+
+ public:
+ void add(int32_t handle, const ID &id)
+ {
+ BLI_assert(handle >= 0);
+ handle_by_id_.add(&id, handle);
+ id_by_handle_.add(handle, &id);
+ }
+
+ PersistentIDHandle lookup(const ID *id) const
+ {
+ const int handle = handle_by_id_.lookup_default(id, -1);
+ return PersistentIDHandle(handle);
+ }
+
+ PersistentObjectHandle lookup(const Object *object) const
+ {
+ const int handle = handle_by_id_.lookup_default((const ID *)object, -1);
+ return PersistentObjectHandle(handle);
+ }
+
+ const ID *lookup(const PersistentIDHandle &handle) const
+ {
+ const ID *id = id_by_handle_.lookup_default(handle.handle_, nullptr);
+ return id;
+ }
+
+ const Object *lookup(const PersistentObjectHandle &handle) const
+ {
+ const ID *id = this->lookup((const PersistentIDHandle &)handle);
+ if (id == nullptr) {
+ return nullptr;
+ }
+ if (GS(id->name) != ID_OB) {
+ return nullptr;
+ }
+ return (const Object *)id;
+ }
+};
+
+} // namespace blender::bke
+
+#endif /* __BKE_PERSISTENT_DATA_HANDLE_H__ */