CmApplication.cpp 7.4 KB

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