emu_window_sdl2_gl.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "video_core/renderer_base.h"
  20. #include "yuzu_cmd/emu_window/emu_window_sdl2_gl.h"
  21. class SDLGLContext : public Core::Frontend::GraphicsContext {
  22. public:
  23. explicit SDLGLContext() {
  24. // create a hidden window to make the shared context against
  25. window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0,
  26. SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
  27. context = SDL_GL_CreateContext(window);
  28. }
  29. ~SDLGLContext() {
  30. DoneCurrent();
  31. SDL_GL_DeleteContext(context);
  32. SDL_DestroyWindow(window);
  33. }
  34. void MakeCurrent() override {
  35. if (is_current) {
  36. return;
  37. }
  38. is_current = SDL_GL_MakeCurrent(window, context) == 0;
  39. }
  40. void DoneCurrent() override {
  41. if (!is_current) {
  42. return;
  43. }
  44. SDL_GL_MakeCurrent(window, nullptr);
  45. is_current = false;
  46. }
  47. private:
  48. SDL_Window* window;
  49. SDL_GLContext context;
  50. bool is_current = false;
  51. };
  52. bool EmuWindow_SDL2_GL::SupportsRequiredGLExtensions() {
  53. std::vector<std::string_view> unsupported_ext;
  54. if (!GLAD_GL_ARB_buffer_storage)
  55. unsupported_ext.push_back("ARB_buffer_storage");
  56. if (!GLAD_GL_ARB_direct_state_access)
  57. unsupported_ext.push_back("ARB_direct_state_access");
  58. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  59. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  60. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  61. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  62. if (!GLAD_GL_ARB_multi_bind)
  63. unsupported_ext.push_back("ARB_multi_bind");
  64. if (!GLAD_GL_ARB_clip_control)
  65. unsupported_ext.push_back("ARB_clip_control");
  66. // Extensions required to support some texture formats.
  67. if (!GLAD_GL_EXT_texture_compression_s3tc)
  68. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  69. if (!GLAD_GL_ARB_texture_compression_rgtc)
  70. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  71. if (!GLAD_GL_ARB_depth_buffer_float)
  72. unsupported_ext.push_back("ARB_depth_buffer_float");
  73. for (const auto& extension : unsupported_ext)
  74. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", extension);
  75. return unsupported_ext.empty();
  76. }
  77. EmuWindow_SDL2_GL::EmuWindow_SDL2_GL(InputCommon::InputSubsystem* input_subsystem, bool fullscreen)
  78. : EmuWindow_SDL2{input_subsystem} {
  79. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  80. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  81. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
  82. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  83. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  84. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  85. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  86. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  87. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  88. if (Settings::values.renderer_debug) {
  89. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
  90. }
  91. SDL_GL_SetSwapInterval(0);
  92. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  93. Common::g_scm_branch, Common::g_scm_desc);
  94. render_window =
  95. SDL_CreateWindow(window_title.c_str(),
  96. SDL_WINDOWPOS_UNDEFINED, // x position
  97. SDL_WINDOWPOS_UNDEFINED, // y position
  98. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  99. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  100. if (render_window == nullptr) {
  101. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  102. exit(1);
  103. }
  104. dummy_window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0,
  105. SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
  106. if (fullscreen) {
  107. Fullscreen();
  108. }
  109. window_context = SDL_GL_CreateContext(render_window);
  110. core_context = CreateSharedContext();
  111. if (window_context == nullptr) {
  112. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context: {}", SDL_GetError());
  113. exit(1);
  114. }
  115. if (core_context == nullptr) {
  116. LOG_CRITICAL(Frontend, "Failed to create shared SDL2 GL context: {}", SDL_GetError());
  117. exit(1);
  118. }
  119. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  120. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  121. exit(1);
  122. }
  123. if (!SupportsRequiredGLExtensions()) {
  124. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  125. exit(1);
  126. }
  127. OnResize();
  128. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  129. SDL_PumpEvents();
  130. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  131. Common::g_scm_desc);
  132. Settings::LogSettings();
  133. }
  134. EmuWindow_SDL2_GL::~EmuWindow_SDL2_GL() {
  135. core_context.reset();
  136. SDL_GL_DeleteContext(window_context);
  137. }
  138. std::unique_ptr<Core::Frontend::GraphicsContext> EmuWindow_SDL2_GL::CreateSharedContext() const {
  139. return std::make_unique<SDLGLContext>();
  140. }