main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. // OpenGL Backend Documentation
  5. /*
  6. 1.1 Display settings
  7. Internal and fullscreen resolution: Since the only internal resolutions allowed
  8. are also fullscreen resolution allowed by the system there is only need for one
  9. resolution setting that applies to both the internal resolution and the
  10. fullscreen resolution. - Apparently no, someone else doesn't agree
  11. Todo: Make the internal resolution option apply instantly, currently only the
  12. native and 2x option applies instantly. To do this we need to be able to change
  13. the reinitialize FramebufferManager:Init() while a game is running.
  14. 1.2 Screenshots
  15. The screenshots should be taken from the internal representation of the picture
  16. regardless of what the current window size is. Since AA and wireframe is
  17. applied together with the picture resizing this rule is not currently applied
  18. to AA or wireframe pictures, they are instead taken from whatever the window
  19. size is.
  20. Todo: Render AA and wireframe to a separate picture used for the screenshot in
  21. addition to the one for display.
  22. 1.3 AA
  23. Make AA apply instantly during gameplay if possible
  24. */
  25. #include <algorithm>
  26. #include <cstdarg>
  27. #include <regex>
  28. #include "Common/Atomic.h"
  29. #include "Common/CommonPaths.h"
  30. #include "Common/FileSearch.h"
  31. #include "Common/Thread.h"
  32. #include "Common/Logging/LogManager.h"
  33. #include "Core/ConfigManager.h"
  34. #include "Core/Core.h"
  35. #include "Core/Host.h"
  36. #include "VideoBackends/OGL/BoundingBox.h"
  37. #include "VideoBackends/OGL/FramebufferManager.h"
  38. #include "VideoBackends/OGL/GLInterfaceBase.h"
  39. #include "VideoBackends/OGL/GLUtil.h"
  40. #include "VideoBackends/OGL/PerfQuery.h"
  41. #include "VideoBackends/OGL/PostProcessing.h"
  42. #include "VideoBackends/OGL/ProgramShaderCache.h"
  43. #include "VideoBackends/OGL/Render.h"
  44. #include "VideoBackends/OGL/SamplerCache.h"
  45. #include "VideoBackends/OGL/TextureCache.h"
  46. #include "VideoBackends/OGL/TextureConverter.h"
  47. #include "VideoBackends/OGL/VertexManager.h"
  48. #include "VideoBackends/OGL/VideoBackend.h"
  49. #include "VideoCommon/BPStructs.h"
  50. #include "VideoCommon/CommandProcessor.h"
  51. #include "VideoCommon/Fifo.h"
  52. #include "VideoCommon/GeometryShaderManager.h"
  53. #include "VideoCommon/ImageWrite.h"
  54. #include "VideoCommon/IndexGenerator.h"
  55. #include "VideoCommon/LookUpTables.h"
  56. #include "VideoCommon/MainBase.h"
  57. #include "VideoCommon/OnScreenDisplay.h"
  58. #include "VideoCommon/OpcodeDecoding.h"
  59. #include "VideoCommon/PixelEngine.h"
  60. #include "VideoCommon/PixelShaderManager.h"
  61. #include "VideoCommon/VertexLoaderManager.h"
  62. #include "VideoCommon/VertexShaderManager.h"
  63. #include "VideoCommon/VideoConfig.h"
  64. #include "VideoCommon/VideoState.h"
  65. namespace OGL
  66. {
  67. std::string VideoBackend::GetName() const
  68. {
  69. return "OGL";
  70. }
  71. std::string VideoBackend::GetDisplayName() const
  72. {
  73. if (GLInterface != nullptr && GLInterface->GetMode() == GLInterfaceMode::MODE_OPENGLES3)
  74. return "OpenGLES";
  75. else
  76. return "OpenGL";
  77. }
  78. static std::vector<std::string> GetShaders(const std::string &sub_dir = "")
  79. {
  80. std::vector<std::string> paths = DoFileSearch({"*.glsl"}, {
  81. File::GetUserPath(D_SHADERS_IDX) + sub_dir,
  82. File::GetSysDirectory() + SHADERS_DIR DIR_SEP + sub_dir
  83. });
  84. std::vector<std::string> result;
  85. for (std::string path : paths)
  86. result.push_back(std::regex_replace(path, std::regex("^.*/(.*)\\.glsl$"), "$1"));
  87. return result;
  88. }
  89. static void InitBackendInfo()
  90. {
  91. g_Config.backend_info.APIType = API_OPENGL;
  92. g_Config.backend_info.bSupportsExclusiveFullscreen = false;
  93. g_Config.backend_info.bSupportsOversizedViewports = true;
  94. g_Config.backend_info.bSupportsGeometryShaders = true;
  95. g_Config.backend_info.bSupports3DVision = false;
  96. g_Config.backend_info.bSupportsPostProcessing = true;
  97. g_Config.backend_info.Adapters.clear();
  98. // aamodes
  99. const char* caamodes[] = {_trans("None"), "2x", "4x", "8x", "4x SSAA"};
  100. g_Config.backend_info.AAModes.assign(caamodes, caamodes + sizeof(caamodes)/sizeof(*caamodes));
  101. // pp shaders
  102. g_Config.backend_info.PPShaders = GetShaders("");
  103. g_Config.backend_info.AnaglyphShaders = GetShaders(ANAGLYPH_DIR DIR_SEP);
  104. }
  105. void VideoBackend::ShowConfig(void *_hParent)
  106. {
  107. if (!s_BackendInitialized)
  108. InitBackendInfo();
  109. Host_ShowVideoConfig(_hParent, GetDisplayName(), "gfx_opengl");
  110. }
  111. bool VideoBackend::Initialize(void *window_handle)
  112. {
  113. InitializeShared();
  114. InitBackendInfo();
  115. frameCount = 0;
  116. g_Config.Load(File::GetUserPath(D_CONFIG_IDX) + "gfx_opengl.ini");
  117. g_Config.GameIniLoad();
  118. g_Config.UpdateProjectionHack();
  119. g_Config.VerifyValidity();
  120. UpdateActiveConfig();
  121. InitInterface();
  122. GLInterface->SetMode(GLInterfaceMode::MODE_DETECT);
  123. if (!GLInterface->Create(window_handle))
  124. return false;
  125. // Do our OSD callbacks
  126. OSD::DoCallbacks(OSD::OSD_INIT);
  127. s_BackendInitialized = true;
  128. return true;
  129. }
  130. // This is called after Initialize() from the Core
  131. // Run from the graphics thread
  132. void VideoBackend::Video_Prepare()
  133. {
  134. GLInterface->MakeCurrent();
  135. g_renderer = new Renderer;
  136. CommandProcessor::Init();
  137. PixelEngine::Init();
  138. BPInit();
  139. g_vertex_manager = new VertexManager;
  140. g_perf_query = GetPerfQuery();
  141. Fifo_Init(); // must be done before OpcodeDecoder_Init()
  142. OpcodeDecoder_Init();
  143. IndexGenerator::Init();
  144. VertexShaderManager::Init();
  145. PixelShaderManager::Init();
  146. GeometryShaderManager::Init();
  147. ProgramShaderCache::Init();
  148. g_texture_cache = new TextureCache();
  149. g_sampler_cache = new SamplerCache();
  150. Renderer::Init();
  151. VertexLoaderManager::Init();
  152. TextureConverter::Init();
  153. BoundingBox::Init();
  154. // Notify the core that the video backend is ready
  155. Host_Message(WM_USER_CREATE);
  156. }
  157. void VideoBackend::Shutdown()
  158. {
  159. s_BackendInitialized = false;
  160. // Do our OSD callbacks
  161. OSD::DoCallbacks(OSD::OSD_SHUTDOWN);
  162. GLInterface->Shutdown();
  163. delete GLInterface;
  164. GLInterface = nullptr;
  165. }
  166. void VideoBackend::Video_Cleanup()
  167. {
  168. if (g_renderer)
  169. {
  170. Fifo_Shutdown();
  171. // The following calls are NOT Thread Safe
  172. // And need to be called from the video thread
  173. Renderer::Shutdown();
  174. BoundingBox::Shutdown();
  175. TextureConverter::Shutdown();
  176. VertexLoaderManager::Shutdown();
  177. delete g_sampler_cache;
  178. g_sampler_cache = nullptr;
  179. delete g_texture_cache;
  180. g_texture_cache = nullptr;
  181. ProgramShaderCache::Shutdown();
  182. VertexShaderManager::Shutdown();
  183. PixelShaderManager::Shutdown();
  184. GeometryShaderManager::Shutdown();
  185. delete g_perf_query;
  186. g_perf_query = nullptr;
  187. delete g_vertex_manager;
  188. g_vertex_manager = nullptr;
  189. OpcodeDecoder_Shutdown();
  190. delete g_renderer;
  191. g_renderer = nullptr;
  192. GLInterface->ClearCurrent();
  193. }
  194. }
  195. }