BsCoreApplication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. preUpdate();
  151. PROFILE_CALL(gCoreSceneManager()._update(), "SceneManager");
  152. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  153. gCoreThread().queueCommand(std::bind(&RenderWindowCoreManager::_update, RenderWindowCoreManager::instancePtr()));
  154. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  155. // Update plugins
  156. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  157. pluginUpdateFunc.second();
  158. postUpdate();
  159. // Send out resource events in case any were loaded/destroyed/modified
  160. ResourceListenerManager::instance().update();
  161. gCoreSceneManager()._updateCoreObjectTransforms();
  162. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  163. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  164. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  165. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  166. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  167. {
  168. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  169. while(!mIsFrameRenderingFinished)
  170. {
  171. TaskScheduler::instance().addWorker();
  172. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  173. TaskScheduler::instance().removeWorker();
  174. }
  175. mIsFrameRenderingFinished = false;
  176. }
  177. gCoreThread().queueCommand(&Platform::_coreUpdate);
  178. gCoreThread().update();
  179. gCoreThread().submitAccessors();
  180. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  181. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  182. gProfilerCPU().endThread();
  183. gProfiler()._update();
  184. }
  185. // Wait until last core frame is finished before exiting
  186. {
  187. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  188. while (!mIsFrameRenderingFinished)
  189. {
  190. TaskScheduler::instance().addWorker();
  191. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  192. TaskScheduler::instance().removeWorker();
  193. }
  194. }
  195. }
  196. void CoreApplication::preUpdate()
  197. {
  198. // Do nothing
  199. }
  200. void CoreApplication::postUpdate()
  201. {
  202. // Do nothing
  203. }
  204. void CoreApplication::stopMainLoop()
  205. {
  206. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  207. // a race condition we might run the loop one extra iteration which is acceptable
  208. }
  209. void CoreApplication::frameRenderingFinishedCallback()
  210. {
  211. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  212. mIsFrameRenderingFinished = true;
  213. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  214. }
  215. void CoreApplication::beginCoreProfiling()
  216. {
  217. gProfilerCPU().beginThread("Core");
  218. ProfilerGPU::instance().beginFrame();
  219. }
  220. void CoreApplication::endCoreProfiling()
  221. {
  222. ProfilerGPU::instance().endFrame();
  223. ProfilerGPU::instance()._update();
  224. gProfilerCPU().endThread();
  225. gProfiler()._updateCore();
  226. }
  227. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  228. {
  229. String name = pluginName;
  230. #if BS_PLATFORM == BS_PLATFORM_LINUX
  231. if (name.substr(name.length() - 3, 3) != ".so")
  232. name += ".so";
  233. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  234. if (name.substr(name.length() - 6, 6) != ".dylib")
  235. name += ".dylib";
  236. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  237. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  238. // if you include a relative path then it does not. So, add it to be sure.
  239. if (name.substr(name.length() - 4, 4) != ".dll")
  240. name += ".dll";
  241. #endif
  242. DynLib* loadedLibrary = gDynLibManager().load(name);
  243. if(library != nullptr)
  244. *library = loadedLibrary;
  245. void* retVal = nullptr;
  246. if(loadedLibrary != nullptr)
  247. {
  248. if (passThrough == nullptr)
  249. {
  250. typedef void* (*LoadPluginFunc)();
  251. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  252. if (loadPluginFunc != nullptr)
  253. retVal = loadPluginFunc();
  254. }
  255. else
  256. {
  257. typedef void* (*LoadPluginFunc)(void*);
  258. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  259. if (loadPluginFunc != nullptr)
  260. retVal = loadPluginFunc(passThrough);
  261. }
  262. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  263. if (loadPluginFunc != nullptr)
  264. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  265. }
  266. return retVal;
  267. }
  268. void CoreApplication::unloadPlugin(DynLib* library)
  269. {
  270. typedef void (*UnloadPluginFunc)();
  271. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  272. if(unloadPluginFunc != nullptr)
  273. unloadPluginFunc();
  274. mPluginUpdateFunctions.erase(library);
  275. gDynLibManager().unload(library);
  276. }
  277. ShaderIncludeHandlerPtr CoreApplication::getShaderIncludeHandler() const
  278. {
  279. return bs_shared_ptr_new<DefaultShaderIncludeHandler>();
  280. }
  281. CoreApplication& gCoreApplication()
  282. {
  283. return CoreApplication::instance();
  284. }
  285. }