CmApplication.cpp 7.4 KB

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