App.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/Core/Common.h>
  7. #include <AnKi/Util/Allocator.h>
  8. #include <AnKi/Util/String.h>
  9. #include <AnKi/Util/Ptr.h>
  10. #include <AnKi/Ui/UiImmediateModeBuilder.h>
  11. namespace anki
  12. {
  13. // Forward
  14. class CoreTracer;
  15. class ConfigSet;
  16. class ThreadHive;
  17. class NativeWindow;
  18. class Input;
  19. class GrManager;
  20. class MainRenderer;
  21. class PhysicsWorld;
  22. class SceneGraph;
  23. class ScriptManager;
  24. class ResourceManager;
  25. class ResourceFilesystem;
  26. class StagingGpuMemoryManager;
  27. class UiManager;
  28. class UiQueueElement;
  29. class RenderQueue;
  30. /// The core class of the engine.
  31. class App
  32. {
  33. public:
  34. App();
  35. virtual ~App();
  36. ANKI_USE_RESULT Error init(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  37. Second getTimerTick() const
  38. {
  39. return m_timerTick;
  40. }
  41. void setTimerTick(const F32 x)
  42. {
  43. m_timerTick = x;
  44. }
  45. const String& getSettingsDirectory() const
  46. {
  47. return m_settingsDir;
  48. }
  49. const String& getCacheDirectory() const
  50. {
  51. return m_cacheDir;
  52. }
  53. AllocAlignedCallback getAllocationCallback() const
  54. {
  55. return m_allocCb;
  56. }
  57. void* getAllocationCallbackData() const
  58. {
  59. return m_allocCbData;
  60. }
  61. ThreadHive& getThreadHive()
  62. {
  63. return *m_threadHive;
  64. }
  65. HeapAllocator<U8>& getAllocator()
  66. {
  67. return m_heapAlloc;
  68. }
  69. Timestamp getGlobalTimestamp() const
  70. {
  71. return m_globalTimestamp;
  72. }
  73. /// Run the main loop.
  74. ANKI_USE_RESULT Error mainLoop();
  75. /// The user code to run along with the other main loop code.
  76. virtual ANKI_USE_RESULT Error userMainLoop(Bool& quit, Second elapsedTime)
  77. {
  78. // Do nothing
  79. return Error::NONE;
  80. }
  81. Input& getInput()
  82. {
  83. return *m_input;
  84. }
  85. SceneGraph& getSceneGraph()
  86. {
  87. return *m_scene;
  88. }
  89. MainRenderer& getMainRenderer()
  90. {
  91. return *m_renderer;
  92. }
  93. ResourceManager& getResourceManager()
  94. {
  95. return *m_resources;
  96. }
  97. ScriptManager& getScriptManager()
  98. {
  99. return *m_script;
  100. }
  101. PhysicsWorld& getPhysicsWorld()
  102. {
  103. return *m_physics;
  104. }
  105. NativeWindow& getWindow()
  106. {
  107. return *m_window;
  108. }
  109. HeapAllocator<U8> getAllocator() const
  110. {
  111. return m_heapAlloc;
  112. }
  113. void setDisplayStats(Bool enable)
  114. {
  115. m_displayStats = enable;
  116. }
  117. Bool getDisplayStats() const
  118. {
  119. return m_displayStats;
  120. }
  121. void setDisplayDeveloperConsole(Bool display)
  122. {
  123. m_consoleEnabled = display;
  124. }
  125. Bool getDisplayDeveloperConsole() const
  126. {
  127. return m_consoleEnabled;
  128. }
  129. private:
  130. class StatsUi;
  131. // Allocation
  132. AllocAlignedCallback m_allocCb;
  133. void* m_allocCbData;
  134. HeapAllocator<U8> m_heapAlloc;
  135. // Sybsystems
  136. #if ANKI_ENABLE_TRACE
  137. CoreTracer* m_coreTracer = nullptr;
  138. #endif
  139. NativeWindow* m_window = nullptr;
  140. Input* m_input = nullptr;
  141. GrManager* m_gr = nullptr;
  142. StagingGpuMemoryManager* m_stagingMem = nullptr;
  143. PhysicsWorld* m_physics = nullptr;
  144. ResourceFilesystem* m_resourceFs = nullptr;
  145. ResourceManager* m_resources = nullptr;
  146. UiManager* m_ui = nullptr;
  147. MainRenderer* m_renderer = nullptr;
  148. SceneGraph* m_scene = nullptr;
  149. ScriptManager* m_script = nullptr;
  150. // Misc
  151. StatsUi* m_statsUi = nullptr;
  152. Bool m_displayStats = false;
  153. UiImmediateModeBuilderPtr m_console;
  154. Bool m_consoleEnabled = false;
  155. Timestamp m_globalTimestamp = 1;
  156. ThreadHive* m_threadHive = nullptr;
  157. String m_settingsDir; ///< The path that holds the configuration
  158. String m_cacheDir; ///< This is used as a cache
  159. Second m_timerTick;
  160. U64 m_resourceCompletedAsyncTaskCount = 0;
  161. class MemStats
  162. {
  163. public:
  164. Atomic<PtrSize> m_allocatedMem = {0};
  165. Atomic<U64> m_allocCount = {0};
  166. Atomic<U64> m_freeCount = {0};
  167. void* m_originalUserData = nullptr;
  168. AllocAlignedCallback m_originalAllocCallback = nullptr;
  169. static void* allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment);
  170. } m_memStats;
  171. void initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData);
  172. ANKI_USE_RESULT Error initInternal(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  173. ANKI_USE_RESULT Error initDirs(const ConfigSet& cfg);
  174. void cleanup();
  175. /// Inject a new UI element in the render queue for displaying various stuff.
  176. void injectUiElements(DynamicArrayAuto<UiQueueElement>& elements, RenderQueue& rqueue);
  177. void setSignalHandlers();
  178. };
  179. } // end namespace anki