2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

video_core/gpu: Create threads separately from initialization

Like with CPU emulation, we generally don't want to fire off the threads
immediately after the relevant classes are initialized, we want to do
this after all necessary data is done loading first.

This splits the thread creation into its own interface member function
to allow controlling when these threads in particular get created.
This commit is contained in:
Lioncash 2019-04-09 14:02:00 -04:00
parent f2331a804a
commit 6d0551196d
10 changed files with 51 additions and 25 deletions

View file

@ -3,9 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <array> #include <array>
#include <map>
#include <memory> #include <memory>
#include <thread>
#include <utility> #include <utility>
#include "common/file_util.h" #include "common/file_util.h"
@ -38,8 +36,6 @@
#include "frontend/applets/software_keyboard.h" #include "frontend/applets/software_keyboard.h"
#include "frontend/applets/web_browser.h" #include "frontend/applets/web_browser.h"
#include "video_core/debug_utils/debug_utils.h" #include "video_core/debug_utils/debug_utils.h"
#include "video_core/gpu_asynch.h"
#include "video_core/gpu_synch.h"
#include "video_core/renderer_base.h" #include "video_core/renderer_base.h"
#include "video_core/video_core.h" #include "video_core/video_core.h"
@ -135,13 +131,9 @@ struct System::Impl {
return ResultStatus::ErrorVideoCore; return ResultStatus::ErrorVideoCore;
} }
is_powered_on = true; gpu_core = VideoCore::CreateGPU(system);
if (Settings::values.use_asynchronous_gpu_emulation) { is_powered_on = true;
gpu_core = std::make_unique<VideoCommon::GPUAsynch>(system, *renderer);
} else {
gpu_core = std::make_unique<VideoCommon::GPUSynch>(system, *renderer);
}
LOG_DEBUG(Core, "Initialized OK"); LOG_DEBUG(Core, "Initialized OK");
@ -188,7 +180,8 @@ struct System::Impl {
} }
// Main process has been loaded and been made current. // Main process has been loaded and been made current.
// Begin CPU execution. // Begin GPU and CPU execution.
gpu_core->Start();
cpu_core_manager.StartThreads(); cpu_core_manager.StartThreads();
status = ResultStatus::Success; status = ResultStatus::Success;

View file

@ -207,6 +207,11 @@ public:
}; };
} regs{}; } regs{};
/// Performs any additional setup necessary in order to begin GPU emulation.
/// This can be used to launch any necessary threads and register any necessary
/// core timing events.
virtual void Start() = 0;
/// Push GPU command entries to be processed /// Push GPU command entries to be processed
virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0; virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;

View file

@ -9,10 +9,14 @@
namespace VideoCommon { namespace VideoCommon {
GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer) GPUAsynch::GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer)
: Tegra::GPU(system, renderer), gpu_thread{system, renderer, *dma_pusher} {} : GPU(system, renderer), gpu_thread{system} {}
GPUAsynch::~GPUAsynch() = default; GPUAsynch::~GPUAsynch() = default;
void GPUAsynch::Start() {
gpu_thread.StartThread(renderer, *dma_pusher);
}
void GPUAsynch::PushGPUEntries(Tegra::CommandList&& entries) { void GPUAsynch::PushGPUEntries(Tegra::CommandList&& entries) {
gpu_thread.SubmitList(std::move(entries)); gpu_thread.SubmitList(std::move(entries));
} }

View file

