CmApplication.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "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 "CmMeshManager.h"
  23. #include "CmMaterialManager.h"
  24. #include "CmFontManager.h"
  25. #include "CmRenderWindowManager.h"
  26. #include "CmRenderer.h"
  27. #include "CmDeferredCallManager.h"
  28. #include "CmCoreThread.h"
  29. #include "CmStringTable.h"
  30. #include "CmProfiler.h"
  31. #include "CmQueryManager.h"
  32. #include "CmMaterial.h"
  33. #include "CmShader.h"
  34. #include "CmTechnique.h"
  35. #include "CmPass.h"
  36. #include "CmRendererManager.h"
  37. namespace CamelotFramework
  38. {
  39. Application::Application()
  40. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  41. {
  42. }
  43. void Application::startUp(START_UP_DESC& desc)
  44. {
  45. Platform::startUp();
  46. MemStack::beginThread();
  47. Profiler::startUp(cm_new<Profiler>());
  48. StringTable::startUp(cm_new<StringTable>());
  49. DeferredCallManager::startUp(cm_new<DeferredCallManager>());
  50. Time::startUp(cm_new<Time>());
  51. DynLibManager::startUp(cm_new<DynLibManager>());
  52. CoreGpuObjectManager::startUp(cm_new<CoreGpuObjectManager>());
  53. Resources::startUp(cm_new<Resources>(desc.resourceCacheDirectory));
  54. HighLevelGpuProgramManager::startUp(cm_new<HighLevelGpuProgramManager>());
  55. CoreThread::startUp(cm_new<CoreThread>());
  56. RenderSystemManager::startUp(cm_new<RenderSystemManager>());
  57. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  58. Input::startUp(cm_new<Input>());
  59. RendererManager::startUp(cm_new<RendererManager>());
  60. loadPlugin(desc.renderer);
  61. RendererManager::instance().setActive(desc.renderer);
  62. mPrimaryCoreAccessor = gCoreThread().createAccessor();
  63. mPrimarySyncedCoreAccessor = &gCoreThread().getSyncedAccessor();
  64. SceneManager::startUp((SceneManager*)loadPlugin(desc.sceneManager));
  65. MeshManager::startUp(cm_new<MeshManager>());
  66. MaterialManager::startUp(cm_new<MaterialManager>());
  67. FontManager::startUp(cm_new<FontManager>());
  68. Importer::startUp(cm_new<Importer>());
  69. for(auto& importerName : desc.importers)
  70. loadPlugin(importerName);
  71. loadPlugin(desc.input);
  72. Platform::setCursor(CursorType::Arrow);
  73. }
  74. void Application::runMainLoop()
  75. {
  76. mRunMainLoop = true;
  77. while(mRunMainLoop)
  78. {
  79. gProfiler().beginThread("Sim");
  80. gCoreThread().update();
  81. Platform::update();
  82. DeferredCallManager::instance().update();
  83. RenderWindowManager::instance().update();
  84. gInput().update();
  85. PROFILE_CALL(gSceneManager().update(), "SceneManager");
  86. gCoreThread().queueCommand(boost::bind(&Application::beginCoreProfiling, this));
  87. gCoreThread().queueCommand(boost::bind(&QueryManager::update, QueryManager::instancePtr()));
  88. if(!mainLoopCallback.empty())
  89. mainLoopCallback();
  90. PROFILE_CALL(RendererManager::instance().getActive()->renderAll(), "Render");
  91. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  92. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  93. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  94. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  95. {
  96. CM_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  97. while(!mIsFrameRenderingFinished)
  98. CM_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  99. mIsFrameRenderingFinished = false;
  100. }
  101. gCoreThread().queueCommand(&Platform::coreUpdate);
  102. PROFILE_CALL(mPrimaryCoreAccessor->submitToCoreThread(), "CommandSubmit");
  103. PROFILE_CALL(mPrimarySyncedCoreAccessor->submitToCoreThread(), "SyncCommandSubmit");
  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. SceneManager::shutDown();
  140. RendererManager::shutDown();
  141. RenderSystem::shutDown();
  142. CoreThread::shutDown();
  143. Input::shutDown();
  144. HighLevelGpuProgramManager::shutDown();
  145. Resources::shutDown();
  146. CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  147. DynLibManager::shutDown();
  148. Time::shutDown();
  149. DeferredCallManager::shutDown();
  150. StringTable::shutDown();
  151. Profiler::shutDown();
  152. MemStack::endThread();
  153. Platform::shutDown();
  154. }
  155. void* Application::loadPlugin(const String& pluginName)
  156. {
  157. String name = pluginName;
  158. #if CM_PLATFORM == CM_PLATFORM_LINUX
  159. // dlopen() does not add .so to the filename, like windows does for .dll
  160. if (name.substr(name.length() - 3, 3) != ".so")
  161. name += ".so";
  162. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  163. // dlopen() does not add .dylib to the filename, like windows does for .dll
  164. if (name.substr(name.length() - 6, 6) != ".dylib")
  165. name += ".dylib";
  166. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  167. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  168. // if you include a relative path then it does not. So, add it to be sure.
  169. if (name.substr(name.length() - 4, 4) != ".dll")
  170. name += ".dll";
  171. #endif
  172. DynLib* library = gDynLibManager().load(name);
  173. if(library != nullptr)
  174. {
  175. typedef void* (*LoadPluginFunc)();
  176. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  177. return loadPluginFunc();
  178. }
  179. return nullptr;
  180. }
  181. UINT64 Application::getAppWindowId()
  182. {
  183. if(!mPrimaryWindow)
  184. {
  185. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window exists!");
  186. }
  187. UINT64 windowId = 0;
  188. mPrimaryWindow->getCustomAttribute("WINDOW", &windowId);
  189. return windowId;
  190. }
  191. Application& gApplication()
  192. {
  193. static Application application;
  194. return application;
  195. }
  196. CoreAccessor& gMainCA()
  197. {
  198. return *gApplication().mPrimaryCoreAccessor.get();
  199. }
  200. SyncedCoreAccessor& gMainSyncedCA()
  201. {
  202. return *gApplication().mPrimarySyncedCoreAccessor;
  203. }
  204. }