BsCoreApplication.cpp 9.8 KB

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