BsCoreApplication.cpp 10 KB

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