BsCoreApplication.cpp 8.5 KB

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