2
0

BsCoreApplication.cpp 10 KB

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