emu_window_sdl2_hide.cpp 4.9 KB

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