BsCoreApplication.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "BsCoreApplication.h"
  2. #include "BsRenderSystem.h"
  3. #include "BsRenderSystemManager.h"
  4. #include "BsPlatform.h"
  5. #include "BsHardwareBufferManager.h"
  6. #include "BsRenderWindow.h"
  7. #include "BsViewport.h"
  8. #include "BsVector2.h"
  9. #include "BsGpuProgram.h"
  10. #include "BsCoreObjectManager.h"
  11. #include "BsGameObjectManager.h"
  12. #include "BsDynLib.h"
  13. #include "BsDynLibManager.h"
  14. #include "BsCoreSceneManager.h"
  15. #include "BsImporter.h"
  16. #include "BsResources.h"
  17. #include "BsMesh.h"
  18. #include "BsSceneObject.h"
  19. #include "BsTime.h"
  20. #include "BsInput.h"
  21. #include "BsRendererManager.h"
  22. #include "BsGpuProgramManager.h"
  23. #include "BsMeshManager.h"
  24. #include "BsMaterialManager.h"
  25. #include "BsFontManager.h"
  26. #include "BsRenderWindowManager.h"
  27. #include "BsRenderer.h"
  28. #include "BsDeferredCallManager.h"
  29. #include "BsCoreThread.h"
  30. #include "BsStringTable.h"
  31. #include "BsProfilingManager.h"
  32. #include "BsProfilerCPU.h"
  33. #include "BsProfilerGPU.h"
  34. #include "BsQueryManager.h"
  35. #include "BsThreadPool.h"
  36. #include "BsTaskScheduler.h"
  37. #include "BsUUID.h"
  38. #include "BsRenderStats.h"
  39. #include "BsMaterial.h"
  40. #include "BsShader.h"
  41. #include "BsTechnique.h"
  42. #include "BsPass.h"
  43. #include "BsRendererManager.h"
  44. namespace BansheeEngine
  45. {
  46. CoreApplication::CoreApplication(START_UP_DESC& desc)
  47. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false),
  48. mSceneManagerPlugin(nullptr), mRendererPlugin(nullptr)
  49. {
  50. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  51. Platform::_startUp();
  52. MemStack::beginThread();
  53. UUIDGenerator::startUp();
  54. ProfilerCPU::startUp();
  55. ProfilingManager::startUp();
  56. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  57. TaskScheduler::startUp();
  58. TaskScheduler::instance().removeWorker();
  59. RenderStats::startUp();
  60. CoreThread::startUp();
  61. StringTable::startUp();
  62. DeferredCallManager::startUp();
  63. Time::startUp();
  64. DynLibManager::startUp();
  65. CoreObjectManager::startUp();
  66. GameObjectManager::startUp();
  67. Resources::startUp();
  68. GpuProgramManager::startUp();
  69. RenderSystemManager::startUp();
  70. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  71. Input::startUp();
  72. RendererManager::startUp();
  73. loadPlugin(desc.renderer, &mRendererPlugin);
  74. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  75. RendererManager::instance().setActive(desc.renderer);
  76. ProfilerGPU::startUp();
  77. MeshManager::startUp();
  78. MaterialManager::startUp();
  79. FontManager::startUp();
  80. Importer::startUp();
  81. for (auto& importerName : desc.importers)
  82. loadPlugin(importerName);
  83. loadPlugin(desc.input, nullptr, mPrimaryWindow.get());
  84. }
  85. CoreApplication::~CoreApplication()
  86. {
  87. mPrimaryWindow->destroy();
  88. mPrimaryWindow = nullptr;
  89. Importer::shutDown();
  90. FontManager::shutDown();
  91. MaterialManager::shutDown();
  92. MeshManager::shutDown();
  93. ProfilerGPU::shutDown();
  94. unloadPlugin(mSceneManagerPlugin);
  95. RendererManager::shutDown();
  96. RenderSystemManager::shutDown();
  97. unloadPlugin(mRendererPlugin);
  98. Input::shutDown();
  99. GpuProgramManager::shutDown();
  100. Resources::shutDown();
  101. GameObjectManager::shutDown();
  102. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  103. // we need to wait for those objects to get destroyed before continuing.
  104. gCoreThread().update();
  105. gCoreThread().submitAccessors(true);
  106. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  107. DynLibManager::shutDown();
  108. Time::shutDown();
  109. DeferredCallManager::shutDown();
  110. StringTable::shutDown();
  111. CoreThread::shutDown();
  112. RenderStats::shutDown();
  113. TaskScheduler::shutDown();
  114. ThreadPool::shutDown();
  115. ProfilingManager::shutDown();
  116. ProfilerCPU::shutDown();
  117. UUIDGenerator::shutDown();
  118. MemStack::endThread();
  119. Platform::_shutDown();
  120. }
  121. void CoreApplication::runMainLoop()
  122. {
  123. mRunMainLoop = true;
  124. while(mRunMainLoop)
  125. {
  126. gProfilerCPU().beginThread("Sim");
  127. gCoreThread().update();
  128. Platform::_update();
  129. DeferredCallManager::instance()._update();
  130. RenderWindowManager::instance()._update();
  131. gInput()._update();
  132. gTime().update();
  133. PROFILE_CALL(gSceneManager()._update(), "SceneManager");
  134. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  135. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  136. update();
  137. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  138. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  139. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  140. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  141. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  142. {
  143. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  144. while(!mIsFrameRenderingFinished)
  145. {
  146. TaskScheduler::instance().addWorker();
  147. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  148. TaskScheduler::instance().removeWorker();
  149. }
  150. mIsFrameRenderingFinished = false;
  151. }
  152. gCoreThread().queueCommand(&Platform::_coreUpdate);
  153. gCoreThread().submitAccessors();
  154. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  155. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  156. gProfilerCPU().endThread();
  157. gProfiler()._update();
  158. }
  159. }
  160. void CoreApplication::update()
  161. {
  162. // Do nothing
  163. }
  164. void CoreApplication::stopMainLoop()
  165. {
  166. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  167. // a race condition we might run the loop one extra iteration which is acceptable
  168. }
  169. void CoreApplication::frameRenderingFinishedCallback()
  170. {
  171. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  172. mIsFrameRenderingFinished = true;
  173. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  174. }
  175. void CoreApplication::beginCoreProfiling()
  176. {
  177. gProfilerCPU().beginThread("Core");
  178. ProfilerGPU::instance().beginFrame();
  179. }
  180. void CoreApplication::endCoreProfiling()
  181. {
  182. ProfilerGPU::instance().endFrame();
  183. ProfilerGPU::instance()._update();
  184. gProfilerCPU().endThread();
  185. gProfiler()._updateCore();
  186. }
  187. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  188. {
  189. String name = pluginName;
  190. #if BS_PLATFORM == BS_PLATFORM_LINUX
  191. if (name.substr(name.length() - 3, 3) != ".so")
  192. name += ".so";
  193. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  194. if (name.substr(name.length() - 6, 6) != ".dylib")
  195. name += ".dylib";
  196. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  197. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  198. // if you include a relative path then it does not. So, add it to be sure.
  199. if (name.substr(name.length() - 4, 4) != ".dll")
  200. name += ".dll";
  201. #endif
  202. DynLib* loadedLibrary = gDynLibManager().load(name);
  203. if(library != nullptr)
  204. *library = loadedLibrary;
  205. if(loadedLibrary != nullptr)
  206. {
  207. if (passThrough == nullptr)
  208. {
  209. typedef void* (*LoadPluginFunc)();
  210. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  211. if (loadPluginFunc != nullptr)
  212. return loadPluginFunc();
  213. }
  214. else
  215. {
  216. typedef void* (*LoadPluginFunc)(void*);
  217. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  218. if (loadPluginFunc != nullptr)
  219. return loadPluginFunc(passThrough);
  220. }
  221. }
  222. return nullptr;
  223. }
  224. void CoreApplication::unloadPlugin(DynLib* library)
  225. {
  226. typedef void (*UnloadPluginFunc)();
  227. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  228. if(unloadPluginFunc != nullptr)
  229. unloadPluginFunc();
  230. gDynLibManager().unload(library);
  231. }
  232. CoreApplication& gCoreApplication()
  233. {
  234. return CoreApplication::instance();
  235. }
  236. }