BsCoreApplication.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 about to be destroyed (They're only ever destroyed from accessors)
  178. gCoreThread().queueCommand(std::bind(&CoreObjectManager::syncDownload, CoreObjectManager::instancePtr(), CoreObjectSync::Core, gCoreThread().getFrameAlloc()));
  179. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  180. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  181. gProfilerCPU().endThread();
  182. gProfiler()._update();
  183. }
  184. }
  185. void CoreApplication::update()
  186. {
  187. // Do nothing
  188. }
  189. void CoreApplication::stopMainLoop()
  190. {
  191. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  192. // a race condition we might run the loop one extra iteration which is acceptable
  193. }
  194. void CoreApplication::frameRenderingFinishedCallback()
  195. {
  196. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  197. mIsFrameRenderingFinished = true;
  198. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  199. }
  200. void CoreApplication::beginCoreProfiling()
  201. {
  202. gProfilerCPU().beginThread("Core");
  203. ProfilerGPU::instance().beginFrame();
  204. }
  205. void CoreApplication::endCoreProfiling()
  206. {
  207. ProfilerGPU::instance().endFrame();
  208. ProfilerGPU::instance()._update();
  209. gProfilerCPU().endThread();
  210. gProfiler()._updateCore();
  211. }
  212. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  213. {
  214. String name = pluginName;
  215. #if BS_PLATFORM == BS_PLATFORM_LINUX
  216. if (name.substr(name.length() - 3, 3) != ".so")
  217. name += ".so";
  218. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  219. if (name.substr(name.length() - 6, 6) != ".dylib")
  220. name += ".dylib";
  221. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  222. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  223. // if you include a relative path then it does not. So, add it to be sure.
  224. if (name.substr(name.length() - 4, 4) != ".dll")
  225. name += ".dll";
  226. #endif
  227. DynLib* loadedLibrary = gDynLibManager().load(name);
  228. if(library != nullptr)
  229. *library = loadedLibrary;
  230. void* retVal = nullptr;
  231. if(loadedLibrary != nullptr)
  232. {
  233. if (passThrough == nullptr)
  234. {
  235. typedef void* (*LoadPluginFunc)();
  236. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  237. if (loadPluginFunc != nullptr)
  238. retVal = loadPluginFunc();
  239. }
  240. else
  241. {
  242. typedef void* (*LoadPluginFunc)(void*);
  243. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  244. if (loadPluginFunc != nullptr)
  245. retVal = loadPluginFunc(passThrough);
  246. }
  247. UpdatePluginFunc loadPluginFunc = (UpdatePluginFunc)loadedLibrary->getSymbol("updatePlugin");
  248. if (loadPluginFunc != nullptr)
  249. mPluginUpdateFunctions[loadedLibrary] = loadPluginFunc;
  250. }
  251. return retVal;
  252. }
  253. void CoreApplication::unloadPlugin(DynLib* library)
  254. {
  255. typedef void (*UnloadPluginFunc)();
  256. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  257. if(unloadPluginFunc != nullptr)
  258. unloadPluginFunc();
  259. gDynLibManager().unload(library);
  260. }
  261. void CoreApplication::shutdownPlugin(DynLib* library)
  262. {
  263. typedef void(*ShutdownPluginFunc)();
  264. ShutdownPluginFunc shutdownPluginFunc = (ShutdownPluginFunc)library->getSymbol("shutdownPlugin");
  265. if (shutdownPluginFunc != nullptr)
  266. shutdownPluginFunc();
  267. mPluginUpdateFunctions.erase(library);
  268. }
  269. CoreApplication& gCoreApplication()
  270. {
  271. return CoreApplication::instance();
  272. }
  273. }