diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index f026cb939d..c573f79838 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -457,14 +457,14 @@ static void FifoPlayerThread(Core::System& system, const std::optionalSetPassthrough(!system.IsDualCoreMode()); // Must happen on the proper thread for some video backends, e.g. OpenGL. - return g_video_backend->Initialize(wsi); + return g_video_backend->Initialize(system, wsi); }; - const auto deinit_video = [] { + auto deinit_video = [&] { // Clear on screen messages that haven't expired OSD::ClearMessages(); - g_video_backend->Shutdown(); + g_video_backend->Shutdown(system); }; if (system.IsDualCoreMode()) @@ -472,7 +472,11 @@ static void FifoPlayerThread(Core::System& system, const std::optional init_from_thread; // Spawn the GPU thread. - std::thread gpu_thread{[&] { + // Capture deinit_video by value to prevent a reference to it from being used by gpu_thread + // after GetInitializedVideoGuard returns. init_video doesn't need to be captured by value + // because it's only used before init_from_thread is set, while GetInitializedVideoGuard waits + // on init_from_thread below and can only return after that. + std::thread gpu_thread{[&, deinit_video] { Common::SetCurrentThreadName("Video thread"); const bool is_init = init_video(); @@ -503,7 +507,7 @@ static void FifoPlayerThread(Core::System& system, const std::optional(deinit_video); + return std::make_unique(std::move(deinit_video)); } return ReturnType{}; diff --git a/Source/Core/Core/HW/VideoInterface.cpp b/Source/Core/Core/HW/VideoInterface.cpp index c0f46bed35..01e41493ec 100644 --- a/Source/Core/Core/HW/VideoInterface.cpp +++ b/Source/Core/Core/HW/VideoInterface.cpp @@ -864,7 +864,7 @@ void VideoInterfaceManager::OutputField(FieldType field, u64 ticks) // can change the register values during scanout. To correctly emulate the scanout process, we // would need to collate all changes to the VI registers during scanout. if (xfbAddr) - g_video_backend->Video_OutputXFB(xfbAddr, fbWidth, fbStride, fbHeight, ticks); + g_video_backend->Video_OutputXFB(m_system, xfbAddr, fbWidth, fbStride, fbHeight, ticks); } void VideoInterfaceManager::BeginField(FieldType field, u64 ticks) diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index ffd811bd6c..6ee7fcceec 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -175,7 +175,7 @@ static void DoState(Core::System& system, PointerWrap& p) // Begin with video backend, so that it gets a chance to clear its caches and writeback modified // things to RAM - g_video_backend->DoState(p); + g_video_backend->DoState(system, p); p.DoMarker("video_backend"); // CoreTiming needs to be restored before restoring Hardware because diff --git a/Source/Core/VideoBackends/D3D/D3DMain.cpp b/Source/Core/VideoBackends/D3D/D3DMain.cpp index 2e09dd82cb..0c6e8fc493 100644 --- a/Source/Core/VideoBackends/D3D/D3DMain.cpp +++ b/Source/Core/VideoBackends/D3D/D3DMain.cpp @@ -26,6 +26,11 @@ #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + namespace DX11 { std::string VideoBackend::GetConfigName() const @@ -139,7 +144,7 @@ void VideoBackend::FillBackendInfo() } } -bool VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { if (!D3D::Create(g_Config.iAdapter, g_Config.bEnableValidationLayer)) return false; @@ -151,7 +156,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) if (wsi.render_surface && !(swap_chain = SwapChain::Create(wsi))) { PanicAlertFmtT("Failed to create D3D swap chain"); - ShutdownShared(); + ShutdownShared(system); D3D::Destroy(); return false; } @@ -161,13 +166,13 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) auto perf_query = std::make_unique(); auto bounding_box = std::make_unique(); - return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + return InitializeShared(system, std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box)); } -void VideoBackend::Shutdown() +void VideoBackend::Shutdown(Core::System& system) { - ShutdownShared(); + ShutdownShared(system); D3D::Destroy(); } } // namespace DX11 diff --git a/Source/Core/VideoBackends/D3D/VideoBackend.h b/Source/Core/VideoBackends/D3D/VideoBackend.h index ae25848105..81872f2849 100644 --- a/Source/Core/VideoBackends/D3D/VideoBackend.h +++ b/Source/Core/VideoBackends/D3D/VideoBackend.h @@ -4,15 +4,21 @@ #pragma once #include + #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace DX11 { class VideoBackend : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override; std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/D3D12/VideoBackend.cpp b/Source/Core/VideoBackends/D3D12/VideoBackend.cpp index a5659642c9..eef3597766 100644 --- a/Source/Core/VideoBackends/D3D12/VideoBackend.cpp +++ b/Source/Core/VideoBackends/D3D12/VideoBackend.cpp @@ -24,6 +24,11 @@ #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + namespace DX12 { std::string VideoBackend::GetConfigName() const @@ -104,7 +109,7 @@ void VideoBackend::FillBackendInfo() } } -bool VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { if (!DXContext::Create(g_Config.iAdapter, g_Config.bEnableValidationLayer)) { @@ -119,7 +124,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) { PanicAlertFmtT("Failed to create D3D12 global resources"); DXContext::Destroy(); - ShutdownShared(); + ShutdownShared(system); return false; } @@ -128,7 +133,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) { PanicAlertFmtT("Failed to create D3D swap chain"); DXContext::Destroy(); - ShutdownShared(); + ShutdownShared(system); return false; } @@ -138,17 +143,17 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) auto perf_query = std::make_unique(); auto bounding_box = std::make_unique(); - return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + return InitializeShared(system, std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box)); } -void VideoBackend::Shutdown() +void VideoBackend::Shutdown(Core::System& system) { // Keep the debug runtime happy... if (g_gfx) Gfx::GetInstance()->ExecuteCommandList(true); - ShutdownShared(); + ShutdownShared(system); DXContext::Destroy(); } } // namespace DX12 diff --git a/Source/Core/VideoBackends/D3D12/VideoBackend.h b/Source/Core/VideoBackends/D3D12/VideoBackend.h index 7be8291bcd..cddbe49ddb 100644 --- a/Source/Core/VideoBackends/D3D12/VideoBackend.h +++ b/Source/Core/VideoBackends/D3D12/VideoBackend.h @@ -7,13 +7,18 @@ #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace DX12 { class VideoBackend final : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override; std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/Metal/MTLMain.mm b/Source/Core/VideoBackends/Metal/MTLMain.mm index ff8480edda..5aff484e67 100644 --- a/Source/Core/VideoBackends/Metal/MTLMain.mm +++ b/Source/Core/VideoBackends/Metal/MTLMain.mm @@ -29,6 +29,11 @@ #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + std::string Metal::VideoBackend::GetConfigName() const { return CONFIG_NAME; @@ -63,7 +68,7 @@ static bool WindowSystemTypeSupportsMetal(WindowSystemType type) } } -bool Metal::VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool Metal::VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { @autoreleasepool { @@ -125,15 +130,16 @@ bool Metal::VideoBackend::Initialize(const WindowSystemInfo& wsi) ObjectCache::Initialize(std::move(adapter)); g_state_tracker = std::make_unique(); - return InitializeShared( - std::make_unique(std::move(layer)), std::make_unique(), - std::make_unique(), std::make_unique()); + return InitializeShared(system, std::make_unique(std::move(layer)), + std::make_unique(), + std::make_unique(), + std::make_unique()); } } -void Metal::VideoBackend::Shutdown() +void Metal::VideoBackend::Shutdown(Core::System& system) { - ShutdownShared(); + ShutdownShared(system); g_state_tracker.reset(); ObjectCache::Shutdown(); diff --git a/Source/Core/VideoBackends/Metal/VideoBackend.h b/Source/Core/VideoBackends/Metal/VideoBackend.h index 008b5f824e..a1d74bf6a8 100644 --- a/Source/Core/VideoBackends/Metal/VideoBackend.h +++ b/Source/Core/VideoBackends/Metal/VideoBackend.h @@ -4,15 +4,21 @@ #pragma once #include + #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace Metal { class VideoBackend : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override; std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/Null/NullBackend.cpp b/Source/Core/VideoBackends/Null/NullBackend.cpp index 02b75fe407..904bc415fd 100644 --- a/Source/Core/VideoBackends/Null/NullBackend.cpp +++ b/Source/Core/VideoBackends/Null/NullBackend.cpp @@ -19,6 +19,11 @@ #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + namespace Null { void VideoBackend::InitBackendInfo(const WindowSystemInfo& wsi) @@ -65,16 +70,16 @@ void VideoBackend::InitBackendInfo(const WindowSystemInfo& wsi) g_backend_info.AAModes = {1}; } -bool VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { - return InitializeShared(std::make_unique(), std::make_unique(), + return InitializeShared(system, std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique()); } -void VideoBackend::Shutdown() +void VideoBackend::Shutdown(Core::System& system) { - ShutdownShared(); + ShutdownShared(system); } std::string VideoBackend::GetDisplayName() const diff --git a/Source/Core/VideoBackends/Null/VideoBackend.h b/Source/Core/VideoBackends/Null/VideoBackend.h index 5ced7951a2..8695f5ae38 100644 --- a/Source/Core/VideoBackends/Null/VideoBackend.h +++ b/Source/Core/VideoBackends/Null/VideoBackend.h @@ -3,15 +3,22 @@ #pragma once +#include + #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace Null { class VideoBackend final : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override { return CONFIG_NAME; } std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/OGL/OGLMain.cpp b/Source/Core/VideoBackends/OGL/OGLMain.cpp index e47eaaf940..13a10f6b14 100644 --- a/Source/Core/VideoBackends/OGL/OGLMain.cpp +++ b/Source/Core/VideoBackends/OGL/OGLMain.cpp @@ -59,6 +59,11 @@ Make AA apply instantly during gameplay if possible #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + namespace OGL { std::string VideoBackend::GetConfigName() const @@ -193,7 +198,7 @@ bool VideoBackend::FillBackendInfo(GLContext* context) return true; } -bool VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { std::unique_ptr main_gl_context = GLContext::Create(wsi, g_Config.stereo_mode == StereoMode::QuadBuffer, true, false, @@ -212,13 +217,13 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) auto perf_query = GetPerfQuery(gfx->IsGLES()); auto bounding_box = std::make_unique(); - return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + return InitializeShared(system, std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box)); } -void VideoBackend::Shutdown() +void VideoBackend::Shutdown(Core::System& system) { - ShutdownShared(); + ShutdownShared(system); ProgramShaderCache::Shutdown(); g_sampler_cache.reset(); diff --git a/Source/Core/VideoBackends/OGL/VideoBackend.h b/Source/Core/VideoBackends/OGL/VideoBackend.h index 734748a74a..ce2209655f 100644 --- a/Source/Core/VideoBackends/OGL/VideoBackend.h +++ b/Source/Core/VideoBackends/OGL/VideoBackend.h @@ -4,8 +4,14 @@ #pragma once #include + #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + class GLContext; namespace OGL @@ -13,8 +19,8 @@ namespace OGL class VideoBackend : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override; std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/Software/SWmain.cpp b/Source/Core/VideoBackends/Software/SWmain.cpp index 2f51789ae4..9b16ea1e49 100644 --- a/Source/Core/VideoBackends/Software/SWmain.cpp +++ b/Source/Core/VideoBackends/Software/SWmain.cpp @@ -23,6 +23,11 @@ #include "VideoCommon/VideoCommon.h" #include "VideoCommon/VideoConfig.h" +namespace Core +{ +class System; +} + namespace SW { class PerfQuery : public PerfQueryBase @@ -90,7 +95,7 @@ void VideoSoftware::InitBackendInfo(const WindowSystemInfo& wsi) g_backend_info.AAModes = {1}; } -bool VideoSoftware::Initialize(const WindowSystemInfo& wsi) +bool VideoSoftware::Initialize(Core::System& system, const WindowSystemInfo& wsi) { std::unique_ptr window = SWOGLWindow::Create(wsi); if (!window) @@ -99,14 +104,14 @@ bool VideoSoftware::Initialize(const WindowSystemInfo& wsi) Clipper::Init(); Rasterizer::Init(); - return InitializeShared(std::make_unique(std::move(window)), + return InitializeShared(system, std::make_unique(std::move(window)), std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique()); } -void VideoSoftware::Shutdown() +void VideoSoftware::Shutdown(Core::System& system) { - ShutdownShared(); + ShutdownShared(system); } } // namespace SW diff --git a/Source/Core/VideoBackends/Software/VideoBackend.h b/Source/Core/VideoBackends/Software/VideoBackend.h index 5fa4127916..8572ff9c6b 100644 --- a/Source/Core/VideoBackends/Software/VideoBackend.h +++ b/Source/Core/VideoBackends/Software/VideoBackend.h @@ -4,14 +4,20 @@ #pragma once #include + #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace SW { class VideoSoftware : public VideoBackendBase { - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override; std::string GetDisplayName() const override; diff --git a/Source/Core/VideoBackends/Vulkan/VKMain.cpp b/Source/Core/VideoBackends/Vulkan/VKMain.cpp index 6338e52e3d..b521fa53a5 100644 --- a/Source/Core/VideoBackends/Vulkan/VKMain.cpp +++ b/Source/Core/VideoBackends/Vulkan/VKMain.cpp @@ -23,6 +23,11 @@ #include #endif +namespace Core +{ +class System; +} + namespace Vulkan { void VideoBackend::InitBackendInfo(const WindowSystemInfo& wsi) @@ -88,7 +93,7 @@ static bool ShouldEnableDebugUtils(bool enable_validation_layers) return enable_validation_layers || IsHostGPULoggingEnabled(); } -bool VideoBackend::Initialize(const WindowSystemInfo& wsi) +bool VideoBackend::Initialize(Core::System& system, const WindowSystemInfo& wsi) { if (!LoadVulkanLibrary()) { @@ -192,7 +197,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) if (!g_object_cache->Initialize()) { PanicAlertFmt("Failed to initialize Vulkan object cache."); - Shutdown(); + Shutdown(system); return false; } @@ -204,7 +209,7 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) if (!swap_chain) { PanicAlertFmt("Failed to create Vulkan swap chain."); - Shutdown(); + Shutdown(system); return false; } } @@ -216,14 +221,14 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) if (!g_command_buffer_mgr->Initialize(swapchain_image_count)) { PanicAlertFmt("Failed to create Vulkan command buffers"); - Shutdown(); + Shutdown(system); return false; } if (!StateTracker::CreateInstance()) { PanicAlertFmt("Failed to create state tracker"); - Shutdown(); + Shutdown(system); return false; } @@ -232,11 +237,11 @@ bool VideoBackend::Initialize(const WindowSystemInfo& wsi) auto perf_query = std::make_unique(); auto bounding_box = std::make_unique(); - return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + return InitializeShared(system, std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box)); } -void VideoBackend::Shutdown() +void VideoBackend::Shutdown(Core::System& system) { if (g_vulkan_context) vkDeviceWaitIdle(g_vulkan_context->GetDevice()); @@ -244,7 +249,7 @@ void VideoBackend::Shutdown() if (g_object_cache) g_object_cache->Shutdown(); - ShutdownShared(); + ShutdownShared(system); g_object_cache.reset(); StateTracker::DestroyInstance(); diff --git a/Source/Core/VideoBackends/Vulkan/VideoBackend.h b/Source/Core/VideoBackends/Vulkan/VideoBackend.h index 2f4187d61b..6aa05f9087 100644 --- a/Source/Core/VideoBackends/Vulkan/VideoBackend.h +++ b/Source/Core/VideoBackends/Vulkan/VideoBackend.h @@ -3,16 +3,23 @@ #pragma once +#include + #include "Common/Common.h" #include "VideoCommon/VideoBackendBase.h" +namespace Core +{ +class System; +} + namespace Vulkan { class VideoBackend : public VideoBackendBase { public: - bool Initialize(const WindowSystemInfo& wsi) override; - void Shutdown() override; + bool Initialize(Core::System& system, const WindowSystemInfo& wsi) override; + void Shutdown(Core::System& system) override; std::string GetConfigName() const override { return CONFIG_NAME; } std::string GetDisplayName() const override { return _trans("Vulkan"); } diff --git a/Source/Core/VideoCommon/PixelEngine.cpp b/Source/Core/VideoCommon/PixelEngine.cpp index 63642d017d..2b5e319187 100644 --- a/Source/Core/VideoCommon/PixelEngine.cpp +++ b/Source/Core/VideoCommon/PixelEngine.cpp @@ -111,12 +111,13 @@ void PixelEngineManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base) }; for (auto& pq_reg : pq_regs) { - mmio->Register(base | pq_reg.addr, MMIO::ComplexRead([pq_reg](Core::System&, u32) { - return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) & 0xFFFF; + mmio->Register(base | pq_reg.addr, MMIO::ComplexRead([pq_reg](Core::System& system, u32) { + return g_video_backend->Video_GetQueryResult(system, pq_reg.pqtype) & 0xFFFF; }), MMIO::InvalidWrite()); - mmio->Register(base | (pq_reg.addr + 2), MMIO::ComplexRead([pq_reg](Core::System&, u32) { - return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) >> 16; + mmio->Register(base | (pq_reg.addr + 2), + MMIO::ComplexRead([pq_reg](Core::System& system, u32) { + return g_video_backend->Video_GetQueryResult(system, pq_reg.pqtype) >> 16; }), MMIO::InvalidWrite()); } @@ -152,7 +153,7 @@ void PixelEngineManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base) mmio->Register(base | (PE_BBOX_LEFT + 2 * i), MMIO::ComplexRead([i](Core::System& system, u32) { g_bounding_box->Disable(system.GetPixelShaderManager()); - return g_video_backend->Video_GetBoundingBox(i); + return g_video_backend->Video_GetBoundingBox(system, i); }), MMIO::InvalidWrite()); } diff --git a/Source/Core/VideoCommon/VideoBackendBase.cpp b/Source/Core/VideoCommon/VideoBackendBase.cpp index d964eac3f9..119e832982 100644 --- a/Source/Core/VideoCommon/VideoBackendBase.cpp +++ b/Source/Core/VideoCommon/VideoBackendBase.cpp @@ -90,12 +90,11 @@ std::string VideoBackendBase::BadShaderFilename(const char* shader_stage, int co } // Run from the CPU thread (from VideoInterface.cpp) -void VideoBackendBase::Video_OutputXFB(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height, - u64 ticks) +void VideoBackendBase::Video_OutputXFB(Core::System& system, u32 xfb_addr, u32 fb_width, + u32 fb_stride, u32 fb_height, u64 ticks) { if (m_initialized && g_presenter && !g_ActiveConfig.bImmediateXFB) { - auto& system = Core::System::GetInstance(); system.GetFifo().SyncGPU(Fifo::SyncGPUReason::Swap); const TimePoint presentation_time = system.GetCoreTiming().GetTargetHostTime(ticks); @@ -105,14 +104,13 @@ void VideoBackendBase::Video_OutputXFB(u32 xfb_addr, u32 fb_width, u32 fb_stride } } -u32 VideoBackendBase::Video_GetQueryResult(PerfQueryType type) +u32 VideoBackendBase::Video_GetQueryResult(Core::System& system, PerfQueryType type) { if (!g_perf_query->ShouldEmulate()) { return 0; } - auto& system = Core::System::GetInstance(); system.GetFifo().SyncGPU(Fifo::SyncGPUReason::PerfQuery); if (!g_perf_query->IsFlushed()) @@ -123,7 +121,7 @@ u32 VideoBackendBase::Video_GetQueryResult(PerfQueryType type) return g_perf_query->GetQueryResult(type); } -u16 VideoBackendBase::Video_GetBoundingBox(int index) +u16 VideoBackendBase::Video_GetBoundingBox(Core::System& system, int index) { DolphinAnalytics::Instance().ReportGameQuirk(GameQuirk::ReadsBoundingBox); @@ -151,7 +149,6 @@ u16 VideoBackendBase::Video_GetBoundingBox(int index) warn_once = false; } - auto& system = Core::System::GetInstance(); system.GetFifo().SyncGPU(Fifo::SyncGPUReason::BBox); return AsyncRequests::GetInstance()->PushBlockingEvent( @@ -249,23 +246,22 @@ void VideoBackendBase::PopulateBackendInfo(const WindowSystemInfo& wsi) g_Config.VerifyValidity(); } -void VideoBackendBase::DoState(PointerWrap& p) +void VideoBackendBase::DoState(Core::System& system, PointerWrap& p) { - auto& system = Core::System::GetInstance(); if (!system.IsDualCoreMode()) { - VideoCommon_DoState(p); + VideoCommon_DoState(system, p); return; } - AsyncRequests::GetInstance()->PushBlockingEvent([&] { VideoCommon_DoState(p); }); + AsyncRequests::GetInstance()->PushBlockingEvent([&] { VideoCommon_DoState(system, p); }); // Let the GPU thread sleep after loading the state, so we're not spinning if paused after loading // a state. The next GP burst will wake it up again. system.GetFifo().GpuMaySleep(); } -bool VideoBackendBase::InitializeShared(std::unique_ptr gfx, +bool VideoBackendBase::InitializeShared(Core::System& system, std::unique_ptr gfx, std::unique_ptr vertex_manager, std::unique_ptr perf_query, std::unique_ptr bounding_box) @@ -273,12 +269,12 @@ bool VideoBackendBase::InitializeShared(std::unique_ptr gfx, // All hardware backends use the default EFBInterface and TextureCacheBase. // Only Null and Software backends override them - return InitializeShared(std::move(gfx), std::move(vertex_manager), std::move(perf_query), + return InitializeShared(system, std::move(gfx), std::move(vertex_manager), std::move(perf_query), std::move(bounding_box), std::make_unique(), std::make_unique()); } -bool VideoBackendBase::InitializeShared(std::unique_ptr gfx, +bool VideoBackendBase::InitializeShared(Core::System& system, std::unique_ptr gfx, std::unique_ptr vertex_manager, std::unique_ptr perf_query, std::unique_ptr bounding_box, @@ -315,11 +311,10 @@ bool VideoBackendBase::InitializeShared(std::unique_ptr gfx, !g_graphics_mod_manager->Initialize()) { PanicAlertFmtT("Failed to initialize renderer classes"); - Shutdown(); + Shutdown(system); return false; } - auto& system = Core::System::GetInstance(); auto& command_processor = system.GetCommandProcessor(); command_processor.Init(); system.GetFifo().Init(); @@ -347,9 +342,8 @@ bool VideoBackendBase::InitializeShared(std::unique_ptr gfx, return true; } -void VideoBackendBase::ShutdownShared() +void VideoBackendBase::ShutdownShared(Core::System& system) { - auto& system = Core::System::GetInstance(); system.GetCustomResourceManager().Shutdown(); g_frame_dumper.reset(); diff --git a/Source/Core/VideoCommon/VideoBackendBase.h b/Source/Core/VideoCommon/VideoBackendBase.h index 82d9adb589..3114f44f6d 100644 --- a/Source/Core/VideoCommon/VideoBackendBase.h +++ b/Source/Core/VideoCommon/VideoBackendBase.h @@ -12,6 +12,11 @@ #include "Common/WindowSystemInfo.h" #include "VideoCommon/PerfQueryBase.h" +namespace Core +{ +class System; +} + namespace MMIO { class Mapping; @@ -34,8 +39,8 @@ class VideoBackendBase { public: virtual ~VideoBackendBase() {} - virtual bool Initialize(const WindowSystemInfo& wsi) = 0; - virtual void Shutdown() = 0; + virtual bool Initialize(Core::System& system, const WindowSystemInfo& wsi) = 0; + virtual void Shutdown(Core::System& system) = 0; virtual std::string GetConfigName() const = 0; virtual std::string GetDisplayName() const { return GetConfigName(); } @@ -48,10 +53,11 @@ public: static std::string BadShaderFilename(const char* shader_stage, int counter); - void Video_OutputXFB(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height, u64 ticks); + void Video_OutputXFB(Core::System& system, u32 xfb_addr, u32 fb_width, u32 fb_stride, + u32 fb_height, u64 ticks); - u32 Video_GetQueryResult(PerfQueryType type); - u16 Video_GetBoundingBox(int index); + u32 Video_GetQueryResult(Core::System& system, PerfQueryType type); + u16 Video_GetBoundingBox(Core::System& system, int index); static std::string GetDefaultBackendConfigName(); static std::string GetDefaultBackendDisplayName(); @@ -62,23 +68,23 @@ public: static void PopulateBackendInfo(const WindowSystemInfo& wsi); // Wrapper function which pushes the event to the GPU thread. - void DoState(PointerWrap& p); + void DoState(Core::System& system, PointerWrap& p); protected: // For hardware backends - bool InitializeShared(std::unique_ptr gfx, + bool InitializeShared(Core::System& system, std::unique_ptr gfx, std::unique_ptr vertex_manager, std::unique_ptr perf_query, std::unique_ptr bounding_box); // For software and null backends. Allows overriding the default EFBInterface and TextureCache - bool InitializeShared(std::unique_ptr gfx, + bool InitializeShared(Core::System& system, std::unique_ptr gfx, std::unique_ptr vertex_manager, std::unique_ptr perf_query, std::unique_ptr bounding_box, std::unique_ptr efb_interface, std::unique_ptr texture_cache); - void ShutdownShared(); + void ShutdownShared(Core::System& system); bool m_initialized = false; }; diff --git a/Source/Core/VideoCommon/VideoState.cpp b/Source/Core/VideoCommon/VideoState.cpp index d5f53f4963..25a554d6b1 100644 --- a/Source/Core/VideoCommon/VideoState.cpp +++ b/Source/Core/VideoCommon/VideoState.cpp @@ -29,7 +29,7 @@ #include "VideoCommon/XFMemory.h" #include "VideoCommon/XFStateManager.h" -void VideoCommon_DoState(PointerWrap& p) +void VideoCommon_DoState(Core::System& system, PointerWrap& p) { bool software = false; p.Do(software); @@ -65,7 +65,6 @@ void VideoCommon_DoState(PointerWrap& p) p.DoMarker("TMEM"); // FIFO - auto& system = Core::System::GetInstance(); system.GetFifo().DoState(p); p.DoMarker("Fifo"); diff --git a/Source/Core/VideoCommon/VideoState.h b/Source/Core/VideoCommon/VideoState.h index 39cea234bd..62defc389b 100644 --- a/Source/Core/VideoCommon/VideoState.h +++ b/Source/Core/VideoCommon/VideoState.h @@ -3,6 +3,11 @@ #pragma once +namespace Core +{ +class System; +} + class PointerWrap; -void VideoCommon_DoState(PointerWrap& p); +void VideoCommon_DoState(Core::System& system, PointerWrap& p);