BsEditorApplication.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsMainEditorWindow.h"
  4. #include "BsApplication.h"
  5. #include "CmApplication.h"
  6. #include "CmRenderWindow.h"
  7. #include "BsEditorGUI.h"
  8. #include "BsUndoRedo.h"
  9. // DEBUG ONLY
  10. #include "DbgEditorWidget1.h"
  11. #include "DbgEditorWidget2.h"
  12. #include "CmResources.h"
  13. #include "CmSceneObject.h"
  14. #include "CmImporter.h"
  15. #include "CmGpuProgram.h"
  16. #include "CmGpuProgramImportOptions.h"
  17. #include "CmShader.h"
  18. #include "CmTexture.h"
  19. #include "CmMaterial.h"
  20. #include "CmTechnique.h"
  21. #include "CmPass.h"
  22. #include "BsRenderable.h"
  23. #include "BsDbgTestGameObjectRef.h"
  24. #include "BsVirtualInput.h"
  25. #include "CmWin32FolderMonitor.h"
  26. #include "BsProjectLibrary.h"
  27. using namespace CamelotFramework;
  28. using namespace BansheeEngine;
  29. namespace BansheeEditor
  30. {
  31. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  32. :mActiveRSPlugin(renderSystemPlugin)
  33. {
  34. RENDER_WINDOW_DESC renderWindowDesc;
  35. renderWindowDesc.width = 1280;
  36. renderWindowDesc.height = 720;
  37. renderWindowDesc.title = "BansheeEditor";
  38. renderWindowDesc.fullscreen = false;
  39. renderWindowDesc.border = WindowBorder::None;
  40. const String& renderSystemLibraryName = getLibraryNameForRenderSystem(renderSystemPlugin);
  41. gBansheeApp().startUp(renderWindowDesc, renderSystemLibraryName, "BansheeForwardRenderer"); // TODO - Make renderer and resource cache dir customizable
  42. EditorGUI::startUp(cm_new<EditorGUI>());
  43. {
  44. auto inputConfig = VirtualInput::instance().getConfiguration();
  45. inputConfig->registerButton("Rename", BC_F2);
  46. inputConfig->registerButton("Undo", BC_Z, VButtonModifier::Ctrl);
  47. inputConfig->registerButton("Redo", BC_Y, VButtonModifier::Ctrl);
  48. inputConfig->registerButton("Copy", BC_C, VButtonModifier::Ctrl);
  49. inputConfig->registerButton("Cut", BC_X, VButtonModifier::Ctrl);
  50. inputConfig->registerButton("Paste", BC_V, VButtonModifier::Ctrl);
  51. inputConfig->registerButton("Delete", BC_DELETE);
  52. }
  53. ProjectLibrary::startUp(cm_new<ProjectLibrary>(getActiveProjectPath()));
  54. //gApplication().loadPlugin("SBansheeEditor"); // Managed part of the editor
  55. /************************************************************************/
  56. /* DEBUG CODE */
  57. /************************************************************************/
  58. RenderSystem* renderSystem = RenderSystem::instancePtr();
  59. RenderWindowPtr renderWindow = gApplication().getPrimaryWindow();
  60. HSceneObject testModelGO = SceneObject::create("TestMesh");
  61. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  62. WString psLoc;
  63. WString vsLoc;
  64. GpuProgramProfile psProfile;
  65. GpuProgramProfile vsProfile;
  66. String psEntry;
  67. String vsEntry;
  68. String language;
  69. switch (renderSystemPlugin)
  70. {
  71. case RenderSystemPlugin::DX11:
  72. {
  73. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_ps.gpuprog";
  74. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_vs.gpuprog";
  75. language = "hlsl";
  76. psProfile = GPP_PS_4_0;
  77. vsProfile = GPP_VS_4_0;
  78. psEntry = "ps_main";
  79. vsEntry = "vs_main";
  80. break;
  81. }
  82. case RenderSystemPlugin::DX9:
  83. {
  84. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_ps.gpuprog";
  85. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_vs.gpuprog";
  86. language = "hlsl";
  87. psProfile = GPP_PS_2_0;
  88. vsProfile = GPP_VS_2_0;
  89. psEntry = "ps_main";
  90. vsEntry = "vs_main";
  91. break;
  92. }
  93. case RenderSystemPlugin::OpenGL:
  94. {
  95. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_ps.gpuprog";
  96. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_vs.gpuprog";
  97. language = "glsl";
  98. psProfile = GPP_PS_2_0;
  99. vsProfile = GPP_VS_2_0;
  100. psEntry = "main";
  101. vsEntry = "main";
  102. break;
  103. }
  104. }
  105. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(psLoc);
  106. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  107. {
  108. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  109. importOptions->setEntryPoint(psEntry);
  110. importOptions->setLanguage(language);
  111. importOptions->setProfile(psProfile);
  112. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  113. }
  114. HHighLevelGpuProgram fragProgRef = Importer::instance().import(psLoc, gpuProgImportOptions);
  115. gpuProgImportOptions = Importer::instance().createImportOptions(vsLoc);
  116. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  117. {
  118. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  119. importOptions->setEntryPoint(vsEntry);
  120. importOptions->setLanguage(language);
  121. importOptions->setProfile(vsProfile);
  122. importOptions->setType(GPT_VERTEX_PROGRAM);
  123. }
  124. HHighLevelGpuProgram vertProgRef = Importer::instance().import(vsLoc, gpuProgImportOptions);
  125. gResources().save(vertProgRef, L"C:\\vertProgCg.vprog", true);
  126. gResources().unload(vertProgRef);
  127. vertProgRef = gResources().load(L"C:\\vertProgCg.vprog");
  128. gResources().save(fragProgRef, L"C:\\fragProgCg.vprog", true);
  129. gResources().unload(fragProgRef);
  130. fragProgRef = gResources().load(L"C:\\fragProgCg.vprog");
  131. ShaderPtr testShader = Shader::create("TestShader");
  132. testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  133. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  134. testShader->addParameter("input", "input", GPDT_STRUCT, 2, 8);
  135. testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  136. testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  137. TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  138. PassPtr newPassGL = newTechniqueGL->addPass();
  139. newPassGL->setVertexProgram(vertProgRef);
  140. newPassGL->setFragmentProgram(fragProgRef);
  141. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  142. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  143. // render systems/renderers per technique
  144. TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
  145. PassPtr newPassDX = newTechniqueDX->addPass();
  146. newPassDX->setVertexProgram(vertProgRef);
  147. newPassDX->setFragmentProgram(fragProgRef);
  148. TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
  149. PassPtr newPassDX11 = newTechniqueDX11->addPass();
  150. newPassDX11->setVertexProgram(vertProgRef);
  151. newPassDX11->setFragmentProgram(fragProgRef);
  152. HMaterial testMaterial = Material::create();
  153. testMaterial->setShader(testShader);
  154. testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
  155. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  156. {
  157. float dbgMultipliers1[2];
  158. dbgMultipliers1[0] = 0.0f;
  159. dbgMultipliers1[1] = 0.0f;
  160. float dbgMultipliers2[2];
  161. dbgMultipliers2[0] = 1.0f;
  162. dbgMultipliers2[1] = 1.0f;
  163. testMaterial->setStructData("input", dbgMultipliers1, sizeof(dbgMultipliers1), 0);
  164. testMaterial->setStructData("input", dbgMultipliers2, sizeof(dbgMultipliers2), 1);
  165. }
  166. HTexture testTexRef = static_resource_cast<Texture>(Importer::instance().import(L"C:\\ArenaTowerDFS.psd"));
  167. HMesh dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"C:\\X_Arena_Tower.FBX"));
  168. gResources().save(testTexRef, L"C:\\ExportTest.tex", true);
  169. gResources().save(dbgMeshRef, L"C:\\ExportMesh.mesh", true);
  170. gResources().unload(testTexRef);
  171. gResources().unload(dbgMeshRef);
  172. testTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  173. dbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  174. dbgMeshRef.synchronize();
  175. testTexRef.synchronize();
  176. testMaterial->setTexture("tex", testTexRef);
  177. gResources().save(testMaterial, L"C:\\ExportMaterial.mat", true);
  178. gResources().unload(testMaterial);
  179. testMaterial = gResources().load(L"C:\\ExportMaterial.mat");
  180. testRenderable->setMesh(dbgMeshRef);
  181. testRenderable->setMaterial(0, testMaterial);
  182. GameObjectHandle<DbgTestGameObjectRef> dbgTestGameObjectRef = testModelGO->addComponent<DbgTestGameObjectRef>();
  183. dbgTestGameObjectRef->mRenderable = testRenderable;
  184. HSceneObject clone = testModelGO->clone();
  185. GameObjectHandle<DbgTestGameObjectRef> clonedDbgTestGameObjectRef = clone->getComponent<DbgTestGameObjectRef>();
  186. testModelGO->destroy();
  187. //Win32FolderMonitor* folderMonitor = cm_new<Win32FolderMonitor>();
  188. //FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  189. // (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  190. //folderMonitor->startMonitor(L"D:\\TestDetect", true, folderChanges);
  191. HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import(L"C:\\CursorDbg.psd"));
  192. PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  193. gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, cursorPixelData);
  194. gMainSyncedCA().submitToCoreThread(true);
  195. /************************************************************************/
  196. /* END DEBUG CODE */
  197. /************************************************************************/
  198. UndoRedo::startUp(cm_new<UndoRedo>());
  199. EditorWindowManager::startUp(cm_new<EditorWindowManager>());
  200. MainEditorWindow* mainWindow = MainEditorWindow::create(gApplication().getPrimaryWindow());
  201. gApplication().mainLoopCallback.connect(boost::bind(&EditorApplication::update, this));
  202. DbgEditorWidget1::open(); // DEBUG ONLY
  203. DbgEditorWidget2::open(); // DEBUG ONLY
  204. gBansheeApp().runMainLoop();
  205. EditorWindowManager::shutDown();
  206. UndoRedo::shutDown();
  207. /************************************************************************/
  208. /* DEBUG CODE */
  209. /************************************************************************/
  210. gResources().unload(testTexRef);
  211. gResources().unload(dbgMeshRef);
  212. gResources().unload(fragProgRef);
  213. gResources().unload(vertProgRef);
  214. gResources().unload(testMaterial);
  215. testMaterial = nullptr;
  216. testTexRef = nullptr;
  217. dbgMeshRef = nullptr;
  218. fragProgRef = nullptr;
  219. vertProgRef = nullptr;
  220. newPassGL = nullptr;
  221. newTechniqueGL = nullptr;
  222. newPassDX = nullptr;
  223. newTechniqueDX = nullptr;
  224. newPassDX11 = nullptr;
  225. newTechniqueDX11 = nullptr;
  226. testShader = nullptr;
  227. renderWindow = nullptr;
  228. /************************************************************************/
  229. /* END DEBUG CODE */
  230. /************************************************************************/
  231. ProjectLibrary::shutDown();
  232. EditorGUI::shutDown();
  233. gBansheeApp().shutDown();
  234. }
  235. EditorApplication::~EditorApplication()
  236. {
  237. // TODO - Move shutdown code from constructor to here. Right now I don't care because cleanup
  238. // isn't working as intended and if I move stuff I will probably break it even more
  239. }
  240. void EditorApplication::runMainLoop()
  241. {
  242. // TODO - Move "runMainLoop" code from constructor to here. Right now I don't care because cleanup
  243. // isn't working as intended and if I move stuff I will probably break it even more
  244. }
  245. void EditorApplication::update()
  246. {
  247. ProjectLibrary::instance().update();
  248. EditorWindowManager::instance().update();
  249. }
  250. bool EditorApplication::isProjectLoaded() const
  251. {
  252. return true; // TODO - DEBUG ONLY
  253. }
  254. const WString& EditorApplication::getActiveProjectPath() const
  255. {
  256. static WString dummyProjectPath = L"D:\\DummyBansheeProject";
  257. return dummyProjectPath;
  258. }
  259. const String& EditorApplication::getLibraryNameForRenderSystem(RenderSystemPlugin plugin)
  260. {
  261. static String DX11Name = "CamelotD3D11RenderSystem";
  262. static String DX9Name = "CamelotD3D9RenderSystem";
  263. static String OpenGLName = "CamelotGLRenderSystem";
  264. switch(plugin)
  265. {
  266. case RenderSystemPlugin::DX11:
  267. return DX11Name;
  268. case RenderSystemPlugin::DX9:
  269. return DX9Name;
  270. case RenderSystemPlugin::OpenGL:
  271. return OpenGLName;
  272. }
  273. return StringUtil::BLANK;
  274. }
  275. }