SceneGraph.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright (C) 2009-present, 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/Common.h>
  7. #include <AnKi/Scene/SceneNode.h>
  8. #include <AnKi/Math.h>
  9. #include <AnKi/Util/HashMap.h>
  10. #include <AnKi/Util/BlockArray.h>
  11. #include <AnKi/Scene/Events/EventManager.h>
  12. #include <AnKi/Resource/Common.h>
  13. #include <AnKi/Util/CVarSet.h>
  14. #include <AnKi/Core/Common.h>
  15. namespace anki {
  16. /// @addtogroup scene
  17. /// @{
  18. inline NumericCVar<F32> g_probeEffectiveDistanceCVar("Scene", "ProbeEffectiveDistance", 256.0f, 1.0f, kMaxF32, "How far various probes can render");
  19. inline NumericCVar<F32> g_probeShadowEffectiveDistanceCVar("Scene", "ProbeShadowEffectiveDistance", 32.0f, 1.0f, kMaxF32,
  20. "How far to render shadows for the various probes");
  21. // Gpu scene arrays
  22. inline NumericCVar<U32> g_minGpuSceneTransformsCVar("Scene", "MinGpuSceneTransforms", 2 * 10 * 1024, 8, 100 * 1024,
  23. "The min number of transforms stored in the GPU scene");
  24. inline NumericCVar<U32> g_minGpuSceneMeshesCVar("Scene", "MinGpuSceneMeshes", 8 * 1024, 8, 100 * 1024,
  25. "The min number of meshes stored in the GPU scene");
  26. inline NumericCVar<U32> g_minGpuSceneParticleEmittersCVar("Scene", "MinGpuSceneParticleEmitters", 1 * 1024, 8, 100 * 1024,
  27. "The min number of particle emitters stored in the GPU scene");
  28. inline NumericCVar<U32> g_minGpuSceneLightsCVar("Scene", "MinGpuSceneLights", 2 * 1024, 8, 100 * 1024,
  29. "The min number of lights stored in the GPU scene");
  30. inline NumericCVar<U32> g_minGpuSceneReflectionProbesCVar("Scene", "MinGpuSceneReflectionProbes", 128, 8, 100 * 1024,
  31. "The min number of reflection probes stored in the GPU scene");
  32. inline NumericCVar<U32> g_minGpuSceneGlobalIlluminationProbesCVar("Scene", "MinGpuSceneGlobalIlluminationProbes", 128, 8, 100 * 1024,
  33. "The min number of GI probes stored in the GPU scene");
  34. inline NumericCVar<U32> g_minGpuSceneDecalsCVar("Scene", "MinGpuSceneDecals", 2 * 1024, 8, 100 * 1024,
  35. "The min number of decals stored in the GPU scene");
  36. inline NumericCVar<U32> g_minGpuSceneFogDensityVolumesCVar("Scene", "MinGpuSceneFogDensityVolumes", 512, 8, 100 * 1024,
  37. "The min number fog density volumes stored in the GPU scene");
  38. inline NumericCVar<U32> g_minGpuSceneRenderablesCVar("Scene", "MinGpuSceneRenderables", 10 * 1024, 8, 100 * 1024,
  39. "The min number of renderables stored in the GPU scene");
  40. class SceneComponentArrays
  41. {
  42. public:
  43. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) \
  44. SceneBlockArray<name##Component>& get##name##s() \
  45. { \
  46. return m_##name##Array; \
  47. }
  48. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  49. private:
  50. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) SceneBlockArray<name##Component> m_##name##Array;
  51. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  52. };
  53. /// The scene graph that all the scene entities
  54. class SceneGraph : public MakeSingleton<SceneGraph>
  55. {
  56. template<typename>
  57. friend class MakeSingleton;
  58. friend class SceneNode;
  59. friend class UpdateSceneNodesTask;
  60. friend class Event;
  61. public:
  62. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  63. StackMemoryPool& getFrameMemoryPool() const
  64. {
  65. return m_framePool;
  66. }
  67. SceneNode& getActiveCameraNode()
  68. {
  69. ANKI_ASSERT(m_mainCam != nullptr);
  70. return *m_mainCam;
  71. }
  72. const SceneNode& getActiveCameraNode() const
  73. {
  74. return *m_mainCam;
  75. }
  76. void setActiveCameraNode(SceneNode* cam)
  77. {
  78. m_mainCam = cam;
  79. m_activeCameraChangeTimestamp = GlobalFrameIndex::getSingleton().m_value;
  80. }
  81. Timestamp getActiveCameraNodeChangeTimestamp() const
  82. {
  83. return m_activeCameraChangeTimestamp;
  84. }
  85. U32 getSceneNodesCount() const
  86. {
  87. return m_nodesCount;
  88. }
  89. EventManager& getEventManager()
  90. {
  91. return m_events;
  92. }
  93. const EventManager& getEventManager() const
  94. {
  95. return m_events;
  96. }
  97. Error update(Second prevUpdateTime, Second crntTime);
  98. SceneNode& findSceneNode(const CString& name);
  99. SceneNode* tryFindSceneNode(const CString& name);
  100. /// Iterate the scene nodes using a lambda
  101. template<typename Func>
  102. Error iterateSceneNodes(Func func)
  103. {
  104. for(SceneNode& psn : m_nodes)
  105. {
  106. Error err = func(psn);
  107. if(err)
  108. {
  109. return err;
  110. }
  111. }
  112. return Error::kNone;
  113. }
  114. /// Iterate a range of scene nodes using a lambda
  115. template<typename Func>
  116. Error iterateSceneNodes(PtrSize begin, PtrSize end, Func func);
  117. /// Create a new SceneNode
  118. template<typename Node, typename... Args>
  119. Error newSceneNode(const CString& name, Node*& node, Args&&... args);
  120. /// Delete a scene node. It actualy marks it for deletion
  121. void deleteSceneNode(SceneNode* node)
  122. {
  123. node->setMarkedForDeletion();
  124. }
  125. void increaseObjectsMarkedForDeletion()
  126. {
  127. m_objectsMarkedForDeletionCount.fetchAdd(1);
  128. }
  129. const Vec3& getSceneMin() const
  130. {
  131. return m_sceneMin;
  132. }
  133. const Vec3& getSceneMax() const
  134. {
  135. return m_sceneMax;
  136. }
  137. /// Get a unique UUID.
  138. /// @note It's thread-safe.
  139. U32 getNewUuid()
  140. {
  141. return m_nodesUuid.fetchAdd(1);
  142. }
  143. SceneComponentArrays& getComponentArrays()
  144. {
  145. return m_componentArrays;
  146. }
  147. void addDirectionalLight(LightComponent* comp)
  148. {
  149. ANKI_ASSERT(m_dirLights.find(comp) == m_dirLights.getEnd());
  150. m_dirLights.emplaceBack(comp);
  151. if(m_dirLights.getSize() > 1)
  152. {
  153. ANKI_SCENE_LOGW("More than one directional lights detected");
  154. }
  155. }
  156. void removeDirectionalLight(LightComponent* comp)
  157. {
  158. auto it = m_dirLights.find(comp);
  159. ANKI_ASSERT(it != m_dirLights.getEnd());
  160. m_dirLights.erase(it);
  161. }
  162. LightComponent* getDirectionalLight() const;
  163. void addSkybox(SkyboxComponent* comp)
  164. {
  165. ANKI_ASSERT(m_skyboxes.find(comp) == m_skyboxes.getEnd());
  166. m_skyboxes.emplaceBack(comp);
  167. if(m_skyboxes.getSize() > 1)
  168. {
  169. ANKI_SCENE_LOGW("More than one skyboxes detected");
  170. }
  171. }
  172. void removeSkybox(SkyboxComponent* comp)
  173. {
  174. auto it = m_skyboxes.find(comp);
  175. ANKI_ASSERT(it != m_skyboxes.getEnd());
  176. m_skyboxes.erase(it);
  177. }
  178. SkyboxComponent* getSkybox() const
  179. {
  180. return (m_skyboxes.getSize()) ? m_skyboxes[0] : nullptr;
  181. }
  182. /// @note It's thread-safe.
  183. void updateSceneBounds(const Vec3& min, const Vec3& max)
  184. {
  185. LockGuard lock(m_sceneBoundsMtx);
  186. m_sceneMin = m_sceneMin.min(min);
  187. m_sceneMax = m_sceneMax.max(max);
  188. }
  189. /// @note It's thread-safe.
  190. Array<Vec3, 2> getSceneBounds() const
  191. {
  192. LockGuard lock(m_sceneBoundsMtx);
  193. ANKI_ASSERT(m_sceneMin < m_sceneMax);
  194. return {m_sceneMin, m_sceneMax};
  195. }
  196. private:
  197. class UpdateSceneNodesCtx;
  198. class InitMemPoolDummy
  199. {
  200. public:
  201. ~InitMemPoolDummy()
  202. {
  203. SceneMemoryPool::freeSingleton();
  204. }
  205. } m_initMemPoolDummy;
  206. mutable StackMemoryPool m_framePool;
  207. IntrusiveList<SceneNode> m_nodes;
  208. U32 m_nodesCount = 0;
  209. GrHashMap<CString, SceneNode*> m_nodesDict;
  210. SceneNode* m_mainCam = nullptr;
  211. Timestamp m_activeCameraChangeTimestamp = 0;
  212. SceneNode* m_defaultMainCam = nullptr;
  213. EventManager m_events;
  214. Vec3 m_sceneMin = Vec3(kMaxF32);
  215. Vec3 m_sceneMax = Vec3(kMinF32);
  216. mutable SpinLock m_sceneBoundsMtx;
  217. Atomic<U32> m_objectsMarkedForDeletionCount = {0};
  218. Atomic<U32> m_nodesUuid = {1};
  219. SceneComponentArrays m_componentArrays;
  220. SceneDynamicArray<LightComponent*> m_dirLights;
  221. SceneDynamicArray<SkyboxComponent*> m_skyboxes;
  222. SceneGraph();
  223. ~SceneGraph();
  224. /// Put a node in the appropriate containers
  225. Error registerNode(SceneNode* node);
  226. void unregisterNode(SceneNode* node);
  227. /// Delete the nodes that are marked for deletion
  228. void deleteNodesMarkedForDeletion();
  229. Error updateNodes(UpdateSceneNodesCtx& ctx);
  230. Error updateNode(Second prevTime, Second crntTime, SceneNode& node);
  231. };
  232. template<typename Node, typename... Args>
  233. inline Error SceneGraph::newSceneNode(const CString& name, Node*& node, Args&&... args)
  234. {
  235. Error err = Error::kNone;
  236. node = newInstance<Node>(SceneMemoryPool::getSingleton(), name);
  237. if(node)
  238. {
  239. err = node->init(std::forward<Args>(args)...);
  240. }
  241. else
  242. {
  243. err = Error::kOutOfMemory;
  244. }
  245. if(!err)
  246. {
  247. err = registerNode(node);
  248. }
  249. if(err)
  250. {
  251. ANKI_SCENE_LOGE("Failed to create scene node: %s", (name.isEmpty()) ? "unnamed" : &name[0]);
  252. if(node)
  253. {
  254. deleteInstance(SceneMemoryPool::getSingleton(), node);
  255. node = nullptr;
  256. }
  257. }
  258. return err;
  259. }
  260. template<typename Func>
  261. Error SceneGraph::iterateSceneNodes(PtrSize begin, PtrSize end, Func func)
  262. {
  263. ANKI_ASSERT(begin < m_nodesCount && end <= m_nodesCount);
  264. auto it = m_nodes.getBegin() + begin;
  265. PtrSize count = end - begin;
  266. Error err = Error::kNone;
  267. while(count-- != 0 && !err)
  268. {
  269. ANKI_ASSERT(it != m_nodes.getEnd());
  270. err = func(*it);
  271. ++it;
  272. }
  273. return Error::kNone;
  274. }
  275. /// @}
  276. } // end namespace anki