CmApplication.cpp 7.6 KB

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