CmApplication.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "CmHighLevelGpuProgram.h"
  10. #include "CmHighLevelGpuProgramManager.h"
  11. #include "CmCoreObjectManager.h"
  12. #include "CmGameObjectManager.h"
  13. #include "CmDynLib.h"
  14. #include "CmDynLibManager.h"
  15. #include "CmSceneManager.h"
  16. #include "CmImporter.h"
  17. #include "CmResources.h"
  18. #include "CmMesh.h"
  19. #include "CmSceneObject.h"
  20. #include "CmTime.h"
  21. #include "CmInput.h"
  22. #include "CmRendererManager.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 "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. Profiler::startUp(cm_new<Profiler>());
  53. ThreadPool::startUp(cm_new<TThreadPool<ThreadBansheePolicy>>(numWorkerThreads));
  54. TaskScheduler::startUp(cm_new<TaskScheduler>());
  55. TaskScheduler::instance().removeWorker();
  56. StringTable::startUp(cm_new<StringTable>());
  57. DeferredCallManager::startUp(cm_new<DeferredCallManager>());
  58. Time::startUp(cm_new<Time>());
  59. DynLibManager::startUp(cm_new<DynLibManager>());
  60. CoreObjectManager::startUp(cm_new<CoreObjectManager>());
  61. GameObjectManager::startUp(cm_new<GameObjectManager>());
  62. Resources::startUp(cm_new<Resources>());
  63. HighLevelGpuProgramManager::startUp(cm_new<HighLevelGpuProgramManager>());
  64. CoreThread::startUp(cm_new<CoreThread>());
  65. RenderSystemManager::startUp(cm_new<RenderSystemManager>());
  66. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  67. Input::startUp(cm_new<Input>());
  68. RendererManager::startUp(cm_new<RendererManager>());
  69. loadPlugin(desc.renderer);
  70. RendererManager::instance().setActive(desc.renderer);
  71. loadPlugin(desc.sceneManager, &mSceneManagerPlugin);
  72. MeshManager::startUp(cm_new<MeshManager>());
  73. MaterialManager::startUp(cm_new<MaterialManager>());
  74. FontManager::startUp(cm_new<FontManager>());
  75. Importer::startUp(cm_new<Importer>());
  76. for(auto& importerName : desc.importers)
  77. loadPlugin(importerName);
  78. loadPlugin(desc.input);
  79. }
  80. void Application::runMainLoop()
  81. {
  82. mRunMainLoop = true;
  83. while(mRunMainLoop)
  84. {
  85. gProfiler().beginThread("Sim");
  86. gCoreThread().update();
  87. Platform::update();
  88. DeferredCallManager::instance().update();
  89. RenderWindowManager::instance().update();
  90. gInput()._update();
  91. PROFILE_CALL(gSceneManager().update(), "SceneManager");
  92. gCoreThread().queueCommand(std::bind(&Application::beginCoreProfiling, this));
  93. gCoreThread().queueCommand(std::bind(&QueryManager::update, QueryManager::instancePtr()));
  94. if(!mainLoopCallback.empty())
  95. mainLoopCallback();
  96. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  97. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  98. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  99. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  100. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  101. {
  102. CM_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  103. while(!mIsFrameRenderingFinished)
  104. {
  105. TaskScheduler::instance().addWorker();
  106. CM_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  107. TaskScheduler::instance().removeWorker();
  108. }
  109. mIsFrameRenderingFinished = false;
  110. }
  111. gCoreThread().queueCommand(&Platform::coreUpdate);
  112. gCoreThread().submitAccessors();
  113. gCoreThread().queueCommand(std::bind(&Application::endCoreProfiling, this));
  114. gCoreThread().queueCommand(std::bind(&Application::frameRenderingFinishedCallback, this));
  115. gTime().update();
  116. gProfiler().endThread();
  117. gProfiler().update();
  118. }
  119. }
  120. void Application::stopMainLoop()
  121. {
  122. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  123. // a race condition we might run the loop one extra iteration which is acceptable
  124. }
  125. void Application::frameRenderingFinishedCallback()
  126. {
  127. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  128. mIsFrameRenderingFinished = true;
  129. CM_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  130. }
  131. void Application::beginCoreProfiling()
  132. {
  133. gProfiler().beginThread("Core");
  134. }
  135. void Application::endCoreProfiling()
  136. {
  137. gProfiler().endThread();
  138. gProfiler().updateCore();
  139. }
  140. void Application::shutDown()
  141. {
  142. mPrimaryWindow->destroy();
  143. mPrimaryWindow = nullptr;
  144. Importer::shutDown();
  145. FontManager::shutDown();
  146. MaterialManager::shutDown();
  147. MeshManager::shutDown();
  148. unloadPlugin(mSceneManagerPlugin);
  149. RendererManager::shutDown();
  150. RenderSystemManager::shutDown();
  151. CoreThread::shutDown();
  152. Input::shutDown();
  153. HighLevelGpuProgramManager::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. TaskScheduler::shutDown();
  162. ThreadPool::shutDown();
  163. Profiler::shutDown();
  164. MemStack::endThread();
  165. Platform::shutDown();
  166. }
  167. void* Application::loadPlugin(const String& pluginName, DynLib** library)
  168. {
  169. String name = pluginName;
  170. #if CM_PLATFORM == CM_PLATFORM_LINUX
  171. // dlopen() does not add .so to the filename, like windows does for .dll
  172. if (name.substr(name.length() - 3, 3) != ".so")
  173. name += ".so";
  174. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  175. // dlopen() does not add .dylib to the filename, like windows does for .dll
  176. if (name.substr(name.length() - 6, 6) != ".dylib")
  177. name += ".dylib";
  178. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  179. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  180. // if you include a relative path then it does not. So, add it to be sure.
  181. if (name.substr(name.length() - 4, 4) != ".dll")
  182. name += ".dll";
  183. #endif
  184. DynLib* loadedLibrary = gDynLibManager().load(name);
  185. if(library != nullptr)
  186. *library = loadedLibrary;
  187. if(loadedLibrary != nullptr)
  188. {
  189. typedef void* (*LoadPluginFunc)();
  190. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)loadedLibrary->getSymbol("loadPlugin");
  191. if(loadPluginFunc != nullptr)
  192. return loadPluginFunc();
  193. }
  194. return nullptr;
  195. }
  196. void Application::unloadPlugin(DynLib* library)
  197. {
  198. typedef void (*UnloadPluginFunc)();
  199. UnloadPluginFunc unloadPluginFunc = (UnloadPluginFunc)library->getSymbol("unloadPlugin");
  200. if(unloadPluginFunc != nullptr)
  201. unloadPluginFunc();
  202. gDynLibManager().unload(library);
  203. }
  204. UINT64 Application::getAppWindowId()
  205. {
  206. if(!mPrimaryWindow)
  207. {
  208. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window exists!");
  209. }
  210. UINT64 windowId = 0;
  211. mPrimaryWindow->getCustomAttribute("WINDOW", &windowId);
  212. return windowId;
  213. }
  214. Application& gApplication()
  215. {
  216. static Application application;
  217. return application;
  218. }
  219. }