BsCoreApplication.cpp 10 KB

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