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