SceneNode.h 9.0 KB

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