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