BsCoreApplication.cpp 9.5 KB

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