BsEditorApplication.cpp 10 KB

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