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