emu_window_sdl2_gl.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Extensions required to support some texture formats.
  55. if (!GLAD_GL_EXT_texture_compression_s3tc) {
  56. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  57. }
  58. if (!GLAD_GL_ARB_texture_compression_rgtc) {
  59. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  60. }
  61. for (const auto& extension : unsupported_ext) {
  62. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", extension);
  63. }
  64. return unsupported_ext.empty();
  65. }
  66. EmuWindow_SDL2_GL::EmuWindow_SDL2_GL(InputCommon::InputSubsystem* input_subsystem, bool fullscreen)
  67. : EmuWindow_SDL2{input_subsystem} {
  68. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  69. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
  70. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
  71. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  72. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  73. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  74. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  75. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  76. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  77. if (Settings::values.renderer_debug) {
  78. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
  79. }
  80. SDL_GL_SetSwapInterval(0);
  81. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  82. Common::g_scm_branch, Common::g_scm_desc);
  83. render_window =
  84. SDL_CreateWindow(window_title.c_str(),
  85. SDL_WINDOWPOS_UNDEFINED, // x position
  86. SDL_WINDOWPOS_UNDEFINED, // y position
  87. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  88. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  89. if (render_window == nullptr) {
  90. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  91. exit(1);
  92. }
  93. dummy_window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0,
  94. SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
  95. SetWindowIcon();
  96. if (fullscreen) {
  97. Fullscreen();
  98. }
  99. window_context = SDL_GL_CreateContext(render_window);
  100. core_context = CreateSharedContext();
  101. if (window_context == nullptr) {
  102. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context: {}", SDL_GetError());
  103. exit(1);
  104. }
  105. if (core_context == nullptr) {
  106. LOG_CRITICAL(Frontend, "Failed to create shared SDL2 GL context: {}", SDL_GetError());
  107. exit(1);
  108. }
  109. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  110. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  111. exit(1);
  112. }
  113. if (!SupportsRequiredGLExtensions()) {
  114. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  115. exit(1);
  116. }
  117. OnResize();
  118. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  119. SDL_PumpEvents();
  120. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  121. Common::g_scm_desc);
  122. Settings::LogSettings();
  123. }
  124. EmuWindow_SDL2_GL::~EmuWindow_SDL2_GL() {
  125. core_context.reset();
  126. SDL_GL_DeleteContext(window_context);
  127. }
  128. std::unique_ptr<Core::Frontend::GraphicsContext> EmuWindow_SDL2_GL::CreateSharedContext() const {
  129. return std::make_unique<SDLGLContext>();
  130. }