BsCoreApplication.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "BsStringTableManager.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 "BsRenderStats.h"
  38. #include "BsMessageHandler.h"
  39. #include "BsResourceListenerManager.h"
  40. #include "BsRenderStateManager.h"
  41. #include "BsShaderManager.h"
  42. namespace BansheeEngine
  43. {
  44. CoreApplication::CoreApplication(START_UP_DESC desc)
  45. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false),
  46. mRendererPlugin(nullptr), mSimThreadId(BS_THREAD_CURRENT_ID), mStartUpDesc(desc)
  47. { }
  48. CoreApplication::~CoreApplication()
  49. {
  50. mPrimaryWindow->destroy();
  51. mPrimaryWindow = nullptr;
  52. Importer::shutDown();
  53. FontManager::shutDown();
  54. MaterialManager::shutDown();
  55. MeshManager::shutDown();
  56. ProfilerGPU::shutDown();
  57. CoreSceneManager::shutDown();
  58. Input::shutDown();
  59. StringTableManager::shutDown();
  60. Resources::shutDown();
  61. ResourceListenerManager::shutDown();
  62. GameObjectManager::shutDown();
  63. RenderStateManager::shutDown();
  64. RendererManager::shutDown();
  65. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  66. // we need to wait for those objects to get destroyed before continuing.
  67. CoreObjectManager::instance().clearDirty();
  68. gCoreThread().update();
  69. gCoreThread().submitAccessors(true);
  70. unloadPlugin(mRendererPlugin);
  71. RenderAPIManager::shutDown();
  72. GpuProgramCoreManager::shutDown();
  73. GpuProgramManager::shutDown();
  74. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  75. DynLibManager::shutDown();
  76. Time::shutDown();
  77. DeferredCallManager::shutDown();
  78. CoreThread::shutDown();
  79. RenderStats::shutDown();
  80. TaskScheduler::shutDown();
  81. ThreadPool::shutDown();
  82. ProfilingManager::shutDown();
  83. ProfilerCPU::shutDown();
  84. MessageHandler::shutDown();
  85. ShaderManager::shutDown();
  86. MemStack::endThread();
  87. Platform::_shutDown();
  88. }
  89. void CoreApplication::onStartUp()
  90. {
  91. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  92. Platform::_startUp();
  93. MemStack::beginThread();
  94. ShaderManager::startUp(getShaderIncludeHandler());
  95. MessageHandler::startUp();
  96. ProfilerCPU::startUp();
  97. ProfilingManager::startUp();
  98. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  99. TaskScheduler::startUp();
  100. TaskScheduler::instance().removeWorker();
  101. RenderStats::startUp();
  102. CoreThread::startUp();
  103. StringTableManager::startUp();
  104. DeferredCallManager::startUp();
  105. Time::startUp();
  106. DynLibManager::startUp();
  107. CoreObjectManager::startUp();
  108. GameObjectManager::startUp();
  109. Resources::startUp();
  110. ResourceListenerManager::startUp();
  111. GpuProgramManager::startUp();
  112. RenderStateManager::startUp();
  113. GpuProgramCoreManager::startUp();
  114. RenderAPIManager::startUp();
  115. mPrimaryWindow = RenderAPIManager::instance().initialize(mStartUpDesc.renderAPI, mStartUpDesc.primaryWindowDesc);
  116. Input::startUp();
  117. RendererManager::startUp();
  118. loadPlugin(mStartUpDesc.renderer, &mRendererPlugin);
  119. SceneManagerFactory::create();
  120. RendererManager::instance().setActive(mStartUpDesc.renderer);
  121. startUpRenderer();
  122. ProfilerGPU::startUp();
  123. MeshManager::startUp();
  124. MaterialManager::startUp();
  125. FontManager::startUp();
  126. Importer::startUp();
  127. for (auto& importerName : mStartUpDesc.importers)
  128. loadPlugin(importerName);
  129. loadPlugin(mStartUpDesc.input, nullptr, mPrimaryWindow.get());
  130. }
  131. void CoreApplication::runMainLoop()
  132. {
  133. mRunMainLoop = true;
  134. while(mRunMainLoop)
  135. {
  136. gProfilerCPU().beginThread("Sim");
  137. Platform::_update();
  138. DeferredCallManager::instance()._update();
  139. gTime().update();
  140. gInput()._update();
  141. // RenderWindowManager::update needs to happen after Input::update and before Input::_triggerCallbacks,
  142. // so that all input is properly captured in case there is a focus change, and so that
  143. // focus change is registered before input events are sent out (mouse press can result in code
  144. // checking if a window is in focus, so it has to be up to date)
  145. RenderWindowManager::instance()._update();
  146. gInput()._triggerCallbacks();
  147. gDebug()._triggerCallbacks();
  148. preUpdate();
  149. PROFILE_CALL(gCoreSceneManager()._update(), "SceneManager");
  150. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  151. gCoreThread().queueCommand(std::bind(&RenderWindowCoreManager::_update, RenderWindowCoreManager::instancePtr()));
  152. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  153. // Update plugins
  154. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  155. pluginUpdateFunc.second();
  156. postUpdate();
  157. // Send out resource events in case any were loaded/destroyed/modified
  158. ResourceListenerManager::instance().update();
  159. gCoreSceneManager()._updateCoreObjectTransforms();
  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::preUpdate()
  195. {
  196. // Do nothing
  197. }
  198. void CoreApplication::postUpdate()
  199. {
  200. // Do nothing
  201. }
  202. void CoreApplication::stopMainLoop()
  203. {
  204. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  205. // a race condition we might run the loop one extra iteration which is acceptable
  206. }
  207. void CoreApplication::quitRequested()
  208. {
  209. stopMainLoop();
  210. }
  211. void CoreApplication::frameRenderingFinishedCallback()
  212. {
  213. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  214. mIsFrameRenderingFinished = true;
  215. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  216. }
  217. void CoreApplication::startUpRenderer()
  218. {
  219. RendererManager::instance().initialize();
  220. }
  221. void CoreApplication::beginCoreProfiling()
  222. {
  223. gProfilerCPU().beginThread("Core");
  224. ProfilerGPU::instance().beginFrame();
  225. }
  226. void CoreApplication::endCoreProfiling()
  227. {
  228. ProfilerGPU::instance().endFrame();
  229. ProfilerGPU::instance()._update();
  230. gProfilerCPU().endThread();
  231. gProfiler()._updateCore();
  232. }
  233. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  234. {
  235. String name = pluginName;
  236. #if BS_PLATFORM == BS_PLATFORM_LINUX
  237. if (name.substr(name.length() - 3, 3) != ".so")
  238. name += ".so";
  239. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  240. if (name.substr(name.length() - 6, 6) != ".dylib")
  241. name += ".dylib";
  242. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  243. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  244. // if you include a relative path then it does not. So, add it to be sure.
  245. if (name.substr(name.length() - 4, 4) != ".dll")
  246. name += ".dll";
  247. #endif
  248. DynLib* loadedLibrary = gDynLibManager().load(name);
  249. if(library != nullptr)
  250. *library = loadedLibrary;
  251. void* retVal = nullptr;
  252. if(loadedLibrary != nullptr)
  253. {
  254. if (passThrough == nullptr)
  255. {
  256. typedef void* (*LoadPluginFunc)();
  257. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  258. if (loadPluginFunc != nullptr)
  259. retVal = loadPluginFunc();
  260. }
  261. else
  262. {
  263. typedef void* (*LoadPluginFunc)(void*);
  264. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  265. if (loadPluginFunc != nullptr)
  266. retVal = loadPluginFunc(passThrough);
  267. }
  268. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  269. if (loadPluginFunc != nullptr)
  270. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  271. }
  272. return retVal;
  273. }
  274. void CoreApplication::unloadPlugin(DynLib* library)
  275. {
  276. typedef void (*UnloadPluginFunc)();
  277. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  278. if(unloadPluginFunc != nullptr)
  279. unloadPluginFunc();
  280. mPluginUpdateFunctions.erase(library);
  281. gDynLibManager().unload(library);
  282. }
  283. ShaderIncludeHandlerPtr CoreApplication::getShaderIncludeHandler() const
  284. {
  285. return bs_shared_ptr_new<DefaultShaderIncludeHandler>();
  286. }
  287. CoreApplication& gCoreApplication()
  288. {
  289. return CoreApplication::instance();
  290. }
  291. }