bootmanager.cpp 22 KB

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