CmApplication.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "CmApplication.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmPlatform.h"
  5. #include "CmHardwareBufferManager.h"
  6. #include "CmRenderWindow.h"
  7. #include "CmViewport.h"
  8. #include "CmVector2.h"
  9. #include "CmGpuProgram.h"
  10. #include "CmCoreObjectManager.h"
  11. #include "CmGameObjectManager.h"
  12. #include "CmDynLib.h"
  13. #include "CmDynLibManager.h"
  14. #include "CmSceneManager.h"
  15. #include "CmImporter.h"
  16. #include "CmResources.h"
  17. #include "CmMesh.h"
  18. #include "CmSceneObject.h"
  19. #include "CmTime.h"
  20. #include "CmInput.h"
  21. #include "CmRendererManager.h"
  22. #include "CmGpuProgramManager.h"
  23. #include "CmMeshManager.h"
  24. #include "CmMaterialManager.h"
  25. #include "CmFontManager.h"
  26. #include "CmRenderWindowManager.h"
  27. #include "CmRenderer.h"
  28. #include "CmDeferredCallManager.h"
  29. #include "CmCoreThread.h"
  30. #include "CmStringTable.h"
  31. #include "CmProfiler.h"
  32. #include "CmQueryManager.h"
  33. #include "BsThreadPool.h"
  34. #include "BsTaskScheduler.h"
  35. #include "CmUUID.h"
  36. #include "CmMaterial.h"
  37. #include "CmShader.h"
  38. #include "CmTechnique.h"
  39. #include "CmPass.h"
  40. #include "CmRendererManager.h"
  41. namespace BansheeEngine
  42. {
  43. Application::Application()
  44. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false), mSceneManagerPlugin(nullptr)
  45. {
  46. }
  47. void Application::startUp(START_UP_DESC& desc)
  48. {
  49. UINT32 numWorkerThreads = CM_THREAD_HARDWARE_CONCURRENCY - 1; // Number of cores while excluding current thread.
  50. Platform::_startUp();
  51. MemStack::beginThread();
  52. UUIDGenerator::startUp();
  53. Profiler::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. RendererManager::instance().setActive(desc.renderer);
  72. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  73. MeshManager::startUp();
  74. MaterialManager::startUp();
  75. FontManager::startUp();
  76. Importer::startUp();
  77. for(auto& importerName : desc.importers)
  78. loadPlugin(importerName);
  79. loadPlugin(desc.input);
  80. }
  81. void Application::runMainLoop()
  82. {
  83. mRunMainLoop = true;
  84. while(mRunMainLoop)
  85. {
  86. gProfiler().beginThread("Sim");
  87. gCoreThread().update();
  88. Platform::_update();
  89. DeferredCallManager::instance().update();
  90. RenderWindowManager::instance()._update();
  91. gInput()._update();
  92. PROFILE_CALL(gSceneManager()._update(), "SceneManager");
  93. gCoreThread().queueCommand(std::bind(&Application::beginCoreProfiling, this));
  94. gCoreThread().queueCommand(std::bind(&QueryManager::_update, QueryManager::instancePtr()));
  95. if(!mainLoopCallback.empty())
  96. mainLoopCallback();
  97. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  98. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  99. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  100. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  101. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  102. {
  103. CM_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  104. while(!mIsFrameRenderingFinished)
  105. {
  106. TaskScheduler::instance().addWorker();
  107. CM_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  108. TaskScheduler::instance().removeWorker();
  109. }
  110. mIsFrameRenderingFinished = false;
  111. }
  112. gCoreThread().queueCommand(&Platform::_coreUpdate);
  113. gCoreThread().submitAccessors();
  114. gCoreThread().queueCommand(std::bind(&Application::endCoreProfiling, this));
  115. gCoreThread().queueCommand(std::bind(&Application::frameRenderingFinishedCallback, this));
  116. gTime().update();
  117. gProfiler().endThread();
  118. gProfiler().update();
  119. }
  120. }
  121. void Application::stopMainLoop()
  122. {
  123. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  124. // a race condition we might run the loop one extra iteration which is acceptable
  125. }
  126. void Application::frameRenderingFinishedCallback()
  127. {
  128. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  129. mIsFrameRenderingFinished = true;
  130. CM_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  131. }
  132. void Application::beginCoreProfiling()
  133. {
  134. gProfiler().beginThread("Core");
  135. }
  136. void Application::endCoreProfiling()
  137. {
  138. gProfiler().endThread();
  139. gProfiler().updateCore();
  140. }
  141. void Application::shutDown()
  142. {
  143. mPrimaryWindow->destroy();
  144. mPrimaryWindow = nullptr;
  145. Importer::shutDown();
  146. FontManager::shutDown();
  147. MaterialManager::shutDown();
  148. MeshManager::shutDown();
  149. unloadPlugin(mSceneManagerPlugin);
  150. RendererManager::shutDown();
  151. RenderSystemManager::shutDown();
  152. Input::shutDown();
  153. GpuProgramManager::shutDown();
  154. Resources::shutDown();
  155. GameObjectManager::shutDown();
  156. CoreObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  157. DynLibManager::shutDown();
  158. Time::shutDown();
  159. DeferredCallManager::shutDown();
  160. StringTable::shutDown();
  161. CoreThread::shutDown();
  162. TaskScheduler::shutDown();
  163. ThreadPool::shutDown();
  164. Profiler::shutDown();
  165. UUIDGenerator::shutDown();
  166. MemStack::endThread();
  167. Platform::_shutDown();
  168. }
  169. void* Application::loadPlugin(const String& pluginName, DynLib** library)
  170. {
  171. String name = pluginName;
  172. #if CM_PLATFORM == CM_PLATFORM_LINUX
  173. // dlopen() does not add .so to the filename, like windows does for .dll
  174. if (name.substr(name.length() - 3, 3) != ".so")
  175. name += ".so";
  176. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  177. // dlopen() does not add .dylib to the filename, like windows does for .dll
  178. if (name.substr(name.length() - 6, 6) != ".dylib")
  179. name += ".dylib";
  180. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  181. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  182. // if you include a relative path then it does not. So, add it to be sure.
  183. if (name.substr(name.length() - 4, 4) != ".dll")
  184. name += ".dll";
  185. #endif
  186. DynLib* loadedLibrary = gDynLibManager().load(name);
  187. if(library != nullptr)
  188. *library = loadedLibrary;
  189. if(loadedLibrary != nullptr)
  190. {
  191. typedef void* (*LoadPluginFunc)();
  192. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  193. if(loadPluginFunc != nullptr)
  194. return loadPluginFunc();
  195. }
  196. return nullptr;
  197. }
  198. void Application::unloadPlugin(DynLib* library)
  199. {
  200. typedef void (*UnloadPluginFunc)();
  201. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  202. if(unloadPluginFunc != nullptr)
  203. unloadPluginFunc();
  204. gDynLibManager().unload(library);
  205. }
  206. Application& gApplication()
  207. {
  208. static Application application;
  209. return application;
  210. }
  211. }