BsEditorApplication.cpp 10 KB

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