emu_window_sdl2_gl.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project & 2024 suyu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <string>
  6. #define SDL_MAIN_HANDLED
  7. #include <SDL.h>
  8. #include <fmt/format.h>
  9. #include <glad/glad.h>
  10. #include "common/logging/log.h"
  11. #include "common/scm_rev.h"
  12. #include "common/settings.h"
  13. #include "common/string_util.h"
  14. #include "core/core.h"
  15. #include "input_common/main.h"
  16. #include "suyu_cmd/emu_window/emu_window_sdl2_gl.h"
  17. #include "video_core/renderer_base.h"
  18. class SDLGLContext : public Core::Frontend::GraphicsContext {
  19. public:
  20. explicit SDLGLContext(SDL_Window* window_) : window{window_} {
  21. context = SDL_GL_CreateContext(window);
  22. }
  23. ~SDLGLContext() {
  24. DoneCurrent();
  25. SDL_GL_DeleteContext(context);
  26. }
  27. void SwapBuffers() override {
  28. SDL_GL_SwapWindow(window);
  29. }
  30. void MakeCurrent() override {
  31. if (is_current) {
  32. return;
  33. }
  34. is_current = SDL_GL_MakeCurrent(window, context) == 0;
  35. }
  36. void DoneCurrent() override {
  37. if (!is_current) {
  38. return;
  39. }
  40. SDL_GL_MakeCurrent(window, nullptr);
  41. is_current = false;
  42. }
  43. private:
  44. SDL_Window* window;
  45. SDL_GLContext context;
  46. bool is_current = false;
  47. };
  48. bool EmuWindow_SDL2_GL::SupportsRequiredGLExtensions() {
  49. std::vector<std::string_view> unsupported_ext;
  50. // Extensions required to support some texture formats.
  51. if (!GLAD_GL_EXT_texture_compression_s3tc) {
  52. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  53. }
  54. if (!GLAD_GL_ARB_texture_compression_rgtc) {
  55. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  56. }
  57. for (const auto& extension : unsupported_ext) {
  58. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", extension);
  59. }
  60. return unsupported_ext.empty();
  61. }
  62. EmuWindow_SDL2_GL::EmuWindow_SDL2_GL(InputCommon::InputSubsystem* input_subsystem_,
  63. Core::System& system_, bool fullscreen)
  64. : EmuWindow_SDL2{input_subsystem_, system_} {
  65. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  66. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
  67. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
  68. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  69. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  70. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  71. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  72. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  73. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  74. if (Settings::values.renderer_debug) {
  75. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
  76. }
  77. SDL_GL_SetSwapInterval(0);
  78. std::string window_title = fmt::format("suyu {} | {}-{}", Common::g_build_fullname,
  79. Common::g_scm_branch, Common::g_scm_desc);
  80. render_window =
  81. SDL_CreateWindow(window_title.c_str(),
  82. SDL_WINDOWPOS_UNDEFINED, // x position
  83. SDL_WINDOWPOS_UNDEFINED, // y position
  84. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  85. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  86. if (render_window == nullptr) {
  87. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  88. exit(1);
  89. }
  90. strict_context_required = strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0;
  91. SetWindowIcon();
  92. if (fullscreen) {
  93. Fullscreen();
  94. ShowCursor(false);
  95. }
  96. window_context = SDL_GL_CreateContext(render_window);
  97. core_context = CreateSharedContext();
  98. if (window_context == nullptr) {
  99. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context: {}", SDL_GetError());
  100. exit(1);
  101. }
  102. if (core_context == nullptr) {
  103. LOG_CRITICAL(Frontend, "Failed to create shared SDL2 GL context: {}", SDL_GetError());
  104. exit(1);
  105. }
  106. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  107. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  108. exit(1);
  109. }
  110. if (!SupportsRequiredGLExtensions()) {
  111. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  112. exit(1);
  113. }
  114. OnResize();
  115. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  116. SDL_PumpEvents();
  117. LOG_INFO(Frontend, "suyu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  118. Common::g_scm_desc);
  119. Settings::LogSettings();
  120. }
  121. EmuWindow_SDL2_GL::~EmuWindow_SDL2_GL() {
  122. core_context.reset();
  123. SDL_GL_DeleteContext(window_context);
  124. }
  125. std::unique_ptr<Core::Frontend::GraphicsContext> EmuWindow_SDL2_GL::CreateSharedContext() const {
  126. return std::make_unique<SDLGLContext>(render_window);
  127. }