emu_window_sdl2_gl.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <string>
  7. #define SDL_MAIN_HANDLED
  8. #include <SDL.h>
  9. #include <fmt/format.h>
  10. #include <glad/glad.h>
  11. #include "common/assert.h"
  12. #include "common/logging/log.h"
  13. #include "common/scm_rev.h"
  14. #include "common/string_util.h"
  15. #include "core/core.h"
  16. #include "core/settings.h"
  17. #include "input_common/keyboard.h"
  18. #include "input_common/main.h"
  19. #include "input_common/motion_emu.h"
  20. #include "video_core/renderer_base.h"
  21. #include "yuzu_cmd/emu_window/emu_window_sdl2_gl.h"
  22. class SDLGLContext : public Core::Frontend::GraphicsContext {
  23. public:
  24. explicit SDLGLContext() {
  25. // create a hidden window to make the shared context against
  26. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0,
  27. SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
  28. context = SDL_GL_CreateContext(window);
  29. }
  30. ~SDLGLContext() {
  31. DoneCurrent();
  32. SDL_GL_DeleteContext(context);
  33. SDL_DestroyWindow(window);
  34. }
  35. void MakeCurrent() override {
  36. if (is_current) {
  37. return;
  38. }
  39. is_current = SDL_GL_MakeCurrent(window, context) == 0;
  40. }
  41. void DoneCurrent() override {
  42. if (!is_current) {
  43. return;
  44. }
  45. SDL_GL_MakeCurrent(window, nullptr);
  46. is_current = false;
  47. }
  48. private:
  49. SDL_Window* window;
  50. SDL_GLContext context;
  51. bool is_current = false;
  52. };
  53. bool EmuWindow_SDL2_GL::SupportsRequiredGLExtensions() {
  54. std::vector<std::string_view> unsupported_ext;
  55. if (!GLAD_GL_ARB_buffer_storage)
  56. unsupported_ext.push_back("ARB_buffer_storage");
  57. if (!GLAD_GL_ARB_direct_state_access)
  58. unsupported_ext.push_back("ARB_direct_state_access");
  59. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  60. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  61. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  62. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  63. if (!GLAD_GL_ARB_multi_bind)
  64. unsupported_ext.push_back("ARB_multi_bind");
  65. if (!GLAD_GL_ARB_clip_control)
  66. unsupported_ext.push_back("ARB_clip_control");
  67. // Extensions required to support some texture formats.
  68. if (!GLAD_GL_EXT_texture_compression_s3tc)
  69. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  70. if (!GLAD_GL_ARB_texture_compression_rgtc)
  71. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  72. if (!GLAD_GL_ARB_depth_buffer_float)
  73. unsupported_ext.push_back("ARB_depth_buffer_float");
  74. for (const auto& extension : unsupported_ext)
  75. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", extension);
  76. return unsupported_ext.empty();
  77. }
  78. EmuWindow_SDL2_GL::EmuWindow_SDL2_GL(Core::System& system, bool fullscreen)
  79. : EmuWindow_SDL2{system, fullscreen} {
  80. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  81. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  82. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
  83. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  84. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  85. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  86. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  87. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  88. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  89. SDL_GL_SetSwapInterval(0);
  90. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  91. Common::g_scm_branch, Common::g_scm_desc);
  92. render_window =
  93. SDL_CreateWindow(window_title.c_str(),
  94. SDL_WINDOWPOS_UNDEFINED, // x position
  95. SDL_WINDOWPOS_UNDEFINED, // y position
  96. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  97. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  98. if (render_window == nullptr) {
  99. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  100. exit(1);
  101. }
  102. dummy_window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0,
  103. SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
  104. if (fullscreen) {
  105. Fullscreen();
  106. }
  107. window_context = SDL_GL_CreateContext(render_window);
  108. core_context = CreateSharedContext();
  109. if (window_context == nullptr) {
  110. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context: {}", SDL_GetError());
  111. exit(1);
  112. }
  113. if (core_context == nullptr) {
  114. LOG_CRITICAL(Frontend, "Failed to create shared SDL2 GL context: {}", SDL_GetError());
  115. exit(1);
  116. }
  117. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  118. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  119. exit(1);
  120. }
  121. if (!SupportsRequiredGLExtensions()) {
  122. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  123. exit(1);
  124. }
  125. OnResize();
  126. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  127. SDL_PumpEvents();
  128. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  129. Common::g_scm_desc);
  130. Settings::LogSettings();
  131. }
  132. EmuWindow_SDL2_GL::~EmuWindow_SDL2_GL() {
  133. core_context.reset();
  134. SDL_GL_DeleteContext(window_context);
  135. }
  136. std::unique_ptr<Core::Frontend::GraphicsContext> EmuWindow_SDL2_GL::CreateSharedContext() const {
  137. return std::make_unique<SDLGLContext>();
  138. }
  139. void EmuWindow_SDL2_GL::Present() {
  140. SDL_GL_MakeCurrent(render_window, window_context);
  141. SDL_GL_SetSwapInterval(Settings::values.use_vsync ? 1 : 0);
  142. while (IsOpen()) {
  143. system.Renderer().TryPresent(100);
  144. SDL_GL_SwapWindow(render_window);
  145. }
  146. SDL_GL_MakeCurrent(render_window, nullptr);
  147. }