BsEditorApplication.cpp 10 KB

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