BsCoreApplication.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsCoreApplication.h"
  5. #include "BsRenderSystem.h"
  6. #include "BsRenderSystemManager.h"
  7. #include "BsPlatform.h"
  8. #include "BsHardwareBufferManager.h"
  9. #include "BsRenderWindow.h"
  10. #include "BsViewport.h"
  11. #include "BsVector2.h"
  12. #include "BsGpuProgram.h"
  13. #include "BsCoreObjectManager.h"
  14. #include "BsGameObjectManager.h"
  15. #include "BsDynLib.h"
  16. #include "BsDynLibManager.h"
  17. #include "BsCoreSceneManager.h"
  18. #include "BsImporter.h"
  19. #include "BsResources.h"
  20. #include "BsMesh.h"
  21. #include "BsSceneObject.h"
  22. #include "BsTime.h"
  23. #include "BsInput.h"
  24. #include "BsRendererManager.h"
  25. #include "BsGpuProgramManager.h"
  26. #include "BsMeshManager.h"
  27. #include "BsMaterialManager.h"
  28. #include "BsFontManager.h"
  29. #include "BsRenderWindowManager.h"
  30. #include "BsRenderer.h"
  31. #include "BsDeferredCallManager.h"
  32. #include "BsCoreThread.h"
  33. #include "BsStringTable.h"
  34. #include "BsProfilingManager.h"
  35. #include "BsProfilerCPU.h"
  36. #include "BsProfilerGPU.h"
  37. #include "BsQueryManager.h"
  38. #include "BsThreadPool.h"
  39. #include "BsTaskScheduler.h"
  40. #include "BsUUID.h"
  41. #include "BsRenderStats.h"
  42. #include "BsMaterial.h"
  43. #include "BsShader.h"
  44. #include "BsTechnique.h"
  45. #include "BsPass.h"
  46. #include "BsRendererManager.h"
  47. namespace BansheeEngine
  48. {
  49. CoreApplication::CoreApplication(START_UP_DESC& desc)
  50. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false),
  51. mSceneManagerPlugin(nullptr), mRendererPlugin(nullptr)
  52. {
  53. UINT32 numWorkerThreads = BS_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  54. Platform::_startUp();
  55. MemStack::beginThread();
  56. UUIDGenerator::startUp();
  57. ProfilerCPU::startUp();
  58. ProfilingManager::startUp();
  59. ThreadPool::startUp<TThreadPool<ThreadBansheePolicy>>((numWorkerThreads));
  60. TaskScheduler::startUp();
  61. TaskScheduler::instance().removeWorker();
  62. RenderStats::startUp();
  63. CoreThread::startUp();
  64. StringTable::startUp();
  65. DeferredCallManager::startUp();
  66. Time::startUp();
  67. DynLibManager::startUp();
  68. CoreObjectManager::startUp();
  69. GameObjectManager::startUp();
  70. Resources::startUp();
  71. GpuProgramManager::startUp();
  72. RenderSystemManager::startUp();
  73. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  74. Input::startUp();
  75. RendererManager::startUp();
  76. loadPlugin(desc.renderer, &mRendererPlugin);
  77. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  78. RendererManager::instance().setActive(desc.renderer);
  79. ProfilerGPU::startUp();
  80. MeshManager::startUp();
  81. MaterialManager::startUp();
  82. FontManager::startUp();
  83. Importer::startUp();
  84. for (auto& importerName : desc.importers)
  85. loadPlugin(importerName);
  86. loadPlugin(desc.input, nullptr, mPrimaryWindow.get());
  87. }
  88. CoreApplication::~CoreApplication()
  89. {
  90. mPrimaryWindow->destroy();
  91. mPrimaryWindow = nullptr;
  92. Importer::shutDown();
  93. FontManager::shutDown();
  94. MaterialManager::shutDown();
  95. MeshManager::shutDown();
  96. ProfilerGPU::shutDown();
  97. unloadPlugin(mSceneManagerPlugin);
  98. RendererManager::shutDown();
  99. RenderSystemManager::shutDown();
  100. unloadPlugin(mRendererPlugin);
  101. Input::shutDown();
  102. GpuProgramManager::shutDown();
  103. Resources::shutDown();
  104. GameObjectManager::shutDown();
  105. // All CoreObject related modules should be shut down now. They have likely queued CoreObjects for destruction, so
  106. // we need to wait for those objects to get destroyed before continuing.
  107. gCoreThread().update();
  108. gCoreThread().submitAccessors(true);
  109. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  110. DynLibManager::shutDown();
  111. Time::shutDown();
  112. DeferredCallManager::shutDown();
  113. StringTable::shutDown();
  114. CoreThread::shutDown();
  115. RenderStats::shutDown();
  116. TaskScheduler::shutDown();
  117. ThreadPool::shutDown();
  118. ProfilingManager::shutDown();
  119. ProfilerCPU::shutDown();
  120. UUIDGenerator::shutDown();
  121. MemStack::endThread();
  122. Platform::_shutDown();
  123. }
  124. void CoreApplication::runMainLoop()
  125. {
  126. mRunMainLoop = true;
  127. while(mRunMainLoop)
  128. {
  129. gProfilerCPU().beginThread("Sim");
  130. gCoreThread().update();
  131. Platform::_update();
  132. DeferredCallManager::instance()._update();
  133. RenderWindowManager::instance()._update();
  134. gInput()._update();
  135. gTime().update();
  136. PROFILE_CALL(gSceneManager()._update(), "SceneManager");
  137. gCoreThread().queueCommand(std::bind(&CoreApplication::beginCoreProfiling, this));
  138. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  139. update();
  140. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  141. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  142. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  143. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  144. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  145. {
  146. BS_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  147. while(!mIsFrameRenderingFinished)
  148. {
  149. TaskScheduler::instance().addWorker();
  150. BS_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  151. TaskScheduler::instance().removeWorker();
  152. }
  153. mIsFrameRenderingFinished = false;
  154. }
  155. gCoreThread().queueCommand(&Platform::_coreUpdate);
  156. gCoreThread().submitAccessors();
  157. gCoreThread().queueCommand(std::bind(&CoreApplication::endCoreProfiling, this));
  158. gCoreThread().queueCommand(std::bind(&CoreApplication::frameRenderingFinishedCallback, this));
  159. gProfilerCPU().endThread();
  160. gProfiler()._update();
  161. }
  162. }
  163. void CoreApplication::update()
  164. {
  165. // Do nothing
  166. }
  167. void CoreApplication::stopMainLoop()
  168. {
  169. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  170. // a race condition we might run the loop one extra iteration which is acceptable
  171. }
  172. void CoreApplication::frameRenderingFinishedCallback()
  173. {
  174. BS_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  175. mIsFrameRenderingFinished = true;
  176. BS_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  177. }
  178. void CoreApplication::beginCoreProfiling()
  179. {
  180. gProfilerCPU().beginThread("Core");
  181. ProfilerGPU::instance().beginFrame();
  182. }
  183. void CoreApplication::endCoreProfiling()
  184. {
  185. ProfilerGPU::instance().endFrame();
  186. ProfilerGPU::instance()._update();
  187. gProfilerCPU().endThread();
  188. gProfiler()._updateCore();
  189. }
  190. void* CoreApplication::loadPlugin(const String& pluginName, DynLib** library, void* passThrough)
  191. {
  192. String name = pluginName;
  193. #if BS_PLATFORM == BS_PLATFORM_LINUX
  194. if (name.substr(name.length() - 3, 3) != ".so")
  195. name += ".so";
  196. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  197. if (name.substr(name.length() - 6, 6) != ".dylib")
  198. name += ".dylib";
  199. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  200. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  201. // if you include a relative path then it does not. So, add it to be sure.
  202. if (name.substr(name.length() - 4, 4) != ".dll")
  203. name += ".dll";
  204. #endif
  205. DynLib* loadedLibrary = gDynLibManager().load(name);
  206. if(library != nullptr)
  207. *library = loadedLibrary;
  208. if(loadedLibrary != nullptr)
  209. {
  210. if (passThrough == nullptr)
  211. {
  212. typedef void* (*LoadPluginFunc)();
  213. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  214. if (loadPluginFunc != nullptr)
  215. return loadPluginFunc();
  216. }
  217. else
  218. {
  219. typedef void* (*LoadPluginFunc)(void*);
  220. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  221. if (loadPluginFunc != nullptr)
  222. return loadPluginFunc(passThrough);
  223. }
  224. }
  225. return nullptr;
  226. }
  227. void CoreApplication::unloadPlugin(DynLib* library)
  228. {
  229. typedef void (*UnloadPluginFunc)();
  230. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  231. if(unloadPluginFunc != nullptr)
  232. unloadPluginFunc();
  233. gDynLibManager().unload(library);
  234. }
  235. CoreApplication& gCoreApplication()
  236. {
  237. return CoreApplication::instance();
  238. }
  239. }