BsEditorApplication.cpp 11 KB

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