bootmanager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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/mouse/mouse_input.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. connect(this, &GRenderWindow::ExecuteProgramSignal, parent, &GMainWindow::OnExecuteProgram,
  253. Qt::QueuedConnection);
  254. }
  255. void GRenderWindow::ExecuteProgram(std::size_t program_index) {
  256. emit ExecuteProgramSignal(program_index);
  257. }
  258. GRenderWindow::~GRenderWindow() {
  259. input_subsystem->Shutdown();
  260. }
  261. void GRenderWindow::OnFrameDisplayed() {
  262. if (!first_frame) {
  263. first_frame = true;
  264. emit FirstFrameDisplayed();
  265. }
  266. }
  267. bool GRenderWindow::IsShown() const {
  268. return !isMinimized();
  269. }
  270. // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels).
  271. //
  272. // Older versions get the window size (density independent pixels),
  273. // and hence, do not support DPI scaling ("retina" displays).
  274. // The result will be a viewport that is smaller than the extent of the window.
  275. void GRenderWindow::OnFramebufferSizeChanged() {
  276. // Screen changes potentially incur a change in screen DPI, hence we should update the
  277. // framebuffer size
  278. const qreal pixel_ratio = windowPixelRatio();
  279. const u32 width = this->width() * pixel_ratio;
  280. const u32 height = this->height() * pixel_ratio;
  281. UpdateCurrentFramebufferLayout(width, height);
  282. }
  283. void GRenderWindow::BackupGeometry() {
  284. geometry = QWidget::saveGeometry();
  285. }
  286. void GRenderWindow::RestoreGeometry() {
  287. // We don't want to back up the geometry here (obviously)
  288. QWidget::restoreGeometry(geometry);
  289. }
  290. void GRenderWindow::restoreGeometry(const QByteArray& geometry) {
  291. // Make sure users of this class don't need to deal with backing up the geometry themselves
  292. QWidget::restoreGeometry(geometry);
  293. BackupGeometry();
  294. }
  295. QByteArray GRenderWindow::saveGeometry() {
  296. // If we are a top-level widget, store the current geometry
  297. // otherwise, store the last backup
  298. if (parent() == nullptr) {
  299. return QWidget::saveGeometry();
  300. }
  301. return geometry;
  302. }
  303. qreal GRenderWindow::windowPixelRatio() const {
  304. return devicePixelRatioF();
  305. }
  306. std::pair<u32, u32> GRenderWindow::ScaleTouch(const QPointF& pos) const {
  307. const qreal pixel_ratio = windowPixelRatio();
  308. return {static_cast<u32>(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})),
  309. static_cast<u32>(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))};
  310. }
  311. void GRenderWindow::closeEvent(QCloseEvent* event) {
  312. emit Closed();
  313. QWidget::closeEvent(event);
  314. }
  315. void GRenderWindow::keyPressEvent(QKeyEvent* event) {
  316. input_subsystem->GetKeyboard()->PressKey(event->key());
  317. }
  318. void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
  319. input_subsystem->GetKeyboard()->ReleaseKey(event->key());
  320. }
  321. void GRenderWindow::mousePressEvent(QMouseEvent* event) {
  322. // Touch input is handled in TouchBeginEvent
  323. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  324. return;
  325. }
  326. auto pos = event->pos();
  327. const auto [x, y] = ScaleTouch(pos);
  328. input_subsystem->GetMouse()->PressButton(x, y, event->button());
  329. if (event->button() == Qt::LeftButton) {
  330. this->TouchPressed(x, 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. input_subsystem->GetMouse()->MouseMove(x, y);
  342. this->TouchMoved(x, y);
  343. QWidget::mouseMoveEvent(event);
  344. }
  345. void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
  346. // Touch input is handled in TouchEndEvent
  347. if (event->source() == Qt::MouseEventSynthesizedBySystem) {
  348. return;
  349. }
  350. input_subsystem->GetMouse()->ReleaseButton(event->button());
  351. if (event->button() == Qt::LeftButton) {
  352. this->TouchReleased();
  353. }
  354. }
  355. void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) {
  356. // TouchBegin always has exactly one touch point, so take the .first()
  357. const auto [x, y] = ScaleTouch(event->touchPoints().first().pos());
  358. this->TouchPressed(x, y);
  359. }
  360. void GRenderWindow::TouchUpdateEvent(const QTouchEvent* event) {
  361. QPointF pos;
  362. int active_points = 0;
  363. // average all active touch points
  364. for (const auto& tp : event->touchPoints()) {
  365. if (tp.state() & (Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointStationary)) {
  366. active_points++;
  367. pos += tp.pos();
  368. }
  369. }
  370. pos /= active_points;
  371. const auto [x, y] = ScaleTouch(pos);
  372. this->TouchMoved(x, y);
  373. }
  374. void GRenderWindow::TouchEndEvent() {
  375. this->TouchReleased();
  376. }
  377. bool GRenderWindow::event(QEvent* event) {
  378. if (event->type() == QEvent::TouchBegin) {
  379. TouchBeginEvent(static_cast<QTouchEvent*>(event));
  380. return true;
  381. } else if (event->type() == QEvent::TouchUpdate) {
  382. TouchUpdateEvent(static_cast<QTouchEvent*>(event));
  383. return true;
  384. } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
  385. TouchEndEvent();
  386. return true;
  387. }
  388. return QWidget::event(event);
  389. }
  390. void GRenderWindow::focusOutEvent(QFocusEvent* event) {
  391. QWidget::focusOutEvent(event);
  392. input_subsystem->GetKeyboard()->ReleaseAllKeys();
  393. }
  394. void GRenderWindow::resizeEvent(QResizeEvent* event) {
  395. QWidget::resizeEvent(event);
  396. OnFramebufferSizeChanged();
  397. }
  398. std::unique_ptr<Core::Frontend::GraphicsContext> GRenderWindow::CreateSharedContext() const {
  399. #ifdef HAS_OPENGL
  400. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  401. auto c = static_cast<OpenGLSharedContext*>(main_context.get());
  402. // Bind the shared contexts to the main surface in case the backend wants to take over
  403. // presentation
  404. return std::make_unique<OpenGLSharedContext>(c->GetShareContext(),
  405. child_widget->windowHandle());
  406. }
  407. #endif
  408. return std::make_unique<DummyContext>();
  409. }
  410. bool GRenderWindow::InitRenderTarget() {
  411. ReleaseRenderTarget();
  412. first_frame = false;
  413. switch (Settings::values.renderer_backend.GetValue()) {
  414. case Settings::RendererBackend::OpenGL:
  415. if (!InitializeOpenGL()) {
  416. return false;
  417. }
  418. break;
  419. case Settings::RendererBackend::Vulkan:
  420. if (!InitializeVulkan()) {
  421. return false;
  422. }
  423. break;
  424. }
  425. // Update the Window System information with the new render target
  426. window_info = GetWindowSystemInfo(child_widget->windowHandle());
  427. child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  428. layout()->addWidget(child_widget);
  429. // Reset minimum required size to avoid resizing issues on the main window after restarting.
  430. setMinimumSize(1, 1);
  431. resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height);
  432. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  433. OnFramebufferSizeChanged();
  434. BackupGeometry();
  435. if (Settings::values.renderer_backend.GetValue() == Settings::RendererBackend::OpenGL) {
  436. if (!LoadOpenGL()) {
  437. return false;
  438. }
  439. }
  440. return true;
  441. }
  442. void GRenderWindow::ReleaseRenderTarget() {
  443. if (child_widget) {
  444. layout()->removeWidget(child_widget);
  445. child_widget->deleteLater();
  446. child_widget = nullptr;
  447. }
  448. main_context.reset();
  449. }
  450. void GRenderWindow::CaptureScreenshot(u32 res_scale, const QString& screenshot_path) {
  451. auto& renderer = Core::System::GetInstance().Renderer();
  452. if (res_scale == 0) {
  453. res_scale = VideoCore::GetResolutionScaleFactor(renderer);
  454. }
  455. const Layout::FramebufferLayout layout{Layout::FrameLayoutFromResolutionScale(res_scale)};
  456. screenshot_image = QImage(QSize(layout.width, layout.height), QImage::Format_RGB32);
  457. renderer.RequestScreenshot(
  458. screenshot_image.bits(),
  459. [=, this] {
  460. const std::string std_screenshot_path = screenshot_path.toStdString();
  461. if (screenshot_image.mirrored(false, true).save(screenshot_path)) {
  462. LOG_INFO(Frontend, "Screenshot saved to \"{}\"", std_screenshot_path);
  463. } else {
  464. LOG_ERROR(Frontend, "Failed to save screenshot to \"{}\"", std_screenshot_path);
  465. }
  466. },
  467. layout);
  468. }
  469. void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  470. setMinimumSize(minimal_size.first, minimal_size.second);
  471. }
  472. bool GRenderWindow::InitializeOpenGL() {
  473. #ifdef HAS_OPENGL
  474. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground,
  475. // WA_DontShowOnScreen, WA_DeleteOnClose
  476. auto child = new OpenGLRenderWidget(this);
  477. child_widget = child;
  478. child_widget->windowHandle()->create();
  479. auto context = std::make_shared<OpenGLSharedContext>(child->windowHandle());
  480. main_context = context;
  481. child->SetContext(
  482. std::make_unique<OpenGLSharedContext>(context->GetShareContext(), child->windowHandle()));
  483. return true;
  484. #else
  485. QMessageBox::warning(this, tr("OpenGL not available!"),
  486. tr("yuzu has not been compiled with OpenGL support."));
  487. return false;
  488. #endif
  489. }
  490. bool GRenderWindow::InitializeVulkan() {
  491. #ifdef HAS_VULKAN
  492. auto child = new VulkanRenderWidget(this);
  493. child_widget = child;
  494. child_widget->windowHandle()->create();
  495. main_context = std::make_unique<DummyContext>();
  496. return true;
  497. #else
  498. QMessageBox::critical(this, tr("Vulkan not available!"),
  499. tr("yuzu has not been compiled with Vulkan support."));
  500. return false;
  501. #endif
  502. }
  503. bool GRenderWindow::LoadOpenGL() {
  504. auto context = CreateSharedContext();
  505. auto scope = context->Acquire();
  506. if (!gladLoadGL()) {
  507. QMessageBox::warning(
  508. this, tr("Error while initializing OpenGL!"),
  509. tr("Your GPU may not support OpenGL, or you do not have the latest graphics driver."));
  510. return false;
  511. }
  512. const QString renderer =
  513. QString::fromUtf8(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
  514. if (!GLAD_GL_VERSION_4_3) {
  515. LOG_ERROR(Frontend, "GPU does not support OpenGL 4.3: {}", renderer.toStdString());
  516. QMessageBox::warning(this, tr("Error while initializing OpenGL 4.3!"),
  517. tr("Your GPU may not support OpenGL 4.3, or you do not have the "
  518. "latest graphics driver.<br><br>GL Renderer:<br>%1")
  519. .arg(renderer));
  520. return false;
  521. }
  522. QStringList unsupported_gl_extensions = GetUnsupportedGLExtensions();
  523. if (!unsupported_gl_extensions.empty()) {
  524. QMessageBox::warning(
  525. this, tr("Error while initializing OpenGL!"),
  526. tr("Your GPU may not support one or more required OpenGL extensions. Please ensure you "
  527. "have the latest graphics driver.<br><br>GL Renderer:<br>%1<br><br>Unsupported "
  528. "extensions:<br>%2")
  529. .arg(renderer)
  530. .arg(unsupported_gl_extensions.join(QStringLiteral("<br>"))));
  531. return false;
  532. }
  533. return true;
  534. }
  535. QStringList GRenderWindow::GetUnsupportedGLExtensions() const {
  536. QStringList unsupported_ext;
  537. if (!GLAD_GL_ARB_buffer_storage)
  538. unsupported_ext.append(QStringLiteral("ARB_buffer_storage"));
  539. if (!GLAD_GL_ARB_direct_state_access)
  540. unsupported_ext.append(QStringLiteral("ARB_direct_state_access"));
  541. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  542. unsupported_ext.append(QStringLiteral("ARB_vertex_type_10f_11f_11f_rev"));
  543. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  544. unsupported_ext.append(QStringLiteral("ARB_texture_mirror_clamp_to_edge"));
  545. if (!GLAD_GL_ARB_multi_bind)
  546. unsupported_ext.append(QStringLiteral("ARB_multi_bind"));
  547. if (!GLAD_GL_ARB_clip_control)
  548. unsupported_ext.append(QStringLiteral("ARB_clip_control"));
  549. // Extensions required to support some texture formats.
  550. if (!GLAD_GL_EXT_texture_compression_s3tc)
  551. unsupported_ext.append(QStringLiteral("EXT_texture_compression_s3tc"));
  552. if (!GLAD_GL_ARB_texture_compression_rgtc)
  553. unsupported_ext.append(QStringLiteral("ARB_texture_compression_rgtc"));
  554. if (!GLAD_GL_ARB_depth_buffer_float)
  555. unsupported_ext.append(QStringLiteral("ARB_depth_buffer_float"));
  556. if (!unsupported_ext.empty()) {
  557. LOG_ERROR(Frontend, "GPU does not support all required extensions: {}",
  558. glGetString(GL_RENDERER));
  559. }
  560. for (const QString& ext : unsupported_ext) {
  561. LOG_ERROR(Frontend, "Unsupported GL extension: {}", ext.toStdString());
  562. }
  563. return unsupported_ext;
  564. }
  565. void GRenderWindow::OnEmulationStarting(EmuThread* emu_thread) {
  566. this->emu_thread = emu_thread;
  567. }
  568. void GRenderWindow::OnEmulationStopping() {
  569. emu_thread = nullptr;
  570. }
  571. void GRenderWindow::showEvent(QShowEvent* event) {
  572. QWidget::showEvent(event);
  573. // windowHandle() is not initialized until the Window is shown, so we connect it here.
  574. connect(windowHandle(), &QWindow::screenChanged, this, &GRenderWindow::OnFramebufferSizeChanged,
  575. Qt::UniqueConnection);
  576. }