BsCoreApplication.cpp 10 KB

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