SceneGraph.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. ANKI_CVAR(NumericCVar<F32>, Scene, ProbeEffectiveDistance, 256.0f, 1.0f, kMaxF32, "How far various probes can render")
  19. ANKI_CVAR(NumericCVar<F32>, Scene, ProbeShadowEffectiveDistance, 32.0f, 1.0f, kMaxF32, "How far to render shadows for the various probes")
  20. // Gpu scene arrays
  21. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneTransforms, 2 * 10 * 1024, 8, 100 * 1024, "The min number of transforms stored in the GPU scene")
  22. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneMeshes, 8 * 1024, 8, 100 * 1024, "The min number of meshes stored in the GPU scene")
  23. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneParticleEmitters, 1 * 1024, 8, 100 * 1024,
  24. "The min number of particle emitters stored in the GPU scene")
  25. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneLights, 2 * 1024, 8, 100 * 1024, "The min number of lights stored in the GPU scene")
  26. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneReflectionProbes, 128, 8, 100 * 1024, "The min number of reflection probes stored in the GPU scene")
  27. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneGlobalIlluminationProbes, 128, 8, 100 * 1024, "The min number of GI probes stored in the GPU scene")
  28. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneDecals, 2 * 1024, 8, 100 * 1024, "The min number of decals stored in the GPU scene")
  29. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneFogDensityVolumes, 512, 8, 100 * 1024, "The min number fog density volumes stored in the GPU scene")
  30. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneRenderables, 10 * 1024, 8, 100 * 1024, "The min number of renderables stored in the GPU scene")
  31. class SceneComponentArrays
  32. {
  33. public:
  34. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) \
  35. SceneBlockArray<name##Component>& get##name##s() \
  36. { \
  37. return m_##name##Array; \
  38. }
  39. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  40. private:
  41. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) SceneBlockArray<name##Component> m_##name##Array;
  42. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  43. };
  44. /// The scene graph that all the scene entities
  45. class SceneGraph : public MakeSingleton<SceneGraph>
  46. {
  47. template<typename>
  48. friend class MakeSingleton;
  49. friend class SceneNode;
  50. friend class UpdateSceneNodesTask;
  51. friend class Event;
  52. public:
  53. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  54. StackMemoryPool& getFrameMemoryPool() const
  55. {
  56. return m_framePool;
  57. }
  58. SceneNode& getActiveCameraNode()
  59. {
  60. const SceneNode& cam = static_cast<const SceneGraph*>(this)->getActiveCameraNode();
  61. return const_cast<SceneNode&>(cam);
  62. }
  63. const SceneNode& getActiveCameraNode() const;
  64. void setActiveCameraNode(SceneNode* cam);
  65. U32 getSceneNodesCount() const
  66. {
  67. return m_nodesCount;
  68. }
  69. EventManager& getEventManager()
  70. {
  71. return m_events;
  72. }
  73. const EventManager& getEventManager() const
  74. {
  75. return m_events;
  76. }
  77. void update(Second prevUpdateTime, Second crntTime);
  78. /// @note Thread-safe against itself. Can be called by SceneNode::update
  79. SceneNode* tryFindSceneNode(const CString& name);
  80. /// @note Thread-safe against itself. Can be called by SceneNode::update
  81. SceneNode& findSceneNode(const CString& name);
  82. /// Iterate the scene nodes using a lambda
  83. template<typename Func>
  84. void visitNodes(Func func)
  85. {
  86. for(SceneNode& psn : m_nodes)
  87. {
  88. const Bool continue_ = func(psn);
  89. if(!continue_)
  90. {
  91. break;
  92. }
  93. }
  94. }
  95. /// Create a new SceneNode
  96. /// @note Thread-safe against itself. Can be called by SceneNode::update
  97. template<typename TNode>
  98. TNode* newSceneNode(CString name)
  99. {
  100. TNode* node = newInstance<TNode>(SceneMemoryPool::getSingleton(), name);
  101. LockGuard lock(m_nodesForRegistrationMtx);
  102. m_nodesForRegistration.pushBack(node);
  103. return node;
  104. }
  105. const Vec3& getSceneMin() const
  106. {
  107. return m_sceneMin;
  108. }
  109. const Vec3& getSceneMax() const
  110. {
  111. return m_sceneMax;
  112. }
  113. /// Get a unique UUID.
  114. /// @note It's thread-safe.
  115. U32 getNewUuid()
  116. {
  117. return m_nodesUuid.fetchAdd(1);
  118. }
  119. SceneComponentArrays& getComponentArrays()
  120. {
  121. return m_componentArrays;
  122. }
  123. void addDirectionalLight(LightComponent* comp)
  124. {
  125. ANKI_ASSERT(m_dirLights.find(comp) == m_dirLights.getEnd());
  126. m_dirLights.emplaceBack(comp);
  127. if(m_dirLights.getSize() > 1)
  128. {
  129. ANKI_SCENE_LOGW("More than one directional lights detected");
  130. }
  131. }
  132. void removeDirectionalLight(LightComponent* comp)
  133. {
  134. auto it = m_dirLights.find(comp);
  135. ANKI_ASSERT(it != m_dirLights.getEnd());
  136. m_dirLights.erase(it);
  137. }
  138. LightComponent* getDirectionalLight() const;
  139. void addSkybox(SkyboxComponent* comp)
  140. {
  141. ANKI_ASSERT(m_skyboxes.find(comp) == m_skyboxes.getEnd());
  142. m_skyboxes.emplaceBack(comp);
  143. if(m_skyboxes.getSize() > 1)
  144. {
  145. ANKI_SCENE_LOGW("More than one skyboxes detected");
  146. }
  147. }
  148. void removeSkybox(SkyboxComponent* comp)
  149. {
  150. auto it = m_skyboxes.find(comp);
  151. ANKI_ASSERT(it != m_skyboxes.getEnd());
  152. m_skyboxes.erase(it);
  153. }
  154. SkyboxComponent* getSkybox() const
  155. {
  156. return (m_skyboxes.getSize()) ? m_skyboxes[0] : nullptr;
  157. }
  158. /// @note It's thread-safe.
  159. void updateSceneBounds(const Vec3& min, const Vec3& max)
  160. {
  161. LockGuard lock(m_sceneBoundsMtx);
  162. m_sceneMin = m_sceneMin.min(min);
  163. m_sceneMax = m_sceneMax.max(max);
  164. }
  165. /// @note It's thread-safe.
  166. Array<Vec3, 2> getSceneBounds() const
  167. {
  168. LockGuard lock(m_sceneBoundsMtx);
  169. ANKI_ASSERT(m_sceneMin < m_sceneMax);
  170. return {m_sceneMin, m_sceneMax};
  171. }
  172. private:
  173. class UpdateSceneNodesCtx;
  174. class InitMemPoolDummy
  175. {
  176. public:
  177. ~InitMemPoolDummy()
  178. {
  179. SceneMemoryPool::freeSingleton();
  180. }
  181. } m_initMemPoolDummy;
  182. mutable StackMemoryPool m_framePool;
  183. IntrusiveList<SceneNode> m_nodes;
  184. U32 m_nodesCount = 0;
  185. GrHashMap<CString, SceneNode*> m_nodesDict;
  186. SceneNode* m_mainCam = nullptr;
  187. SceneNode* m_defaultMainCam = nullptr;
  188. EventManager m_events;
  189. Vec3 m_sceneMin = Vec3(kMaxF32);
  190. Vec3 m_sceneMax = Vec3(kMinF32);
  191. mutable SpinLock m_sceneBoundsMtx;
  192. IntrusiveList<SceneNode> m_nodesForRegistration;
  193. SpinLock m_nodesForRegistrationMtx;
  194. Atomic<U32> m_nodesUuid = {1};
  195. SceneComponentArrays m_componentArrays;
  196. SceneDynamicArray<LightComponent*> m_dirLights;
  197. SceneDynamicArray<SkyboxComponent*> m_skyboxes;
  198. SceneGraph();
  199. ~SceneGraph();
  200. void updateNodes(UpdateSceneNodesCtx& ctx);
  201. void updateNode(Second prevTime, Second crntTime, SceneNode& node);
  202. };
  203. /// @}
  204. } // end namespace anki