BsCoreApplication.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #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), mStartUpDesc(desc)
  57. { }
  58. CoreApplication::~CoreApplication()
  59. {
  60. mPrimaryWindow->destroy();
  61. mPrimaryWindow = nullptr;
  62. Importer::shutDown();
  63. FontManager::shutDown();
  64. MaterialManager::shutDown();
  65. MeshManager::shutDown();
  66. ProfilerGPU::shutDown();
  67. CoreSceneManager::shutDown();
  68. Input::shutDown();
  69. Resources::shutDown();
  70. ResourceListenerManager::shutDown();
  71. GameObjectManager::shutDown();
  72. RenderStateManager::shutDown();
  73. RendererManager::shutDown();
  74. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  75. // we need to wait for those objects to get destroyed before continuing.
  76. gCoreThread().update();
  77. gCoreThread().submitAccessors(true);
  78. unloadPlugin(mRendererPlugin);
  79. RenderAPIManager::shutDown();
  80. GpuProgramCoreManager::shutDown();
  81. GpuProgramManager::shutDown();
  82. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  83. DynLibManager::shutDown();
  84. Time::shutDown();
  85. DeferredCallManager::shutDown();
  86. StringTableManager::shutDown();
  87. CoreThread::shutDown();
  88. RenderStats::shutDown();
  89. TaskScheduler::shutDown();
  90. ThreadPool::shutDown();
  91. ProfilingManager::shutDown();
  92. ProfilerCPU::shutDown();
  93. MessageHandler::shutDown();
  94. ShaderManager::shutDown();
  95. MemStack::endThread();
  96. Platform::_shutDown();
  97. }
  98. void CoreApplication::onStartUp()
  99. {
  100. signal(SIGABRT, handleAbort);
  101. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  102. Platform::_startUp();
  103. MemStack::beginThread();
  104. ShaderManager::startUp(getShaderIncludeHandler());
  105. MessageHandler::startUp();
  106. ProfilerCPU::startUp();
  107. ProfilingManager::startUp();
  108. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  109. TaskScheduler::startUp();
  110. TaskScheduler::instance().removeWorker();
  111. RenderStats::startUp();
  112. CoreThread::startUp();
  113. StringTableManager::startUp();
  114. DeferredCallManager::startUp();
  115. Time::startUp();
  116. DynLibManager::startUp();
  117. CoreObjectManager::startUp();
  118. GameObjectManager::startUp();
  119. Resources::startUp();
  120. ResourceListenerManager::startUp();
  121. GpuProgramManager::startUp();
  122. RenderStateManager::startUp();
  123. GpuProgramCoreManager::startUp();
  124. RenderAPIManager::startUp();
  125. mPrimaryWindow = RenderAPIManager::instance().initialize(mStartUpDesc.renderAPI, mStartUpDesc.primaryWindowDesc);
  126. Input::startUp();
  127. RendererManager::startUp();
  128. loadPlugin(mStartUpDesc.renderer, &mRendererPlugin);
  129. SceneManagerFactory::create();
  130. RendererManager::instance().setActive(mStartUpDesc.renderer);
  131. ProfilerGPU::startUp();
  132. MeshManager::startUp();
  133. MaterialManager::startUp();
  134. FontManager::startUp();
  135. Importer::startUp();
  136. for (auto& importerName : mStartUpDesc.importers)
  137. loadPlugin(importerName);
  138. loadPlugin(mStartUpDesc.input, nullptr, mPrimaryWindow.get());
  139. }
  140. void CoreApplication::runMainLoop()
  141. {
  142. mRunMainLoop = true;
  143. while(mRunMainLoop)
  144. {
  145. gProfilerCPU().beginThread("Sim");
  146. Platform::_update();
  147. DeferredCallManager::instance()._update();
  148. gTime().update();
  149. gInput()._update();
  150. // RenderWindowManager::update needs to happen after Input::update and before Input::_triggerCallbacks,
  151. // so that all input is properly captured in case there is a focus change, and so that
  152. // focus change is registered before input events are sent out (mouse press can result in code
  153. // checking if a window is in focus, so it has to be up to date)
  154. RenderWindowManager::instance()._update();
  155. gInput()._triggerCallbacks();
  156. preUpdate();
  157. PROFILE_CALL(gCoreSceneManager()._update(), "SceneManager");
  158. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  159. gCoreThread().queueCommand(std::bind(&RenderWindowCoreManager::_update, RenderWindowCoreManager::instancePtr()));
  160. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  161. // Update plugins
  162. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  163. pluginUpdateFunc.second();
  164. postUpdate();
  165. // Send out resource events in case any were loaded/destroyed/modified
  166. ResourceListenerManager::instance().update();
  167. gCoreSceneManager()._updateCoreObjectTransforms();
  168. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  169. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  170. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  171. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  172. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  173. {
  174. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  175. while(!mIsFrameRenderingFinished)
  176. {
  177. TaskScheduler::instance().addWorker();
  178. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  179. TaskScheduler::instance().removeWorker();
  180. }
  181. mIsFrameRenderingFinished = false;
  182. }
  183. gCoreThread().queueCommand(&Platform::_coreUpdate);
  184. gCoreThread().update();
  185. gCoreThread().submitAccessors();
  186. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  187. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  188. gProfilerCPU().endThread();
  189. gProfiler()._update();
  190. }
  191. // Wait until last core frame is finished before exiting
  192. {
  193. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  194. while (!mIsFrameRenderingFinished)
  195. {
  196. TaskScheduler::instance().addWorker();
  197. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  198. TaskScheduler::instance().removeWorker();
  199. }
  200. }
  201. }
  202. void CoreApplication::preUpdate()
  203. {
  204. // Do nothing
  205. }
  206. void CoreApplication::postUpdate()
  207. {
  208. // Do nothing
  209. }
  210. void CoreApplication::stopMainLoop()
  211. {
  212. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  213. // a race condition we might run the loop one extra iteration which is acceptable
  214. }
  215. void CoreApplication::frameRenderingFinishedCallback()
  216. {
  217. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  218. mIsFrameRenderingFinished = true;
  219. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  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. }