CmApplication.cpp 7.1 KB

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