From 282adfc70b5d7d958d564bfda0227bb3fbd8d110 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Tue, 24 Mar 2020 20:58:49 -0600 Subject: Frontend/GPU: Refactor context management Changes the GraphicsContext to be managed by the GPU core. This eliminates the need for the frontends to fool around with tricky MakeCurrent/DoneCurrent calls that are dependent on the settings (such as async gpu option). This also refactors out the need to use QWidget::fromWindowContainer as that caused issues with focus and input handling. Now we use a regular QWidget and just access the native windowHandle() directly. Another change is removing the debug tool setting in FrameMailbox. Instead of trying to block the frontend until a new frame is ready, the core will now take over presentation and draw directly to the window if the renderer detects that its hooked by NSight or RenderDoc Lastly, since it was in the way, I removed ScopeAcquireWindowContext and replaced it with a simple subclass in GraphicsContext that achieves the same result --- src/yuzu/bootmanager.cpp | 375 +++++++++++++++++++++++------------------------ src/yuzu/bootmanager.h | 37 ++--- src/yuzu/main.cpp | 3 +- 3 files changed, 198 insertions(+), 217 deletions(-) (limited to 'src/yuzu') diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index c3dbb1a88..d120ee818 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -10,9 +10,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -29,7 +26,6 @@ #include "common/scope_exit.h" #include "core/core.h" #include "core/frontend/framebuffer_layout.h" -#include "core/frontend/scope_acquire_context.h" #include "core/settings.h" #include "input_common/keyboard.h" #include "input_common/main.h" @@ -39,27 +35,13 @@ #include "yuzu/bootmanager.h" #include "yuzu/main.h" -EmuThread::EmuThread(GRenderWindow& window) - : shared_context{window.CreateSharedContext()}, - context{(Settings::values.use_asynchronous_gpu_emulation && shared_context) ? *shared_context - : window} {} +EmuThread::EmuThread() = default; EmuThread::~EmuThread() = default; -static GMainWindow* GetMainWindow() { - for (QWidget* w : qApp->topLevelWidgets()) { - if (GMainWindow* main = qobject_cast(w)) { - return main; - } - } - return nullptr; -} - void EmuThread::run() { MicroProfileOnThreadCreate("EmuThread"); - Core::Frontend::ScopeAcquireContext acquire_context{context}; - emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0); Core::System::GetInstance().Renderer().Rasterizer().LoadDiskResources( @@ -69,6 +51,10 @@ void EmuThread::run() { emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0); + // Main process has been loaded. Make the context current to this thread and begin GPU and CPU + // execution. + Core::System::GetInstance().GPU().Start(); + // Holds whether the cpu was running during the last iteration, // so that the DebugModeLeft signal can be emitted before the // next execution step @@ -111,162 +97,195 @@ void EmuThread::run() { #endif } -class GGLContext : public Core::Frontend::GraphicsContext { +class OpenGLSharedContext : public Core::Frontend::GraphicsContext { public: - explicit GGLContext(QOpenGLContext* shared_context) - : context(new QOpenGLContext(shared_context->parent())), - surface(new QOffscreenSurface(nullptr)) { + /// Create the original context that should be shared from + explicit OpenGLSharedContext(QSurface* surface) : surface(surface) { + QSurfaceFormat format; + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CompatibilityProfile); + format.setOption(QSurfaceFormat::FormatOption::DeprecatedFunctions); + // TODO: expose a setting for buffer value (ie default/single/double/triple) + format.setSwapBehavior(QSurfaceFormat::DefaultSwapBehavior); + format.setSwapInterval(0); + + context = std::make_unique(); + context->setFormat(format); + if (!context->create()) { + LOG_ERROR(Frontend, "Unable to create main openGL context"); + } + } + + /// Create the shared contexts for rendering and presentation + explicit OpenGLSharedContext(QOpenGLContext* share_context, QSurface* main_surface = nullptr) { // disable vsync for any shared contexts - auto format = shared_context->format(); - format.setSwapInterval(0); + auto format = share_context->format(); + format.setSwapInterval(main_surface ? Settings::values.use_vsync : 0); - context->setShareContext(shared_context); + context = std::make_unique(); + context->setShareContext(share_context); context->setFormat(format); - context->create(); - surface->setParent(shared_context->parent()); - surface->setFormat(format); - surface->create(); + if (!context->create()) { + LOG_ERROR(Frontend, "Unable to create shared openGL context"); + } + + if (!main_surface) { + offscreen_surface = std::make_unique(nullptr); + offscreen_surface->setFormat(format); + offscreen_surface->create(); + surface = offscreen_surface.get(); + } else { + surface = main_surface; + } + } + + ~OpenGLSharedContext() { + context->doneCurrent(); + } + + void SwapBuffers() override { + context->swapBuffers(surface); } void MakeCurrent() override { + if (is_current) { + return; + } context->makeCurrent(surface); } void DoneCurrent() override { context->doneCurrent(); + is_current = false; + } + + QOpenGLContext* GetShareContext() const { + return context.get(); } private: - QOpenGLContext* context; - QOffscreenSurface* surface; + // Avoid using Qt parent system here since we might move the QObjects to new threads + // As a note, this means we should avoid using slots/signals with the objects too + std::unique_ptr context; + std::unique_ptr offscreen_surface{}; + QSurface* surface; + bool is_current = false; }; -class ChildRenderWindow : public QWindow { +class DummyContext : public Core::Frontend::GraphicsContext {}; + +class RenderWidget : public QWidget { public: - ChildRenderWindow(QWindow* parent, QWidget* event_handler) - : QWindow{parent}, event_handler{event_handler} {} - - virtual ~ChildRenderWindow() = default; - - virtual void Present() = 0; - -protected: - bool event(QEvent* event) override { - switch (event->type()) { - case QEvent::UpdateRequest: - Present(); - return true; - case QEvent::MouseButtonPress: - case QEvent::MouseButtonRelease: - case QEvent::MouseButtonDblClick: - case QEvent::MouseMove: - case QEvent::KeyPress: - case QEvent::KeyRelease: - case QEvent::FocusIn: - case QEvent::FocusOut: - case QEvent::FocusAboutToChange: - case QEvent::Enter: - case QEvent::Leave: - case QEvent::Wheel: - case QEvent::TabletMove: - case QEvent::TabletPress: - case QEvent::TabletRelease: - case QEvent::TabletEnterProximity: - case QEvent::TabletLeaveProximity: - case QEvent::TouchBegin: - case QEvent::TouchUpdate: - case QEvent::TouchEnd: - case QEvent::InputMethodQuery: - case QEvent::TouchCancel: - return QCoreApplication::sendEvent(event_handler, event); - case QEvent::Drop: - GetMainWindow()->DropAction(static_cast(event)); - return true; - case QEvent::DragResponse: - case QEvent::DragEnter: - case QEvent::DragLeave: - case QEvent::DragMove: - GetMainWindow()->AcceptDropEvent(static_cast(event)); - return true; - default: - return QWindow::event(event); - } + RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) { + setAttribute(Qt::WA_NativeWindow); + setAttribute(Qt::WA_PaintOnScreen); } - void exposeEvent(QExposeEvent* event) override { - QWindow::requestUpdate(); - QWindow::exposeEvent(event); + virtual ~RenderWidget() = default; + + virtual void Present() {} + + void paintEvent(QPaintEvent* event) override { + Present(); + update(); } -private: - QWidget* event_handler{}; -}; + void resizeEvent(QResizeEvent* ev) override { + render_window->resize(ev->size()); + render_window->OnFramebufferSizeChanged(); + } -class OpenGLWindow final : public ChildRenderWindow { -public: - OpenGLWindow(QWindow* parent, QWidget* event_handler, QOpenGLContext* shared_context) - : ChildRenderWindow{parent, event_handler}, - context(new QOpenGLContext(shared_context->parent())) { + void keyPressEvent(QKeyEvent* event) override { + InputCommon::GetKeyboard()->PressKey(event->key()); + } - // disable vsync for any shared contexts - auto format = shared_context->format(); - format.setSwapInterval(Settings::values.use_vsync ? 1 : 0); - this->setFormat(format); + void keyReleaseEvent(QKeyEvent* event) override { + InputCommon::GetKeyboard()->ReleaseKey(event->key()); + } - context->setShareContext(shared_context); - context->setScreen(this->screen()); - context->setFormat(format); - context->create(); + void mousePressEvent(QMouseEvent* event) override { + if (event->source() == Qt::MouseEventSynthesizedBySystem) + return; // touch input is handled in TouchBeginEvent - setSurfaceType(QWindow::OpenGLSurface); + const auto pos{event->pos()}; + if (event->button() == Qt::LeftButton) { + const auto [x, y] = render_window->ScaleTouch(pos); + render_window->TouchPressed(x, y); + } else if (event->button() == Qt::RightButton) { + InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y()); + } + } + + void mouseMoveEvent(QMouseEvent* event) override { + if (event->source() == Qt::MouseEventSynthesizedBySystem) + return; // touch input is handled in TouchUpdateEvent - // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, - // WA_DontShowOnScreen, WA_DeleteOnClose + const auto pos{event->pos()}; + const auto [x, y] = render_window->ScaleTouch(pos); + render_window->TouchMoved(x, y); + InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y()); } - ~OpenGLWindow() override { - context->doneCurrent(); + void mouseReleaseEvent(QMouseEvent* event) override { + if (event->source() == Qt::MouseEventSynthesizedBySystem) + return; // touch input is handled in TouchEndEvent + + if (event->button() == Qt::LeftButton) + render_window->TouchReleased(); + else if (event->button() == Qt::RightButton) + InputCommon::GetMotionEmu()->EndTilt(); } - void Present() override { - if (!isExposed()) { - return; - } + std::pair GetSize() const { + return std::make_pair(width(), height()); + } - context->makeCurrent(this); - Core::System::GetInstance().Renderer().TryPresent(100); - context->swapBuffers(this); - auto f = context->versionFunctions(); - f->glFinish(); - QWindow::requestUpdate(); + QPaintEngine* paintEngine() const override { + return nullptr; } private: - QOpenGLContext* context{}; + GRenderWindow* render_window; }; -#ifdef HAS_VULKAN -class VulkanWindow final : public ChildRenderWindow { +class OpenGLRenderWidget : public RenderWidget { public: - VulkanWindow(QWindow* parent, QWidget* event_handler, QVulkanInstance* instance) - : ChildRenderWindow{parent, event_handler} { - setSurfaceType(QSurface::SurfaceType::VulkanSurface); - setVulkanInstance(instance); + explicit OpenGLRenderWidget(GRenderWindow* parent) : RenderWidget(parent) { + windowHandle()->setSurfaceType(QWindow::OpenGLSurface); } - ~VulkanWindow() override = default; + void SetContext(std::unique_ptr&& context_) { + context = std::move(context_); + } void Present() override { - // TODO(bunnei): ImplementMe + if (!isVisible()) { + return; + } + + context->MakeCurrent(); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + if (Core::System::GetInstance().Renderer().TryPresent(100)) { + context->SwapBuffers(); + glFinish(); + } } private: - QWidget* event_handler{}; + std::unique_ptr context{}; }; -#endif -GRenderWindow::GRenderWindow(QWidget* parent_, EmuThread* emu_thread) +class VulkanRenderWidget : public RenderWidget { +public: + explicit VulkanRenderWidget(GRenderWindow* parent, QVulkanInstance* instance) + : RenderWidget(parent) { + windowHandle()->setSurfaceType(QWindow::VulkanSurface); + windowHandle()->setVulkanInstance(instance); + } +}; + +GRenderWindow::GRenderWindow(GMainWindow* parent_, EmuThread* emu_thread) : QWidget(parent_), emu_thread(emu_thread) { setWindowTitle(QStringLiteral("yuzu %1 | %2-%3") .arg(QString::fromUtf8(Common::g_build_name), @@ -278,26 +297,13 @@ GRenderWindow::GRenderWindow(QWidget* parent_, EmuThread* emu_thread) setLayout(layout); InputCommon::Init(); - GMainWindow* parent = GetMainWindow(); - connect(this, &GRenderWindow::FirstFrameDisplayed, parent, &GMainWindow::OnLoadComplete); + connect(this, &GRenderWindow::FirstFrameDisplayed, parent_, &GMainWindow::OnLoadComplete); } GRenderWindow::~GRenderWindow() { InputCommon::Shutdown(); } -void GRenderWindow::MakeCurrent() { - if (core_context) { - core_context->MakeCurrent(); - } -} - -void GRenderWindow::DoneCurrent() { - if (core_context) { - core_context->DoneCurrent(); - } -} - void GRenderWindow::PollEvents() { if (!first_frame) { first_frame = true; @@ -309,21 +315,6 @@ bool GRenderWindow::IsShown() const { return !isMinimized(); } -void GRenderWindow::RetrieveVulkanHandlers(void* get_instance_proc_addr, void* instance, - void* surface) const { -#ifdef HAS_VULKAN - const auto instance_proc_addr = vk_instance->getInstanceProcAddr("vkGetInstanceProcAddr"); - const VkInstance instance_copy = vk_instance->vkInstance(); - const VkSurfaceKHR surface_copy = vk_instance->surfaceForWindow(child_window); - - std::memcpy(get_instance_proc_addr, &instance_proc_addr, sizeof(instance_proc_addr)); - std::memcpy(instance, &instance_copy, sizeof(instance_copy)); - std::memcpy(surface, &surface_copy, sizeof(surface_copy)); -#else - UNREACHABLE_MSG("Executing Vulkan code without compiling Vulkan"); -#endif -} - // On Qt 5.0+, this correctly gets the size of the framebuffer (pixels). // // Older versions get the window size (density independent pixels), @@ -474,9 +465,13 @@ void GRenderWindow::resizeEvent(QResizeEvent* event) { std::unique_ptr GRenderWindow::CreateSharedContext() const { if (Settings::values.renderer_backend == Settings::RendererBackend::OpenGL) { - return std::make_unique(QOpenGLContext::globalShareContext()); + auto c = static_cast(main_context.get()); + // Bind the shared contexts to the main surface in case the backend wants to take over + // presentation + return std::make_unique(c->GetShareContext(), + child_widget->windowHandle()); } - return {}; + return std::make_unique(); } bool GRenderWindow::InitRenderTarget() { @@ -497,14 +492,11 @@ bool GRenderWindow::InitRenderTarget() { break; } + child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height); + layout()->addWidget(child_widget); // Reset minimum required size to avoid resizing issues on the main window after restarting. setMinimumSize(1, 1); - // Show causes the window to actually be created and the gl context as well, but we don't want - // the widget to be shown yet, so immediately hide it. - show(); - hide(); - resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height); OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size); @@ -523,9 +515,10 @@ bool GRenderWindow::InitRenderTarget() { void GRenderWindow::ReleaseRenderTarget() { if (child_widget) { layout()->removeWidget(child_widget); - delete child_widget; + child_widget->deleteLater(); child_widget = nullptr; } + main_context.reset(); } void GRenderWindow::CaptureScreenshot(u32 res_scale, const QString& screenshot_path) { @@ -557,24 +550,13 @@ void GRenderWindow::OnMinimalClientAreaChangeRequest(std::pair minimal bool GRenderWindow::InitializeOpenGL() { // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, // WA_DontShowOnScreen, WA_DeleteOnClose - QSurfaceFormat fmt; - fmt.setVersion(4, 3); - fmt.setProfile(QSurfaceFormat::CompatibilityProfile); - fmt.setOption(QSurfaceFormat::FormatOption::DeprecatedFunctions); - // TODO: expose a setting for buffer value (ie default/single/double/triple) - fmt.setSwapBehavior(QSurfaceFormat::DefaultSwapBehavior); - fmt.setSwapInterval(0); - QSurfaceFormat::setDefaultFormat(fmt); - - GMainWindow* parent = GetMainWindow(); - QWindow* parent_win_handle = parent ? parent->windowHandle() : nullptr; - child_window = new OpenGLWindow(parent_win_handle, this, QOpenGLContext::globalShareContext()); - child_window->create(); - child_widget = createWindowContainer(child_window, this); - child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height); - layout()->addWidget(child_widget); - - core_context = CreateSharedContext(); + auto child = new OpenGLRenderWidget(this); + child_widget = child; + child_widget->windowHandle()->create(); + auto context = std::make_shared(child->windowHandle()); + main_context = context; + child->SetContext( + std::make_unique(context->GetShareContext(), child->windowHandle())); return true; } @@ -604,13 +586,10 @@ bool GRenderWindow::InitializeVulkan() { return false; } - GMainWindow* parent = GetMainWindow(); - QWindow* parent_win_handle = parent ? parent->windowHandle() : nullptr; - child_window = new VulkanWindow(parent_win_handle, this, vk_instance.get()); - child_window->create(); - child_widget = createWindowContainer(child_window, this); - child_widget->resize(Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height); - layout()->addWidget(child_widget); + auto child = new VulkanRenderWidget(this, vk_instance.get()); + child_widget = child; + child_widget->windowHandle()->create(); + main_context = std::make_unique(); return true; #else @@ -620,8 +599,24 @@ bool GRenderWindow::InitializeVulkan() { #endif } +void GRenderWindow::RetrieveVulkanHandlers(void* get_instance_proc_addr, void* instance, + void* surface) const { +#ifdef HAS_VULKAN + const auto instance_proc_addr = vk_instance->getInstanceProcAddr("vkGetInstanceProcAddr"); + const VkInstance instance_copy = vk_instance->vkInstance(); + const VkSurfaceKHR surface_copy = vk_instance->surfaceForWindow(child_widget->windowHandle()); + + std::memcpy(get_instance_proc_addr, &instance_proc_addr, sizeof(instance_proc_addr)); + std::memcpy(instance, &instance_copy, sizeof(instance_copy)); + std::memcpy(surface, &surface_copy, sizeof(surface_copy)); +#else + UNREACHABLE_MSG("Executing Vulkan code without compiling Vulkan"); +#endif +} + bool GRenderWindow::LoadOpenGL() { - Core::Frontend::ScopeAcquireContext acquire_context{*this}; + auto context = CreateSharedContext(); + auto scope = context->Acquire(); if (!gladLoadGL()) { QMessageBox::critical(this, tr("Error while initializing OpenGL 4.3!"), tr("Your GPU may not support OpenGL 4.3, or you do not have the " diff --git a/src/yuzu/bootmanager.h b/src/yuzu/bootmanager.h index 79b030304..3739ec7ed 100644 --- a/src/yuzu/bootmanager.h +++ b/src/yuzu/bootmanager.h @@ -7,23 +7,20 @@ #include #include #include - +#include #include #include #include #include - #include "common/thread.h" #include "core/core.h" #include "core/frontend/emu_window.h" class GRenderWindow; +class GMainWindow; class QKeyEvent; -class QScreen; class QTouchEvent; class QStringList; -class QSurface; -class QOpenGLContext; #ifdef HAS_VULKAN class QVulkanInstance; #endif @@ -36,7 +33,7 @@ class EmuThread final : public QThread { Q_OBJECT public: - explicit EmuThread(GRenderWindow& window); + explicit EmuThread(); ~EmuThread() override; /** @@ -87,14 +84,8 @@ private: bool exec_step = false; bool running = false; std::atomic_bool stop_run{false}; - std::mutex running_mutex; - std::condition_variable running_cv; - - /// Only used in asynchronous GPU mode - std::unique_ptr shared_context; - - /// This is shared_context in asynchronous GPU mode, core_context in synchronous GPU mode - Core::Frontend::GraphicsContext& context; + std::mutex running_mutex = {}; + std::condition_variable running_cv = {}; signals: /** @@ -124,12 +115,10 @@ class GRenderWindow : public QWidget, public Core::Frontend::EmuWindow { Q_OBJECT public: - GRenderWindow(QWidget* parent, EmuThread* emu_thread); + GRenderWindow(GMainWindow* parent, EmuThread* emu_thread); ~GRenderWindow() override; // EmuWindow implementation. - void MakeCurrent() override; - void DoneCurrent() override; void PollEvents() override; bool IsShown() const override; void RetrieveVulkanHandlers(void* get_instance_proc_addr, void* instance, @@ -165,6 +154,8 @@ public: void CaptureScreenshot(u32 res_scale, const QString& screenshot_path); + std::pair ScaleTouch(const QPointF pos) const; + public slots: void OnEmulationStarting(EmuThread* emu_thread); void OnEmulationStopping(); @@ -176,7 +167,6 @@ signals: void FirstFrameDisplayed(); private: - std::pair ScaleTouch(QPointF pos) const; void TouchBeginEvent(const QTouchEvent* event); void TouchUpdateEvent(const QTouchEvent* event); void TouchEndEvent(); @@ -190,7 +180,10 @@ private: EmuThread* emu_thread; - std::unique_ptr core_context; + // Main context that will be shared with all other contexts that are requested. + // If this is used in a shared context setting, then this should not be used directly, but + // should instead be shared from + std::shared_ptr main_context; #ifdef HAS_VULKAN std::unique_ptr vk_instance; @@ -201,12 +194,6 @@ private: QByteArray geometry; - /// Native window handle that backs this presentation widget - QWindow* child_window = nullptr; - - /// In order to embed the window into GRenderWindow, you need to use createWindowContainer to - /// put the child_window into a widget then add it to the layout. This child_widget can be - /// parented to GRenderWindow and use Qt's lifetime system QWidget* child_widget = nullptr; bool first_frame = false; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 47615adfe..d7684e241 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -984,7 +984,7 @@ void GMainWindow::BootGame(const QString& filename) { return; // Create and start the emulation thread - emu_thread = std::make_unique(*render_window); + emu_thread = std::make_unique(); emit EmulationStarting(emu_thread.get()); emu_thread->start(); @@ -2375,7 +2375,6 @@ int main(int argc, char* argv[]) { // Enables the core to make the qt created contexts current on std::threads QCoreApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity); - QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); QApplication app(argc, argv); // Qt changes the locale and causes issues in float conversion using std::to_string() when -- cgit v1.2.3 From cf9c94d4017120b618194a720942ef4e5c8289bd Mon Sep 17 00:00:00 2001 From: James Rowe Date: Tue, 24 Mar 2020 22:57:36 -0600 Subject: Address review and fix broken yuzu-tester build --- src/core/core.cpp | 4 +- src/core/frontend/emu_window.h | 2 +- src/video_core/renderer_opengl/gl_shader_cache.cpp | 2 +- src/video_core/renderer_opengl/renderer_opengl.cpp | 6 +- src/video_core/video_core.cpp | 4 +- src/yuzu/bootmanager.cpp | 97 +++++++--------------- src/yuzu/bootmanager.h | 9 +- src/yuzu_cmd/yuzu.cpp | 4 +- .../emu_window/emu_window_sdl2_hide.cpp | 42 +++++++--- src/yuzu_tester/emu_window/emu_window_sdl2_hide.h | 9 +- src/yuzu_tester/yuzu.cpp | 6 +- 11 files changed, 83 insertions(+), 102 deletions(-) (limited to 'src/yuzu') diff --git a/src/core/core.cpp b/src/core/core.cpp index 6cc4a0812..26a580cb7 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -169,6 +169,9 @@ struct System::Impl { interrupt_manager = std::make_unique(system); gpu_core = VideoCore::CreateGPU(emu_window, system); + if (!gpu_core) { + return ResultStatus::ErrorVideoCore; + } gpu_core->Renderer().Rasterizer().SetupDirtyFlags(); is_powered_on = true; @@ -181,7 +184,6 @@ struct System::Impl { ResultStatus Load(System& system, Frontend::EmuWindow& emu_window, const std::string& filepath) { - app_loader = Loader::GetLoader(GetGameFileFromPath(virtual_filesystem, filepath)); if (!app_loader) { LOG_CRITICAL(Core, "Failed to obtain loader for {}!", filepath); diff --git a/src/core/frontend/emu_window.h b/src/core/frontend/emu_window.h index bb283d844..72294d4d8 100644 --- a/src/core/frontend/emu_window.h +++ b/src/core/frontend/emu_window.h @@ -30,7 +30,7 @@ public: class Scoped { public: - Scoped(GraphicsContext& context_) : context(context_) { + explicit Scoped(GraphicsContext& context_) : context(context_) { context.MakeCurrent(); } ~Scoped() { diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp index 8f59e0442..046ee55a5 100644 --- a/src/video_core/renderer_opengl/gl_shader_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp @@ -305,6 +305,7 @@ void ShaderCacheOpenGL::LoadDiskCache(const std::atomic_bool& stop_loading, } const std::vector gl_cache = disk_cache.LoadPrecompiled(); + const auto supported_formats = GetSupportedFormats(); // Track if precompiled cache was altered during loading to know if we have to // serialize the virtual precompiled cache file back to the hard drive @@ -327,7 +328,6 @@ void ShaderCacheOpenGL::LoadDiskCache(const std::atomic_bool& stop_loading, const auto worker = [&](Core::Frontend::GraphicsContext* context, std::size_t begin, std::size_t end) { const auto scope = context->Acquire(); - const auto supported_formats = GetSupportedFormats(); for (std::size_t i = begin; i < end; ++i) { if (stop_loading) { diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 6f08803c1..f1a28cc21 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -7,7 +7,9 @@ #include #include #include + #include + #include "common/assert.h" #include "common/logging/log.h" #include "common/microprofile.h" @@ -313,8 +315,8 @@ public: RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system, Core::Frontend::GraphicsContext& context) - : VideoCore::RendererBase{emu_window}, emu_window{emu_window}, system{system}, frame_mailbox{}, - has_debug_tool{HasDebugTool()}, context{context} {} + : VideoCore::RendererBase{emu_window}, emu_window{emu_window}, system{system}, + frame_mailbox{}, context{context}, has_debug_tool{HasDebugTool()} {} RendererOpenGL::~RendererOpenGL() = default; diff --git a/src/video_core/video_core.cpp b/src/video_core/video_core.cpp index fd9fec018..f60bdc60a 100644 --- a/src/video_core/video_core.cpp +++ b/src/video_core/video_core.cpp @@ -30,7 +30,7 @@ std::unique_ptr CreateRenderer(Core::Frontend::EmuWindo return nullptr; } } -} // namespace +} // Anonymous namespace namespace VideoCore { @@ -39,7 +39,7 @@ std::unique_ptr CreateGPU(Core::Frontend::EmuWindow& emu_window, Cor const auto scope = context->Acquire(); auto renderer = CreateRenderer(emu_window, system, *context); if (!renderer->Init()) { - return {}; + return nullptr; } if (Settings::values.use_asynchronous_gpu_emulation) { diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index d120ee818..4e9ced8ba 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -42,6 +42,10 @@ EmuThread::~EmuThread() = default; void EmuThread::run() { MicroProfileOnThreadCreate("EmuThread"); + // Main process has been loaded. Make the context current to this thread and begin GPU and CPU + // execution. + Core::System::GetInstance().GPU().Start(); + emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0); Core::System::GetInstance().Renderer().Rasterizer().LoadDiskResources( @@ -51,10 +55,6 @@ void EmuThread::run() { emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0); - // Main process has been loaded. Make the context current to this thread and begin GPU and CPU - // execution. - Core::System::GetInstance().GPU().Start(); - // Holds whether the cpu was running during the last iteration, // so that the DebugModeLeft signal can be emitted before the // next execution step @@ -152,7 +152,7 @@ public: if (is_current) { return; } - context->makeCurrent(surface); + is_current = context->makeCurrent(surface); } void DoneCurrent() override { @@ -160,7 +160,11 @@ public: is_current = false; } - QOpenGLContext* GetShareContext() const { + QOpenGLContext* GetShareContext() { + return context.get(); + } + + const QOpenGLContext* GetShareContext() const { return context.get(); } @@ -177,13 +181,15 @@ class DummyContext : public Core::Frontend::GraphicsContext {}; class RenderWidget : public QWidget { public: - RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) { + explicit RenderWidget(GRenderWindow* parent) : QWidget(parent), render_window(parent) { setAttribute(Qt::WA_NativeWindow); setAttribute(Qt::WA_PaintOnScreen); } virtual ~RenderWidget() = default; + /// Called on the UI thread when this Widget is ready to draw + /// Dervied classes can override this to draw the latest frame. virtual void Present() {} void paintEvent(QPaintEvent* event) override { @@ -191,56 +197,6 @@ public: update(); } - void resizeEvent(QResizeEvent* ev) override { - render_window->resize(ev->size()); - render_window->OnFramebufferSizeChanged(); - } - - void keyPressEvent(QKeyEvent* event) override { - InputCommon::GetKeyboard()->PressKey(event->key()); - } - - void keyReleaseEvent(QKeyEvent* event) override { - InputCommon::GetKeyboard()->ReleaseKey(event->key()); - } - - void mousePressEvent(QMouseEvent* event) override { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchBeginEvent - - const auto pos{event->pos()}; - if (event->button() == Qt::LeftButton) { - const auto [x, y] = render_window->ScaleTouch(pos); - render_window->TouchPressed(x, y); - } else if (event->button() == Qt::RightButton) { - InputCommon::GetMotionEmu()->BeginTilt(pos.x(), pos.y()); - } - } - - void mouseMoveEvent(QMouseEvent* event) override { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchUpdateEvent - - const auto pos{event->pos()}; - const auto [x, y] = render_window->ScaleTouch(pos); - render_window->TouchMoved(x, y); - InputCommon::GetMotionEmu()->Tilt(pos.x(), pos.y()); - } - - void mouseReleaseEvent(QMouseEvent* event) override { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchEndEvent - - if (event->button() == Qt::LeftButton) - render_window->TouchReleased(); - else if (event->button() == Qt::RightButton) - InputCommon::GetMotionEmu()->EndTilt(); - } - - std::pair GetSize() const { - return std::make_pair(width(), height()); - } - QPaintEngine* paintEngine() const override { return nullptr; } @@ -276,6 +232,7 @@ private: std::unique_ptr context{}; }; +#ifdef HAS_VULKAN class VulkanRenderWidget : public RenderWidget { public: explicit VulkanRenderWidget(GRenderWindow* parent, QVulkanInstance* instance) @@ -284,6 +241,7 @@ public: windowHandle()->setVulkanInstance(instance); } }; +#endif GRenderWindow::GRenderWindow(GMainWindow* parent_, EmuThread* emu_thread) : QWidget(parent_), emu_thread(emu_thread) { @@ -358,7 +316,7 @@ qreal GRenderWindow::windowPixelRatio() const { return devicePixelRatio(); } -std::pair GRenderWindow::ScaleTouch(const QPointF pos) const { +std::pair GRenderWindow::ScaleTouch(const QPointF& pos) const { const qreal pixel_ratio = windowPixelRatio(); return {static_cast(std::max(std::round(pos.x() * pixel_ratio), qreal{0.0})), static_cast(std::max(std::round(pos.y() * pixel_ratio), qreal{0.0}))}; @@ -378,8 +336,10 @@ void GRenderWindow::keyReleaseEvent(QKeyEvent* event) { } void GRenderWindow::mousePressEvent(QMouseEvent* event) { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchBeginEvent + // touch input is handled in TouchBeginEvent + if (event->source() == Qt::MouseEventSynthesizedBySystem) { + return; + } auto pos = event->pos(); if (event->button() == Qt::LeftButton) { @@ -391,8 +351,10 @@ void GRenderWindow::mousePressEvent(QMouseEvent* event) { } void GRenderWindow::mouseMoveEvent(QMouseEvent* event) { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchUpdateEvent + // touch input is handled in TouchUpdateEvent + if (event->source() == Qt::MouseEventSynthesizedBySystem) { + return; + } auto pos = event->pos(); const auto [x, y] = ScaleTouch(pos); @@ -401,13 +363,16 @@ void GRenderWindow::mouseMoveEvent(QMouseEvent* event) { } void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) { - if (event->source() == Qt::MouseEventSynthesizedBySystem) - return; // touch input is handled in TouchEndEvent + // touch input is handled in TouchEndEvent + if (event->source() == Qt::MouseEventSynthesizedBySystem) { + return; + } - if (event->button() == Qt::LeftButton) + if (event->button() == Qt::LeftButton) { this->TouchReleased(); - else if (event->button() == Qt::RightButton) + } else if (event->button() == Qt::RightButton) { InputCommon::GetMotionEmu()->EndTilt(); + } } void GRenderWindow::TouchBeginEvent(const QTouchEvent* event) { diff --git a/src/yuzu/bootmanager.h b/src/yuzu/bootmanager.h index 3739ec7ed..d69078df1 100644 --- a/src/yuzu/bootmanager.h +++ b/src/yuzu/bootmanager.h @@ -7,11 +7,12 @@ #include #include #include -#include + #include #include #include #include + #include "common/thread.h" #include "core/core.h" #include "core/frontend/emu_window.h" @@ -84,8 +85,8 @@ private: bool exec_step = false; bool running = false; std::atomic_bool stop_run{false}; - std::mutex running_mutex = {}; - std::condition_variable running_cv = {}; + std::mutex running_mutex; + std::condition_variable running_cv; signals: /** @@ -154,7 +155,7 @@ public: void CaptureScreenshot(u32 res_scale, const QString& screenshot_path); - std::pair ScaleTouch(const QPointF pos) const; + std::pair ScaleTouch(const QPointF& pos) const; public slots: void OnEmulationStarting(EmuThread* emu_thread); diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index e5db7d819..4d2ea7e9e 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -230,11 +230,11 @@ int main(int argc, char** argv) { system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDL"); - system.Renderer().Rasterizer().LoadDiskResources(); - // Core is loaded, start the GPU (makes the GPU contexts current to this thread) system.GPU().Start(); + system.Renderer().Rasterizer().LoadDiskResources(); + std::thread render_thread([&emu_window] { emu_window->Present(); }); while (emu_window->IsOpen()) { system.RunLoop(); diff --git a/src/yuzu_tester/emu_window/emu_window_sdl2_hide.cpp b/src/yuzu_tester/emu_window/emu_window_sdl2_hide.cpp index a1bdb1a12..a837430cc 100644 --- a/src/yuzu_tester/emu_window/emu_window_sdl2_hide.cpp +++ b/src/yuzu_tester/emu_window/emu_window_sdl2_hide.cpp @@ -102,8 +102,6 @@ EmuWindow_SDL2_Hide::EmuWindow_SDL2_Hide() { LOG_INFO(Frontend, "yuzu-tester Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch, Common::g_scm_desc); Settings::LogSettings(); - - DoneCurrent(); } EmuWindow_SDL2_Hide::~EmuWindow_SDL2_Hide() { @@ -114,14 +112,6 @@ EmuWindow_SDL2_Hide::~EmuWindow_SDL2_Hide() { void EmuWindow_SDL2_Hide::PollEvents() {} -void EmuWindow_SDL2_Hide::MakeCurrent() { - SDL_GL_MakeCurrent(render_window, gl_context); -} - -void EmuWindow_SDL2_Hide::DoneCurrent() { - SDL_GL_MakeCurrent(render_window, nullptr); -} - bool EmuWindow_SDL2_Hide::IsShown() const { return false; } @@ -129,3 +119,35 @@ bool EmuWindow_SDL2_Hide::IsShown() const { void EmuWindow_SDL2_Hide::RetrieveVulkanHandlers(void*, void*, void*) const { UNREACHABLE(); } + +class SDLGLContext : public Core::Frontend::GraphicsContext { +public: + explicit SDLGLContext() { + // create a hidden window to make the shared context against + window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0, 0, + SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL); + context = SDL_GL_CreateContext(window); + } + + ~SDLGLContext() { + DoneCurrent(); + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); + } + + void MakeCurrent() override { + SDL_GL_MakeCurrent(window, context); + } + + void DoneCurrent() override { + SDL_GL_MakeCurrent(window, nullptr); + } + +private: + SDL_Window* window; + SDL_GLContext context; +}; + +std::unique_ptr EmuWindow_SDL2_Hide::CreateSharedContext() const { + return std::make_unique(); +} diff --git a/src/yuzu_tester/emu_window/emu_window_sdl2_hide.h b/src/yuzu_tester/emu_window/emu_window_sdl2_hide.h index b13e15309..9f5d04fca 100644 --- a/src/yuzu_tester/emu_window/emu_window_sdl2_hide.h +++ b/src/yuzu_tester/emu_window/emu_window_sdl2_hide.h @@ -16,12 +16,6 @@ public: /// Polls window events void PollEvents() override; - /// Makes the graphics context current for the caller thread - void MakeCurrent() override; - - /// Releases the GL context from the caller thread - void DoneCurrent() override; - /// Whether the screen is being shown or not. bool IsShown() const override; @@ -29,8 +23,7 @@ public: void RetrieveVulkanHandlers(void* get_instance_proc_addr, void* instance, void* surface) const override; - /// Whether the window is still open, and a close request hasn't yet been sent - bool IsOpen() const; + std::unique_ptr CreateSharedContext() const override; private: /// Whether the GPU and driver supports the OpenGL extension required diff --git a/src/yuzu_tester/yuzu.cpp b/src/yuzu_tester/yuzu.cpp index 94ad50cb3..676e70ebd 100644 --- a/src/yuzu_tester/yuzu.cpp +++ b/src/yuzu_tester/yuzu.cpp @@ -164,11 +164,6 @@ int main(int argc, char** argv) { std::unique_ptr emu_window{std::make_unique()}; - if (!Settings::values.use_multi_core) { - // Single core mode must acquire OpenGL context for entire emulation session - emu_window->MakeCurrent(); - } - bool finished = false; int return_value = 0; const auto callback = [&finished, @@ -257,6 +252,7 @@ int main(int argc, char** argv) { system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDLHideTester"); + system.GPU().Start(); system.Renderer().Rasterizer().LoadDiskResources(); while (!finished) { -- cgit v1.2.3 From f1da3ec584e5956c8090ac9a958447e4f5e78da2 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Mon, 30 Mar 2020 14:52:46 -0600 Subject: Frontend: Don't call DoneCurrent if the context isnt already current --- src/yuzu/bootmanager.cpp | 5 ++++- src/yuzu_cmd/emu_window/emu_window_sdl2_gl.cpp | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src/yuzu') diff --git a/src/yuzu/bootmanager.cpp b/src/yuzu/bootmanager.cpp index 4e9ced8ba..eaded2640 100644 --- a/src/yuzu/bootmanager.cpp +++ b/src/yuzu/bootmanager.cpp @@ -141,7 +141,7 @@ public: } ~OpenGLSharedContext() { - context->doneCurrent(); + DoneCurrent(); } void SwapBuffers() override { @@ -156,6 +156,9 @@ public: } void DoneCurrent() override { + if (!is_current) { + return; + } context->doneCurrent(); is_current = false; } diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2_gl.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2_gl.cpp index ee61179a0..3522dcf6d 100644 --- a/src/yuzu_cmd/emu_window/emu_window_sdl2_gl.cpp +++ b/src/yuzu_cmd/emu_window/emu_window_sdl2_gl.cpp @@ -37,16 +37,24 @@ public: } void MakeCurrent() override { - SDL_GL_MakeCurrent(window, context); + if (is_current) { + return; + } + is_current = SDL_GL_MakeCurrent(window, context) == 0; } void DoneCurrent() override { + if (!is_current) { + return; + } SDL_GL_MakeCurrent(window, nullptr); + is_current = false; } private: SDL_Window* window; SDL_GLContext context; + bool is_current = false; }; bool EmuWindow_SDL2_GL::SupportsRequiredGLExtensions() { -- cgit v1.2.3