BsEditorApplication.cpp 10 KB

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