BsCoreApplication.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "BsMaterial.h"
  39. #include "BsShader.h"
  40. #include "BsTechnique.h"
  41. #include "BsPass.h"
  42. #include "BsRendererManager.h"
  43. namespace BansheeEngine
  44. {
  45. CoreApplication::CoreApplication(START_UP_DESC& desc)
  46. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false), mSceneManagerPlugin(nullptr)
  47. {
  48. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  49. Platform::_startUp();
  50. MemStack::beginThread();
  51. UUIDGenerator::startUp();
  52. ProfilerCPU::startUp();
  53. ProfilingManager::startUp();
  54. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  55. TaskScheduler::startUp();
  56. TaskScheduler::instance().removeWorker();
  57. CoreThread::startUp();
  58. StringTable::startUp();
  59. DeferredCallManager::startUp();
  60. Time::startUp();
  61. DynLibManager::startUp();
  62. CoreObjectManager::startUp();
  63. GameObjectManager::startUp();
  64. Resources::startUp();
  65. GpuProgramManager::startUp();
  66. RenderSystemManager::startUp();
  67. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  68. Input::startUp();
  69. RendererManager::startUp();
  70. loadPlugin(desc.renderer);
  71. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  72. RendererManager::instance().setActive(desc.renderer);
  73. ProfilerGPU::startUp();
  74. MeshManager::startUp();
  75. MaterialManager::startUp();
  76. FontManager::startUp();
  77. Importer::startUp();
  78. for (auto& importerName : desc.importers)
  79. loadPlugin(importerName);
  80. loadPlugin(desc.input, nullptr, mPrimaryWindow.get());
  81. }
  82. CoreApplication::~CoreApplication()
  83. {
  84. mPrimaryWindow->destroy();
  85. mPrimaryWindow = nullptr;
  86. Importer::shutDown();
  87. FontManager::shutDown();
  88. MaterialManager::shutDown();
  89. MeshManager::shutDown();
  90. ProfilerGPU::shutDown();
  91. unloadPlugin(mSceneManagerPlugin);
  92. RendererManager::shutDown();
  93. RenderSystemManager::shutDown();
  94. Input::shutDown();
  95. GpuProgramManager::shutDown();
  96. Resources::shutDown();
  97. GameObjectManager::shutDown();
  98. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  99. DynLibManager::shutDown();
  100. Time::shutDown();
  101. DeferredCallManager::shutDown();
  102. StringTable::shutDown();
  103. CoreThread::shutDown();
  104. TaskScheduler::shutDown();
  105. ThreadPool::shutDown();
  106. ProfilingManager::shutDown();
  107. ProfilerCPU::shutDown();
  108. UUIDGenerator::shutDown();
  109. MemStack::endThread();
  110. Platform::_shutDown();
  111. }
  112. void CoreApplication::runMainLoop()
  113. {
  114. mRunMainLoop = true;
  115. while(mRunMainLoop)
  116. {
  117. gProfilerCPU().beginThread("Sim");
  118. gCoreThread().update();
  119. Platform::_update();
  120. DeferredCallManager::instance()._update();
  121. RenderWindowManager::instance()._update();
  122. gInput()._update();
  123. gTime().update();
  124. PROFILE_CALL(gSceneManager()._update(), "SceneManager");
  125. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  126. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  127. update();
  128. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  129. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  130. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  131. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  132. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  133. {
  134. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  135. while(!mIsFrameRenderingFinished)
  136. {
  137. TaskScheduler::instance().addWorker();
  138. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  139. TaskScheduler::instance().removeWorker();
  140. }
  141. mIsFrameRenderingFinished = false;
  142. }
  143. gCoreThread().queueCommand(&Platform::_coreUpdate);
  144. gCoreThread().submitAccessors();
  145. gCoreThread().queueCommand(std::bind(&ProfilerGPU::_update, ProfilerGPU::instancePtr()));
  146. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  147. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  148. gProfilerCPU().endThread();
  149. gProfiler()._update();
  150. }
  151. }
  152. void CoreApplication::update()
  153. {
  154. // Do nothing
  155. }
  156. void CoreApplication::stopMainLoop()
  157. {
  158. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  159. // a race condition we might run the loop one extra iteration which is acceptable
  160. }
  161. void CoreApplication::frameRenderingFinishedCallback()
  162. {
  163. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  164. mIsFrameRenderingFinished = true;
  165. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  166. }
  167. void CoreApplication::beginCoreProfiling()
  168. {
  169. gProfilerCPU().beginThread("Core");
  170. }
  171. void CoreApplication::endCoreProfiling()
  172. {
  173. gProfilerCPU().endThread();
  174. gProfiler()._updateCore();
  175. }
  176. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  177. {
  178. String name = pluginName;
  179. #if BS_PLATFORM == BS_PLATFORM_LINUX
  180. // dlopen() does not add .so to the filename, like windows does for .dll
  181. if (name.substr(name.length() - 3, 3) != ".so")
  182. name += ".so";
  183. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  184. // dlopen() does not add .dylib to the filename, like windows does for .dll
  185. if (name.substr(name.length() - 6, 6) != ".dylib")
  186. name += ".dylib";
  187. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  188. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  189. // if you include a relative path then it does not. So, add it to be sure.
  190. if (name.substr(name.length() - 4, 4) != ".dll")
  191. name += ".dll";
  192. #endif
  193. DynLib* loadedLibrary = gDynLibManager().load(name);
  194. if(library != nullptr)
  195. *library = loadedLibrary;
  196. if(loadedLibrary != nullptr)
  197. {
  198. if (passThrough == nullptr)
  199. {
  200. typedef void* (*LoadPluginFunc)();
  201. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  202. if (loadPluginFunc != nullptr)
  203. return loadPluginFunc();
  204. }
  205. else
  206. {
  207. typedef void* (*LoadPluginFunc)(void*);
  208. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  209. if (loadPluginFunc != nullptr)
  210. return loadPluginFunc(passThrough);
  211. }
  212. }
  213. return nullptr;
  214. }
  215. void CoreApplication::unloadPlugin(DynLib* library)
  216. {
  217. typedef void (*UnloadPluginFunc)();
  218. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  219. if(unloadPluginFunc != nullptr)
  220. unloadPluginFunc();
  221. gDynLibManager().unload(library);
  222. }
  223. CoreApplication& gCoreApplication()
  224. {
  225. return CoreApplication::instance();
  226. }
  227. }