SceneNode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Components/SceneComponent.h>
  7. #include <AnKi/Util/Hierarchy.h>
  8. #include <AnKi/Util/BitMask.h>
  9. #include <AnKi/Util/BitSet.h>
  10. #include <AnKi/Util/List.h>
  11. #include <AnKi/Util/Enum.h>
  12. namespace anki
  13. {
  14. // Forward
  15. class ResourceManager;
  16. /// @addtogroup scene
  17. /// @{
  18. /// Interface class backbone of scene
  19. class SceneNode : public Hierarchy<SceneNode>, public IntrusiveListEnabled<SceneNode>
  20. {
  21. public:
  22. using Base = Hierarchy<SceneNode>;
  23. /// The one and only constructor.
  24. /// @param scene The owner scene.
  25. /// @param name The unique name of the node. If it's empty the the node is not searchable.
  26. SceneNode(SceneGraph* scene, CString name);
  27. /// Unregister node
  28. virtual ~SceneNode();
  29. /// A dummy init for those scene nodes that don't need it.
  30. ANKI_USE_RESULT Error init()
  31. {
  32. return Error::NONE;
  33. }
  34. SceneGraph& getSceneGraph()
  35. {
  36. return *m_scene;
  37. }
  38. const SceneGraph& getSceneGraph() const
  39. {
  40. return *m_scene;
  41. }
  42. /// Return the name. It may be empty for nodes that we don't want to track
  43. CString getName() const
  44. {
  45. return (!m_name.isEmpty()) ? m_name.toCString() : CString();
  46. }
  47. U64 getUuid() const
  48. {
  49. return m_uuid;
  50. }
  51. Bool getMarkedForDeletion() const
  52. {
  53. return m_markedForDeletion;
  54. }
  55. void setMarkedForDeletion();
  56. Timestamp getGlobalTimestamp() const;
  57. Timestamp getComponentMaxTimestamp() const
  58. {
  59. return m_maxComponentTimestamp;
  60. }
  61. void setComponentMaxTimestamp(Timestamp maxComponentTimestamp)
  62. {
  63. ANKI_ASSERT(maxComponentTimestamp > 0);
  64. m_maxComponentTimestamp = maxComponentTimestamp;
  65. }
  66. SceneAllocator<U8> getAllocator() const;
  67. SceneFrameAllocator<U8> getFrameAllocator() const;
  68. void addChild(SceneNode* obj)
  69. {
  70. Base::addChild(getAllocator(), obj);
  71. }
  72. /// This is called by the scenegraph every frame after all component updates. By default it does nothing.
  73. /// @param prevUpdateTime Timestamp of the previous update
  74. /// @param crntTime Timestamp of this update
  75. virtual ANKI_USE_RESULT Error frameUpdate(Second prevUpdateTime, Second crntTime)
  76. {
  77. (void)prevUpdateTime;
  78. (void)crntTime;
  79. return Error::NONE;
  80. }
  81. /// Iterate all components.
  82. template<typename TFunct>
  83. ANKI_USE_RESULT Error iterateComponents(TFunct func) const
  84. {
  85. Error err = Error::NONE;
  86. auto it = m_components.getBegin();
  87. auto end = m_components.getEnd();
  88. for(; !err && it != end; ++it)
  89. {
  90. const SceneComponent* c = *it;
  91. err = func(*c, it->isFeedbackComponent());
  92. }
  93. return err;
  94. }
  95. /// Iterate all components.
  96. template<typename TFunct>
  97. ANKI_USE_RESULT Error iterateComponents(TFunct func)
  98. {
  99. Error err = Error::NONE;
  100. auto it = m_components.getBegin();
  101. auto end = m_components.getEnd();
  102. for(; !err && it != end; ++it)
  103. {
  104. SceneComponent* c = *it;
  105. err = func(*c, it->isFeedbackComponent());
  106. }
  107. return err;
  108. }
  109. /// Iterate all components of a specific type
  110. template<typename TComponent, typename TFunct>
  111. void iterateComponentsOfType(TFunct func) const
  112. {
  113. auto it = m_components.getBegin();
  114. auto end = m_components.getEnd();
  115. for(; it != end; ++it)
  116. {
  117. if(it->getComponentClassId() == TComponent::getStaticClassId())
  118. {
  119. const SceneComponent* comp = *it;
  120. func(static_cast<const TComponent&>(*comp));
  121. }
  122. }
  123. }
  124. /// Iterate all components of a specific type
  125. template<typename TComponent, typename TFunct>
  126. void iterateComponentsOfType(TFunct func)
  127. {
  128. auto it = m_components.getBegin();
  129. auto end = m_components.getEnd();
  130. for(; it != end; ++it)
  131. {
  132. if(it->getComponentClassId() == TComponent::getStaticClassId())
  133. {
  134. SceneComponent* comp = *it;
  135. func(static_cast<TComponent&>(*comp));
  136. }
  137. }
  138. }
  139. /// Try geting a pointer to the first component of the requested type
  140. template<typename TComponent>
  141. const TComponent* tryGetFirstComponentOfType() const
  142. {
  143. for(const ComponentsArrayElement& el : m_components)
  144. {
  145. if(el.getComponentClassId() == TComponent::getStaticClassId())
  146. {
  147. const SceneComponent* comp = el;
  148. return static_cast<const TComponent*>(comp);
  149. }
  150. }
  151. return nullptr;
  152. }
  153. /// Try geting a pointer to the first component of the requested type
  154. template<typename TComponent>
  155. TComponent* tryGetFirstComponentOfType()
  156. {
  157. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetFirstComponentOfType<TComponent>();
  158. return const_cast<TComponent*>(c);
  159. }
  160. /// Get a pointer to the first component of the requested type
  161. template<typename TComponent>
  162. const TComponent& getFirstComponentOfType() const
  163. {
  164. const TComponent* out = tryGetFirstComponentOfType<TComponent>();
  165. ANKI_ASSERT(out != nullptr);
  166. return *out;
  167. }
  168. /// Get a pointer to the first component of the requested type
  169. template<typename TComponent>
  170. TComponent& getFirstComponentOfType()
  171. {
  172. const TComponent& c = static_cast<const SceneNode*>(this)->getFirstComponentOfType<TComponent>();
  173. return const_cast<TComponent&>(c);
  174. }
  175. /// Try geting a pointer to the nth component of the requested type.
  176. template<typename TComponent>
  177. const TComponent* tryGetNthComponentOfType(U32 nth) const
  178. {
  179. I32 inth = I32(nth);
  180. for(const ComponentsArrayElement& el : m_components)
  181. {
  182. if(el.getComponentClassId() == TComponent::getStaticClassId() && inth-- == 0)
  183. {
  184. const SceneComponent* comp = el;
  185. return static_cast<const TComponent*>(comp);
  186. }
  187. }
  188. return nullptr;
  189. }
  190. /// Try geting a pointer to the nth component of the requested type.
  191. template<typename TComponent>
  192. TComponent* tryGetNthComponentOfType(U32 nth)
  193. {
  194. const TComponent* c = static_cast<const SceneNode*>(this)->tryGetNthComponentOfType<TComponent>(nth);
  195. return const_cast<TComponent*>(c);
  196. }
  197. template<typename TComponent>
  198. const TComponent& getNthComponentOfType(U32 nth) const
  199. {
  200. const TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  201. ANKI_ASSERT(out);
  202. return *out;
  203. }
  204. template<typename TComponent>
  205. TComponent& getNthComponentOfType(U32 nth)
  206. {
  207. TComponent* out = tryGetNthComponentOfType<TComponent>(nth);
  208. ANKI_ASSERT(out);
  209. return *out;
  210. }
  211. /// Get the nth component.
  212. template<typename TComponent>
  213. TComponent& getComponentAt(U32 idx)
  214. {
  215. ANKI_ASSERT(m_components[idx].getComponentClassId() == TComponent::getStaticClassId());
  216. SceneComponent* c = m_components[idx];
  217. return *static_cast<TComponent*>(c);
  218. }
  219. /// Get the nth component.
  220. template<typename TComponent>
  221. const TComponent& getComponentAt(U32 idx) const
  222. {
  223. ANKI_ASSERT(m_components[idx].getComponentClassId() == TComponent::getStaticClassId());
  224. const SceneComponent* c = m_components[idx];
  225. return *static_cast<const TComponent*>(c);
  226. }
  227. U32 getComponentCount() const
  228. {
  229. return m_components.getSize();
  230. }
  231. template<typename TComponent>
  232. U32 countComponentsOfType() const
  233. {
  234. U32 count = 0;
  235. iterateComponentsOfType<TComponent>([&](const TComponent& c) { ++count; });
  236. return count;
  237. }
  238. protected:
  239. /// Create and append a component to the components container. The SceneNode has the ownership.
  240. template<typename TComponent>
  241. TComponent* newComponent()
  242. {
  243. TComponent* comp = getAllocator().newInstance<TComponent>(this);
  244. m_components.emplaceBack(getAllocator(), comp);
  245. return comp;
  246. }
  247. ResourceManager& getResourceManager();
  248. private:
  249. /// This class packs a pointer to a SceneComponent and its type at the same 64bit value. Used to avoid cache misses
  250. /// when iterating the m_components.
  251. class ComponentsArrayElement
  252. {
  253. public:
  254. /// Encodes the SceneComponent's class ID, the SceneComponent* and if it's feedback component or not.
  255. PtrSize m_combo;
  256. ComponentsArrayElement(SceneComponent* comp)
  257. {
  258. set(comp);
  259. }
  260. ComponentsArrayElement(const ComponentsArrayElement& b)
  261. : m_combo(b.m_combo)
  262. {
  263. }
  264. ComponentsArrayElement& operator=(const ComponentsArrayElement& b)
  265. {
  266. m_combo = b.m_combo;
  267. return *this;
  268. }
  269. operator SceneComponent*()
  270. {
  271. return getPtr();
  272. }
  273. operator const SceneComponent*() const
  274. {
  275. return getPtr();
  276. }
  277. const SceneComponent* operator->() const
  278. {
  279. return getPtr();
  280. }
  281. SceneComponent* operator->()
  282. {
  283. return getPtr();
  284. }
  285. U8 getComponentClassId() const
  286. {
  287. return m_combo & 0x7F;
  288. }
  289. Bool isFeedbackComponent() const
  290. {
  291. return m_combo & PtrSize(1 << 7);
  292. }
  293. private:
  294. void set(SceneComponent* comp)
  295. {
  296. m_combo = ptrToNumber(comp) << 8;
  297. m_combo |= PtrSize(comp->isFeedbackComponent()) << 7;
  298. m_combo |= PtrSize(comp->getClassId()) & 0x7F;
  299. ANKI_ASSERT(getPtr() == comp);
  300. ANKI_ASSERT(getComponentClassId() == comp->getClassId());
  301. ANKI_ASSERT(isFeedbackComponent() == comp->isFeedbackComponent());
  302. }
  303. SceneComponent* getPtr() const
  304. {
  305. return numberToPtr<SceneComponent*>(m_combo >> 8);
  306. }
  307. };
  308. static_assert(sizeof(ComponentsArrayElement) == sizeof(void*), "Wrong size");
  309. SceneGraph* m_scene = nullptr;
  310. U64 m_uuid;
  311. String m_name; ///< A unique name.
  312. DynamicArray<ComponentsArrayElement> m_components;
  313. Timestamp m_maxComponentTimestamp = 0;
  314. Bool m_markedForDeletion = false;
  315. };
  316. /// @}
  317. } // end namespace anki