BsCoreApplication.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. shutdownPlugin(mSceneManagerPlugin);
  105. unloadPlugin(mSceneManagerPlugin);
  106. RendererManager::shutDown();
  107. shutdownPlugin(mRendererPlugin);
  108. unloadPlugin(mRendererPlugin);
  109. Input::shutDown();
  110. Resources::shutDown();
  111. GameObjectManager::shutDown();
  112. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  113. // we need to wait for those objects to get destroyed before continuing.
  114. gCoreThread().update();
  115. gCoreThread().submitAccessors(true);
  116. RenderSystemManager::shutDown();
  117. GpuProgramManager::shutDown();
  118. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  119. DynLibManager::shutDown();
  120. Time::shutDown();
  121. DeferredCallManager::shutDown();
  122. StringTable::shutDown();
  123. CoreThread::shutDown();
  124. RenderStats::shutDown();
  125. TaskScheduler::shutDown();
  126. ThreadPool::shutDown();
  127. ProfilingManager::shutDown();
  128. ProfilerCPU::shutDown();
  129. UUIDGenerator::shutDown();
  130. RenderTargetManager::shutDown();
  131. MessageHandler::shutDown();
  132. MemStack::endThread();
  133. Platform::_shutDown();
  134. }
  135. void CoreApplication::runMainLoop()
  136. {
  137. mRunMainLoop = true;
  138. while(mRunMainLoop)
  139. {
  140. gProfilerCPU().beginThread("Sim");
  141. gCoreThread().update();
  142. RenderTargetManager::instance().update();
  143. Platform::_update();
  144. DeferredCallManager::instance()._update();
  145. RenderWindowManager::instance()._update();
  146. gTime().update();
  147. gInput()._update();
  148. PROFILE_CALL(gCoreSceneManager()._update(), "SceneManager");
  149. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  150. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  151. // Update plugins
  152. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  153. pluginUpdateFunc.second();
  154. update();
  155. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  156. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  157. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  158. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  159. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  160. {
  161. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  162. while(!mIsFrameRenderingFinished)
  163. {
  164. TaskScheduler::instance().addWorker();
  165. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  166. TaskScheduler::instance().removeWorker();
  167. }
  168. mIsFrameRenderingFinished = false;
  169. }
  170. gCoreThread().queueCommand(&Platform::_coreUpdate);
  171. gCoreThread().submitAccessors();
  172. gCoreThread().queueCommand(std::bind(&RenderTargetManager::updateCore, RenderTargetManager::instancePtr()));
  173. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  174. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  175. gProfilerCPU().endThread();
  176. gProfiler()._update();
  177. }
  178. }
  179. void CoreApplication::update()
  180. {
  181. // Do nothing
  182. }
  183. void CoreApplication::stopMainLoop()
  184. {
  185. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  186. // a race condition we might run the loop one extra iteration which is acceptable
  187. }
  188. void CoreApplication::frameRenderingFinishedCallback()
  189. {
  190. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  191. mIsFrameRenderingFinished = true;
  192. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  193. }
  194. void CoreApplication::beginCoreProfiling()
  195. {
  196. gProfilerCPU().beginThread("Core");
  197. ProfilerGPU::instance().beginFrame();
  198. }
  199. void CoreApplication::endCoreProfiling()
  200. {
  201. ProfilerGPU::instance().endFrame();
  202. ProfilerGPU::instance()._update();
  203. gProfilerCPU().endThread();
  204. gProfiler()._updateCore();
  205. }
  206. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  207. {
  208. String name = pluginName;
  209. #if BS_PLATFORM == BS_PLATFORM_LINUX
  210. if (name.substr(name.length() - 3, 3) != ".so")
  211. name += ".so";
  212. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  213. if (name.substr(name.length() - 6, 6) != ".dylib")
  214. name += ".dylib";
  215. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  216. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  217. // if you include a relative path then it does not. So, add it to be sure.
  218. if (name.substr(name.length() - 4, 4) != ".dll")
  219. name += ".dll";
  220. #endif
  221. DynLib* loadedLibrary = gDynLibManager().load(name);
  222. if(library != nullptr)
  223. *library = loadedLibrary;
  224. void* retVal = nullptr;
  225. if(loadedLibrary != nullptr)
  226. {
  227. if (passThrough == nullptr)
  228. {
  229. typedef void* (*LoadPluginFunc)();
  230. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  231. if (loadPluginFunc != nullptr)
  232. retVal = loadPluginFunc();
  233. }
  234. else
  235. {
  236. typedef void* (*LoadPluginFunc)(void*);
  237. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  238. if (loadPluginFunc != nullptr)
  239. retVal = loadPluginFunc(passThrough);
  240. }
  241. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  242. if (loadPluginFunc != nullptr)
  243. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  244. }
  245. return retVal;
  246. }
  247. void CoreApplication::unloadPlugin(DynLib* library)
  248. {
  249. typedef void (*UnloadPluginFunc)();
  250. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  251. if(unloadPluginFunc != nullptr)
  252. unloadPluginFunc();
  253. gDynLibManager().unload(library);
  254. }
  255. void CoreApplication::shutdownPlugin(DynLib* library)
  256. {
  257. typedef void(*ShutdownPluginFunc)();
  258. ShutdownPluginFunc shutdownPluginFunc = (ShutdownPluginFunc)library->getSymbol("shutdownPlugin");
  259. if (shutdownPluginFunc != nullptr)
  260. shutdownPluginFunc();
  261. mPluginUpdateFunctions.erase(library);
  262. }
  263. CoreApplication& gCoreApplication()
  264. {
  265. return CoreApplication::instance();
  266. }
  267. }