CmApplication.cpp 7.7 KB

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