emu_window_sdl2_gl.cpp 5.1 KB

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