@ -13,16 +13,13 @@ class RendererBase;
namespace VideoCommon { namespace VideoCommon {
namespace GPUThread {
class ThreadManager;
} // namespace GPUThread
/// Implementation of GPU interface that runs the GPU asynchronously /// Implementation of GPU interface that runs the GPU asynchronously
class GPUAsynch : public Tegra::GPU { class GPUAsynch : public Tegra::GPU {
public: public:
explicit GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer); explicit GPUAsynch(Core::System& system, VideoCore::RendererBase& renderer);
~GPUAsynch() override; ~GPUAsynch() override;
void Start() override;
void PushGPUEntries(Tegra::CommandList&& entries) override; void PushGPUEntries(Tegra::CommandList&& entries) override;
void SwapBuffers( void SwapBuffers(
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) override; std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) override;

View file

@ -8,10 +8,12 @@
namespace VideoCommon { namespace VideoCommon {
GPUSynch::GPUSynch(Core::System& system, VideoCore::RendererBase& renderer) GPUSynch::GPUSynch(Core::System& system, VideoCore::RendererBase& renderer)
: Tegra::GPU(system, renderer) {} : GPU(system, renderer) {}
GPUSynch::~GPUSynch() = default; GPUSynch::~GPUSynch() = default;
void GPUSynch::Start() {}
void GPUSynch::PushGPUEntries(Tegra::CommandList&& entries) { void GPUSynch::PushGPUEntries(Tegra::CommandList&& entries) {
dma_pusher->Push(std::move(entries)); dma_pusher->Push(std::move(entries));
dma_pusher->DispatchCalls(); dma_pusher->DispatchCalls();

View file

@ -18,6 +18,7 @@ public:
explicit GPUSynch(Core::System& system, VideoCore::RendererBase& renderer); explicit GPUSynch(Core::System& system, VideoCore::RendererBase& renderer);
~GPUSynch() override; ~GPUSynch() override;
void Start() override;
void PushGPUEntries(Tegra::CommandList&& entries) override; void PushGPUEntries(Tegra::CommandList&& entries) override;
void SwapBuffers( void SwapBuffers(
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) override; std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) override;

View file

@ -55,19 +55,24 @@ static void RunThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_p
} }
} }
ThreadManager::ThreadManager(Core::System& system, VideoCore::RendererBase& renderer, ThreadManager::ThreadManager(Core::System& system) : system{system} {}
Tegra::DmaPusher& dma_pusher)
: system{system}, thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)} {
synchronization_event = system.CoreTiming().RegisterEvent(
"GPUThreadSynch", [this](u64 fence, s64) { state.WaitForSynchronization(fence); });
}
ThreadManager::~ThreadManager() { ThreadManager::~ThreadManager() {
if (!thread.joinable()) {
return;
}
// Notify GPU thread that a shutdown is pending // Notify GPU thread that a shutdown is pending
PushCommand(EndProcessingCommand()); PushCommand(EndProcessingCommand());
thread.join(); thread.join();
} }
void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher) {
thread = std::thread{RunThread, std::ref(renderer), std::ref(dma_pusher), std::ref(state)};
synchronization_event = system.CoreTiming().RegisterEvent(
"GPUThreadSynch", [this](u64 fence, s64) { state.WaitForSynchronization(fence); });
}
void ThreadManager::SubmitList(Tegra::CommandList&& entries) { void ThreadManager::SubmitList(Tegra::CommandList&& entries) {
const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))}; const u64 fence{PushCommand(SubmitListCommand(std::move(entries)))};
const s64 synchronization_ticks{Core::Timing::usToCycles(9000)}; const s64 synchronization_ticks{Core::Timing::usToCycles(9000)};

View file

@ -138,10 +138,12 @@ struct SynchState final {
/// Class used to manage the GPU thread /// Class used to manage the GPU thread
class ThreadManager final { class ThreadManager final {
public: public:
explicit ThreadManager(Core::System& system, VideoCore::RendererBase& renderer, explicit ThreadManager(Core::System& system);
Tegra::DmaPusher& dma_pusher);
~ThreadManager(); ~ThreadManager();
/// Creates and starts the GPU thread.
void StartThread(VideoCore::RendererBase& renderer, Tegra::DmaPusher& dma_pusher);
/// Push GPU command entries to be processed /// Push GPU command entries to be processed
void SubmitList(Tegra::CommandList&& entries); void SubmitList(Tegra::CommandList&& entries);

View file

@ -5,6 +5,8 @@
#include <memory> #include <memory>
#include "core/core.h" #include "core/core.h"
#include "core/settings.h" #include "core/settings.h"
#include "video_core/gpu_asynch.h"
#include "video_core/gpu_synch.h"
#include "video_core/renderer_base.h" #include "video_core/renderer_base.h"
#include "video_core/renderer_opengl/renderer_opengl.h" #include "video_core/renderer_opengl/renderer_opengl.h"
#include "video_core/video_core.h" #include "video_core/video_core.h"
@ -16,6 +18,14 @@ std::unique_ptr<RendererBase> CreateRenderer(Core::Frontend::EmuWindow& emu_wind
return std::make_unique<OpenGL::RendererOpenGL>(emu_window, system); return std::make_unique<OpenGL::RendererOpenGL>(emu_window, system);
} }
std::unique_ptr<Tegra::GPU> CreateGPU(Core::System& system) {
if (Settings::values.use_asynchronous_gpu_emulation) {
return std::make_unique<VideoCommon::GPUAsynch>(system, system.Renderer());
}
return std::make_unique<VideoCommon::GPUSynch>(system, system.Renderer());
}
u16 GetResolutionScaleFactor(const RendererBase& renderer) { u16 GetResolutionScaleFactor(const RendererBase& renderer) {
return static_cast<u16>( return static_cast<u16>(
Settings::values.resolution_factor Settings::values.resolution_factor

View file

@ -14,6 +14,10 @@ namespace Core::Frontend {
class EmuWindow; class EmuWindow;
} }
namespace Tegra {
class GPU;
}
namespace VideoCore { namespace VideoCore {
class RendererBase; class RendererBase;
@ -27,6 +31,9 @@ class RendererBase;
std::unique_ptr<RendererBase> CreateRenderer(Core::Frontend::EmuWindow& emu_window, std::unique_ptr<RendererBase> CreateRenderer(Core::Frontend::EmuWindow& emu_window,
Core::System& system); Core::System& system);
/// Creates an emulated GPU instance using the given system context.
std::unique_ptr<Tegra::GPU> CreateGPU(Core::System& system);
u16 GetResolutionScaleFactor(const RendererBase& renderer); u16 GetResolutionScaleFactor(const RendererBase& renderer);
} // namespace VideoCore } // namespace VideoCore