BsCoreApplication.cpp 8.7 KB

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