emu_window_sdl2_gl.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/settings.h"
  15. #include "common/string_util.h"
  16. #include "core/core.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(SDL_Window* window_) : window{window_} {
  24. context = SDL_GL_CreateContext(window);
  25. }
  26. ~SDLGLContext() {
  27. DoneCurrent();
  28. SDL_GL_DeleteContext(context);
  29. }
  30. void SwapBuffers() override {
  31. SDL_GL_SwapWindow(window);
  32. }
  33. void MakeCurrent() override {
  34. if (is_current) {
  35. return;
  36. }
  37. is_current = SDL_GL_MakeCurrent(window, context) == 0;
  38. }
  39. void DoneCurrent() override {
  40. if (!is_current) {
  41. return;
  42. }
  43. SDL_GL_MakeCurrent(window, nullptr);
  44. is_current = false;
  45. }
  46. private:
  47. SDL_Window* window;
  48. SDL_GLContext context;
  49. bool is_current = false;
  50. };
  51. bool EmuWindow_SDL2_GL::SupportsRequiredGLExtensions() {
  52. std::vector<std::string_view> unsupported_ext;
  53. // Extensions required to support some texture formats.
  54. if (!GLAD_GL_EXT_texture_compression_s3tc) {
  55. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  56. }
  57. if (!GLAD_GL_ARB_texture_compression_rgtc) {
  58. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  59. }
  60. for (const auto& extension : unsupported_ext) {
  61. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", extension);
  62. }
  63. return unsupported_ext.empty();
  64. }
  65. EmuWindow_SDL2_GL::EmuWindow_SDL2_GL(InputCommon::InputSubsystem* input_subsystem,
  66. Core::System& system_, bool fullscreen)
  67. : EmuWindow_SDL2{input_subsystem, system_} {
  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. SetWindowIcon();
  94. if (fullscreen) {
  95. Fullscreen();
  96. ShowCursor(false);
  97. }
  98. window_context = SDL_GL_CreateContext(render_window);
  99. core_context = CreateSharedContext();
  100. if (window_context == nullptr) {
  101. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context: {}", SDL_GetError());
  102. exit(1);
  103. }
  104. if (core_context == nullptr) {
  105. LOG_CRITICAL(Frontend, "Failed to create shared SDL2 GL context: {}", SDL_GetError());
  106. exit(1);
  107. }
  108. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  109. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  110. exit(1);
  111. }
  112. if (!SupportsRequiredGLExtensions()) {
  113. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  114. exit(1);
  115. }
  116. OnResize();
  117. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  118. SDL_PumpEvents();
  119. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  120. Common::g_scm_desc);
  121. Settings::LogSettings();
  122. }
  123. EmuWindow_SDL2_GL::~EmuWindow_SDL2_GL() {
  124. core_context.reset();
  125. SDL_GL_DeleteContext(window_context);
  126. }
  127. std::unique_ptr<Core::Frontend::GraphicsContext> EmuWindow_SDL2_GL::CreateSharedContext() const {
  128. return std::make_unique<SDLGLContext>(render_window);
  129. }