BsCoreApplication.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "BsPath.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)
  58. {
  59. signal(SIGABRT, handleAbort);
  60. Vector<Path> outPaths;
  61. Platform::openBrowseDialog((FileDialogType)((UINT32)FileDialogType::OpenFile | (UINT32)FileDialogType::Multiselect), "D:\\Downloads", L"*.exe", outPaths);
  62. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  63. Platform::_startUp();
  64. MemStack::beginThread();
  65. MessageHandler::startUp();
  66. UUIDGenerator::startUp();
  67. ProfilerCPU::startUp();
  68. ProfilingManager::startUp();
  69. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  70. TaskScheduler::startUp();
  71. TaskScheduler::instance().removeWorker();
  72. RenderStats::startUp();
  73. CoreThread::startUp();
  74. StringTable::startUp();
  75. DeferredCallManager::startUp();
  76. Time::startUp();
  77. DynLibManager::startUp();
  78. CoreObjectManager::startUp();
  79. GameObjectManager::startUp();
  80. Resources::startUp();
  81. ResourceListenerManager::startUp();
  82. GpuProgramManager::startUp();
  83. RenderStateManager::startUp();
  84. GpuProgramCoreManager::startUp();
  85. RenderAPIManager::startUp();
  86. mPrimaryWindow = RenderAPIManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  87. Input::startUp();
  88. RendererManager::startUp();
  89. loadPlugin(desc.renderer, &mRendererPlugin);
  90. SceneManagerFactory::create();
  91. RendererManager::instance().setActive(desc.renderer);
  92. ProfilerGPU::startUp();
  93. MeshManager::startUp();
  94. MaterialManager::startUp();
  95. FontManager::startUp();
  96. Importer::startUp();
  97. for (auto& importerName : desc.importers)
  98. loadPlugin(importerName);
  99. loadPlugin(desc.input, nullptr, mPrimaryWindow.get());
  100. }
  101. CoreApplication::~CoreApplication()
  102. {
  103. mPrimaryWindow->destroy();
  104. mPrimaryWindow = nullptr;
  105. Importer::shutDown();
  106. FontManager::shutDown();
  107. MaterialManager::shutDown();
  108. MeshManager::shutDown();
  109. ProfilerGPU::shutDown();
  110. CoreSceneManager::shutDown();
  111. Input::shutDown();
  112. Resources::shutDown();
  113. ResourceListenerManager::shutDown();
  114. GameObjectManager::shutDown();
  115. RenderStateManager::shutDown();
  116. RendererManager::shutDown();
  117. shutdownPlugin(mRendererPlugin);
  118. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  119. // we need to wait for those objects to get destroyed before continuing.
  120. gCoreThread().update();
  121. gCoreThread().submitAccessors(true);
  122. unloadPlugin(mRendererPlugin);
  123. RenderAPIManager::shutDown();
  124. GpuProgramCoreManager::shutDown();
  125. GpuProgramManager::shutDown();
  126. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  127. DynLibManager::shutDown();
  128. Time::shutDown();
  129. DeferredCallManager::shutDown();
  130. StringTable::shutDown();
  131. CoreThread::shutDown();
  132. RenderStats::shutDown();
  133. TaskScheduler::shutDown();
  134. ThreadPool::shutDown();
  135. ProfilingManager::shutDown();
  136. ProfilerCPU::shutDown();
  137. UUIDGenerator::shutDown();
  138. MessageHandler::shutDown();
  139. MemStack::endThread();
  140. Platform::_shutDown();
  141. }
  142. void CoreApplication::runMainLoop()
  143. {
  144. mRunMainLoop = true;
  145. while(mRunMainLoop)
  146. {
  147. gProfilerCPU().beginThread("Sim");
  148. Platform::_update();
  149. DeferredCallManager::instance()._update();
  150. RenderWindowManager::instance()._update();
  151. gTime().update();
  152. gInput()._update();
  153. PROFILE_CALL(gCoreSceneManager()._update(), "SceneManager");
  154. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  155. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  156. // Update plugins
  157. for (auto& pluginUpdateFunc : mPluginUpdateFunctions)
  158. pluginUpdateFunc.second();
  159. update();
  160. // Send out resource events in case any were loaded/destroyed/modified
  161. ResourceListenerManager::instance().update();
  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::update()
  197. {
  198. // Do nothing
  199. }
  200. void CoreApplication::stopMainLoop()
  201. {
  202. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  203. // a race condition we might run the loop one extra iteration which is acceptable
  204. }
  205. void CoreApplication::frameRenderingFinishedCallback()
  206. {
  207. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  208. mIsFrameRenderingFinished = true;
  209. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  210. }
  211. void CoreApplication::beginCoreProfiling()
  212. {
  213. gProfilerCPU().beginThread("Core");
  214. ProfilerGPU::instance().beginFrame();
  215. }
  216. void CoreApplication::endCoreProfiling()
  217. {
  218. ProfilerGPU::instance().endFrame();
  219. ProfilerGPU::instance()._update();
  220. gProfilerCPU().endThread();
  221. gProfiler()._updateCore();
  222. }
  223. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  224. {
  225. String name = pluginName;
  226. #if BS_PLATFORM == BS_PLATFORM_LINUX
  227. if (name.substr(name.length() - 3, 3) != ".so")
  228. name += ".so";
  229. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  230. if (name.substr(name.length() - 6, 6) != ".dylib")
  231. name += ".dylib";
  232. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  233. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  234. // if you include a relative path then it does not. So, add it to be sure.
  235. if (name.substr(name.length() - 4, 4) != ".dll")
  236. name += ".dll";
  237. #endif
  238. DynLib* loadedLibrary = gDynLibManager().load(name);
  239. if(library != nullptr)
  240. *library = loadedLibrary;
  241. void* retVal = nullptr;
  242. if(loadedLibrary != nullptr)
  243. {
  244. if (passThrough == nullptr)
  245. {
  246. typedef void* (*LoadPluginFunc)();
  247. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  248. if (loadPluginFunc != nullptr)
  249. retVal = loadPluginFunc();
  250. }
  251. else
  252. {
  253. typedef void* (*LoadPluginFunc)(void*);
  254. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  255. if (loadPluginFunc != nullptr)
  256. retVal = loadPluginFunc(passThrough);
  257. }
  258. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  259. if (loadPluginFunc != nullptr)
  260. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  261. }
  262. return retVal;
  263. }
  264. void CoreApplication::unloadPlugin(DynLib* library)
  265. {
  266. typedef void (*UnloadPluginFunc)();
  267. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  268. if(unloadPluginFunc != nullptr)
  269. unloadPluginFunc();
  270. gDynLibManager().unload(library);
  271. }
  272. void CoreApplication::shutdownPlugin(DynLib* library)
  273. {
  274. typedef void(*ShutdownPluginFunc)();
  275. ShutdownPluginFunc shutdownPluginFunc = (ShutdownPluginFunc)library->getSymbol("shutdownPlugin");
  276. if (shutdownPluginFunc != nullptr)
  277. shutdownPluginFunc();
  278. mPluginUpdateFunctions.erase(library);
  279. }
  280. CoreApplication& gCoreApplication()
  281. {
  282. return CoreApplication::instance();
  283. }
  284. }