BsCoreApplication.cpp 8.8 KB

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