bootmanager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <glad/glad.h>
  5. #include <QApplication>
  6. #include <QHBoxLayout>
  7. #include <QKeyEvent>
  8. #include <QMessageBox>
  9. #include <QPainter>
  10. #include <QScreen>
  11. #include <QString>
  12. #include <QStringList>
  13. #include <QWindow>
  14. #ifdef HAS_OPENGL
  15. #include <QOffscreenSurface>
  16. #include <QOpenGLContext>
  17. #endif
  18. #if !defined(WIN32) && HAS_VULKAN
  19. #include <qpa/qplatformnativeinterface.h>
  20. #endif
  21. #include <fmt/format.h>
  22. #include "common/assert.h"
  23. #include "common/microprofile.h"
  24. #include "common/scm_rev.h"
  25. #include "common/scope_exit.h"
  26. #include "core/core.h"
  27. #include "core/frontend/framebuffer_layout.h"
  28. #include "core/hle/kernel/process.h"
  29. #include "core/settings.h"
  30. #include "input_common/keyboard.h"
  31. #include "input_common/main.h"
  32. #include "input_common/motion_emu.h"
  33. #include "video_core/renderer_base.h"
  34. #include "video_core/video_core.h"
  35. #include "yuzu/bootmanager.h"
  36. #include "yuzu/main.h"
  37. EmuThread::EmuThread() = default;
  38. EmuThread::~EmuThread() = default;
  39. void EmuThread::run() {
  40. std::string name = "yuzu:EmuControlThread";
  41. MicroProfileOnThreadCreate(name.c_str());
  42. Common::SetCurrentThreadName(name.c_str());
  43. auto& system = Core::System::GetInstance();
  44. system.RegisterHostThread();
  45. auto& gpu = system.GPU();
  46. // Main process has been loaded. Make the context current to this thread and begin GPU and CPU
  47. // execution.
  48. gpu.Start();
  49. gpu.ObtainContext();
  50. emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);
  51. system.Renderer().Rasterizer().LoadDiskResources(
  52. system.CurrentProcess()->GetTitleID(), stop_run,
  53. [this](VideoCore::LoadCallbackStage stage, std::size_t value, std::size_t total) {
  54. emit LoadProgress(stage, value, total);
  55. });
  56. emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0);
  57. gpu.ReleaseContext();
  58. // Holds whether the cpu was running during the last iteration,
  59. // so that the DebugModeLeft signal can be emitted before the
  60. // next execution step
  61. bool was_active = false;
  62. while (!stop_run) {
  63. if (running) {
  64. if (was_active) {
  65. emit DebugModeLeft();
  66. }
  67. running_guard = true;
  68. Core::System::ResultStatus result = system.Run();
  69. if (result != Core::System::ResultStatus::Success) {
  70. running_guard = false;
  71. this->SetRunning(false);
  72. emit ErrorThrown(result, system.GetStatusDetails());
  73. }
  74. running_wait.Wait();
  75. result = system.Pause();
  76. if (result != Core::System::ResultStatus::Success) {
  77. running_guard = false;
  78. this->SetRunning(false);
  79. emit ErrorThrown(result, system.GetStatusDetails());
  80. }
  81. running_guard = false;
  82. if (!stop_run) {
  83. was_active = true;
  84. emit DebugModeEntered();
  85. }
  86. } else if (exec_step) {
  87. UNIMPLEMENTED();
  88. } else {
  89. std::unique_lock lock{running_mutex};
  90. running_cv.wait(lock, [this] { return IsRunning() || exec_step || stop_run; });
  91. }
  92. }
  93. // Shutdown the core emulation
  94. system.Shutdown();
  95. #if MICROPROFILE_ENABLED
  96. MicroProfileOnThreadExit();
  97. #endif
  98. }
  99. #ifdef HAS_OPENGL
  100. class OpenGLSharedContext : public Core::Frontend::GraphicsContext {
  101. public:
  102. /// Create the original context that should be shared from
  103. explicit OpenGLSharedContext(QSurface* surface) : surface(surface) {
  104. QSurfaceFormat format;
  105. format.setVersion(4, 3);
  106. format.setProfile(QSurfaceFormat::CompatibilityProfile);
  107. format.setOption(QSurfaceFormat::FormatOption::DeprecatedFunctions);
  108. if (Settings::values.renderer_debug) {
  109. format.setOption(QSurfaceFormat::FormatOption::DebugContext);
  110. }
  111. // TODO: expose a setting for buffer value (ie default/single/double/triple)
  112. format.setSwapBehavior(QSurfaceFormat::DefaultSwapBehavior);
  113. format.setSwapInterval(0);
  114. context = std::make_unique<QOpenGLContext>();
  115. context->setFormat(format);
  116. if (!context->create()) {
  117. LOG_ERROR(Frontend, "Unable to create main openGL context");
  118. }
  119. }
  120. /// Create the shared contexts for rendering and presentation
  121. explicit OpenGLSharedContext(QOpenGLContext* share_context, QSurface* main_surface = nullptr) {
  122. // disable vsync for any shared contexts
  123. auto format = share_context->format();
  124. format.setSwapInterval(main_surface ? Settings::values.use_vsync.GetValue() : 0);
  125. context = std::make_unique<QOpenGLContext>();
  126. context->setShareContext(share_context);
  127. context->setFormat(format);
  128. if (!context->create()) {
  129. LOG_ERROR(Frontend, "Unable to create shared openGL context");
  130. }
  131. if (!main_surface) {
  132. offscreen_surface = std::make_unique<QOffscreenSurface>(nullptr);
  133. offscreen_surface->setFormat(format);
  134. offscreen_surface->create();
  135. surface = offscreen_surface.get();
  136. } else {
  137. surface = main_surface;
  138. }
  139. }
  140. ~OpenGLSharedContext() {
  141. DoneCurrent();
  142. }
  143. void SwapBuffers() override {
  144. context->swapBuffers(surface);
  145. }
  146. void MakeCurrent() override {
  147. // We can't track the current state of the underlying context in this wrapper class because
  148. // Qt may make the underlying context not current for one reason or another. In particular,
  149. // the WebBrowser uses GL, so it seems to conflict if we aren't careful.
  150. // Instead of always just making the context current (which does not have any caching to
  151. // check if the underlying context is already current) we can check for the current context
  152. // in the thread local data by calling `currentContext()` and checking if its ours.
  153. if (QOpenGLContext::currentContext() != context.get()) {
  154. context->makeCurrent(surface);
  155. }
  156. }
  157. void DoneCurrent() override {
  158. context->doneCurrent();
  159. }
  160. QOpenGLContext* GetShareContext() {
  161. return context.get();
  162. }
  163. const QOpenGLContext* GetShareContext() const {
  164. return context.get();
  165. }
  166. private:
  167. // Avoid using Qt parent system here since we might move the QObjects to new threads
  168. // As a note, this means we should avoid using slots/signals with the objects too
  169. std::unique_ptr<QOpenGLContext> context;
  170. std::unique_ptr<QOffscreenSurface> offscreen_surface{};
  171. QSurface* surface;
  172. };
  173. #endif
  174. class DummyContext : public Core::Frontend::GraphicsContext {};
  175. class RenderWidget : public QWidget {
  176. public:
  177. explicit RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) {
  178. setAttribute(Qt::WA_NativeWindow);
  179. setAttribute(Qt::WA_PaintOnScreen);
  180. }
  181. virtual ~RenderWidget() = default;
  182. QPaintEngine* paintEngine() const override {
  183. return nullptr;
  184. }
  185. private:
  186. GRenderWindow* render_window;
  187. };
  188. class OpenGLRenderWidget : public RenderWidget {
  189. public:
  190. explicit OpenGLRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  191. windowHandle()->setSurfaceType(QWindow::OpenGLSurface);
  192. }
  193. void SetContext(std::unique_ptr<Core::Frontend::GraphicsContext>&& context_) {
  194. context = std::move(context_);
  195. }
  196. private:
  197. std::unique_ptr<Core::Frontend::GraphicsContext> context;
  198. };
  199. #ifdef HAS_VULKAN
  200. class VulkanRenderWidget : public RenderWidget {
  201. public:
  202. explicit VulkanRenderWidget(GRenderWindow* parent) : RenderWidget(parent) {
  203. windowHandle()->setSurfaceType(QWindow::VulkanSurface);
  204. }
  205. };
  206. #endif
  207. static Core::Frontend::WindowSystemType GetWindowSystemType() {
  208. // Determine WSI type based on Qt platform.
  209. QString platform_name = QGuiApplication::platformName();
  210. if (platform_name == QStringLiteral("windows"))
  211. return Core::Frontend::WindowSystemType::Windows;
  212. else if (platform_name == QStringLiteral("xcb"))
  213. return Core::Frontend::WindowSystemType::X11;
  214. else if (platform_name == QStringLiteral("wayland"))
  215. return Core::Frontend::WindowSystemType::Wayland;
  216. LOG_CRITICAL(Frontend, "Unknown Qt platform!");
  217. return Core::Frontend::WindowSystemType::Windows;
  218. }
  219. static Core::Frontend::EmuWindow::WindowSystemInfo GetWindowSystemInfo(QWindow* window) {
  220. Core::Frontend::EmuWindow::WindowSystemInfo wsi;
  221. wsi.type = GetWindowSystemType();
  222. #ifdef HAS_VULKAN
  223. // Our Win32 Qt external doesn't have the private API.
  224. #if defined(WIN32) || defined(__APPLE__)
  225. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  226. #else
  227. QPlatformNativeInterface* pni = QGuiApplication::platformNativeInterface();
  228. wsi.display_connection = pni->nativeResourceForWindow("display", window);
  229. if (wsi.type == Core::Frontend::WindowSystemType::Wayland)
  230. wsi.render_surface = window ? pni->nativeResourceForWindow("surface", window) : nullptr;
  231. else
  232. wsi.render_surface = window ? reinterpret_cast<void*>(window->winId()) : nullptr;
  233. #endif
  234. wsi.render_surface_scale = window ? static_cast<float>(window->devicePixelRatio()) : 1.0f;
  235. #endif
  236. return wsi;
  237. }
  238. GRenderWindow::GRenderWindow(GMainWindow* parent, EmuThread* emu_thread_,
  239. std::shared_ptr<InputCommon::InputSubsystem> input_subsystem_)
  240. : QWidget(parent), emu_thread(emu_thread_), input_subsystem{std::move(input_subsystem_)} {
  241. setWindowTitle(QStringLiteral("yuzu %1 | %2-%3")
  242. .arg(QString::fromUtf8(Common::g_build_name),
  243. QString::fromUtf8(Common::g_scm_branch),
  244. QString::fromUtf8(Common::g_scm_desc)));
  245. setAttribute(Qt::WA_AcceptTouchEvents);
  246. auto layout = new QHBoxLayout(this);
  247. layout->setMargin(0);
  248. setLayout(layout);
  249. input_subsystem->Initialize();
  250. this->setMouseTracking(true);
  251. connect(this, &GRenderWindow::FirstFrameDisplayed, parent, &GMainWindow::OnLoadComplete);
  252. }
  253. GRenderWindow::~GRenderWindow() {
  254. input_subsystem->Shutdown();
  255. }
  256. void GRenderWindow::PollEvents() {
  257. if (!first_frame) {
  258. first_frame = true;
  259. emit FirstFrameDisplayed();
  260. }
  261. }
  262. bool GRenderWindow::IsShown() const {
  263. return !isMinimized();
  264. }
  265. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  266. //
  267. // Older versions get the window size (density independent pixels),
  268. // and hence, do not support DPI scaling ("retina" displays).
  269. // The result will be a viewport that is smaller than the extent of the window.
  270. void GRenderWindow::OnFramebufferSizeChanged() {
  271. // Screen changes potentially incur a change in screen DPI, hence we should update the
  272. // framebuffer size
  273. const qreal pixel_ratio = windowPixelRatio();
  274. const u32 width = this->width() * pixel_ratio;
  275. const u32 height = this->height() * pixel_ratio;
  276. UpdateCurrentFramebufferLayout(width, height);
  277. }
  278. void GRenderWindow::BackupGeometry() {
  279. geometry = QWidget::saveGeometry();
  280. }
  281. void GRenderWindow::RestoreGeometry() {
  282. // We don't want to back up the geometry here (obviously)
  283. QWidget::restoreGeometry(geometry);
  284. }
  285. void GRenderWindow::restoreGeometry(const QByteArray& geometry) {
  286. // Make sure users of this class don't need to deal with backing up the geometry themselves
  287. QWidget::restoreGeometry(geometry);
  288. BackupGeometry();
  289. }
  290. QByteArray GRenderWindow::saveGeometry() {
  291. // If we are a top-level widget, store the current geometry
  292. // otherwise, store the last backup
  293. if (parent() == nullptr) {
  294. return QWidget::saveGeometry();
  295. }
  296. return geometry;
  297. }
  298. qreal GRenderWindow::windowPixelRatio() const {
  299. return devicePixelRatioF();
  300. }
  301. std::pair<u32, u32> GRenderWindow::ScaleTouch(const QPointF& pos) const {
  302. const qreal pixel_ratio = windowPixelRatio();
  303. return {static_cast<u32>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
  304. static_cast<u32>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
  305. }
  306. void GRenderWindow::closeEvent(QCloseEvent* event) {
  307. emit Closed();
  308. QWidget::closeEvent(event);
  309. }
  310. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  311. input_subsystem->GetKeyboard()->PressKey(event->key());
  312. }
  313. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  314. input_subsystem->GetKeyboard()->ReleaseKey(event->key());
  315. }
  316. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  317. if (!Settings::values.touchscreen.enabled) {
  318. input_subsystem->GetKeyboard()->PressKey(event->button());
  319. return;
  320. }
  321. // Touch input is handled in TouchBeginEvent
  322. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  323. return;
  324. }
  325. auto pos = event->pos();
  326. if (event->button() == Qt::LeftButton) {
  327. const auto [x, y] = ScaleTouch(pos);
  328. this->TouchPressed(x, y);
  329. } else if (event->button() == Qt::RightButton) {
  330. input_subsystem->GetMotionEmu()->BeginTilt(pos.x(), pos.y());
  331. }
  332. QWidget::mousePressEvent(event);
  333. }
  334. void GRenderWindow::mouseMoveEvent(QMouseEvent* event) {
  335. // Touch input is handled in TouchUpdateEvent
  336. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  337. return;
  338. }
  339. auto pos = event->pos();
  340. const auto [x, y] = ScaleTouch(pos);
  341. this->TouchMoved(x, y);
  342. input_subsystem->GetMotionEmu()->Tilt(pos.x(), pos.y());
  343. QWidget::mouseMoveEvent(event);
  344. }
  345. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  346. if (!Settings::values.touchscreen.enabled) {
  347. input_subsystem->GetKeyboard()->ReleaseKey(event->button());
  348. return;
  349. }
  350. // Touch input is handled in TouchEndEvent
  351. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  352. return;
  353. }
  354. if (event->button() == Qt::LeftButton) {
  355. this->TouchReleased();
  356. } else if (event->button() == Qt::RightButton) {
  357. input_subsystem->GetMotionEmu()->EndTilt();
  358. }
  359. }
  360. void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
  361. // TouchBegin always has exactly one touch point, so take the .first()
  362. const auto [x, y] = ScaleTouch(event->touchPoints().first().pos());
  363. this->TouchPressed(x, y);
  364. }
  365. void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
  366. QPointF pos;
  367. int active_points = 0;
  368. // average all active touch points
  369. for (const auto& tp : event->touchPoints()) {
  370. if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) {
  371. active_points++;
  372. pos += tp.pos();
  373. }
  374. }
  375. pos /= active_points;
  376. const auto [x, y] = ScaleTouch(pos);
  377. this->TouchMoved(x, y);
  378. }
  379. void GRenderWindow::TouchEndEvent() {
  380. this->TouchReleased();
  381. }
  382. bool GRenderWindow::event(QEvent* event) {
  383. if (event->type() == QEvent::TouchBegin) {
  384. TouchBeginEvent(static_cast<QTouchEvent*>(event));
  385. return true;
  386. } else if (event->type() == QEvent::TouchUpdate) {
  387. TouchUpdateEvent(static_cast<QTouchEvent*>(event));
  388. return true;
  389. } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
  390. TouchEndEvent();
  391. return true;
  392. }
  393. return QWidget::event(event);
  394. }
  395. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  396. QWidget::focusOutEvent(event);
  397. input_subsystem->GetKeyboard()->ReleaseAllKeys();
  398. }
  399. void GRenderWindow::resizeEvent(QResizeEvent* event) {
  400. QWidget::resizeEvent(event);
  401. OnFramebufferSizeChanged();
  402. }
  403. std::unique_ptr<Core::Frontend::GraphicsContext> GRenderWindow::CreateSharedContext() const {
  404. #ifdef HAS_OPENGL
  405. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  406. auto c = static_cast<OpenGLSharedContext*>(main_context.get());
  407. // Bind the shared contexts to the main surface in case the backend wants to take over
  408. // presentation
  409. return std::make_unique<OpenGLSharedContext>(c->GetShareContext(),
  410. child_widget->windowHandle());
  411. }
  412. #endif
  413. return std::make_unique<DummyContext>();
  414. }
  415. bool GRenderWindow::InitRenderTarget() {
  416. ReleaseRenderTarget();
  417. first_frame = false;
  418. switch (Settings::values.renderer_backend.GetValue()) {
  419. case Settings::RendererBackend::OpenGL:
  420. if (!InitializeOpenGL()) {
  421. return false;
  422. }
  423. break;
  424. case Settings::RendererBackend::Vulkan:
  425. if (!InitializeVulkan()) {
  426. return false;
  427. }
  428. break;
  429. }
  430. // Update the Window System information with the new render target
  431. window_info = GetWindowSystemInfo(child_widget->windowHandle());
  432. child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  433. layout()->addWidget(child_widget);
  434. // Reset minimum required size to avoid resizing issues on the main window after restarting.
  435. setMinimumSize(1, 1);
  436. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  437. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  438. OnFramebufferSizeChanged();
  439. BackupGeometry();
  440. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  441. if (!LoadOpenGL()) {
  442. return false;
  443. }
  444. }
  445. return true;
  446. }
  447. void GRenderWindow::ReleaseRenderTarget() {
  448. if (child_widget) {
  449. layout()->removeWidget(child_widget);
  450. child_widget->deleteLater();
  451. child_widget = nullptr;
  452. }
  453. main_context.reset();
  454. }
  455. void GRenderWindow::CaptureScreenshot(u32 res_scale, const QString& screenshot_path) {
  456. auto& renderer = Core::System::GetInstance().Renderer();
  457. if (res_scale == 0) {
  458. res_scale = VideoCore::GetResolutionScaleFactor(renderer);
  459. }
  460. const Layout::FramebufferLayout layout{Layout::FrameLayoutFromResolutionScale(res_scale)};
  461. screenshot_image = QImage(QSize(layout.width, layout.height), QImage::Format_RGB32);
  462. renderer.RequestScreenshot(
  463. screenshot_image.bits(),
  464. [=, this] {
  465. const std::string std_screenshot_path = screenshot_path.toStdString();
  466. if (screenshot_image.mirrored(false, true).save(screenshot_path)) {
  467. LOG_INFO(Frontend, "Screenshot saved to \"{}\"", std_screenshot_path);
  468. } else {
  469. LOG_ERROR(Frontend, "Failed to save screenshot to \"{}\"", std_screenshot_path);
  470. }
  471. },
  472. layout);
  473. }
  474. void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  475. setMinimumSize(minimal_size.first, minimal_size.second);
  476. }
  477. bool GRenderWindow::InitializeOpenGL() {
  478. #ifdef HAS_OPENGL
  479. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  480. // WA_DontShowOnScreen, WA_DeleteOnClose
  481. auto child = new OpenGLRenderWidget(this);
  482. child_widget = child;
  483. child_widget->windowHandle()->create();
  484. auto context = std::make_shared<OpenGLSharedContext>(child->windowHandle());
  485. main_context = context;
  486. child->SetContext(
  487. std::make_unique<OpenGLSharedContext>(context->GetShareContext(), child->windowHandle()));
  488. return true;
  489. #else
  490. QMessageBox::warning(this, tr("OpenGL not available!"),
  491. tr("yuzu has not been compiled with OpenGL support."));
  492. return false;
  493. #endif
  494. }
  495. bool GRenderWindow::InitializeVulkan() {
  496. #ifdef HAS_VULKAN
  497. auto child = new VulkanRenderWidget(this);
  498. child_widget = child;
  499. child_widget->windowHandle()->create();
  500. main_context = std::make_unique<DummyContext>();
  501. return true;
  502. #else
  503. QMessageBox::critical(this, tr("Vulkan not available!"),
  504. tr("yuzu has not been compiled with Vulkan support."));
  505. return false;
  506. #endif
  507. }
  508. bool GRenderWindow::LoadOpenGL() {
  509. auto context = CreateSharedContext();
  510. auto scope = context->Acquire();
  511. if (!gladLoadGL()) {
  512. QMessageBox::warning(
  513. this, tr("Error while initializing OpenGL!"),
  514. tr("Your GPU may not support OpenGL, or you do not have the latest graphics driver."));
  515. return false;
  516. }
  517. const QString renderer =
  518. QString::fromUtf8(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
  519. if (!GLAD_GL_VERSION_4_3) {
  520. LOG_ERROR(Frontend, "GPU does not support OpenGL 4.3: {}", renderer.toStdString());
  521. QMessageBox::warning(this, tr("Error while initializing OpenGL 4.3!"),
  522. tr("Your GPU may not support OpenGL 4.3, or you do not have the "
  523. "latest graphics driver.<br><br>GL Renderer:<br>%1")
  524. .arg(renderer));
  525. return false;
  526. }
  527. QStringList unsupported_gl_extensions = GetUnsupportedGLExtensions();
  528. if (!unsupported_gl_extensions.empty()) {
  529. QMessageBox::warning(
  530. this, tr("Error while initializing OpenGL!"),
  531. tr("Your GPU may not support one or more required OpenGL extensions. Please ensure you "
  532. "have the latest graphics driver.<br><br>GL Renderer:<br>%1<br><br>Unsupported "
  533. "extensions:<br>%2")
  534. .arg(renderer)
  535. .arg(unsupported_gl_extensions.join(QStringLiteral("<br>"))));
  536. return false;
  537. }
  538. return true;
  539. }
  540. QStringList GRenderWindow::GetUnsupportedGLExtensions() const {
  541. QStringList unsupported_ext;
  542. if (!GLAD_GL_ARB_buffer_storage)
  543. unsupported_ext.append(QStringLiteral("ARB_buffer_storage"));
  544. if (!GLAD_GL_ARB_direct_state_access)
  545. unsupported_ext.append(QStringLiteral("ARB_direct_state_access"));
  546. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  547. unsupported_ext.append(QStringLiteral("ARB_vertex_type_10f_11f_11f_rev"));
  548. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  549. unsupported_ext.append(QStringLiteral("ARB_texture_mirror_clamp_to_edge"));
  550. if (!GLAD_GL_ARB_multi_bind)
  551. unsupported_ext.append(QStringLiteral("ARB_multi_bind"));
  552. if (!GLAD_GL_ARB_clip_control)
  553. unsupported_ext.append(QStringLiteral("ARB_clip_control"));
  554. // Extensions required to support some texture formats.
  555. if (!GLAD_GL_EXT_texture_compression_s3tc)
  556. unsupported_ext.append(QStringLiteral("EXT_texture_compression_s3tc"));
  557. if (!GLAD_GL_ARB_texture_compression_rgtc)
  558. unsupported_ext.append(QStringLiteral("ARB_texture_compression_rgtc"));
  559. if (!GLAD_GL_ARB_depth_buffer_float)
  560. unsupported_ext.append(QStringLiteral("ARB_depth_buffer_float"));
  561. if (!unsupported_ext.empty()) {
  562. LOG_ERROR(Frontend, "GPU does not support all required extensions: {}",
  563. glGetString(GL_RENDERER));
  564. }
  565. for (const QString& ext : unsupported_ext) {
  566. LOG_ERROR(Frontend, "Unsupported GL extension: {}", ext.toStdString());
  567. }
  568. return unsupported_ext;
  569. }
  570. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  571. this->emu_thread = emu_thread;
  572. }
  573. void GRenderWindow::OnEmulationStopping() {
  574. emu_thread = nullptr;
  575. }
  576. void GRenderWindow::showEvent(QShowEvent* event) {
  577. QWidget::showEvent(event);
  578. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  579. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  580. Qt::UniqueConnection);
  581. }