BsCoreApplication.cpp 7.4 KB

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