BsCoreApplication.cpp 10 KB

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