BsCoreApplication.cpp 9.9 KB

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