SceneGraph.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. ANKI_CVAR(NumericCVar<F32>, Scene, ProbeEffectiveDistance, 256.0f, 1.0f, kMaxF32, "How far various probes can render")
  17. ANKI_CVAR(NumericCVar<F32>, Scene, ProbeShadowEffectiveDistance, 32.0f, 1.0f, kMaxF32, "How far to render shadows for the various probes")
  18. // Gpu scene arrays
  19. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneTransforms, 2 * 10 * 1024, 8, 100 * 1024, "The min number of transforms stored in the GPU scene")
  20. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneMeshes, 8 * 1024, 8, 100 * 1024, "The min number of meshes stored in the GPU scene")
  21. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneParticleEmitters, 1 * 1024, 8, 100 * 1024,
  22. "The min number of particle emitters stored in the GPU scene")
  23. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneLights, 2 * 1024, 8, 100 * 1024, "The min number of lights stored in the GPU scene")
  24. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneReflectionProbes, 128, 8, 100 * 1024, "The min number of reflection probes stored in the GPU scene")
  25. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneGlobalIlluminationProbes, 128, 8, 100 * 1024, "The min number of GI probes stored in the GPU scene")
  26. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneDecals, 2 * 1024, 8, 100 * 1024, "The min number of decals stored in the GPU scene")
  27. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneFogDensityVolumes, 512, 8, 100 * 1024, "The min number fog density volumes stored in the GPU scene")
  28. ANKI_CVAR(NumericCVar<U32>, Scene, MinGpuSceneRenderables, 10 * 1024, 8, 100 * 1024, "The min number of renderables stored in the GPU scene")
  29. class SceneComponentArrays
  30. {
  31. public:
  32. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight, sceneNodeCanHaveMany, icon, serializable) \
  33. SceneBlockArray<name##Component>& get##name##s() \
  34. { \
  35. return m_##name##Array; \
  36. } \
  37. const SceneBlockArray<name##Component>& get##name##s() const \
  38. { \
  39. return m_##name##Array; \
  40. }
  41. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  42. private:
  43. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight, sceneNodeCanHaveMany, icon, serializable) SceneBlockArray<name##Component> m_##name##Array;
  44. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  45. };
  46. // The scene graph that all the scene entities
  47. class SceneGraph : public MakeSingleton<SceneGraph>
  48. {
  49. template<typename>
  50. friend class MakeSingleton;
  51. friend class SceneNode;
  52. friend class UpdateSceneNodesTask;
  53. friend class Event;
  54. public:
  55. Error init(AllocAlignedCallback allocCallback, void* allocCallbackData);
  56. StackMemoryPool& getFrameMemoryPool() const
  57. {
  58. return m_framePool;
  59. }
  60. SceneNode& getActiveCameraNode()
  61. {
  62. const SceneNode& cam = static_cast<const SceneGraph*>(this)->getActiveCameraNode();
  63. return const_cast<SceneNode&>(cam);
  64. }
  65. const SceneNode& getActiveCameraNode() const;
  66. void setActiveCameraNode(SceneNode* cam);
  67. SceneNode& getEditorUiNode()
  68. {
  69. ANKI_ASSERT(m_editorUi);
  70. return *m_editorUi;
  71. }
  72. U32 getSceneNodesCount() const
  73. {
  74. return m_nodesCount;
  75. }
  76. EventManager& getEventManager()
  77. {
  78. return m_events;
  79. }
  80. const EventManager& getEventManager() const
  81. {
  82. return m_events;
  83. }
  84. void update(Second prevUpdateTime, Second crntTime);
  85. // Note: Thread-safe against itself. Can be called by SceneNode::update
  86. SceneNode* tryFindSceneNode(const CString& name);
  87. // Note: Thread-safe against itself. Can be called by SceneNode::update
  88. SceneNode& findSceneNode(const CString& name);
  89. // Iterate the scene nodes using a lambda
  90. template<typename Func>
  91. void visitNodes(Func func)
  92. {
  93. for(SceneNode& psn : m_nodes)
  94. {
  95. if(func(psn) == FunctorContinue::kStop)
  96. {
  97. break;
  98. }
  99. }
  100. }
  101. // Create a new SceneNode
  102. // Note: Thread-safe against itself. Can be called by SceneNode::update
  103. template<typename TNode>
  104. TNode* newSceneNode(CString name)
  105. {
  106. TNode* node = newInstance<TNode>(SceneMemoryPool::getSingleton(), name);
  107. LockGuard lock(m_nodesForRegistrationMtx);
  108. m_nodesForRegistration.pushBack(node);
  109. return node;
  110. }
  111. const Vec3& getSceneMin() const
  112. {
  113. return m_sceneMin;
  114. }
  115. const Vec3& getSceneMax() const
  116. {
  117. return m_sceneMax;
  118. }
  119. // Get a unique UUID.
  120. // Note: It's thread-safe.
  121. U32 getNewUuid()
  122. {
  123. return m_nodesUuid.fetchAdd(1);
  124. }
  125. SceneComponentArrays& getComponentArrays()
  126. {
  127. return m_componentArrays;
  128. }
  129. LightComponent* getDirectionalLight() const
  130. {
  131. return m_activeDirLight;
  132. }
  133. SkyboxComponent* getSkybox() const
  134. {
  135. return m_activeSkybox;
  136. }
  137. Array<Vec3, 2> getSceneBounds() const
  138. {
  139. ANKI_ASSERT(m_sceneMin < m_sceneMax);
  140. return {m_sceneMin, m_sceneMax};
  141. }
  142. // If enable is true the components will be checking for updates of resources. Useful for the editor resource updates. It has a perf hit so it
  143. // should be enabled only by the editor
  144. void setCheckForResourceUpdates(Bool enable)
  145. {
  146. m_checkForResourceUpdates = enable;
  147. }
  148. Error saveToTextFile(CString filename);
  149. private:
  150. class UpdateSceneNodesCtx;
  151. class InitMemPoolDummy
  152. {
  153. public:
  154. ~InitMemPoolDummy()
  155. {
  156. SceneMemoryPool::freeSingleton();
  157. }
  158. } m_initMemPoolDummy;
  159. static constexpr U32 kForceSetSceneBoundsFrameCount = 60 * 2; // Re-set the scene bounds after 2".
  160. mutable StackMemoryPool m_framePool;
  161. IntrusiveList<SceneNode> m_nodes;
  162. U32 m_nodesCount = 0;
  163. GrHashMap<CString, SceneNode*> m_nodesDict;
  164. SceneNode* m_mainCam = nullptr;
  165. SceneNode* m_defaultMainCam = nullptr;
  166. SceneNode* m_editorUi = nullptr;
  167. EventManager m_events;
  168. U64 m_frame = 0;
  169. Vec3 m_sceneMin = Vec3(-0.1f);
  170. Vec3 m_sceneMax = Vec3(+0.1f);
  171. IntrusiveList<SceneNode> m_nodesForRegistration;
  172. SceneDynamicArray<std::pair<SceneNode*, SceneString>> m_nodesRenamed;
  173. SpinLock m_nodesForRegistrationMtx;
  174. Bool m_checkForResourceUpdates = false;
  175. Atomic<U32> m_nodesUuid = {1};
  176. SceneComponentArrays m_componentArrays;
  177. LightComponent* m_activeDirLight = nullptr;
  178. SkyboxComponent* m_activeSkybox = nullptr;
  179. SceneGraph();
  180. ~SceneGraph();
  181. void updateNodes(U32 tid, UpdateSceneNodesCtx& ctx);
  182. void updateNode(U32 tid, SceneNode& node, UpdateSceneNodesCtx& ctx);
  183. void sceneNodeChangedName(SceneNode& node, CString oldName);
  184. };
  185. } // end namespace anki