BsCoreApplication.cpp 7.6 KB

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