BsEditorApplication.cpp 11 KB

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