BsCoreApplication.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. void atExit()
  47. {
  48. BS_EXCEPT(InternalErrorException, "exit() called. Please shut down Banshee normally and do not use exit().");
  49. }
  50. CoreApplication::CoreApplication(START_UP_DESC& desc)
  51. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false),
  52. mSceneManagerPlugin(nullptr), mRendererPlugin(nullptr)
  53. {
  54. atexit(atExit);
  55. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  56. Platform::_startUp();
  57. MemStack::beginThread();
  58. UUIDGenerator::startUp();
  59. ProfilerCPU::startUp();
  60. ProfilingManager::startUp();
  61. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  62. TaskScheduler::startUp();
  63. TaskScheduler::instance().removeWorker();
  64. RenderStats::startUp();
  65. CoreThread::startUp();
  66. StringTable::startUp();
  67. DeferredCallManager::startUp();
  68. Time::startUp();
  69. DynLibManager::startUp();
  70. CoreObjectManager::startUp();
  71. GameObjectManager::startUp();
  72. Resources::startUp();
  73. GpuProgramManager::startUp();
  74. RenderSystemManager::startUp();
  75. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  76. Input::startUp();
  77. RendererManager::startUp();
  78. loadPlugin(desc.renderer, &mRendererPlugin);
  79. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  80. RendererManager::instance().setActive(desc.renderer);
  81. ProfilerGPU::startUp();
  82. MeshManager::startUp();
  83. MaterialManager::startUp();
  84. FontManager::startUp();
  85. Importer::startUp();
  86. for (auto& importerName : desc.importers)
  87. loadPlugin(importerName);
  88. loadPlugin(desc.input, nullptr, mPrimaryWindow.get());
  89. }
  90. CoreApplication::~CoreApplication()
  91. {
  92. mPrimaryWindow->destroy();
  93. mPrimaryWindow = nullptr;
  94. Importer::shutDown();
  95. FontManager::shutDown();
  96. MaterialManager::shutDown();
  97. MeshManager::shutDown();
  98. ProfilerGPU::shutDown();
  99. unloadPlugin(mSceneManagerPlugin);
  100. RendererManager::shutDown();
  101. unloadPlugin(mRendererPlugin);
  102. Input::shutDown();
  103. Resources::shutDown();
  104. GameObjectManager::shutDown();
  105. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  106. // we need to wait for those objects to get destroyed before continuing.
  107. gCoreThread().update();
  108. gCoreThread().submitAccessors(true);
  109. RenderSystemManager::shutDown();
  110. GpuProgramManager::shutDown();
  111. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  112. DynLibManager::shutDown();
  113. Time::shutDown();
  114. DeferredCallManager::shutDown();
  115. StringTable::shutDown();
  116. CoreThread::shutDown();
  117. RenderStats::shutDown();
  118. TaskScheduler::shutDown();
  119. ThreadPool::shutDown();
  120. ProfilingManager::shutDown();
  121. ProfilerCPU::shutDown();
  122. UUIDGenerator::shutDown();
  123. MemStack::endThread();
  124. Platform::_shutDown();
  125. }
  126. void CoreApplication::runMainLoop()
  127. {
  128. mRunMainLoop = true;
  129. while(mRunMainLoop)
  130. {
  131. gProfilerCPU().beginThread("Sim");
  132. gCoreThread().update();
  133. Platform::_update();
  134. DeferredCallManager::instance()._update();
  135. RenderWindowManager::instance()._update();
  136. gInput()._update();
  137. gTime().update();
  138. PROFILE_CALL(gSceneManager()._update(), "SceneManager");
  139. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  140. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  141. // Update plugins
  142. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  143. pluginUpdateFunc.second();
  144. update();
  145. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  146. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  147. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  148. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  149. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  150. {
  151. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  152. while(!mIsFrameRenderingFinished)
  153. {
  154. TaskScheduler::instance().addWorker();
  155. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  156. TaskScheduler::instance().removeWorker();
  157. }
  158. mIsFrameRenderingFinished = false;
  159. }
  160. gCoreThread().queueCommand(&Platform::_coreUpdate);
  161. gCoreThread().submitAccessors();
  162. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  163. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  164. gProfilerCPU().endThread();
  165. gProfiler()._update();
  166. }
  167. }
  168. void CoreApplication::update()
  169. {
  170. // Do nothing
  171. }
  172. void CoreApplication::stopMainLoop()
  173. {
  174. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  175. // a race condition we might run the loop one extra iteration which is acceptable
  176. }
  177. void CoreApplication::frameRenderingFinishedCallback()
  178. {
  179. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  180. mIsFrameRenderingFinished = true;
  181. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  182. }
  183. void CoreApplication::beginCoreProfiling()
  184. {
  185. gProfilerCPU().beginThread("Core");
  186. ProfilerGPU::instance().beginFrame();
  187. }
  188. void CoreApplication::endCoreProfiling()
  189. {
  190. ProfilerGPU::instance().endFrame();
  191. ProfilerGPU::instance()._update();
  192. gProfilerCPU().endThread();
  193. gProfiler()._updateCore();
  194. }
  195. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  196. {
  197. String name = pluginName;
  198. #if BS_PLATFORM == BS_PLATFORM_LINUX
  199. if (name.substr(name.length() - 3, 3) != ".so")
  200. name += ".so";
  201. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  202. if (name.substr(name.length() - 6, 6) != ".dylib")
  203. name += ".dylib";
  204. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  205. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  206. // if you include a relative path then it does not. So, add it to be sure.
  207. if (name.substr(name.length() - 4, 4) != ".dll")
  208. name += ".dll";
  209. #endif
  210. DynLib* loadedLibrary = gDynLibManager().load(name);
  211. if(library != nullptr)
  212. *library = loadedLibrary;
  213. void* retVal = nullptr;
  214. if(loadedLibrary != nullptr)
  215. {
  216. if (passThrough == nullptr)
  217. {
  218. typedef void* (*LoadPluginFunc)();
  219. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  220. if (loadPluginFunc != nullptr)
  221. retVal = loadPluginFunc();
  222. }
  223. else
  224. {
  225. typedef void* (*LoadPluginFunc)(void*);
  226. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  227. if (loadPluginFunc != nullptr)
  228. retVal = loadPluginFunc(passThrough);
  229. }
  230. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  231. if (loadPluginFunc != nullptr)
  232. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  233. }
  234. return retVal;
  235. }
  236. void CoreApplication::unloadPlugin(DynLib* library)
  237. {
  238. typedef void (*UnloadPluginFunc)();
  239. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  240. if(unloadPluginFunc != nullptr)
  241. unloadPluginFunc();
  242. mPluginUpdateFunctions.erase(library);
  243. gDynLibManager().unload(library);
  244. }
  245. CoreApplication& gCoreApplication()
  246. {
  247. return CoreApplication::instance();
  248. }
  249. }