emu_window_sdl2_hide.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "core/settings.h"
  14. #include "input_common/main.h"
  15. #include "yuzu_tester/emu_window/emu_window_sdl2_hide.h"
  16. bool EmuWindow_SDL2_Hide::SupportsRequiredGLExtensions() {
  17. std::vector<std::string> unsupported_ext;
  18. if (!GLAD_GL_ARB_direct_state_access)
  19. unsupported_ext.push_back("ARB_direct_state_access");
  20. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  21. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  22. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  23. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  24. if (!GLAD_GL_ARB_multi_bind)
  25. unsupported_ext.push_back("ARB_multi_bind");
  26. // Extensions required to support some texture formats.
  27. if (!GLAD_GL_EXT_texture_compression_s3tc)
  28. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  29. if (!GLAD_GL_ARB_texture_compression_rgtc)
  30. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  31. if (!GLAD_GL_ARB_depth_buffer_float)
  32. unsupported_ext.push_back("ARB_depth_buffer_float");
  33. for (const std::string& ext : unsupported_ext)
  34. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", ext);
  35. return unsupported_ext.empty();
  36. }
  37. EmuWindow_SDL2_Hide::EmuWindow_SDL2_Hide() {
  38. // Initialize the window
  39. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  40. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  41. exit(1);
  42. }
  43. InputCommon::Init();
  44. SDL_SetMainReady();
  45. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  46. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  47. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  48. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  49. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  50. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  51. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  52. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  53. std::string window_title = fmt::format("yuzu-tester {} | {}-{}", Common::g_build_fullname,
  54. Common::g_scm_branch, Common::g_scm_desc);
  55. render_window = SDL_CreateWindow(window_title.c_str(),
  56. SDL_WINDOWPOS_UNDEFINED, // x position
  57. SDL_WINDOWPOS_UNDEFINED, // y position
  58. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  59. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE |
  60. SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
  61. if (render_window == nullptr) {
  62. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  63. exit(1);
  64. }
  65. gl_context = SDL_GL_CreateContext(render_window);
  66. if (gl_context == nullptr) {
  67. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! {}", SDL_GetError());
  68. exit(1);
  69. }
  70. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  71. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  72. exit(1);
  73. }
  74. if (!SupportsRequiredGLExtensions()) {
  75. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  76. exit(1);
  77. }
  78. SDL_PumpEvents();
  79. SDL_GL_SetSwapInterval(false);
  80. LOG_INFO(Frontend, "yuzu-tester Version: {} | {}-{}", Common::g_build_fullname,
  81. Common::g_scm_branch, Common::g_scm_desc);
  82. Settings::LogSettings();
  83. DoneCurrent();
  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::SwapBuffers() {
  91. SDL_GL_SwapWindow(render_window);
  92. }
  93. void EmuWindow_SDL2_Hide::PollEvents() {}
  94. void EmuWindow_SDL2_Hide::MakeCurrent() {
  95. SDL_GL_MakeCurrent(render_window, gl_context);
  96. }
  97. void EmuWindow_SDL2_Hide::DoneCurrent() {
  98. SDL_GL_MakeCurrent(render_window, nullptr);
  99. }